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