2
from twisted.internet import reactor
3
from twisted.spread import pb
4
from twisted.python import log
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
16
return 'Alert(%r, %r, %r, %r)' % (
17
self.hostname, self.service,
18
self.returncode, self.output)
19
pb.setUnjellyableForClass(Alert, Alert)
21
class MuninAlertListener:
23
Mixin for a PB Referenceable which accepts Munin alerts.
25
def remote_alert(self, alert):
26
self.proto.alert(alert)
30
alertMessageFormat = (
31
'Alert from %(hostname)s [%(service)s]: %(output)s')
33
def alert(self, alert):
34
self.join(config.ALERT_CHANNEL)
37
self.alertMessageFormat % vars(alert),
40
def _sendAlert(alert):
41
cf = pb.PBClientFactory()
42
reactor.connectTCP(config.BOT_HOST, config.BOT_PORT, cf)
45
return rootObj.callRemote('alert', alert)
47
rootD = cf.getRootObject()
48
rootD.addCallback(cbRoot)
49
rootD.addErrback(log.err)
50
rootD.addCallback(lambda ign: reactor.stop())
53
reactor.callWhenRunning(lambda: [_sendAlert(Alert(hostname, service, returncode, output))
54
for (hostname, service, returncode, output) in alerts])