commit 257cdecd13da28d11f59cc7b6e51262c292cbcb9
Author: Mariusz Mazur <[email protected]>
Date:   Tue Nov 27 13:53:40 2012 +0100

    rel 8; try a second send if there's a resource name conflict
    
    If sending fails due to stream error, most likely due to a resource name
    conflict, wait a second and try to send again with a randomized resource
    name.
    For jabber clients with <thread> support these two past changes won't be
    noticeable, however for the rest they'll mean that 99% of the times
    msgs come from a constant jid/resource pair with only a very occasional
    msg coming from a randomized resource.

 nagios-alert-jabber.spec |  2 +-
 nagios-jabber.alert      | 61 ++++++++++++++++++++++++++++++++++--------------
 2 files changed, 44 insertions(+), 19 deletions(-)
---
diff --git a/nagios-alert-jabber.spec b/nagios-alert-jabber.spec
index 0ba5e6a..3f23d4f 100644
--- a/nagios-alert-jabber.spec
+++ b/nagios-alert-jabber.spec
@@ -2,7 +2,7 @@ Summary:        Program to send (Nagios) alerts via jabber
 Summary(pl.UTF-8):     Program do wysyłania alarmów (Nagiosa) przez jabbera
 Name:          nagios-alert-jabber
 Version:       1.2
-Release:       7
+Release:       8
 License:       GPL
 Group:         Networking
 Source0:       nagios-jabber.alert
diff --git a/nagios-jabber.alert b/nagios-jabber.alert
index d426c90..7183f10 100644
--- a/nagios-jabber.alert
+++ b/nagios-jabber.alert
@@ -11,6 +11,7 @@
 #   jabber.alert [-x] [-a account_id][,otheraccount_id] [-t timeout ] [-J 
from_jid -P password] to_jid1 to_jid2 to_jid3
 
 import os
+import hashlib
 import re
 import sys
 import getopt
@@ -94,6 +95,12 @@ if len(body.strip()) == 0:
 
 message_type = 'chat'
 
+class XMPPStreamError(Exception):
+       def __init__(self, msg):
+               self.msg = msg
+       def __str__(self):
+               return self.msg
+
 class Client(JabberClient):
        def session_started(self):
                if (html == True):
@@ -105,7 +112,6 @@ class Client(JabberClient):
                        message = body
 
                for r in recpt:
-
                        jid_r = JID(r)
                        msg = Message(to_jid = jid_r, body = message, subject = 
subject,
                                        stanza_type = message_type, thread = 
"Nagios")
@@ -124,27 +130,46 @@ class Client(JabberClient):
                if debug:
                        print "*** State changed: %s %r ***" % (state,arg)
 
+       def stream_error(self,err):
+               raise XMPPStreamError(err.get_message())
+
+
 err = []
 for section in jids:
-       jid = JID(section['jid'])
-       if not jid.resource:
-               jid = JID(jid.node, jid.domain, "Nagios/%d" % os.getpid())
-
-       c = Client(jid, section['password'], auth_methods = ['sasl:DIGEST-MD5', 
'sasl:PLAIN', 'digest'],
-                       tls_settings = TLSSettings(require = False, verify_peer 
= False))
-       try:
-               c.connect()
+       for attempt in ('first', 'second'):
+               jid = JID(section['jid'])
+               resource = "Nagios/" + 
hashlib.md5(''.join(recpt)).hexdigest()[:10]
+               if attempt == 'second':
+                       # if something went wrong the second time around, it's
+                       # most likely a resource name conflict on login, so 
let's
+                       # wait a bit, randomize the resource name and try again
+                       resource = resource + '/' + repr(os.getpid())
+                       time.sleep(0.8)
+               if not jid.resource:
+                       jid = JID(jid.node, jid.domain, resource)
+
+               c = Client(jid, section['password'], auth_methods = 
['sasl:DIGEST-MD5', 'sasl:PLAIN', 'digest'],
+                               tls_settings = TLSSettings(require = False, 
verify_peer = False))
                try:
-                       c.loop(1)
-               except Exception, e:
-                       err.append("ERROR1: %s: %s" % (section['jid'], e))
+                       c.connect()
+                       try:
+                               c.loop(1)
+                       except XMPPStreamError, e:
+                               # Most likely a duplicate stream problem
+                               # don't log anything, just try again
+                               c.disconnect()
+                               continue
+                       except Exception, e:
+                               err.append("ERROR1: %s: %s" % (section['jid'], 
e))
+                               c.disconnect()
+                               # don't try another attempt, jump straigt to
+                               # another section
+                               break
                        c.disconnect()
-                       continue
-               c.disconnect()
-               # stop after first succeeded attempt
-               sys.exit(0)
-       except Exception, e:
-               err.append("ERROR2: %s: %s" % (section['jid'], e))
+                       # stop after first successful attempt at sending the msg
+                       sys.exit(0)
+               except Exception, e:
+                       err.append("ERROR2: %s: %s" % (section['jid'], e))
 
 print >> sys.stderr, "\n".join(err)
 sys.exit(1)
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/packages/nagios-alert-jabber.git/commitdiff/257cdecd13da28d11f59cc7b6e51262c292cbcb9

_______________________________________________
pld-cvs-commit mailing list
[email protected]
http://lists.pld-linux.org/mailman/listinfo/pld-cvs-commit

Reply via email to