]> git.pld-linux.org Git - packages/nagios-alert-jabber.git/blame - nagios-jabber.alert
- add -d for debugging
[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)
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
12import os
13import re
14import sys
15import getopt
16import string
17import time
ebbef88a 18import libxml2
02673eb0
ER
19
20from pyxmpp.jid import JID
21from pyxmpp.message import Message
22from pyxmpp.jabber.client import JabberClient
ad7ac473 23from pyxmpp.streamtls import TLSSettings
02673eb0
ER
24
25try:
f985e683 26 opts, args = getopt.getopt(sys.argv[1:], "J:P:a:dx")
02673eb0
ER
27except getopt.GetoptError, e:
28 print "%s: %s " % (sys.argv[0], e)
29 sys.exit(1)
30
31jid = None
32password = None
ebbef88a 33html = False
f985e683 34debug = False
02673eb0
ER
35
36for 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
54recpt = args
55
56if jid == None or password == None:
57 print "%s: jid (-J) and password (-P) are required" % sys.argv[0]
58 sys.exit(1)
59
a28c1e4b 60if 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
64if debug:
65 logger=logging.getLogger()
66 logger.addHandler(logging.StreamHandler())
67 logger.setLevel(logging.DEBUG)
02673eb0
ER
68
69subject = "Nagios alert"
70server = None
71port = None
72
73body = ""
74stdin_body = ""
75do_print = True
76for line in sys.stdin.readlines():
77 stdin_body += line
78
79body += stdin_body
80
8ccfe18b
AM
81if len(body.strip()) == 0:
82 body = "(nagios-jabber.alert warning: missing message body)";
83
02673eb0
ER
84message_type = 'chat'
85
86jid = JID(jid)
87if not jid.resource:
88 jid = JID(jid.node, jid.domain, "Nagios")
89
90class 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 115c = 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
116c.connect()
117try:
118 c.loop(1)
8bbff2dd
ER
119except Exception, e:
120 print "ERROR: %s" % e
02673eb0
ER
121 c.disconnect()
122c.disconnect()
This page took 2.2515 seconds and 4 git commands to generate.