Quantcast
Channel: Andrea Veri's Blog » Notifications
Viewing all articles
Browse latest Browse all 2

Nagios XMPP Notifications for GTalk

$
0
0

While improving GNOME’s servers Nagios Notifications,  I ended up working on a nice way to notify the relevant folks through GTalk in case something could go wrong on any of the hosted services. Looking around on the web, I found Seth Vidal’s script, modified it to suit my needs and made it working with GTalk, here’s the result:

#!/usr/bin/python -tt

import warnings
warnings.simplefilter("ignore")

import xmpp
from xmpp.protocol import Message

from optparse import OptionParser
import ConfigParser
import sys
import os

parser = OptionParser()
opts, args = parser.parse_args()

if len(args) < 1:
    print "xmppsend message [to whom, multiple args]"
    sys.exit(1)

msg = args[0]

msg = msg.replace('n', 'n')

# Connect to the server
c  =  xmpp.Client('gmail.com')
c.connect( ( 'talk.google.com', 5223 ) )

# Authenticate to the server
jid  =  xmpp.protocol.JID( 'example@gmail.com' )
c.auth( jid.getNode( ), 'yourgmailpassword' )

if len(args) < 2:
    r = c.getRoster()
    for user in r.keys():
        if user == username:
            continue
        c.send(Message(user, '%s' % msg))
else:
    for user in args[1:]:
        c.send(Message(user, '%s' % msg))

I, then, added the command definitions on the relevant Nagios configuration file:

define command{
        command_name    host-notify-by-xmpp
        command_line    /home/user/bin/xmppsend "Host '$HOSTALIAS$' is $HOSTSTATE$ - Info : $HOSTOUTPUT$" $CONTACTPAGER$
        }

define command{
        command_name    notify-by-xmpp
        command_line    /home/user/bin/xmppsend "$NOTIFICATIONTYPE$ $HOSTNAME$ $SERVICEDESC$ $SERVICESTATE$ $SERVICEOUTPUT$ $LONGDATETIME$" $CONTACTPAGER$
        }

And in the end on contacts.cfg:

define contact {
        contact_name    admin
        use             generic-contact
        alias           Full Name
        email           example@gmail.com
        pager           example@gmail.com
        service_notification_commands   notify-by-xmpp
        host_notification_commands      host-notify-by-xmpp
}

When done just reload the configuration files with:

sudo /etc/init.d/nagios3 reload

Enjoy your new XMPP Nagios notifications!

Update: if you don’t want the script to store your username or password, you can use the following modified script together with a nice config file like this one:

[xmpp_nagios]
username=example@gmail.com
password=yourgmailpassword

Then you can invoke xmppsend this way:

xmppsend -a config.ini

Viewing all articles
Browse latest Browse all 2

Trending Articles