Welcome, guest | Sign In | My Account | Store | Cart

If you have long-running scripts on many remote machines and you want to be alarmed if one of them crashes, use the JabberHandler to log it to your account.

Python, 43 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import xmpp # Jabber client/server lib from http://pyxmpp.jajcus.net/

class JabberHandler(logging.Handler):
    """
    A handler class which sends a Jabber message for each logging event.
    """
    def __init__(self, from_id, passwd, to_id):
        logging.Handler.__init__(self)
        
        self.from_id, self.passwd = from_id, passwd
        self.to_id = to_id
        jid=xmpp.JID(self.from_id)
        self.user, self.server = jid.getNode(), jid.getDomain()
    
        conn=xmpp.Client(self.server)#,debug=[])
        conres=conn.connect()
        
        if not conres:
            print "Unable to connect to server %s!"%self.server
            sys.exit(1)
        if conres<>'tls':
            print "Warning: unable to estabilish secure connection - TLS failed!"
        authres=conn.auth(self.user, self.passwd)
        if not authres:
            print "Unable to authorize on %s - check login/password."%self.server
            sys.exit(1)
        if authres<>'sasl':
            print "Warning: unable to perform SASL auth os %s. Old authentication method used!"%self.server
        conn.sendInitPresence()
        self.conn = conn

    def emit(self, record):
        try:
            msg = self.format(record)
            self.conn.send(xmpp.Message(to=self.to_id,body=msg, frm="Logger"))
        except:
            self.handleError(record)

if __name__=="__main__":
    log = logging.getLogger("URGENT")
    log.setLevel(logging.ERROR)
    log.addHandler(JabberHandler("myID@jabber.org", "mypasswd", "myID@jabber.org"))
    log.error("Serious error because of ...")

1 comment

Yoan Blanc 14 years, 11 months ago  # | flag

A version that works with the latest version (0.5). Thanks!

import sys
import xmpp
import logging

class JabberHandler(logging.Handler):
    def __init__(self, from_jid, password, to_jid):
        logging.Handler.__init__(self)

        self.to_jid = to_jid

        jid = xmpp.protocol.JID(from_jid)

        conn = xmpp.Client(jid.getDomain())
        connres = conn.connect()

        if not connres:
            print >>sys.stderr, \
                  "Unable to connect to %s" % jid.getDomain()
            sys.exit(1)
        if connres<>"tls":
            print >>sys.stderr, \
                  "Warning: unable to establish secure connection - TLS failed"
        authres = conn.auth(jid.getNode(), password)
        if not authres:
            print >>sys.stderr, \
                  "Unable to authorize on %s - check login/password" % jid.getDomain()
            sys.exit(1)
        if authres<>"sasl":
            print >>sys.stderr, \
                  "Warning: unable to establish SASL auth on %s" % jid.getDomain()
        self.conn = conn

    def emit(self, record):
        try:
            msg = self.format(record)
            self.conn.send(xmpp.protocol.Message(self.to_jid,
                                                 msg))
        except:
            self.handleError(record)