]> git.pld-linux.org Git - packages/nagios-alert-jabber.git/blame - nagios-jabber.alert
- added support for fallback between multiple source accounts
[packages/nagios-alert-jabber.git] / nagios-jabber.alert
CommitLineData
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)
b6621e54 9# arekm@pld-linux.org, 2009-07 - added fallback accounts support
02673eb0 10# usage:
b6621e54 11# jabber.alert [-x] [-a account_id][,otheraccount_id] [-J from_jid -P password] to_jid1 to_jid2 to_jid3
02673eb0
ER
12
13import os
14import re
15import sys
16import getopt
eca05a2f 17import logging
02673eb0
ER
18import string
19import time
ebbef88a 20import libxml2
02673eb0
ER
21
22from pyxmpp.jid import JID
23from pyxmpp.message import Message
24from pyxmpp.jabber.client import JabberClient
ad7ac473 25from pyxmpp.streamtls import TLSSettings
02673eb0
ER
26
27try:
f985e683 28 opts, args = getopt.getopt(sys.argv[1:], "J:P:a:dx")
02673eb0 29except getopt.GetoptError, e:
b6621e54 30 print >> sys.stderr, "%s: %s " % (sys.argv[0], e)
02673eb0
ER
31 sys.exit(1)
32
b6621e54 33jids = []
ebbef88a 34html = False
f985e683 35debug = False
02673eb0 36
b6621e54 37tjid = None
02673eb0 38for o, a in opts:
f985e683
AM
39 if o == '-d':
40 debug = True
ebbef88a
ER
41 if o == '-x':
42 html = True
02673eb0 43 if o == '-J':
b6621e54 44 tjid = a
02673eb0 45 if o == '-P':
b6621e54 46 jids.append({ 'jid': tjid, 'password': a })
20bb0aca
ER
47 if o == '-a':
48 import ConfigParser
49
50 config = ConfigParser.ConfigParser()
51 config.read('/etc/nagios/jabber-notify.ini')
52
b6621e54
AM
53 for section in a.split(','):
54 jids.append({ 'jid': config.get(section, 'jid'), 'password': config.get(section, 'password')})
02673eb0
ER
55
56recpt = args
57
b6621e54
AM
58for section in jids:
59 if not section['jid'] or not section['password']:
60 print >> sys.stderr, "%s: jid (-J) and password (-P) are required for `%s'" % (sys.argv[0], section)
61 sys.exit(1)
62
63if not jids:
64 print >> sys.stderr, "%s: no configured jid accounts found" % sys.argv[0]
02673eb0
ER
65 sys.exit(1)
66
b6621e54
AM
67if not recpt:
68 print >> sys.stderr, "%s: recipient jids are required" % sys.argv[0]
02673eb0
ER
69 sys.exit(1)
70
f985e683
AM
71if debug:
72 logger=logging.getLogger()
73 logger.addHandler(logging.StreamHandler())
74 logger.setLevel(logging.DEBUG)
02673eb0
ER
75
76subject = "Nagios alert"
02673eb0
ER
77
78body = ""
79stdin_body = ""
80do_print = True
81for line in sys.stdin.readlines():
82 stdin_body += line
83
84body += stdin_body
85
8ccfe18b
AM
86if len(body.strip()) == 0:
87 body = "(nagios-jabber.alert warning: missing message body)";
88
02673eb0
ER
89message_type = 'chat'
90
02673eb0
ER
91class Client(JabberClient):
92 def session_started(self):
ebbef88a
ER
93 if (html == True):
94 import re
95 message = re.sub('<.*?>', '', body)
b538c51f
ER
96 doc = libxml2.parseDoc('<body>' + body + '</body>')
97 doc_element = doc.getRootElement().children
ebbef88a
ER
98 else:
99 message = body
100
02673eb0 101 for r in recpt:
ebbef88a 102
02673eb0 103 jid_r = JID(r)
ebbef88a
ER
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)
b538c51f 111 xbody.addChildList(doc_element.docCopyNodeList(xbody.doc))
ebbef88a 112
02673eb0
ER
113 self.stream.send(msg)
114 self.disconnect()
ad7ac473 115
eca05a2f
AM
116 def stream_state_changed(self,state,arg):
117 if debug:
118 print "*** State changed: %s %r ***" % (state,arg)
119
b6621e54
AM
120err = []
121for section in jids:
122 jid = JID(section['jid'])
123 if not jid.resource:
124 jid = JID(jid.node, jid.domain, "Nagios")
125
126 c = Client(jid, section['password'], auth_methods = ['sasl:DIGEST-MD5', 'sasl:PLAIN', 'digest'],
127 tls_settings = TLSSettings(require = False, verify_peer = False))
128 try:
129 c.connect()
130 try:
131 c.loop(1)
132 except Exception, e:
133 err.append("ERROR1: %s: %s" % (section['jid'], e))
134 c.disconnect()
135 continue
136 c.disconnect()
137 # stop after first succeeded attempt
138 sys.exit(0)
139 except Exception, e:
140 err.append("ERROR2: %s: %s" % (section['jid'], e))
141
142print >> sys.stderr, "\n".join(err)
143sys.exit(1)
This page took 0.057879 seconds and 4 git commands to generate.