2 # arekm@pld-linux.org, 2006-01
3 # glen@pld-linux.org, 2006-03-14
4 # glen@pld-linux.org,arekm@pld-linux.org, 2006-10-30 - added ssl support (for gmail.com)
5 # glen@pld-linux.org 2006-11-03 - made it work with jabber.pld-linux.org again
6 # glen@pld-linux.org,arekm@pld-linux.org, 2006-11-13 - added config file support
7 # glen@pld-linux.org, 2006-12-07 - added html messages support (-x), thx goes to to jajcus
8 # luzik@pld-linux.org, 2007-03 - added digest auth method(jabber.gda.pl)
10 # jabber.alert [-x] [-a account_id] [-J from_jid -P password] to_jid1 to_jid2 to_jid3
21 from pyxmpp.jid import JID
22 from pyxmpp.message import Message
23 from pyxmpp.jabber.client import JabberClient
24 from pyxmpp.streamtls import TLSSettings
27 opts, args = getopt.getopt(sys.argv[1:], "J:P:a:dx")
28 except getopt.GetoptError, e:
29 print "%s: %s " % (sys.argv[0], e)
49 config = ConfigParser.ConfigParser()
50 config.read('/etc/nagios/jabber-notify.ini')
52 jid = config.get(a, 'jid')
53 password = config.get(a, 'password')
57 if jid == None or password == None:
58 print "%s: jid (-J) and password (-P) are required" % sys.argv[0]
61 if recpt == None or len(recpt) == 0:
62 print "%s: recipient jids are required" % sys.argv[0]
66 logger=logging.getLogger()
67 logger.addHandler(logging.StreamHandler())
68 logger.setLevel(logging.DEBUG)
70 subject = "Nagios alert"
77 for line in sys.stdin.readlines():
82 if len(body.strip()) == 0:
83 body = "(nagios-jabber.alert warning: missing message body)";
89 jid = JID(jid.node, jid.domain, "Nagios")
91 class Client(JabberClient):
92 def session_started(self):
95 message = re.sub('<.*?>', '', body)
96 doc = libxml2.parseDoc('<body>' + body + '</body>')
97 doc_element = doc.getRootElement().children
104 msg = Message(to_jid = jid_r, body = message, subject = subject, stanza_type = message_type)
107 node = msg.add_new_content('http://jabber.org/protocol/xhtml-im', 'html')
108 xbody = node.newChild(None, "body", None)
109 html_ns = xbody.newNs('http://www.w3.org/1999/xhtml', None)
111 xbody.addChildList(doc_element.docCopyNodeList(xbody.doc))
113 self.stream.send(msg)
116 def stream_state_changed(self,state,arg):
118 print "*** State changed: %s %r ***" % (state,arg)
121 c = Client(jid, password, server = server, port = port, auth_methods = ['sasl:DIGEST-MD5', 'sasl:PLAIN', 'digest'], tls_settings = TLSSettings(require = False, verify_peer = False))
126 print "ERROR: %s" % e