Chinaunix首页 | 论坛 | 博客
  • 博客访问: 161662
  • 博文数量: 16
  • 博客积分: 170
  • 博客等级: 入伍新兵
  • 技术积分: 753
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 10:57
文章分类

全部博文(16)

文章存档

2017年(3)

2016年(2)

2015年(3)

2014年(1)

2013年(2)

2012年(5)

我的朋友

分类: 系统运维

2012-04-11 11:01:45

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

点击(此处)折叠或打开

  1. #!/usr/bin/perl
  2. # Version : 0.1
  3. # Date : 2012-04-05
  4. # Licence : GPL - http://www.fsf.org/licenses/gpl.txt

  5. # help perldoc Net::SNMP

  6. use strict;
  7. use Net::SNMP;
  8. use Getopt::Std;


  9. my $script = GetScriptName($0);
  10. my $script_version = "0.1";


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

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

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

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

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

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

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

  55. main();

  56. # Close the session
  57. $session->close();

  58. if ($returnstring eq ""){
  59.     $status = 3;
  60. }

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

  74. exit $status;


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



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

  95. SnmpGet on All devices

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

  106. --------------------------------------------------------------------

  107. This program is free software; you can redistribute it or modify
  108. it under the terms of the GNU General Public License
  109. --------------------------------------------------------------------

  110. USAGE
  111.      exit 1;
  112. }


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

使用说明
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
 
结果如图所示:
 
阅读(6466) | 评论(4) | 转发(0) |
给主人留下些什么吧!~~

鸪灵2012-04-16 22:36:05

例子很好,我收藏了,博主多发发类似的吧,我也学习一下

常虫清2012-04-15 21:18:58

不错的帖子,辛苦楼主原创了,以后也多多发一下,多谢

泥亚鳅2012-04-14 12:53:15

不错,原创的现在真的不太多了~~说什么也要要顶

最乖啦啦2012-04-12 11:44:32

$USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 1
这句话什么意思  楼主知道嘛?  小弟找不到答案了