move clean_policy.py into monitor package
[monitor.git] / www / gadgets / sitemonitor.py
1 #!/usr/bin/python
2
3 import cgi
4 import cgitb
5 cgitb.enable()
6 print "Content-Type: text/html\r\n"
7
8 import time
9 from monitor.model import *
10 from monitor import database
11 from HyperText.HTML import A, BR, IMG, TABLE, TR, TH, TD, EM, quote_body, CENTER
12 from HyperText.Documents import Document
13
14 form = cgi.FieldStorage()
15
16 print """
17 <style>
18 table {
19         align: center;
20     border-color: #ccc;
21     border-width: 0 0 1px 1px;
22     border-style: solid;
23 }
24 th {
25     border-color: #fff;
26     border-width: 1px 1px 1px 1px;
27     border-style: solid;
28     margin: 0;
29     padding: 0px;
30 }
31 td {
32     border-color: #ccc;
33     border-width: 1px 1px 0 0;
34     border-style: solid;
35     margin: 0;
36     padding: 3px;
37 }
38 </style>
39 """
40
41 def get(fb, path):
42     indexes = path.split("/")
43     values = fb
44     for index in indexes:
45         if index in values:
46             values = values[index]
47         else:
48             return None
49     return values
50
51 def diff_time(timestamp, abstime=True):
52         import math
53         now = time.time()
54         if timestamp == None:
55                 return "unknown"
56         if abstime:
57                 diff = now - timestamp
58         else:
59                 diff = timestamp
60         # return the number of seconds as a difference from current time.
61         t_str = ""
62         if diff < 60: # sec in min.
63                 t = diff / 1
64                 t_str = "%s sec ago" % int(math.ceil(t))
65         elif diff < 60*60: # sec in hour
66                 t = diff / (60)
67                 t_str = "%s min ago" % int(math.ceil(t))
68         elif diff < 60*60*24: # sec in day
69                 t = diff / (60*60)
70                 t_str = "%s hrs ago" % int(math.ceil(t))
71         elif diff < 60*60*24*14: # sec in week
72                 t = diff / (60*60*24)
73                 t_str = "%s days ago" % int(math.ceil(t))
74         elif diff <= 60*60*24*30: # approx sec in month
75                 t = diff / (60*60*24*7)
76                 t_str = "%s weeks ago" % int(math.ceil(t))
77         elif diff > 60*60*24*30: # approx sec in month
78                 t = diff / (60*60*24*30)
79                 t_str = "%s months ago" % int(t)
80         return t_str
81
82 def get_value(val):
83         
84         if form.has_key(val):
85                 retvalue = form.getvalue(val)
86         else:
87                 retvalue = None
88         
89         return retvalue
90
91 def state_to_color(state):
92         if state == "BOOT":
93                 return "darkseagreen"
94         elif state == "DEBUG":
95                 return "gold"
96         elif state == "DOWN":
97                 return "indianred"
98         else:
99                 return "lightgrey"
100
101 def main():
102         
103         if form.has_key('loginbase'):
104                 loginbase = form.getvalue('loginbase')
105                 loginbase = loginbase.rstrip("_")
106         else:
107                 loginbase = "undefined"
108
109         fb = database.dbLoad("findbad")
110         lb2hn = database.dbLoad("plcdb_lb2hn")
111         pf = database.dbLoad("node_persistflags")
112
113         # SETUP header
114         t = TABLE(border="0", cellspacing="0", cellpadding="0")
115         r = TR()
116
117         if loginbase not in lb2hn:
118                 value = ("""Select 'Edit settings' to enter your Site's loginbase.<br><br>
119                                         The loginbase is the unchangable portion of your slicename.  
120                                         For instance, your slice follows the pattern loginbase_slicename.<br><br>
121                                         If this hint is unclear, then you can find your loginbase by visiting 'My Site' at <a target=_blank href=http://www.planet-lab.org>'PlanetLab.org'</a>""", "")
122                 r = TR(TD(value[0]))
123                 t.append(r)
124         else:
125                 for value in ['Hostname', 'Since']:
126                         r.append(TH(value))
127                 t.append(r)
128                 nodes = lb2hn[loginbase]
129                 hostnames = [ n['hostname'] for n in nodes ]
130                 hostnames.sort()
131
132                 for host in hostnames:
133                         r = TR()
134                         color = state_to_color(fb['nodes'][host]['values']['state'])
135                         url = 'http://www.planet-lab.org/db/nodes/index.php?nodepattern=%s' % host
136                         td = TD(A(host, target='_blank', href=url), bgcolor=color)
137                         r.append(td)
138                         lc = pf[host].last_changed
139                         td = TD(diff_time(lc))
140                         r.append(td)
141                         t.append(r)
142                         
143         #d = Document(t)
144         print CENTER(t)
145
146 if __name__ == "__main__":
147         main()