文件信息如下:
1。err-disable.pl 从Trap包分析出err-diable信息,并输入出nagios外部命令。
2。nagios_object_host.txt 为err-disable状态的服务检测对像。
3。readme.txt 使用说明.
详细如下:
err-disable.pl
点击(此处)折叠或打开
- #!/usr/bin/perl
- use strict;
- use POSIX;
- #my $filename="$ARGV[0]";
- #open (FILE_HANDER, "<$filename") || die ("Could not open file : $!");
- #my @LINE = <FILE_HANDER>;
- #close FILE_HANDER;
- my @LINE=<STDIN>;
- shift @LINE;
- my $now_time = localtime();
- my $log =1; #1 write log to /tmp/trap.log
- #----get network type------------
- 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"]
- );
- my $ip_str = &ipinfo($LINE[0]);
- my $network_description;
- my $network_info;
- my $def_tp;
- foreach $network_info(@Network_Def){
- #print "$network_info->[0];$network_info->[1]\n";
- my $def_tp = $network_info->[0];
- if ( $ip_str =~ /$def_tp/ ){
- $network_description = $network_info->[1];
- #print "IP:$ip_str;type:$tp;desp:$network_description;\n";
- }
- }
- #----------------------------------
- foreach (@LINE)
- {
- my $output;
- my $err_type;
- my $flag_print=0;
- my $ext_cmd;
- my $log_cmd;
- my $log_text;
- chomp $_;
- if ($_ =~ /err-disable/ )
- {
- if (($_ =~ /err-disable/ ) && ($_ =~ /error/ )) {
- my @arry_str = split("\"",$_);
- $output = "$arry_str[1]($ip_str)";
- $err_type = "error";
- $flag_print = 1;
- }
- if (($_ =~ /err-disable/ ) && ($_ =~ /recover/ )) {
- my @arry_str = split("\"",$_);
- $output = "$arry_str[1]($ip_str)";
- $err_type = "recover";
- $flag_print = 1;
- }
- my @arry_str = split("\"",$_);
- $log_text = "$arry_str[1]($ip_str)";
- }
- #print "$now_time;$ip_str;$err_type;$output\n" if ($flag_print);
- #print "/usr/local/nagios/eventhandlers/submit_check_result \"test-host\" \"err-disble\" \"1\" \"$output\"\n" if ($flag_print);
- #my $ext_cmd = "/usr/local/nagios/eventhandlers/submit_check_result \"test-host\" \"err-disble\" \"1\" \"$output\"\n" if ($flag_print);
- #print $ext_cmd;
- if ($flag_print) {
- $ext_cmd = "/usr/local/nagios/eventhandlers/submit_check_result \"err-disable-device\" \"$network_description\" \"2\" \"$output\"\n" if ($err_type =~ /error/);
- $ext_cmd = "/usr/local/nagios/eventhandlers/submit_check_result \"err-disable-device\" \"$network_description\" \"3\" \"$output\"\n" if ($err_type =~ /recover/);
- system $ext_cmd;
- #print $ext_cmd;
- if ($log) {
- $log_cmd = "/bin/echo \"$now_time;$ip_str($network_description);$err_type;$log_text\" >> /tmp/trap.log";
- #print $log_cmd,"\n";
- system $log_cmd;
- }
- }
-
- }
- #--------------------------------------------------
- sub ipinfo
- {
- my $st= @_[0];
- my $b = index($st,"[");
- my $e = index($st,"]");
- my $ok = substr($st,$b+1,$e - $b-1);
- return $ok;
- }
nagios_object_host.txt
nagios_object_host.txtnagios_object_host.txt服务检测对像
点击(此处)折叠或打开
- define host {
- use szHost
- host_name err-disable-device //固定名称,由脚本固定输出决定.
- address 127.0.0.1 //该值无意义
- notifications_enabled 0 //无需报警
- active_checks_enabled 0 //无需检测
- passive_checks_enabled 1 //无需检测
- check_freshness 0
- flap_detection_enabled 0
- notifications_enabled 0
- }
- define service{
- use szSRnet
- host_name err-disable-device //固定名称,由脚本固定输出决定.
- service_description sz-network //脚本中的"$Network_Def[n][1]"的值
- active_checks_enabled 0 //关闭“自动检测”
- passive_checks_enabled 1 //开启“被动检测”
- check_freshness 0 //关闭刷新服务。
- flap_detection_enabled 0 //关闭抖动处理,确保每个Trap都会报警.
- check_command check_ping //无意议,但必须有.
- max_check_attempts 1
- retry_check_interval 1
- is_volatile 1 //可变服务,允许状态值非0情况下的重复信息重复发送通知。并忽略notification_interval配置。
- contacts guofusheng //报警接收人员
- contact_groups null
- notification_options w,u,c,r,f,s //报警类型
- event_handler_enabled 0 //关闭事件处理,当is_volatile=1时,该配置被默认配置为1,可手工关闭。
- }
使用说明:
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
收到的报警信息如下: