04c2a1c8b0fdc0306f221ed0726e82736e2dd964
[monitor.git] / policy.py
1 #
2 # Copyright (c) 2004  The Trustees of Princeton University (Trustees).
3 #
4 # Faiyaz Ahmed <faiyaza@cs.princeton.edu>
5 #
6 # $Id: $
7 #
8 # Policy Engine.
9
10 #from monitor import *
11 from threading import *
12 import time
13 import logging
14 import mailer
15 import emailTxt
16
17 logger = logging.getLogger("monitor")
18
19 # Time to enforce policy
20 POLSLEEP = 7200
21
22 # IF:
23 #  no SSH, down.
24 #  bad disk, down
25 #  DNS, kinda down (sick)
26 #  clock, kinda down (sick)
27 #  Full disk, going to be down
28
29 # Actions:
30 #  Email
31 #  suspend slice creation
32 #  kill slices
33 class Policy(Thread):
34         def __init__(self, comonthread, tickets):
35                 self.cmn = comonthread
36                 self.tickets = tickets
37                 # host - > time of email
38                 self.emailed = {}
39                 # all sick nodes w/ tickets
40                 self.cursickw = tickets
41                 # all sick nodes w/o tickets
42                 self.cursick = []
43                 Thread.__init__(self)
44         '''
45         Gets all nodes without tickets and puts them in self.cursick
46         '''
47         def getAllSick(self):
48                 self.cursick = []
49                 for bucket in self.cmn.comonbkts.keys():
50                         for host in getattr(self.cmn, bucket):
51                                 if host not in self.cursickw.keys():
52                                         if host not in self.cursick: 
53                                                 self.cursick.append(host)
54                 logger.debug("Nodes sick wo tickets %s " % len(self.cursick))
55
56         '''
57         Acts on sick nodes.
58         '''
59         def emailSick(self):
60                 for node in self.cmn.ssh:
61                         if node in self.cursick:
62                                 if node not in self.emailed.keys():
63                                         logger.debug("Emailing " + node)
64                                         try:
65                                                 self.emailed[node] = "ssh"
66                                                 mailer.email('DISREGARD', 
67                                                 emailTxt.mailtxt.STANDARD % {'hostname': node}, 
68                                                 "tech-" + mailer.siteId(node) + "@sites.planet-lab.org")
69                                         except Exception, err:
70                                                 logger.info(err)
71
72         '''
73         Prints, logs, and emails status of up nodes, down nodes, and buckets.
74         '''
75         def status(self):
76                 return 0
77
78         def run(self):
79                 #while 1:
80                 self.getAllSick()
81                 self.emailSick()