]> git.pld-linux.org Git - packages/nagios-alert-jabber.git/blame_incremental - nagios-jabber.alert
- add -d for debugging
[packages/nagios-alert-jabber.git] / nagios-jabber.alert
... / ...
CommitLineData
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
12import os
13import re
14import sys
15import getopt
16import string
17import time
18import libxml2
19
20from pyxmpp.jid import JID
21from pyxmpp.message import Message
22from pyxmpp.jabber.client import JabberClient
23from pyxmpp.streamtls import TLSSettings
24
25try:
26 opts, args = getopt.getopt(sys.argv[1:], "J:P:a:dx")
27except getopt.GetoptError, e:
28 print "%s: %s " % (sys.argv[0], e)
29 sys.exit(1)
30
31jid = None
32password = None
33html = False
34debug = False
35
36for 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
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
60if recpt == None or len(recpt) == 0:
61 print "%s: recipient jids are required" % sys.argv[0]
62 sys.exit(1)
63
64if debug:
65 logger=logging.getLogger()
66 logger.addHandler(logging.StreamHandler())
67 logger.setLevel(logging.DEBUG)
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
81if len(body.strip()) == 0:
82 body = "(nagios-jabber.alert warning: missing message body)";
83
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):
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
115c = Client(jid, password, server = server, port = port, auth_methods = ['sasl:DIGEST-MD5', 'sasl:PLAIN', 'digest'], tls_settings = TLSSettings(require = False, verify_peer = False))
116c.connect()
117try:
118 c.loop(1)
119except Exception, e:
120 print "ERROR: %s" % e
121 c.disconnect()
122c.disconnect()
This page took 0.072862 seconds and 4 git commands to generate.