48fa5144a9e890ac3e165efec4b0759bbd474597
[monitor.git] / mailmonitor.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2004  The Trustees of Princeton University (Trustees).
4
5 # Stephen Soltesz <soltesz@cs.princeton.edu>
6 #
7 # $Id: monitor.py,v 1.7 2007/07/03 19:59:02 soltesz Exp $
8
9 import database
10
11 from monitor_policy import *
12 import rt
13 import sys
14
15 import plc
16 api = plc.getAuthAPI()
17
18 from clean_policy import *
19
20 def reboot(hostname):
21         print "calling reboot!!! %s " % hostname
22
23         l_nodes = api.GetNodes(hostname)
24         if len(l_nodes) == 0:
25                 raise Exception("No such host: %s" % hostname)
26         
27         l_blacklist = database.if_cached_else(1, "l_blacklist", lambda : [])
28         l_ticket_blacklist = database.if_cached_else(1,"l_ticket_blacklist",lambda : [])
29
30         l_nodes  = filter(lambda x : not x['hostname'] in l_blacklist, l_nodes)
31         if len(l_nodes) == 0:
32                 raise Exception("Host removed via blacklist: %s" % hostname)
33
34         ad_dbTickets = database.if_cached_else_refresh(True, False, "ad_dbTickets", lambda : [])
35         if ad_dbTickets == None:
36                 raise Exception("Could not find cached dbTickets")
37
38         print "starting new thing"
39         mon = MonitorMergeDiagnoseSendEscellate(hostname, True)
40         mon.run()
41
42         #print "merge"
43         #merge = Merge( [node['hostname'] for node in l_nodes])
44         #record_list = merge.run()
45         ##print "rt"
46         #rt = RT(record_list, ad_dbTickets, l_ticket_blacklist)
47         #record_list = rt.run()
48         ##print "diagnose"
49         #diag = Diagnose(record_list)
50         #diagnose_out = diag.run()
51         #print diagnose_out
52         #print "action"
53         #action = Action(diagnose_out)
54         #action.run()
55
56         return True
57
58 def reboot2(hostname):
59         l_nodes = api.GetNodes(hostname)
60         if len(l_nodes) == 0:
61                 raise Exception("No such host: %s" % hostname)
62         
63         l_blacklist = database.if_cached_else(1, "l_blacklist", lambda : [])
64         l_ticket_blacklist = database.if_cached_else(1,"l_ticket_blacklist",lambda : [])
65
66         l_nodes  = filter(lambda x : not x['hostname'] in l_blacklist, l_nodes)
67         if len(l_nodes) == 0:
68                 raise Exception("Host removed via blacklist: %s" % hostname)
69
70         ad_dbTickets = database.if_cached_else_refresh(True, False, "ad_dbTickets", lambda : None)
71         if ad_dbTickets == None:
72                 raise Exception("Could not find cached dbTickets")
73
74
75         args = {}
76         args['hostname'] = "%s" % hostname
77         args['hostname_list'] = "%s" % hostname
78         args['loginbase'] = plc.siteId(hostname)
79
80         m = PersistMessage(hostname, "Please Update Boot Image for %s" % hostname,
81                                                         mailtxt.newalphacd_one[1] % args, True, db='bootcd_persistmessages')
82         
83         #print "merge"
84         merge = Merge( [node['hostname'] for node in l_nodes])
85         record_list = merge.run()
86         #print "rt"
87         rt = RT(record_list, ad_dbTickets, l_ticket_blacklist)
88         record_list = rt.run()
89         #print "diagnose"
90         diag = Diagnose(record_list)
91         diagnose_out = diag.run()
92         #print diagnose_out
93         #print "action"
94         action = Action(diagnose_out)
95         action.run()
96
97         return True
98
99
100 def main():
101         for host in sys.argv[1:]:
102                 reboot(host)
103
104 if __name__ == '__main__':
105         print "calling main"
106         main()