A quickie post detailing a perl script for Nagios that will broadcast the alert details over the xAP network!
It will send xAP messages like this:
xap-header { v=12 hop=1 UID=FF969900 class=nagios.alert Source=ahs.nagios.ahssheevaplug1 } alert.details { event=Host Alert type=Failure host=test.aceshigh.local hostaddress=192.168.1.1 state=down service=NONE info=fubar date=2012-01-24 time=12:00:00 }
The host command for Nagios should look like this:
define command { command_name notify-host-by-xap command_line /usr/bin/perl -w /usr/lib/nagios/plugins/xap.pl -application="Nagios" -event="Host Alert" -type="$NOTIFICATIONTYPE$" -host="$HOSTALIAS$" -hostaddress="$HOSTADDRESS$" -state="$HOSTSTATE$" -service="NONE" -info="$HOSTOUTPUT$" -date="$DATE$" -time="$TIME$" }
The service command for Nagios should look like this:
define command { command_name notify-service-by-xap command_line /usr/bin/perl -w /usr/lib/nagios/plugins/xap.pl -application="Nagios" -event="Service Alert" -type="$NOTIFICATIONTYPE$" -host="$HOSTALIAS$" -hostaddress="$HOSTADDRESS$" -state="$SERVICESTATE$" -service="$SERVICEDESC$" -info="$SERVICEOUTPUT$" -date="$DATE$" -time="$TIME$" }
Here’s the perl script:
#!/usr/bin/perl # # xAP Nagios Alert Sender # # Version: 0.1 # #define command { # command_name notify-host-by-xap # command_line /usr/bin/perl -w /usr/lib/nagios/plugins/xap.pl -application="Nagios" -event="Host Alert" -type="$NOTIFICATIONTYPE$" -host="$HOSTALIAS$" -hostaddress="$HOSTADDRESS$" -state="$HOSTSTATE$" -service="NONE" -info="$HOSTOUTPUT$" -date="$DATE$" -time="$TIME$" #} # #define command { # command_name notify-service-by-xap # command_line /usr/bin/perl -w /usr/lib/nagios/plugins/xap.pl -application="Nagios" -event="Service Alert" -type="$NOTIFICATIONTYPE$" -host="$HOSTALIAS$" -hostaddress="$HOSTADDRESS$" -state="$SERVICESTATE$" -service="$SERVICEDESC$" -info="$SERVICEOUTPUT$" -date="$DATE$" -time="$TIME$" #} # #./xap.pl -application="Nagios" -event="Host Alert" -type="Failure" -host="test.aceshigh.local" -hostaddress="192.168.1.1" -state="down" -service="NONE" -info="fubar" -date="2012-01-24" -time="12:00:00" # #xap-header #{ #v=12 #hop=1 #UID=FF969900 #class=nagios.alert #Source=ahs.nagios.ahssheevaplug1 #} #alert.details #{ #event=Host Alert #type=Failure #host=test.aceshigh.local #hostaddress=192.168.1.1 #state=down #service=NONE #info=fubar #date=2012-01-24 #time=12:00:00 #} use warnings; use strict; use IO::Socket::INET; use Log::Log4perl; use Getopt::Long; my $xap_base_uid = "FF9699"; my $xap_uid = "00"; my $xap_base_name = "ahs.nagios.ahssheevaplug1"; my $xap_broadcast = '255.255.255.255'; my $xap_port = 3639; my $debug = 1; my $log_conf = q~ log4perl.category = INFO, Logfile, Screen log4perl.appender.Logfile = Log::Dispatch::FileRotate log4perl.appender.Logfile.filename = /var/log/nagios3/xap.log log4perl.appender.Logfile.max = 7 log4perl.appender.Logfile.DatePattern = yyyy-MM-dd log4perl.appender.Logfile.TZ = GMT log4perl.appender.Logfile.mode = write log4perl.appender.Logfile.layout = Log::Log4perl::Layout::PatternLayout log4perl.appender.Logfile.layout.ConversionPattern = %d %p> %m%n log4perl.appender.Screen = Log::Log4perl::Appender::Screen log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout log4perl.appender.Screen.layout.ConversionPattern = %d %p> %m%n ~; Log::Log4perl::init( \$log_conf ); my $logger = Log::Log4perl::get_logger(); my $xap_client = IO::Socket::INET->new(PeerAddr => $xap_broadcast, PeerPort => $xap_port, Proto => 'udp', Broadcast => 1); my $i = 1; my %options = (); GetOptions(\%options, 'application=s', 'event=s', 'type=s', 'host=s', 'hostaddress=s', 'state=s', 'service=s', 'info=s', 'date=s', 'time=s'); &send_event($options{'application'}, $options{'event'}, $options{'type'}, $options{'host'}, $options{'hostaddress'}, $options{'state'}, $options{'service'}, $options{'info'}, $options{'date'}, $options{'time'}); $xap_client->close(); sub send_event { my ($application, $event, $type, $host, $hostaddress, $state, $service, $info, $date, $time) = @_; $logger->info("<send_event> application $application") if $debug; $logger->info("<send_event> event $event") if $debug; $logger->info("<send_event> type $type") if $debug; $logger->info("<send_event> host $host") if $debug; $logger->info("<send_event> hostaddress $hostaddress") if $debug; $logger->info("<send_event> state $state") if $debug; $logger->info("<send_event> service $service") if $debug; $logger->info("<send_event> info $info") if $debug; $logger->info("<send_event> date $date") if $debug; $logger->info("<send_event> time $time") if $debug; my $msg = "xap-header\n"; $msg .= "{\n"; $msg .= "v=12\n"; $msg .= "hop=1\n"; $msg .= "uid=$xap_base_uid$xap_uid\n"; $msg .= "class=" . cleanstring($application) . ".alert\n"; $msg .= "source=$xap_base_name\n"; $msg .= "}\n"; $msg .= "alert.details\n"; $msg .= "{\n"; $msg .= "event=" . cleanstring($event) . "\n"; $msg .= "type=" . cleanstring($type) . "\n"; $msg .= "host=" . cleanstring($host) . "\n"; $msg .= "hostaddress=" . cleanstring($hostaddress) . "\n"; $msg .= "state=" . cleanstring($state) . "\n"; $msg .= "service=" . cleanstring($service) . "\n"; $msg .= "info=" . cleanstring($info) . "\n"; $msg .= "date=" . cleanstring($date) . "\n"; $msg .= "time=" . cleanstring($time) . "\n"; $msg .= "}\n"; $xap_client->send($msg); $logger->info("<send_event> sending \n$msg") if $debug; } sub cleanstring { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; $string =~ s/[^A-Za-z0-9\.\,\-\%\: ]+//g; $string =~ s/\:$//g; return lc($string); }
It can also be downloaded by clicking xap.pl
Hope you find it useful!
Thanks for reading,
Martyn Wendon