本插件主要功能是可以通过一个OID采集一个数据,并设置告警阀值,将处理结果返回给nagios来处理。
应用场景:如监查各类设备的CPU是否过高,或某条带宽线路是否拥塞等等。
代码如下
- #!/usr/bin/perl
- # Version : 0.1
- # Date : 2012-04-05
- # Author : Darry Guo
- # Help : http://www.imyboy.com
- # Licence : GPL - http://www.fsf.org/licenses/gpl.txt
- # help perldoc Net::SNMP
- use strict;
- use Net::SNMP;
- use Getopt::Std;
- my $script = GetScriptName($0);
- my $script_version = "0.1";
- my $ipaddress = "127.0.0.1";
- my $port = 161;
- my $version = "snmpv2c";
- my $community = "public";
- my $vOID = '1.3.6.1.4.1.9.2.2.1.1.3.1';
- my $timeout = 2;
- my $warning = 90;
- my $critical = 95;
- my $vMAX = 100;
- my $status = 0;
- my $returnstring = "";
- my $temp = 5;
- if (@ARGV < 1) {
- print "Too few arguments\n";
- usage();
- }
- my %opt=();
- getopts("hvH:p:C:o:w:c:m:t:",\%opt);
- if($opt{h} || $opt{v})
- {
- usage();
- exit(0);
- }
- $ipaddress = $opt{H} if defined $opt{H} ;
- $port = $opt{p} if defined $opt{p} ;
- $community = $opt{C} if defined $opt{C} ;
- $vOID = $opt{o} if defined $opt{o} ;
- $warning = $opt{w} if defined $opt{w} ;
- $critical = $opt{c} if defined $opt{c} ;
- $vMAX = $opt{m} if defined $opt{m} ;
- #print "-H $ipaddress\n";
- #print "-p $port\n";
- #print "-C $community\n";
- #print "-o $vOID\n";
- #print "-w $warning\n";
- #print "-c $critical\n";
- #print "-m $vMAX\n";
- # Create the SNMP session
- my ($session, $error) = Net::SNMP->session(
- -community => $community,
- -hostname => $ipaddress,
- -port => $port,
- -version => $version,
- -timeout => $timeout
- main();
- # Close the session
- $session->close();
- if ($returnstring eq ""){
- $status = 3;
- }
- if ($status == 0){
- print "Status is OK - $returnstring\n";
- # print "$returnstring\n";
- }
- elsif ($status == 1){
- print "Status is a WARNING level - $returnstring\n";
- }
- elsif ($status == 2){
- print "Status is CRITICAL - $returnstring\n";
- }
- else{
- print "Status is UNKNOWN\n";
- }
- exit $status;
- ####################################################################
- # sub program #
- ####################################################################
- sub main()
- {
- my $result = $session->get_request(-varbindlist=>[$vOID]);
- my $len = length($result->{$vOID});
- if ( $len == 0)
- {
- $status =3;
- }
- else
- {
- my $OID_value = $result->{$vOID};
$returnstring = "Current $OID_value,OK:0-$warning,Warning:$warning-$critical,Critical:$critical-$vMAX | $vOID=$OID_value\n";
$status =0 if( $OID_value <= $warning );
$status =1 if( ($OID_value > $warning) && ($OID_value <= $critical) );
$status =2 if( $OID_value > $critical );
- }
- }
- sub usage {
- print << "USAGE";
- --------------------------------------------------------------------
- $script v$script_version
- SnmpGet on All devices
- Usage: $script -H <hostname> -C <community> [...]
- Options: -H Hostname or IP address
- -p snmp port(default is tcp161)
- -v snmp Version(default is v2c)
- -o snmp OID
- -C Community (default is public)
- -w Warning threshold
- -c Critical threshold
- -m MAX vlaue
- -h or -v Help
- --------------------------------------------------------------------
- Copyright 2012 www.imyboy.com
- This program is free software; you can redistribute it or modify
- it under the terms of the GNU General Public License
- --------------------------------------------------------------------
- USAGE
- exit 1;
- }
- sub GetScriptName
- {
- my @str = @_;
- my $len = rindex($str[0],"/");
- return substr($str[0],$len + 1);
- }
使用说明
step1:
将check_snmp_perl.pl复制到nagios脚本目录,并设置可执行权限。
step2:
在nagios中定义命令,代码如下:
- #vi /usr/local/nagios/etc/objects/commands.cfg
- ...
- define command{
- command_name check_perl
- command_line $USER1$/check_snmp_perl -H $HOSTADDRESS$ -C $ARG1$ -o $ARG2$ -w $ARG3$ -c $ARG4$ -m $ARG5$
- }
step3:定义对像
- #vi /usr/local/nagios/etc/objects/list/ciscoCPU254.cfg
- ;Host monitor
- define host {
- use generic-switch
- host_name cisco3750-1.254
- address 10.1.1.2
- active_checks_enabled 0
- }
- ;SNMP test
- define service{
- use generic-service
- host_name cisco3750-1.254
- service_description SNMP-cpu
- check_command check_perl!abc123!1.3.6.1.4.1.9.9.109.1.1.1.1.3.1!50!80!100
- check_interval 0.3
- retry_interval 0.1
- max_check_attempts 2
- notifications_enabled 1
- notification_interval 120
- notification_period 24x7
- notification_options w,u,c,r
- contact_groups IDC-group
step4:
#vi /usr/local/nagios/etc/nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/list/ciscoCPU254.cfg
service nagios restart
结果如图所示:
阅读(827) | 评论(0) | 转发(1) |