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

全部博文(16)

文章存档

2017年(3)

2016年(2)

2015年(3)

2014年(1)

2013年(2)

2012年(5)

我的朋友

分类: 网络与安全

2014-07-16 14:15:59

在日常工作有大量的接入层交换机cisco2950/2960等的端口,因某种原因会出现err-disable状态,有时适合自动恢复,有时需要查明原因后手式恢复。若不能及时恢复,可能会误以为接入线故障,会增加查找线位工作量。为此,通知SNMPTrap包将该类状态的端口发给Nagios,由Nagios自动发报警信息给网络管理员。

文件信息如下:
1。err-disable.pl  从Trap包分析出err-diable信息,并输入出nagios外部命令。
2。nagios_object_host.txt   为err-disable状态的服务检测对像。
3。readme.txt  使用说明.


详细如下:
err-disable.pl

点击(此处)折叠或打开

  1. #!/usr/bin/perl
  2. use strict;
  3. use POSIX;

  4. #my $filename="$ARGV[0]";
  5. #open (FILE_HANDER, "<$filename") || die ("Could not open file : $!");
  6. #my @LINE = <FILE_HANDER>;
  7. #close FILE_HANDER;
  8. my @LINE=<STDIN>;

  9. shift @LINE;
  10. my $now_time = localtime();
  11. my $log =1; #1 write log to /tmp/trap.log

  12. #----get network type------------
  13. my @Network_Def = ( #format : [network_address,"description"],10.1. ---> 10.1.0.0/16,ext:A.B.--->A.B.0.0/16,A.B.C--->A.B.C.0/24
  14.    ["10.1.","sz-network"],
  15.    ["10.9.","gz-network"],
  16.    ["10.2.","bj-network"]
  17. );
  18. my $ip_str = &ipinfo($LINE[0]);
  19. my $network_description;

  20. my $network_info;
  21. my $def_tp;
  22. foreach $network_info(@Network_Def){
  23.   #print "$network_info->[0];$network_info->[1]\n";
  24.   my $def_tp = $network_info->[0];
  25.   if ( $ip_str =~ /$def_tp/ ){
  26.      $network_description = $network_info->[1];
  27.      #print "IP:$ip_str;type:$tp;desp:$network_description;\n";
  28.   }
  29. }

  30. #----------------------------------
  31. foreach (@LINE)
  32. {
  33.   my $output;
  34.   my $err_type;
  35.   my $flag_print=0;
  36.   my $ext_cmd;
  37.   my $log_cmd;
  38.   my $log_text;

  39.   chomp $_;

  40.   if ($_ =~ /err-disable/ )
  41.   {
  42.     if (($_ =~ /err-disable/ ) && ($_ =~ /error/ )) {
  43.       my @arry_str = split("\"",$_);
  44.       $output = "$arry_str[1]($ip_str)";
  45.       $err_type = "error";
  46.       $flag_print = 1;
  47.     }

  48.     if (($_ =~ /err-disable/ ) && ($_ =~ /recover/ )) {
  49.       my @arry_str = split("\"",$_);
  50.       $output = "$arry_str[1]($ip_str)";
  51.       $err_type = "recover";
  52.       $flag_print = 1;
  53.     }

  54.     my @arry_str = split("\"",$_);
  55.     $log_text = "$arry_str[1]($ip_str)";
  56.   }

  57.   #print "$now_time;$ip_str;$err_type;$output\n" if ($flag_print);
  58.   #print "/usr/local/nagios/eventhandlers/submit_check_result \"test-host\" \"err-disble\" \"1\" \"$output\"\n" if ($flag_print);
  59.   #my $ext_cmd = "/usr/local/nagios/eventhandlers/submit_check_result \"test-host\" \"err-disble\" \"1\" \"$output\"\n" if ($flag_print);
  60.   #print $ext_cmd;

  61.   if ($flag_print) {
  62.     $ext_cmd = "/usr/local/nagios/eventhandlers/submit_check_result \"err-disable-device\" \"$network_description\" \"2\" \"$output\"\n" if ($err_type =~ /error/);
  63.     $ext_cmd = "/usr/local/nagios/eventhandlers/submit_check_result \"err-disable-device\" \"$network_description\" \"3\" \"$output\"\n" if ($err_type =~ /recover/);
  64.     system $ext_cmd;
  65.     #print $ext_cmd;
  66.     if ($log) {
  67.       $log_cmd = "/bin/echo \"$now_time;$ip_str($network_description);$err_type;$log_text\" >> /tmp/trap.log";
  68.       #print $log_cmd,"\n";
  69.       system $log_cmd;
  70.     }
  71.   }
  72.   
  73. }


  74. #--------------------------------------------------
  75. sub ipinfo
  76. {
  77.    my $st= @_[0];
  78.    my $b = index($st,"[");
  79.    my $e = index($st,"]");
  80.    my $ok = substr($st,$b+1,$e - $b-1);
  81.    return $ok;
  82. }


nagios_object_host.txt

nagios_object_host.txtnagios_object_host.txt服务检测对像

点击(此处)折叠或打开

  1. define host {
  2.         use szHost
  3.         host_name err-disable-device //固定名称,由脚本固定输出决定.
  4.         address 127.0.0.1 //该值无意义
  5.         notifications_enabled 0 //无需报警
  6.         active_checks_enabled 0 //无需检测
  7.         passive_checks_enabled 1 //无需检测
  8.         check_freshness 0
  9.         flap_detection_enabled 0
  10.         notifications_enabled 0
  11. }


  12. define service{
  13.         use szSRnet
  14.         host_name err-disable-device //固定名称,由脚本固定输出决定.
  15.         service_description sz-network //脚本中的"$Network_Def[n][1]"的值
  16.         active_checks_enabled 0 //关闭“自动检测”
  17.         passive_checks_enabled 1 //开启“被动检测”
  18.         check_freshness 0 //关闭刷新服务。
  19.         flap_detection_enabled 0 //关闭抖动处理,确保每个Trap都会报警.
  20.         check_command check_ping //无意议,但必须有.
  21.         max_check_attempts 1
  22.         retry_check_interval 1
  23.         is_volatile 1 //可变服务,允许状态值非0情况下的重复信息重复发送通知。并忽略notification_interval配置。
  24.         contacts guofusheng //报警接收人员
  25.         contact_groups null
  26.         notification_options w,u,c,r,f,s //报警类型
  27.         event_handler_enabled 0 //关闭事件处理,当is_volatile=1时,该配置被默认配置为1,可手工关闭。
  28. }

使用说明:

step1:
  配置脚本err-disable.pl(位于err-disable-perl.txt文件中)
  如下代码中的定义被monitor的交换机在哪个网络,其中的"description"项,被nagios中的service_description对应,必须一致.
my @Network_Def = (        #format : [network_address,"description"],10.1. ---> 10.1.0.0/16,ext:A.B.--->A.B.0.0/16,A.B.C--->A.B.C.0/24
   ["10.1.","sz-network"],
   ["10.9.","gz-network"],
   ["10.2.","bj-network"]
);

step2:定义nagios的服务检测对像,如文件nagios_object_host.txt


step3:配置trapd文件.
#cat /etc/snmp/snmptrapd.conf
authcommunity execute,log,net abc12345
pidFile      /var/run/snmptrapd.pid
traphandle      default                 /tmp/err-disable.pl

收到的报警信息如下:

阅读(2436) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~