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