~twisted-dev/twisted-trac-integration/trunk

« back to all changes in this revision

Viewing changes to commit-bot/alert.py

  • Committer: tom.prince at ualberta
  • Date: 2013-05-21 17:27:11 UTC
  • Revision ID: tom.prince@ualberta.net-20130521172711-o9vifzpokcyf2i55
Remove commit-bot and diffresource.

The code and history for them now reside in seperate repositories at
https://github.com/twisted-infra.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
from twisted.internet import reactor
3
 
from twisted.spread import pb
4
 
from twisted.python import log
5
 
 
6
 
import config
7
 
 
8
 
class Alert(pb.Copyable, pb.RemoteCopy):
9
 
    def __init__(self, hostname, service, returncode, output):
10
 
        self.hostname = hostname
11
 
        self.service = service
12
 
        self.returncode = returncode
13
 
        self.output = output
14
 
 
15
 
    def __repr__(self):
16
 
        return 'Alert(%r, %r, %r, %r)' % (
17
 
            self.hostname, self.service,
18
 
            self.returncode, self.output)
19
 
pb.setUnjellyableForClass(Alert, Alert)
20
 
 
21
 
class MuninAlertListener:
22
 
    """
23
 
    Mixin for a PB Referenceable which accepts Munin alerts.
24
 
    """
25
 
    def remote_alert(self, alert):
26
 
        self.proto.alert(alert)
27
 
 
28
 
 
29
 
class MuninAlert:
30
 
    alertMessageFormat = (
31
 
        'Alert from %(hostname)s [%(service)s]: %(output)s')
32
 
 
33
 
    def alert(self, alert):
34
 
        self.join(config.ALERT_CHANNEL)
35
 
        self.msg(
36
 
            config.ALERT_CHANNEL,
37
 
            self.alertMessageFormat % vars(alert),
38
 
            config.LINE_LENGTH)
39
 
 
40
 
def _sendAlert(alert):
41
 
    cf = pb.PBClientFactory()
42
 
    reactor.connectTCP(config.BOT_HOST, config.BOT_PORT, cf)
43
 
 
44
 
    def cbRoot(rootObj):
45
 
        return rootObj.callRemote('alert', alert)
46
 
 
47
 
    rootD = cf.getRootObject()
48
 
    rootD.addCallback(cbRoot)
49
 
    rootD.addErrback(log.err)
50
 
    rootD.addCallback(lambda ign: reactor.stop())
51
 
 
52
 
def main(alerts):
53
 
    reactor.callWhenRunning(lambda: [_sendAlert(Alert(hostname, service, returncode, output))
54
 
                                       for (hostname, service, returncode, output) in alerts])
55
 
    reactor.run()