Browse Source

Bugfix : display alert before pulling it from array -- asr

Gaétan RYCKEBOER 10 years ago
parent
commit
10ac5f0cd1
1 changed files with 72 additions and 54 deletions
  1. 72 54
      bin/nagios-fifo.pl

+ 72 - 54
bin/nagios-fifo.pl

@@ -35,7 +35,7 @@ our ( $FIFO,        # fifo absolute filename (expanded from Irssi config)
       $FIFO_HANDLE, # fifo filehandle for `open' et al.
       $FIFO_HANDLE, # fifo filehandle for `open' et al.
       $FIFO_TAG );  # fifo signal tag for `input_add'
       $FIFO_TAG );  # fifo signal tag for `input_add'
 
 
-$VERSION = "0.4.1";
+$VERSION = "0.4.2";
 %IRSSI = (
 %IRSSI = (
     authors     => 'asr',
     authors     => 'asr',
     contact     => 'root@lautre.net',
     contact     => 'root@lautre.net',
@@ -170,9 +170,9 @@ sub nagios_inject;
 # state : state string, from @NagStates or %state_to_color
 # state : state string, from @NagStates or %state_to_color
 sub format_alert_id($$$$); # ($Flag,$I,$state,$acked)
 sub format_alert_id($$$$); # ($Flag,$I,$state,$acked)
 
 
-# Add alert to array / index
-# returns array (ID,char) : ID of new alert (-1 if not inserted) ; char in !,c,?,+,-.
-sub insert_alert($$$$$$); #( $host, $svc, $state, $msg, $d )
+# is_alert : is this alert in the DB ?
+# returns ID of alert ; -1 if not.
+sub is_alert($$$$$$) ;
 
 
 
 
 ##########################################################################
 ##########################################################################
@@ -325,51 +325,23 @@ sub format_alert_id($$$$) {
 	}
 	}
 }
 }
 
 
-# Add alert to array / index
-sub insert_alert($$$$$$) {
-# returns ID of new alert ; -1 if not inserted.
+# is_alert : is this alert in the DB ?
+sub is_alert($$$$$$) {
+# returns ID of alert ; -1 if not.
 	#Irssi::print(">> On vire $host/$svc");
 	#Irssi::print(">> On vire $host/$svc");
 	my $i; 
 	my $i; 
 	my $acked;
 	my $acked;
 	( $host, $svc, $state, $msg, $d, $acked ) = @_;
 	( $host, $svc, $state, $msg, $d, $acked ) = @_;
 	# ALERT
 	# ALERT
-	if ( $state eq 'WARNING' || $state eq 'CRITICAL' || $state eq 'UNKNOWN' || $state eq 'DOWN' ) {
-		while ($i <= $#ACKS) {
-			# Update, or insert ?
-			if (($ACKS[$i]->[0] eq $host) && ($ACKS[$i]->[1] eq $svc)) {
-				$ACKS[$i]->[4]=$d;
-				Irssi::print(">> found $i");
-				if ($ACKS[$i]->[2] ne $state) {
-					# Same alert, but different level
-					$ACKS[$i]->[2]=$state;
-					return ($i+1,'c');
-				} else {
-					# Same alert
-					return ($i+1, '!');
-					# No need to display...
-				}
-			}
-			$i++;
-		}
-		# New alert, insert.
-		push (@ACKS, [ $host, $svc, $state, $msg, $d, $acked ] );
-		return (scalar @ACKS,'+');
-	# Clear alert
-	} else {
-#		Irssi::print(">> On vire $host/$svc");
-		$i=0;
-		# Find alert (need to be used by service/host index)
-		while ($i <= $#ACKS) {
-			if (($ACKS[$i]->[0] eq $host) && ($ACKS[$i]->[1] eq $svc)) {
-				$ACKS[$i] = pop @ACKS;
-				$i=$#ACKS;
-				Irssi::print(">> found $i");
-				return (-1,'-');
-			}
-			$i++;
+	while ($i <= $#ACKS) {
+		# Update, or insert ?
+		if (($ACKS[$i]->[0] eq $host) && ($ACKS[$i]->[1] eq $svc)) {
+			Irssi::print(">> found $i");
+			return $i;
 		}
 		}
-		return (-1,'?');
+		$i++;
 	}
 	}
+	return -1;
 }
 }
 
 
 
 
@@ -542,24 +514,62 @@ sub parse_nagios_log($){
 }
 }
 
 
 # Use alert fields to print and insert it
 # Use alert fields to print and insert it
-sub handle_alert($$$$$$$$) { # $d,$type,$host,$state,$output,$service,acked
-	my $option;
-	my $acked;
-
+sub handle_alert($$$$$$$$) { # $option,$d,$type,$host,$state,$output,$service,acked
         # Temporisation
         # Temporisation
+	my ($option,$acked);
 	($option,$d,$type,$host,$state,$output,$service,$acked)=@_;
 	($option,$d,$type,$host,$state,$output,$service,$acked)=@_;
 	next if exists $renot{"$host:$service"} && $renot{"$host:$service"} >= time() - 5;
 	next if exists $renot{"$host:$service"} && $renot{"$host:$service"} >= time() - 5;
 	$renot{"$host:$service"} = time();
 	$renot{"$host:$service"} = time();
 
 
-	# HOST or SERVICE ?
-	($id,$stat) = insert_alert($host,$service,$state,$output,$d,$acked);
+	my $id=is_alert ( $host, $service, $state, $output, $d, $acked ) ;
+	#( $host, $service, $state, $output, $d, $acked ) = @_;
+	my $acked;
 
 
 	# Silently ignore previously sent/acked alerts
 	# Silently ignore previously sent/acked alerts
-	Irssi::print(">> $nagios_ack_channel,$d,$id,$state,$acked,$host,$service,$output");
-	return if $id == -1;
+	Irssi::print(">> $stat,$d,$id,$state,$acked,$host,$service,$output");
 	$nagios_ack_channel= Irssi::settings_get_str("nagios_ack_channel");
 	$nagios_ack_channel= Irssi::settings_get_str("nagios_ack_channel");
-	display_alert(Irssi::active_server,$nagios_ack_channel,$id,$stat),
-		unless ($option eq "silent");
+	# ALERT
+	if ($id == -1) {
+		$id=scalar @ACKS;
+		if ( $state eq 'WARNING' || $state eq 'CRITICAL' || $state eq 'UNKNOWN' || $state eq 'DOWN' ) {
+			push (@ACKS, [ $host, $service, $state, $output, $d, $acked ] );
+			# display
+			display_alert(Irssi::active_server,$nagios_ack_channel,$id,'+'),
+				unless ($option eq "silent");
+			return ($#ACKS,'+');
+		} else {
+			push (@ACKS, [ $host, $service, $state, $output, $d, $acked ] );
+			# display
+			display_alert(Irssi::active_server,$nagios_ack_channel,$id,'?'),
+				unless ($option eq "silent");
+			pop @ACKS;
+			return ($#ACKS,'?');
+		}
+	} else {
+		if ( $state eq 'WARNING' || $state eq 'CRITICAL' || $state eq 'UNKNOWN' || $state eq 'DOWN' ) {
+			$ACKS[$id]->[4]=$d;
+			if ($ACKS[$id]->[2] ne $state) {
+				# Same alert, but different level
+				$ACKS[$id]->[2]=$state;
+				# display
+				display_alert(Irssi::active_server,$nagios_ack_channel,$id,'c'),
+					unless ($option eq "silent");
+				return ($id+1,'c');
+			} else {
+				# Same alert
+				return ($id+1, '!');
+				# No need to display...
+			}
+		# Clear alert
+		} else {
+			# display
+			display_alert(Irssi::active_server,$nagios_ack_channel,$id,'-'),
+				unless ($option eq "silent");
+			# Put the last- alert instead of existing one.
+			$ACKS[$id] = pop @ACKS;
+			return ($i,'-');
+		}
+	}
 }
 }
 
 
 ##########################################################################
 ##########################################################################
@@ -599,12 +609,12 @@ sub event_privmsg($$$$) {
 				$server->command ( "msg  ".$nagios_ack_channel." Local alerts cleared" );
 				$server->command ( "msg  ".$nagios_ack_channel." Local alerts cleared" );
 				@ACKS=();
 				@ACKS=();
 			}
 			}
-			if ($arg =~ /\bclear\b/) {
+			if ($arg =~ /\bsilent\b/) {
 				nagios_query_status("silent");
 				nagios_query_status("silent");
 			} else {
 			} else {
 				nagios_query_status("");
 				nagios_query_status("");
 				$server->command ( "msg  ".$nagios_ack_channel.
 				$server->command ( "msg  ".$nagios_ack_channel.
-				'Refresh nagios. Use "silent" keyword to disable display.');
+				' Refresh nagios. Use "silent" keyword to disable display.');
 			}
 			}
 			$server->command ( "msg  ".$nagios_ack_channel.
 			$server->command ( "msg  ".$nagios_ack_channel.
 				" ".scalar @ACKS." alertes");
 				" ".scalar @ACKS." alertes");
@@ -652,6 +662,14 @@ sub event_privmsg($$$$) {
 				" !nagios refresh [silent] [clear] : interroge le nagios pour avoir la liste de toutes les alertes");
 				" !nagios refresh [silent] [clear] : interroge le nagios pour avoir la liste de toutes les alertes");
 			$server->command ( "msg  ".$nagios_ack_channel.
 			$server->command ( "msg  ".$nagios_ack_channel.
 				"                                    si le mot clef 'clear' est ajouté, il purge les alertes locales");
 				"                                    si le mot clef 'clear' est ajouté, il purge les alertes locales");
+			$server->command ( "msg  ".$nagios_ack_channel.
+				" [!+00]/00                          le numéro et le statut d'alerte sont résumés dans le premier symbole :");
+			$server->command ( "msg  ".$nagios_ack_channel.
+				" [ ACK STATUT NUM ] / TOTAL         un '!' signifie 'nouvelle alerte, unack'. le \"statut\" peut être de la forme :");
+			$server->command ( "msg  ".$nagios_ack_channel.
+				"     ! : alerte identique. C : niveau d'alerte changé. - : alerte terminée. ? : 'feu vert' inconnu. + : nouvelle alerte");
+			$server->command ( "msg  ".$nagios_ack_channel.
+				"                                                                     ");
 		} elsif ( $arg =~ /^check ?(.*)/i ){
 		} elsif ( $arg =~ /^check ?(.*)/i ){
 			nagios_check($1,$server,undef);
 			nagios_check($1,$server,undef);
 		} elsif ( $arg =~ /^ack ?(.*)/i ){
 		} elsif ( $arg =~ /^ack ?(.*)/i ){