Chinaunix首页 | 论坛 | 博客
  • 博客访问: 452064
  • 博文数量: 403
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: -70
  • 用 户 组: 普通用户
  • 注册时间: 2016-09-05 12:45
文章分类

全部博文(403)

文章存档

2014年(3)

2013年(1)

2012年(3)

2011年(21)

2010年(13)

2009年(64)

2008年(9)

2007年(36)

2006年(253)

分类: LINUX

2012-12-11 14:40:00

本插件主要功能是可以通过一个OID采集一个数据,并设置告警阀值,将处理结果返回给nagios来处理。
应用场景:如监查各类设备的CPU是否过高,或某条带宽线路是否拥塞等等。
 
代码如下

  1. #!/usr/bin/perl
  2. # Version : 0.1
  3. # Date : 2012-04-05
  4. # Author : Darry Guo
  5. # Help : http://www.imyboy.com
  6. # Licence : GPL - http://www.fsf.org/licenses/gpl.txt

  7. # help perldoc Net::SNMP

  8. use strict;
  9. use Net::SNMP;
  10. use Getopt::Std;


  11. my $script = GetScriptName($0);
  12. my $script_version = "0.1";


  13. my $ipaddress = "127.0.0.1";
  14. my $port = 161;
  15. my $version = "snmpv2c";
  16. my $community = "public";
  17. my $vOID = '1.3.6.1.4.1.9.2.2.1.1.3.1';
  18. my $timeout = 2;
  19. my $warning = 90;
  20. my $critical = 95;
  21. my $vMAX = 100;
  22. my $status = 0;
  23. my $returnstring = "";
  24. my $temp = 5;

  25. if (@ARGV < 1) {
  26.      print "Too few arguments\n";
  27.      usage();
  28. }

  29. my %opt=();
  30. getopts("hvH:p:C:o:w:c:m:t:",\%opt);

  31. if($opt{h} || $opt{v})
  32. {
  33.     usage();
  34.     exit(0);
  35. }

  36. $ipaddress = $opt{H} if defined $opt{H} ;
  37. $port = $opt{p} if defined $opt{p} ;
  38. $community = $opt{C} if defined $opt{C} ;
  39. $vOID = $opt{o} if defined $opt{o} ;
  40. $warning = $opt{w} if defined $opt{w} ;
  41. $critical = $opt{c} if defined $opt{c} ;
  42. $vMAX = $opt{m} if defined $opt{m} ;

  43. #print "-H $ipaddress\n";
  44. #print "-p $port\n";
  45. #print "-C $community\n";
  46. #print "-o $vOID\n";
  47. #print "-w $warning\n";
  48. #print "-c $critical\n";
  49. #print "-m $vMAX\n";

  50. # Create the SNMP session
  51. my ($session, $error) = Net::SNMP->session(
  52.      -community => $community,
  53.      -hostname => $ipaddress,
  54.      -port => $port,
  55.      -version => $version,
  56.      -timeout => $timeout

  57. main();

  58. # Close the session
  59. $session->close();

  60. if ($returnstring eq ""){
  61.     $status = 3;
  62. }

  63. if ($status == 0){
  64.     print "Status is OK - $returnstring\n";
  65.     # print "$returnstring\n";
  66. }
  67. elsif ($status == 1){
  68.     print "Status is a WARNING level - $returnstring\n";
  69. }
  70. elsif ($status == 2){
  71.     print "Status is CRITICAL - $returnstring\n";
  72. }
  73. else{
  74.     print "Status is UNKNOWN\n";
  75. }

  76. exit $status;


  77. ####################################################################
  78. # sub program #
  79. ####################################################################
  80. sub main()
  81. {
  82.     my $result = $session->get_request(-varbindlist=>[$vOID]);
  83.     my $len = length($result->{$vOID});
  84.     if ( $len == 0)
  85.     {
  86.        $status =3;
  87.     }
  88.     else
  89.     {
  90.        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 );
  91.     }
  92. }



  93. sub usage {
  94.     print << "USAGE";
  95. --------------------------------------------------------------------
  96. $script v$script_version

  97. SnmpGet on All devices

  98. Usage: $script -H <hostname> -C <community> [...]
  99. Options: -H Hostname or IP address
  100.                  -p snmp port(default is tcp161)
  101.                  -v snmp Version(default is v2c)
  102.                  -o snmp OID
  103.                  -C Community (default is public)
  104.                  -w Warning threshold
  105.                  -c Critical threshold
  106.                  -m MAX vlaue
  107.                  -h or -v Help

  108. --------------------------------------------------------------------
  109. Copyright 2012 www.imyboy.com

  110. This program is free software; you can redistribute it or modify
  111. it under the terms of the GNU General Public License
  112. --------------------------------------------------------------------

  113. USAGE
  114.      exit 1;
  115. }


  116. sub GetScriptName
  117. {
  118.    my @str = @_;
  119.    my $len = rindex($str[0],"/");
  120.    return substr($str[0],$len + 1);
  121. }

使用说明
step1:
    将check_snmp_perl.pl复制到nagios脚本目录,并设置可执行权限。
step2:
     在nagios中定义命令,代码如下:

  1. #vi /usr/local/nagios/etc/objects/commands.cfg
  2. ...
  3. define command{
  4.         command_name check_perl
  5.         command_line $USER1$/check_snmp_perl -H $HOSTADDRESS$ -C $ARG1$ -o $ARG2$ -w $ARG3$ -c $ARG4$ -m $ARG5$
  6.         }
step3:定义对像

  1. #vi /usr/local/nagios/etc/objects/list/ciscoCPU254.cfg
  2. ;Host monitor
  3. define host {
  4.         use generic-switch
  5.         host_name cisco3750-1.254
  6.         address 10.1.1.2
  7.         active_checks_enabled 0
  8. }
  9. ;SNMP test
  10. define service{
  11.         use generic-service
  12.         host_name cisco3750-1.254
  13.         service_description SNMP-cpu
  14.         check_command check_perl!abc123!1.3.6.1.4.1.9.9.109.1.1.1.1.3.1!50!80!100
  15.         check_interval 0.3
  16.         retry_interval 0.1
  17.         max_check_attempts 2
  18.         notifications_enabled 1
  19.         notification_interval 120
  20.         notification_period 24x7
  21.         notification_options w,u,c,r
  22.         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
 
结果如图所示:
 
阅读(842) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~