Nagios jabber Configuration

Nagios jabber notification using XMPP protocol and SASL PLAIN authentication

Nagios can be configured to send notifications by various means, including Jabber, which these days includes Google talk’s XMPP service.

 

1)  Create notify_via_jabber plugin

#########################################################

#!/usr/bin/perl -w
#
# script for nagios notify via Jabber / Google Talk Instant Messaging
#   using XMPP protocol and SASL PLAIN authentication.
#
# author: Andrew Elwell <A.Elwell@physics.gla.ac.uk>
# based on work by Thus0 <Thus0@free.fr> and  David Cox
#
# released under the terms of the GNU General Public License v2
# Copyright 2007 Andrew Elwell.
use strict;
use Net::XMPP;
## Configuration
my $username = "your.google.username";
my $password = "your.google.password";
my $resource = "nagios";
## End of configuration
my $len = scalar @ARGV;
if ($len ne 2) {
   die "Usage...\n $0 [jabberid] [message]\n";
}
my @field=split(/,/,$ARGV[0]);
#------------------------------------
# Google Talk & Jabber parameters :
my $hostname = 'talk.google.com';
my $port = 5222;
my $componentname = 'gmail.com';
my $connectiontype = 'tcpip';
my $tls = 1;
#------------------------------------
my $Connection = new Net::XMPP::Client();
# Connect to talk.google.com
my $status = $Connection->Connect(
       hostname => $hostname, port => $port,
       componentname => $componentname,
       connectiontype => $connectiontype, tls => $tls);
if (!(defined($status))) {
   print "ERROR:  XMPP connection failed.\n";
   print "        ($!)\n";
   exit(0);
}
# Change hostname
my $sid = $Connection->{SESSION}->{id};
$Connection->{STREAM}->{SIDS}->{$sid}->{hostname} = $componentname;
# Authenticate
my @result = $Connection->AuthSend(
       username => $username, password => $password,
       resource => $resource);
if ($result[0] ne "ok") {
   print "ERROR: Authorization failed: $result[0] - $result[1]\n";
   exit(0);
}
# Send messages
foreach ( @field ) {
$Connection->MessageSend(
        to       => "$_\@$componentname",
        resource => $resource,
        subject  => "Notification",
        type     => "chat",
        body     => $ARGV[1]);
}

#########################################################

 

2) We need to configure new command in command.cfg as follows

#Host Notifications

define command{
 command_name host-notify-by-jabber
 command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$ \nState: $HOSTSTATE$\n Address: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/lib/nagios/plugins/notify_via_jabber $CONTACTPAGER$ "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **"
 }

# Service notifications

define command{
 command_name notify-by-jabber
 command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$ \nState: $HOSTSTATE$\n Address: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/lib/nagios/plugins/notify_via_jabber $CONTACTPAGER$ "** $NOTIFICATIONTYPE$ Service Alert: $SERVICEDESC$ on $HOSTNAME$ State: $SERVICESTATE$ **"
 }

 

3)  Verify the new configuration

nagios -v /etc/nagios/nagios.cfg

 

4)  Restart Nagios

If everything is configured fine then nagios will alert you about host/service status to the given address.