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