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