Browse Source

Add silent mode -- ASR

Gaétan RYCKEBOER 10 years ago
parent
commit
6d8206559b
1 changed files with 28 additions and 17 deletions
  1. 28 17
      bin/nagios-fifo.pl

+ 28 - 17
bin/nagios-fifo.pl

@@ -35,7 +35,7 @@ our ( $FIFO,        # fifo absolute filename (expanded from Irssi config)
 
 # probably not very useful for other people but who knows?
 
-$VERSION = "0.2.0";
+$VERSION = "0.2.2";
 %IRSSI = (
     authors     => 'asr',
     contact     => 'root@lautre.net',
@@ -191,9 +191,9 @@ sub mark($$$) {
 	my $M=sprintf('%02d', scalar @ACKS % 100);
 	my $N=sprintf('%02d', $I % 100);
 	if (($I == 0) && (scalar @ACKS == 0)) {
-		return "[".$C{W}.$Flag.$C{Z}."  ]/$M ";
+		return "[".$Flag.$Flag."  ]/$M ";
 	} else {
-		return "[".$C{W}.$Flag.$state_to_color->{$state}.$N.$C{Z}."]/$M ";
+		return "[".$Flag.$Flag.$state_to_color->{$state}.$N.$C{Z}."]/$M ";
 	}
 }
 
@@ -216,6 +216,7 @@ sub ackable {
 				} else { 
 					# Same alert
 					return mark ('!', $i, $state);
+					# No need to display...
 				}
 			}
 			$i++;
@@ -332,14 +333,14 @@ sub create_fifo($) {                           # [2004-08-14]
 # Query nagios ###########################################################
 sub query_nagios_status($){
 	# 1412478149;hyppocampe;apt;2;CRITICAL : unknown: qemu-utils, qemu-kvm, qemu-keymaps
-	my ($server)=@_;
+	my ($option)=@_;
 	my @unixcat; 
 	my $unixline;
         foreach $unixline ( `echo "GET services
 Columns: last_state_change host_name display_name state plugin_output
 Filter: state > 1" | unixcat $nagioscmd` ) {
 		($d,$host,$service,$state,$output)=split /;/,$unixline;
-		validate_alert($d,'SERVICE',$host,$NagStates[$state+10],$output,$service);
+		validate_alert($option,$d,'SERVICE',$host,$NagStates[$state+10],$output,$service);
 		Irssi::print("%B>>%n $unixline", MSGLEVEL_CLIENTCRAP);
 	}
 
@@ -348,7 +349,7 @@ Filter: state > 1" | unixcat $nagioscmd` ) {
 Columns: last_state_change host_name state plugin_output
 Filter: state > 1" | unixcat $nagioscmd` ) {
 		($d,$host,$state,$output)=split /;/,$unixline;
-		validate_alert($d,'HOST',$host,$NagStates[$state],$output,"");
+		validate_alert($option,$d,'HOST',$host,$NagStates[$state],$output,"");
 		Irssi::print("%B>>%n $unixline", MSGLEVEL_CLIENTCRAP);
 	}
 
@@ -361,6 +362,7 @@ Filter: state > 1" | unixcat $nagioscmd` ) {
 
 # Parse alert to find vars ###############################################
 sub parse_naglog(){
+	my $option="";
 	$d=0; $status_line=""; $host="";$output="";$service="";
 # Search for services/host informations, and post them to IRC ############
 #
@@ -373,14 +375,14 @@ sub parse_naglog(){
 	if (@match=$status_line =~ /\[?(\d+)\]? HOST ALERT: ([^@;]+)[@;](\w+)[@;]HARD[@;].*[@;](.+)/) {
 	# HOST ########################
 		($d,$host,$state,$output)=@match;
-		validate_alert($d,"HOST",$host,$state,$output,$service);
+		validate_alert($option,$d,"HOST",$host,$state,$output,$service);
 		
 	# SERVICE #####################
 	} elsif (@match=$status_line =~ /\[?(\d+)\]? (\w+) ALERT: ([^;@]+)[@;]([^;@]+)[@;](\w+)[@;]HARD[@;].*[@;](.+)/) {
 		($d,$type,$host,$service,$state,$output)=@match;
 
 		$service=~s/[^\w\d_-]/_/g;
-		validate_alert($d,$type,$host,$state,$output,$service);
+		validate_alert($option,$d,$type,$host,$state,$output,$service);
 
 	# OTHER #######################
 	} elsif (@match=$status_line =~ /\[\d+\] (\w+) ALERT: (.*)/) {
@@ -395,8 +397,9 @@ sub parse_naglog(){
 	}
 }
 
-sub validate_alert($$$$$$) { # $d,$type,$host,$state,$output,$service
-	($d,$type,$host,$state,$output,$service)=@_;
+sub validate_alert($$$$$$$) { # $d,$type,$host,$state,$output,$service
+	my $option;
+	($option,$d,$type,$host,$state,$output,$service)=@_;
 	next if exists $renot{"$host:$service"} && $renot{"$host:$service"} >= time() - 5;
 	$renot{"$host:$service"} = time();
 
@@ -406,11 +409,13 @@ sub validate_alert($$$$$$) { # $d,$type,$host,$state,$output,$service
 	$msg = "$id".$state_to_color->{$state} . "$host:$service".$C{Z}." is $state : $output";
 	#Irssi::print( "%B>>%n $IRSSI{name} $msg", MSGLEVEL_CLIENTCRAP);		
 
-	$last_alert="NAGIOS - $service/$host is $state";
+	$last_alert="NAGIOS - $host/$service is $state";
 	$d=time2date($d);
 	$message = " $d - $last_alert - $msg";
+	# Silently ignore previously sent alerts
+	return if $id =~ '\[!' ;
 	Irssi::active_server->command('MSG ' . Irssi::settings_get_str("nagios_ack_channel") .
-		" ".$message);
+		" ".$message) unless ($option eq "silent");
 	#		Irssi::print( "%B>>%n $IRSSI{name} $msg", MSGLEVEL_CLIENTCRAP);		
 }
 
@@ -423,15 +428,21 @@ sub event_privmsg {
         #print ( "C:$target X:$text A:$admin D:$warndate L:$last W:$warn N:$nick D:$data" );
         if ( $text =~ /^!nagios ?(.*)/i ) {
 		$arg=$1;
-		Irssi::print(">> $arg");
 		# !nagios [*] :
 		$server->command ( "msg  ".Irssi::settings_get_str("nagios_ack_channel").
 			" ".scalar @ACKS." alertes");
 		
 		# !nagios list [*] : 
-		if ($arg =~ /^refresh/i) {
-		Irssi::print(">> $arg");
-			query_nagios_status($server);
+		if ($arg =~ /^refresh ?(.*)/i) {
+			$arg=$1;
+			Irssi::print(">> $arg");
+			if ($arg =~ /^silent/) {
+				query_nagios_status("silent");
+			} else {
+				query_nagios_status("");
+				$server->command ( "msg  ".Irssi::settings_get_str("nagios_ack_channel").
+				'Refresh nagios. Use "silent" keyword to disable display.');
+			}
 		}
 		elsif ($arg =~ /^list/i) {
 		Irssi::print(">> $arg");
@@ -453,7 +464,7 @@ sub event_privmsg {
 			$server->command ( "msg  ".Irssi::settings_get_str("nagios_ack_channel").
 				" !nagios check <#ALERTE> : recheck une alerte donnée (service/host)");
 			$server->command ( "msg  ".Irssi::settings_get_str("nagios_ack_channel").
-				" !nagios refresh : interroge le nagios pour avoir la liste de toutes les alertes");
+				" !nagios refresh [silent] : interroge le nagios pour avoir la liste de toutes les alertes");
 		} elsif ( $arg =~ /^check ?(.*)/i ){
 		Irssi::print(">> $arg");
 			nagios_check($1,$server,undef);