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