#!/usr/bin/python # # jabber_notify - simple script to send e.g. nagios notifications via # XMPP to their destination # (c)2008 Dennis Kaarsemaker # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import optparse import socket import sys import syslog import time try: import xmpp except ImportError: print >>sys.stderr, "This program requires the python-xmpp library" sys.exit(1) def log(msg): syslog.syslog(syslog.LOG_INFO, msg.strip()) msg = "%s %s\n" % (time.ctime(), msg.strip()) if sys.stderr.isatty(): sys.stderr.write(msg) def send_msg(jid, pwd, msg, rcpt): log("Sending message") jid = xmpp.JID(jid) clt = xmpp.Client(jid.getDomain(), debug = []) if not clt.connect(): log("Unable to connect to jabber server") return 2 if not clt.auth(jid.getNode(), pwd, resource=socket.gethostname()): log("Could not authenticate") return 3 log("Jabber session for %s established" % jid) log("Sending message to %s" % rcpt) clt.send(xmpp.Message(rcpt, body=msg, typ="chat")) if __name__ == '__main__': usage = """%prog [options] %prog -j ... -p ... --msg ... --to ... will send a message to the jid you specify""" parser = optparse.OptionParser(usage=usage) parser.add_option('-j','--jid', dest="jid", default=None, help="Which jid should the script use", metavar="JID") parser.add_option('-p','--password', dest="password", default=None, help="Password for that jid", metavar="PASS") parser.add_option('--msg', dest="message", default=None, help="Specify a single message to send", metavar="MSG") parser.add_option('--to', dest="recipient", default=None, help="Where to send that message", metavar="JID") options, args = parser.parse_args() if not options.jid or not options.password or not options.recipient or not options.message: parser.print_help() sys.exit(1) sys.exit(send_msg(options.jid, options.password, options.message, options.recipient))