e92a9cccef8b9484163bfd37c29d04de9b4f7750
[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 unified_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         else:
106                 loginbase = "undefined"
107
108         fb = database.dbLoad("findbad")
109         lb2hn = database.dbLoad("plcdb_lb2hn")
110         pf = database.dbLoad("node_persistflags")
111
112         # SETUP header
113         t = TABLE(border="0", cellspacing="0", cellpadding="0")
114         r = TR()
115
116         if loginbase not in lb2hn:
117                 value = ("Select 'Edit settings' to enter your Site's loginbase.", "")
118                 r = TR(TD(value[0]))
119                 t.append(r)
120         else:
121                 for value in ['Hostname', 'Since']:
122                         r.append(TH(value))
123                 t.append(r)
124                 nodes = lb2hn[loginbase]
125                 hostnames = [ n['hostname'] for n in nodes ]
126                 hostnames.sort()
127
128                 for host in hostnames:
129                         r = TR()
130                         color = state_to_color(fb['nodes'][host]['values']['state'])
131                         url = 'http://www.planet-lab.org/db/nodes/index.php?nodepattern=%s' % host
132                         td = TD(A(host, target='_blank', href=url), bgcolor=color)
133                         r.append(td)
134                         lc = pf[host].last_changed
135                         td = TD(diff_time(lc))
136                         r.append(td)
137                         t.append(r)
138                         
139         #d = Document(t)
140         print CENTER(t)
141
142 if __name__ == "__main__":
143         main()