This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalert.py
55 lines (43 loc) · 1.54 KB
/
alert.py
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
44
45
46
47
48
49
50
51
52
53
54
55
from twisted.internet import reactor
from twisted.spread import pb
from twisted.python import log
import config
class Alert(pb.Copyable, pb.RemoteCopy):
def __init__(self, hostname, service, returncode, output):
self.hostname = hostname
self.service = service
self.returncode = returncode
self.output = output
def __repr__(self):
return 'Alert(%r, %r, %r, %r)' % (
self.hostname, self.service,
self.returncode, self.output)
pb.setUnjellyableForClass(Alert, Alert)
class MuninAlertListener:
"""
Mixin for a PB Referenceable which accepts Munin alerts.
"""
def remote_alert(self, alert):
self.proto.alert(alert)
class MuninAlert:
alertMessageFormat = (
'Alert from %(hostname)s [%(service)s]: %(output)s')
def alert(self, alert):
self.join(config.ALERT_CHANNEL)
self.msg(
config.ALERT_CHANNEL,
self.alertMessageFormat % vars(alert),
config.LINE_LENGTH)
def _sendAlert(alert):
cf = pb.PBClientFactory()
reactor.connectTCP(config.BOT_HOST, config.BOT_PORT, cf)
def cbRoot(rootObj):
return rootObj.callRemote('alert', alert)
rootD = cf.getRootObject()
rootD.addCallback(cbRoot)
rootD.addErrback(log.err)
rootD.addCallback(lambda ign: reactor.stop())
def main(alerts):
reactor.callWhenRunning(lambda: [_sendAlert(Alert(hostname, service, returncode, output))
for (hostname, service, returncode, output) in alerts])
reactor.run()