changes for 3.0
[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 util.file
14
15 import parser as parsermodule
16
17
18 parser = parsermodule.getParser()
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 = parsermodule.parse_args(parser)
33
34 from unified_model import *
35 def color_sitestatus(status):
36         if status == "good":
37                 return green(status)
38         elif status == "down":
39                 return red(status)
40         else:
41                 return status
42                 
43
44 def pf_print_siteinfo(sitename):
45         pf = PersistFlags(sitename, 1, db='site_persistflags')
46         if pf.checkattr('last_changed'):
47                 print "   Checked: %s" % diff_time(pf.last_checked)
48                 print "\t status | nodes up / total | last_change"
49                 print "\t   %6s | %8s / %5s | %s" % \
50                         ( color_sitestatus(pf.status), pf.nodes_up, pf.nodes_total, diff_time(pf.last_changed) )
51         else:
52                 print "no  such site in pf"
53         del pf
54
55
56 def plc_print_siteinfo(plcsite):
57         print ""
58         print "   Checked: %s" % time.ctime()
59         print "\t login_base    | used / max | enabled | last_updated "
60         print "\t %13s | %4s / %3s | %7s | %12s" % \
61                         (plcsite['login_base'], 
62                          len(plcsite['slice_ids']),
63                          plcsite['max_slices'],
64                          plcsite['enabled'],
65                          diff_time(plcsite['last_updated']))
66
67         print ""
68         nodes = api.GetNodes(plcsite['node_ids'])
69         print "   Checked: %s" % time.ctime()
70         print "\t                               host     | state | obs   |   created   |   updated   | last_contact "
71         for plcnode in nodes:
72                 fbnode = fb['nodes'][plcnode['hostname']]['values']
73                 plcnode['state'] = color_boot_state(get_current_state(fbnode))
74                 print "\t  %37s |  %5s |  %5s | %11.11s | %11.11s | %12s " % \
75                 (plcnode['hostname'], color_boot_state(plcnode['boot_state']), plcnode['state'], 
76                         diff_time(plcnode['date_created']), diff_time(plcnode['last_updated']), 
77                 diff_time(plcnode['last_contact']))
78
79
80 fb = database.dbLoad("findbad")
81 act_all = database.dbLoad("act_all")
82
83 for site in config.args:
84         config.site = site
85
86         plc_siteinfo = api.GetSites({'login_base': config.site})[0]
87         url = "https://www.planet-lab.org/db/sites/index.php?site_pattern="
88         plc_siteinfo['url'] = url + plc_siteinfo['login_base']
89
90         if config.findbad:
91                 # rerun findbad with the nodes in the given nodes.
92                 import os
93                 file = "findbad.txt"
94                 nodes = api.GetNodes(plc_siteinfo['node_ids'], ['hostname'])
95                 nodes = [ n['hostname'] for n in nodes ]
96                 util.file.setFileFromList(file, nodes)
97                 os.system("./findbad.py --cachenodes --debug=0 --dbname=findbad --increment --nodelist %s" % file)
98
99         print "%(login_base)s %(url)s" % plc_siteinfo
100         pf_print_siteinfo(config.site)
101         plc_print_siteinfo(plc_siteinfo)
102
103         if config.enable:
104                 api.UpdateSite(config.site, {'enabled' : True})
105         if config.disable:
106                 api.UpdateSite(config.site, {'enabled' : False})