设为首页 收藏本站
查看: 1217|回复: 0

自己写了一个perl脚本检测redis(nagios插件)

[复制链接]

尚未签到

发表于 2015-11-22 17:20:31 | 显示全部楼层 |阅读模式
  放在我的code库内了,http://farmerluo.googlecode.com/files/check_redis.pl

介绍下怎么安装:

脚本用到了perl的Redis库,需要先安装这个:
# perl -MCPAN -e shell
# install Redis

wget http://farmerluo.googlecode.com/files/check_redis.pl

cp check_redis.pl  /etc/nagios/command/

chown cacti.nagios check_redis.pl

在nagios内加入这个插件:
vi /etc/nagios/objects/command.cfg

# 'check_redis' command definition
define command{
command_name    check_redis
command_line    /etc/nagios/command/check_redis.pl -h $HOSTADDRESS$ $ARG1$
}


加入一个服务:
vi /etc/nagios/objects/linuxhost.cfg

define service{
use                             generic-service         ; Name of service template to use
host_name                       memcached.ha2,memcached.web2
service_description             redis
check_command                   check_redis
notifications_enabled           1
}

检查下nagios配置是否正解:
nagios -v  /etc/nagios/nagios.cfg

Nagios Core 3.2.1
Copyright (c) 2009-2010 Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 03-09-2010
License: GPL

Website: http://www.nagios.org
Reading configuration data...
Read main config file okay...
Processing object config file '/etc/nagios/objects/commands.cfg'...
Processing object config file '/etc/nagios/objects/contacts.cfg'...
Processing object config file '/etc/nagios/objects/timeperiods.cfg'...
Processing object config file '/etc/nagios/objects/templates.cfg'...
Processing object config file '/etc/nagios/objects/linuxhosts.cfg'...
Processing object config file '/etc/nagios/objects/windows.cfg'...
Read object config files okay...

Running pre-flight check on configuration data...

Checking services...
Checked 32 services.
Checking hosts...
Checked 14 hosts.
Checking host groups...
Checked 4 host groups.
Checking service groups...
Checked 0 service groups.
Checking contacts...
Checked 3 contacts.
Checking contact groups...
Checked 2 contact groups.
Checking service escalations...
Checked 0 service escalations.
Checking service dependencies...
Checked 0 service dependencies.
Checking host escalations...
Checked 0 host escalations.
Checking host dependencies...
Checked 0 host dependencies.
Checking commands...
Checked 29 commands.
Checking time periods...
Checked 5 time periods.
Checking for circular paths between hosts...
Checking for circular host and service dependencies...
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:   0

Things look okay - No serious problems were detected during the pre-flight check

没问题,我们重新载入配置。

service nagios reload

再介绍一下用perl写nagios插件需要注意的地方:

  • 总是要生成一些输出内容;
  • 加上引用'use utils'并引用些通用模块来输出($TIMEOUT %ERRORS &print_revision &支持等);
  • 总 是知道一些Perl插件的标准习惯,如:

    • 退出时总是exit带 着$ERRORS{CRITICAL}、$ERRORS{OK}等;
    • 使用getopt函数来处理命令行;
    • 程序处理超 时问题;
    • 当没有命令参数时要给出可调用print_usage;
    • 使用标准的命令行选项开关(象-H 'host'、-V 'version'等)。

  check_redisl.pl代码:

#!/usr/bin/perl

# nagios: -epn

################################################################################
# check_redis - Nagios Plugin for Redis checks.
#
# @author  farmer.luo at gmail.com
# @date    2010-05-12
# @license GPL v2
#
# check_nagios.pl -h <redis host> -p <redis port> -w <warning time> -c <critica time>
#
# Run the script need:
#
# perl -MCPAN -e shell
# install Redis
#
################################################################################

use strict;
use warnings;
use Redis;
use File::Basename;
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use Time::Local;
use vars qw($opt_h); # Redis 主机
use vars qw($opt_p); # Redis 端口
use vars qw($opt_w); # 超过这个时间发出警告
use vars qw($opt_c); # 超过这个时间发出严重警告
use Getopt::Std;

$opt_h = &quot;&quot;;
$opt_p = &quot;6379&quot;;
$opt_w = 5;
$opt_c = 10;
my $r = &quot;&quot;;

getopt('hpwcd');

if ( $opt_h eq &quot;&quot; ) {
help();
exit(1);
}

my $start = time();

redis_connect();

# print $@;
if ( $@ ) {
print &quot;UNKNOWN - cann't connect to redis server:&quot; . $opt_h . &quot;.&quot;;
exit $ERRORS{&quot;UNKNOWN&quot;};
}

if ( redis_set() ) {
print &quot;WARNING - redis server:&quot; . $opt_h . &quot;,set key error.&quot;;
exit $ERRORS{&quot;WARNING&quot;};
}

if ( redis_get() ) {
print &quot;WARNING - redis server:&quot; . $opt_h . &quot;,get key error.&quot;;
exit $ERRORS{&quot;WARNING&quot;};
}

if ( redis_del() ) {
print &quot;WARNING - redis server:&quot; . $opt_h . &quot;,del key error.&quot;;
exit $ERRORS{&quot;WARNING&quot;};
}

#sleep(3);
my $stop = time();

my $run = $stop - $start;

if ( $run > $opt_c ) {

print &quot;CRITICAL - redis server(&quot; . $opt_h . &quot;) run for &quot; . $run . &quot; seconds!&quot;;
exit $ERRORS{&quot;CRITICAL&quot;};

} elsif ( $run > $opt_w ) {

print &quot;WARNING - redis server(&quot; . $opt_h . &quot;) run for &quot; . $run . &quot; seconds!&quot;;
exit $ERRORS{&quot;WARNING&quot;};

} else {

redis_info();
redis_quit();
exit $ERRORS{&quot;OK&quot;};

}


sub help{

die &quot;Usage:/n&quot; , basename( $0 ) ,  &quot; -h hostname -p port -w warning time -c critical time -d down time/n&quot;

}

sub redis_connect{

my $redis_hp = $opt_h . &quot;:&quot; . $opt_p;

eval{ $r = Redis->new( server => $redis_hp ); };

}

sub redis_set{

$r->set( redis_nagios_key => 'test' ) || return 1;

return 0;
}

sub redis_get{

my $value = $r->get( 'redis_nagios_key' ) || return 1;

return 0;
}

sub redis_del{

$r->del( 'redis_nagios_key' ) || return 1;

return 0;
}

sub redis_info{

my $info_hash = $r->info;

print &quot;OK - redis server(&quot; . $opt_h . &quot;) info:&quot;;

while ( my ($key, $value) = each(%$info_hash) ) {
print &quot;$key => $value, &quot;;
}

}

sub redis_quit{

$r->quit();

}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-142300-1-1.html 上篇帖子: 安装nagios-plugins-1.4.15的make的报错 下篇帖子: nagios + snmp配置,自己配出来,才能有收获!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表