-
Notifications
You must be signed in to change notification settings - Fork 1
/
NotifierManager.py
84 lines (73 loc) · 2.64 KB
/
NotifierManager.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import sys
import os
import logging
import config
from time import time
from sqlalchemy import or_
from PluginManager import PluginManager
from db import session, Node
logger = logging.getLogger(__name__)
class NotifierManager(PluginManager):
modulename = 'Notifier'
def find_matching(self, contact):
for notifier in self.plugins:
try:
if notifier.suitable(contact):
return notifier
except:
logger.exception(
"Exception while determining if %s is suitable for %s",
notifier.__class__.__name__,
contact
)
@staticmethod
def split_contacts(contacts):
try:
return contacts.split(', ')
except AttributeError:
return [contacts]
def notify_node(self, node):
contacts = self.split_contacts(node.contact)
copy_contacts = self.split_contacts(config.copy_contact)
notified = False
for contact in contacts:
notifier = self.find_matching(contact)
if notifier:
try:
notified |= bool(notifier.notify(contact, node))
except:
logger.exception(
"Exception during notifiction via %s",
notifier.__class__.__name__
)
if notified:
for contact in set(copy_contacts) - set(contacts):
notifier = self.find_matching(contact)
if notifier:
try:
notifier.notify(contact, node)
except:
logger.exception(
"Exception during notifiction via %s",
notifier.__class__.__name__
)
return notified
def notify_down(self):
to_notify = session.query(Node).filter(
Node.lastseen <= time() - config.notify_timeout,
or_(Node.lastcontact < Node.lastseen, Node.lastcontact == None),
Node.contact != None,
Node.ignore == 0 if config.whitelisting
else or_(Node.ignore == None, Node.ignore == 0),
)
for node in to_notify:
logger.info("Notifying node %s (ID %s)", node.name, node.id)
if self.notify_node(node):
node.lastcontact = time()
session.commit()
if __name__ == '__main__':
m = NotifierManager()
print("Available notifiers:")
for notifier in m.plugins:
print("%s\t%s" % (notifier.__class__.__name__, notifier.regex.pattern))
m.quit()