]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/python -u | |
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) | |
9 | # usage: | |
10 | # jabber.alert [-x] [-a account_id] [-J from_jid -P password] to_jid1 to_jid2 to_jid3 | |
11 | ||
12 | import os | |
13 | import re | |
14 | import sys | |
15 | import getopt | |
16 | import string | |
17 | import time | |
18 | import libxml2 | |
19 | ||
20 | from pyxmpp.jid import JID | |
21 | from pyxmpp.message import Message | |
22 | from pyxmpp.jabber.client import JabberClient | |
23 | from pyxmpp.streamtls import TLSSettings | |
24 | ||
25 | try: | |
26 | opts, args = getopt.getopt(sys.argv[1:], "J:P:a:dx") | |
27 | except getopt.GetoptError, e: | |
28 | print "%s: %s " % (sys.argv[0], e) | |
29 | sys.exit(1) | |
30 | ||
31 | jid = None | |
32 | password = None | |
33 | html = False | |
34 | debug = False | |
35 | ||
36 | for o, a in opts: | |
37 | if o == '-d': | |
38 | debug = True | |
39 | if o == '-x': | |
40 | html = True | |
41 | if o == '-J': | |
42 | jid = a | |
43 | if o == '-P': | |
44 | password = a | |
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') | |
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 | ||
60 | if recpt == None or len(recpt) == 0: | |
61 | print "%s: recipient jids are required" % sys.argv[0] | |
62 | sys.exit(1) | |
63 | ||
64 | if debug: | |
65 | logger=logging.getLogger() | |
66 | logger.addHandler(logging.StreamHandler()) | |
67 | logger.setLevel(logging.DEBUG) | |
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 | ||
81 | if len(body.strip()) == 0: | |
82 | body = "(nagios-jabber.alert warning: missing message body)"; | |
83 | ||
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): | |
92 | if (html == True): | |
93 | import re | |
94 | message = re.sub('<.*?>', '', body) | |
95 | doc = libxml2.parseDoc('<body>' + body + '</body>') | |
96 | doc_element = doc.getRootElement().children | |
97 | else: | |
98 | message = body | |
99 | ||
100 | for r in recpt: | |
101 | ||
102 | jid_r = JID(r) | |
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) | |
110 | xbody.addChildList(doc_element.docCopyNodeList(xbody.doc)) | |
111 | ||
112 | self.stream.send(msg) | |
113 | self.disconnect() | |
114 | ||
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)) | |
116 | c.connect() | |
117 | try: | |
118 | c.loop(1) | |
119 | except Exception, e: | |
120 | print "ERROR: %s" % e | |
121 | c.disconnect() | |
122 | c.disconnect() |