]> git.pld-linux.org Git - packages/nagios-alert-jabber.git/blob - nagios-jabber.alert
fe409769cc615e63983886a8cfe7bc4c718fc540
[packages/nagios-alert-jabber.git] / nagios-jabber.alert
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 logging
17 import string
18 import time
19 import libxml2
20
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
25
26 try:
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)
30         sys.exit(1)
31
32 jid = None
33 password = None
34 html = False
35 debug = False
36
37 for o, a in opts:
38         if o == '-d':
39                 debug = True
40         if o == '-x':
41                 html = True
42         if o == '-J':
43                 jid = a
44         if o == '-P':
45                 password = a
46         if o == '-a':
47                 import ConfigParser
48
49                 config = ConfigParser.ConfigParser()
50                 config.read('/etc/nagios/jabber-notify.ini')
51
52                 jid = config.get(a, 'jid')
53                 password = config.get(a, 'password')
54
55 recpt = args
56
57 if jid == None or password == None:
58         print "%s: jid (-J) and password (-P) are required" % sys.argv[0]
59         sys.exit(1)
60
61 if recpt == None or len(recpt) == 0:
62         print "%s: recipient jids are required" % sys.argv[0]
63         sys.exit(1)
64
65 if debug:
66         logger=logging.getLogger()
67         logger.addHandler(logging.StreamHandler())
68         logger.setLevel(logging.DEBUG)
69
70 subject = "Nagios alert"
71 server = None
72 port = None
73
74 body = ""
75 stdin_body = ""
76 do_print = True
77 for line in sys.stdin.readlines():
78         stdin_body += line
79
80 body += stdin_body
81
82 if len(body.strip()) == 0:
83         body = "(nagios-jabber.alert warning: missing message body)";
84
85 message_type = 'chat'
86
87 jid = JID(jid)
88 if not jid.resource:
89         jid = JID(jid.node, jid.domain, "Nagios")
90
91 class Client(JabberClient):
92         def session_started(self):
93                 if (html == True):
94                         import re
95                         message = re.sub('<.*?>', '', body)
96                         doc = libxml2.parseDoc('<body>' + body + '</body>')
97                         doc_element = doc.getRootElement().children
98                 else:
99                         message = body
100
101                 for r in recpt:
102
103                         jid_r = JID(r)
104                         msg = Message(to_jid = jid_r, body = message, subject = subject, stanza_type = message_type)
105
106                         if (html == True):
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)
110                                 xbody.setNs(html_ns)
111                                 xbody.addChildList(doc_element.docCopyNodeList(xbody.doc))
112
113                         self.stream.send(msg)
114                 self.disconnect()
115
116         def stream_state_changed(self,state,arg):
117                 if debug:
118                         print "*** State changed: %s %r ***" % (state,arg)
119
120
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))
122 c.connect()
123 try:
124         c.loop(1)
125 except Exception, e:
126         print "ERROR: %s" % e
127         c.disconnect()
128 c.disconnect()
This page took 0.136867 seconds and 3 git commands to generate.