]>
Commit | Line | Data |
---|---|---|
02673eb0 ER |
1 | #!/usr/bin/python -u |
2 | # arekm@pld-linux.org, 2006-01 | |
3 | # glen@pld-linux.org, 2006-03-14 | |
ad7ac473 | 4 | # glen@pld-linux.org,arekm@pld-linux.org, 2006-10-30 - added ssl support (for gmail.com) |
8a0d8b38 | 5 | # glen@pld-linux.org 2006-11-03 - made it work with jabber.pld-linux.org again |
20bb0aca | 6 | # glen@pld-linux.org,arekm@pld-linux.org, 2006-11-13 - added config file support |
ebbef88a | 7 | # glen@pld-linux.org, 2006-12-07 - added html messages support (-x), thx goes to to jajcus |
071905aa | 8 | # luzik@pld-linux.org, 2007-03 - added digest auth method(jabber.gda.pl) |
02673eb0 | 9 | # usage: |
ebbef88a | 10 | # jabber.alert [-x] [-a account_id] [-J from_jid -P password] to_jid1 to_jid2 to_jid3 |
02673eb0 ER |
11 | |
12 | import os | |
13 | import re | |
14 | import sys | |
15 | import getopt | |
16 | import string | |
17 | import time | |
ebbef88a | 18 | import libxml2 |
02673eb0 ER |
19 | |
20 | from pyxmpp.jid import JID | |
21 | from pyxmpp.message import Message | |
22 | from pyxmpp.jabber.client import JabberClient | |
ad7ac473 | 23 | from pyxmpp.streamtls import TLSSettings |
02673eb0 ER |
24 | |
25 | try: | |
f985e683 | 26 | opts, args = getopt.getopt(sys.argv[1:], "J:P:a:dx") |
02673eb0 ER |
27 | except getopt.GetoptError, e: |
28 | print "%s: %s " % (sys.argv[0], e) | |
29 | sys.exit(1) | |
30 | ||
31 | jid = None | |
32 | password = None | |
ebbef88a | 33 | html = False |
f985e683 | 34 | debug = False |
02673eb0 ER |
35 | |
36 | for o, a in opts: | |
f985e683 AM |
37 | if o == '-d': |
38 | debug = True | |
ebbef88a ER |
39 | if o == '-x': |
40 | html = True | |
02673eb0 ER |
41 | if o == '-J': |
42 | jid = a | |
43 | if o == '-P': | |
44 | password = a | |
20bb0aca ER |
45 | if o == '-a': |
46 | import ConfigParser | |
47 | ||
48 | config = ConfigParser.ConfigParser() | |
49 | config.read('/etc/nagios/jabber-notify.ini') | |
50 | ||
51 | jid = config.get(a, 'jid') | |
52 | password = config.get(a, 'password') | |
02673eb0 ER |
53 | |
54 | recpt = args | |
55 | ||
56 | if jid == None or password == None: | |
57 | print "%s: jid (-J) and password (-P) are required" % sys.argv[0] | |
58 | sys.exit(1) | |
59 | ||
a28c1e4b | 60 | if recpt == None or len(recpt) == 0: |
02673eb0 ER |
61 | print "%s: recipient jids are required" % sys.argv[0] |
62 | sys.exit(1) | |
63 | ||
f985e683 AM |
64 | if debug: |
65 | logger=logging.getLogger() | |
66 | logger.addHandler(logging.StreamHandler()) | |
67 | logger.setLevel(logging.DEBUG) | |
02673eb0 ER |
68 | |
69 | subject = "Nagios alert" | |
70 | server = None | |
71 | port = None | |
72 | ||
73 | body = "" | |
74 | stdin_body = "" | |
75 | do_print = True | |
76 | for line in sys.stdin.readlines(): | |
77 | stdin_body += line | |
78 | ||
79 | body += stdin_body | |
80 | ||
8ccfe18b AM |
81 | if len(body.strip()) == 0: |
82 | body = "(nagios-jabber.alert warning: missing message body)"; | |
83 | ||
02673eb0 ER |
84 | message_type = 'chat' |
85 | ||
86 | jid = JID(jid) | |
87 | if not jid.resource: | |
88 | jid = JID(jid.node, jid.domain, "Nagios") | |
89 | ||
90 | class Client(JabberClient): | |
91 | def session_started(self): | |
ebbef88a ER |
92 | if (html == True): |
93 | import re | |
94 | message = re.sub('<.*?>', '', body) | |
b538c51f ER |
95 | doc = libxml2.parseDoc('<body>' + body + '</body>') |
96 | doc_element = doc.getRootElement().children | |
ebbef88a ER |
97 | else: |
98 | message = body | |
99 | ||
02673eb0 | 100 | for r in recpt: |
ebbef88a | 101 | |
02673eb0 | 102 | jid_r = JID(r) |
ebbef88a ER |
103 | msg = Message(to_jid = jid_r, body = message, subject = subject, stanza_type = message_type) |
104 | ||
105 | if (html == True): | |
106 | node = msg.add_new_content('http://jabber.org/protocol/xhtml-im', 'html') | |
107 | xbody = node.newChild(None, "body", None) | |
108 | html_ns = xbody.newNs('http://www.w3.org/1999/xhtml', None) | |
109 | xbody.setNs(html_ns) | |
b538c51f | 110 | xbody.addChildList(doc_element.docCopyNodeList(xbody.doc)) |
ebbef88a | 111 | |
02673eb0 ER |
112 | self.stream.send(msg) |
113 | self.disconnect() | |
ad7ac473 | 114 | |
0dea86c2 | 115 | c = Client(jid, password, server = server, port = port, auth_methods = ['sasl:DIGEST-MD5', 'sasl:PLAIN', 'digest'], tls_settings = TLSSettings(require = False, verify_peer = False)) |
02673eb0 ER |
116 | c.connect() |
117 | try: | |
118 | c.loop(1) | |
8bbff2dd ER |
119 | except Exception, e: |
120 | print "ERROR: %s" % e | |
02673eb0 ER |
121 | c.disconnect() |
122 | c.disconnect() |