1 from twisted.internet.protocol import DatagramProtocol
2 from twisted.internet import reactor
3 from twisted.internet import reactor
4 from time import strftime, strptime, localtime
7 from Screens.MessageBox import MessageBox
8 from Tools import Notifications
10 from GrowleeConnection import emergencyDisable
11 from . import NOTIFICATIONID
16 'kern': 0, 'user': 1, 'mail': 2, 'daemon': 3,
17 'auth': 4, 'syslog': 5, 'lpr': 6, 'news': 7,
18 'uucp': 8, 'cron': 9, 'authpriv': 10, 'ftp': 11,
19 'local0': 16, 'local1': 17, 'local2': 18, 'local3': 19,
20 'local4': 20, 'local5': 21, 'local6': 22, 'local7': 23,
24 'emerg': 0, 'alert':1, 'crit': 2, 'err': 3,
25 'warning': 4, 'notice': 5, 'info': 6, 'debug': 7
28 reverse = lambda map: dict(zip(map.values(), map.keys()))
31 MessageBox.TYPE_YESNO: SEVERITY['debug'],
32 MessageBox.TYPE_INFO: SEVERITY['info'],
33 MessageBox.TYPE_WARNING: SEVERITY['warning'],
34 MessageBox.TYPE_ERROR: SEVERITY['err'],
37 class SyslogNetworkProtocol(DatagramProtocol):
39 def __init__(self, host):
43 self.addr = (ip, SYSLOG_UDP_PORT)
45 def noIP(self, error):
46 print "--------------------------------", error
49 def startProtocol(self):
50 reactor.resolve(self.host.address.value).addCallback(self.gotIP).addErrback(self.noIP)
52 def sendNotification(self, title='No title.', description='No message.', priority=0):
53 if not self.transport or not self.addr or not self.host.enable_outgoing.value:
57 day = strftime("%d", ltime)
60 value = strftime("%b %%s %H:%M:%S", ltime)
61 timestamp = value % (day,)
62 payload = "<%d>%s %s growlee: (%s) %s" % (FACILITY['local0'] * 8 + SEVERITYMAP[priority], timestamp, uname()[1], title, description.replace('\n', ' '),)
63 self.transport.write(payload, self.addr)
65 def datagramReceived(self, data, addr):
66 if not self.host.enable_incoming.value:
70 if Len > 1024: # invalid according to rfc
74 prio, data = data.split('>', 1)
76 facility, severity = divmod(prio, 8) # just the ids
77 #facility = reverse(FACILITY)[facility]
78 type = reverse(SEVERITYMAP).get(severity, MessageBox.TYPE_ERROR)
80 # parse remaining header
82 # try to parse timestamp to determine validity
83 timestamp = strptime(payload[:15], '%b %d %H:%M:%S')
87 hostname, payload = payload[16:].split(' ', 1)
88 # NOTE: we could re-process timestamp to get a customized display format,
89 # but lets just keep this for now
90 message = hostname + '@' + payload[:15] + ':' + payload
92 Notifications.AddNotificationWithID(
97 timeout = 10, # XXX: un-hardcode timeout?
98 close_on_any_key = True,
101 class SyslogAbstraction:
102 def __init__(self, host):
103 self.syslog = SyslogNetworkProtocol(host)
104 self.serverPort = reactor.listenUDP(SYSLOG_UDP_PORT, self.syslog)
106 def sendNotification(self, title='No title.', description='No description.', priority=-1, timeout=-1):
107 self.syslog.sendNotification(title=title, description=description, priority=priority)
110 return self.serverPort.stopListening()