packages: nagios-alert-jabber/nagios-jabber.alert - added support for fallb...

arekm arekm at pld-linux.org
Sun Jul 12 10:11:00 CEST 2009


Author: arekm                        Date: Sun Jul 12 08:10:59 2009 GMT
Module: packages                      Tag: HEAD
---- Log message:
- added support for fallback between multiple source accounts

---- Files affected:
packages/nagios-alert-jabber:
   nagios-jabber.alert (1.14 -> 1.15) 

---- Diffs:

================================================================
Index: packages/nagios-alert-jabber/nagios-jabber.alert
diff -u packages/nagios-alert-jabber/nagios-jabber.alert:1.14 packages/nagios-alert-jabber/nagios-jabber.alert:1.15
--- packages/nagios-alert-jabber/nagios-jabber.alert:1.14	Sun Sep  7 21:44:09 2008
+++ packages/nagios-alert-jabber/nagios-jabber.alert	Sun Jul 12 10:10:54 2009
@@ -6,8 +6,9 @@
 # glen at pld-linux.org,arekm at pld-linux.org, 2006-11-13 - added config file support
 # glen at pld-linux.org, 2006-12-07 - added html messages support (-x), thx goes to to jajcus
 # luzik at pld-linux.org, 2007-03 - added digest auth method(jabber.gda.pl)
+# arekm at pld-linux.org, 2009-07 - added fallback accounts support
 # usage:
-#   jabber.alert [-x] [-a account_id] [-J from_jid -P password] to_jid1 to_jid2 to_jid3
+#   jabber.alert [-x] [-a account_id][,otheraccount_id] [-J from_jid -P password] to_jid1 to_jid2 to_jid3
 
 import os
 import re
@@ -26,40 +27,45 @@
 try:
 	opts, args = getopt.getopt(sys.argv[1:], "J:P:a:dx")
 except getopt.GetoptError, e:
-	print "%s: %s " % (sys.argv[0], e)
+	print >> sys.stderr, "%s: %s " % (sys.argv[0], e)
 	sys.exit(1)
 
-jid = None
-password = None
+jids = []
 html = False
 debug = False
 
+tjid = None
 for o, a in opts:
 	if o == '-d':
 		debug = True
 	if o == '-x':
 		html = True
 	if o == '-J':
-		jid = a
+		tjid = a
 	if o == '-P':
-		password = a
+		jids.append({ 'jid': tjid, 'password': a })
 	if o == '-a':
 		import ConfigParser
 
 		config = ConfigParser.ConfigParser()
 		config.read('/etc/nagios/jabber-notify.ini')
 
-		jid = config.get(a, 'jid')
-		password = config.get(a, 'password')
+		for section in a.split(','):
+			jids.append({ 'jid': config.get(section, 'jid'), 'password': config.get(section, 'password')})
 
 recpt = args
 
-if jid == None or password == None:
-	print "%s: jid (-J) and password (-P) are required" % sys.argv[0]
+for section in jids:
+	if not section['jid'] or not section['password']:
+		print >> sys.stderr, "%s: jid (-J) and password (-P) are required for `%s'" % (sys.argv[0], section)
+		sys.exit(1)
+
+if not jids:
+	print >> sys.stderr, "%s: no configured jid accounts found" % sys.argv[0]
 	sys.exit(1)
 
-if recpt == None or len(recpt) == 0:
-	print "%s: recipient jids are required" % sys.argv[0]
+if not recpt:
+	print >> sys.stderr, "%s: recipient jids are required" % sys.argv[0]
 	sys.exit(1)
 
 if debug:
@@ -68,8 +74,6 @@
 	logger.setLevel(logging.DEBUG)
 
 subject = "Nagios alert"
-server = None
-port = None
 
 body = ""
 stdin_body = ""
@@ -84,10 +88,6 @@
 
 message_type = 'chat'
 
-jid = JID(jid)
-if not jid.resource:
-	jid = JID(jid.node, jid.domain, "Nagios")
-
 class Client(JabberClient):
 	def session_started(self):
 		if (html == True):
@@ -117,12 +117,27 @@
 		if debug:
 			print "*** State changed: %s %r ***" % (state,arg)
 
+err = []
+for section in jids:
+	jid = JID(section['jid'])
+	if not jid.resource:
+		jid = JID(jid.node, jid.domain, "Nagios")
+
+	c = Client(jid, section['password'], auth_methods = ['sasl:DIGEST-MD5', 'sasl:PLAIN', 'digest'],
+			tls_settings = TLSSettings(require = False, verify_peer = False))
+	try:
+		c.connect()
+		try:
+			c.loop(1)
+		except Exception, e:
+			err.append("ERROR1: %s: %s" % (section['jid'], e))
+			c.disconnect()
+			continue
+		c.disconnect()
+		# stop after first succeeded attempt
+		sys.exit(0)
+	except Exception, e:
+		err.append("ERROR2: %s: %s" % (section['jid'], e))
 
-c = Client(jid, password, server = server, port = port, auth_methods = ['sasl:DIGEST-MD5', 'sasl:PLAIN', 'digest'], tls_settings = TLSSettings(require = False, verify_peer = False))
-c.connect()
-try:
-	c.loop(1)
-except Exception, e:
-	print "ERROR: %s" % e
-	c.disconnect()
-c.disconnect()
+print >> sys.stderr, "\n".join(err)
+sys.exit(1)
================================================================

---- CVS-web:
    http://cvs.pld-linux.org/cgi-bin/cvsweb.cgi/packages/nagios-alert-jabber/nagios-jabber.alert?r1=1.14&r2=1.15&f=u



More information about the pld-cvs-commit mailing list