1 #!/usr/bin/python 2 # 3 # jabber_notify - simple script to send e.g. nagios notifications via 4 # XMPP to their destination 5 # (c)2008 Dennis Kaarsemaker <dennis@kaarsemaker.net> 6 # 7 # This program is free software: you can redistribute it and/or modify 8 # it under the terms of the GNU General Public License as published by 9 # the Free Software Foundation, either version 3 of the License, or 10 # (at your option) any later version. 11 # 12 # This program is distributed in the hope that it will be useful, 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # GNU General Public License for more details.
1 #!/usr/bin/python
2 #
3 # jabber_notify - simple script to send e.g. nagios notifications via
4 # XMPP to their destination
5 # (c)2008 Dennis Kaarsemaker <dennis@kaarsemaker.net>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 import optparse
21 import socket
22 import sys
23 import syslog
24 import time
25 try:
26 import xmpp
27 except ImportError:
28 print >>sys.stderr, "This program requires the python-xmpp library"
29 sys.exit(1)
30
31 def log(msg):
32 syslog.syslog(syslog.LOG_INFO, msg.strip())
33 msg = "%s %s\n" % (time.ctime(), msg.strip())
34 if sys.stderr.isatty():
35 sys.stderr.write(msg)
36
37 def send_msg(jid, pwd, msg, rcpt):
38 log("Sending message")
39 jid = xmpp.JID(jid)
40 clt = xmpp.Client(jid.getDomain(), debug = [])
41 if not clt.connect():
42 log("Unable to connect to jabber server")
43 return 2
44 if not clt.auth(jid.getNode(), pwd, resource=socket.gethostname()):
45 log("Could not authenticate")
46 return 3
47 log("Jabber session for %s established" % jid)
48 log("Sending message to %s" % rcpt)
49 clt.send(xmpp.Message(rcpt, body=msg, typ="chat"))
50
51 if __name__ == '__main__':
52 usage = """%prog [options]
53
54 %prog -j ... -p ... --msg ... --to ... will send a
55 message to the jid you specify"""
56 parser = optparse.OptionParser(usage=usage)
57 parser.add_option('-j','--jid', dest="jid", default=None,
58 help="Which jid should the script use", metavar="JID")
59 parser.add_option('-p','--password', dest="password", default=None,
60 help="Password for that jid", metavar="PASS")
61 parser.add_option('--msg', dest="message", default=None,
62 help="Specify a single message to send", metavar="MSG")
63 parser.add_option('--to', dest="recipient", default=None,
64 help="Where to send that message", metavar="JID")
65
66 options, args = parser.parse_args()
67 if not options.jid or not options.password or not options.recipient or not options.message:
68 parser.print_help()
69 sys.exit(1)
70 sys.exit(send_msg(options.jid, options.password, options.message, options.recipient))
Show all