AM nagios/plc2nagios.py
[monitor.git] / siteinfo.py
1 #!/usr/bin/python
2
3 import plc
4 import auth
5 api = plc.PLC(auth.auth, auth.plc)
6
7 import soltesz
8 import reboot
9
10 import time
11 from model import *
12 from nodecommon import *
13
14 import config as configmodule
15
16 from config import config as cfg
17 from optparse import OptionParser
18
19 parser = OptionParser()
20 parser.set_defaults(site=None, 
21                                         findbad=False,
22                                         enable=False,
23                                         disable=False
24                                         )
25 parser.add_option("", "--site", dest="site", metavar="login_base", 
26                                         help="The sitename to present")
27 parser.add_option("", "--findbad", dest="findbad", action="store_true", 
28                                         help="Re-run findbad on the nodes we're going to check before acting.")
29 parser.add_option("", "--enable", dest="enable", action="store_true",
30                                         help="")
31 parser.add_option("", "--disable", dest="disable", action="store_true",
32                                         help="")
33 config = cfg(parser)
34 config.parse_args()
35
36 from unified_model import *
37 def color_sitestatus(status):
38         if status == "good":
39                 return green(status)
40         elif status == "down":
41                 return red(status)
42         else:
43                 return status
44                 
45
46 def pf_print_siteinfo(sitename):
47         pf = PersistFlags(sitename, 1, db='site_persistflags')
48         if pf.checkattr('last_changed'):
49                 print "   Checked: %s" % diff_time(pf.last_checked)
50                 print "\t status | nodes up / total | last_change"
51                 print "\t   %6s | %8s / %5s | %s" % \
52                         ( color_sitestatus(pf.status), pf.nodes_up, pf.nodes_total, diff_time(pf.last_changed) )
53         else:
54                 print "no  such site in pf"
55         del pf
56
57
58 def plc_print_siteinfo(plcsite):
59         print ""
60         print "   Checked: %s" % time.ctime()
61         print "\t login_base    | used / max | enabled | last_updated "
62         print "\t %13s | %4s / %3s | %7s | %12s" % \
63                         (plcsite['login_base'], 
64                          len(plcsite['slice_ids']),
65                          plcsite['max_slices'],
66                          plcsite['enabled'],
67                          diff_time(plcsite['last_updated']))
68
69         print ""
70         nodes = api.GetNodes(plcsite['node_ids'])
71         print "   Checked: %s" % time.ctime()
72         print "\t                               host     | state | obs   |   created   |   updated   | last_contact "
73         for plcnode in nodes:
74                 fbnode = fb['nodes'][plcnode['hostname']]['values']
75                 plcnode['state'] = color_boot_state(get_current_state(fbnode))
76                 print "\t  %37s |  %5s |  %5s | %11.11s | %11.11s | %12s " % \
77                 (plcnode['hostname'], color_boot_state(plcnode['boot_state']), plcnode['state'], 
78                         diff_time(plcnode['date_created']), diff_time(plcnode['last_updated']), 
79                 diff_time(plcnode['last_contact']))
80
81
82 fb = soltesz.dbLoad("findbad")
83 act_all = soltesz.dbLoad("act_all")
84
85 for site in config.args:
86         config.site = site
87
88         plc_siteinfo = api.GetSites({'login_base': config.site})[0]
89         url = "https://www.planet-lab.org/db/sites/index.php?site_pattern="
90         plc_siteinfo['url'] = url + plc_siteinfo['login_base']
91
92         if config.findbad:
93                 # rerun findbad with the nodes in the given nodes.
94                 import os
95                 file = "findbad.txt"
96                 nodes = api.GetNodes(plc_siteinfo['node_ids'], ['hostname'])
97                 nodes = [ n['hostname'] for n in nodes ]
98                 configmodule.setFileFromList(file, nodes)
99                 os.system("./findbad.py --cachenodes --debug=0 --dbname=findbad --increment --nodelist %s" % file)
100
101         print "%(login_base)s %(url)s" % plc_siteinfo
102         pf_print_siteinfo(config.site)
103         plc_print_siteinfo(plc_siteinfo)
104
105         if config.enable:
106                 api.UpdateSite(config.site, {'enabled' : True})
107         if config.disable:
108                 api.UpdateSite(config.site, {'enabled' : False})