remove a lot of deprecated files ;
[monitor.git] / www / runlevels.py
1 #!/usr/bin/python
2
3 import cgi
4 import cgitb; 
5 from monitor import database
6 import time
7 cgitb.enable()
8
9 from HyperText.HTML import A, BR, IMG, TABLE, TR, TH, TD, EM, quote_body
10 from HyperText.Documents import Document
11 print "Content-Type: text/html\r\n"
12
13 form = cgi.FieldStorage()
14
15 def get(fb, path):
16     indexes = path.split("/")
17     values = fb
18     for index in indexes:
19         if index in values:
20             values = values[index]
21         else:
22             return None
23     return values
24
25 def diff_time(timestamp, abstime=True):
26         import math
27         now = time.time()
28         if timestamp == None:
29                 return "unknown"
30         if abstime:
31                 diff = now - timestamp
32         else:
33                 diff = timestamp
34         # return the number of seconds as a difference from current time.
35         t_str = ""
36         if diff < 60: # sec in min.
37                 t = diff / 1
38                 t_str = "%s sec ago" % int(math.ceil(t))
39         elif diff < 60*60: # sec in hour
40                 t = diff / (60)
41                 t_str = "%s min ago" % int(math.ceil(t))
42         elif diff < 60*60*24: # sec in day
43                 t = diff / (60*60)
44                 t_str = "%s hrs ago" % int(math.ceil(t))
45         elif diff < 60*60*24*14: # sec in week
46                 t = diff / (60*60*24)
47                 t_str = "%s days ago" % int(math.ceil(t))
48         elif diff <= 60*60*24*30: # approx sec in month
49                 t = diff / (60*60*24*7)
50                 t_str = "%s wks ago" % int(math.ceil(t))
51         elif diff > 60*60*24*30: # approx sec in month
52                 t = diff / (60*60*24*30)
53                 t_str = "%s mnths ago" % int(t)
54         return t_str
55
56 def get_value(val):
57         
58         if form.has_key(val):
59                 retvalue = form.getvalue(val)
60         else:
61                 retvalue = None
62         
63         return retvalue
64
65 vals = {}
66 vals['ssh'] = get_value('ssh')
67 vals['state'] = get_value('state')
68 vals['nm'] = get_value('nm')
69 vals['dns'] = None
70 vals['readonlyfs'] = None
71 vals['plcnode/last_contact'] = None
72 vals['comonstats/uptime'] = None
73 vals['princeton_comon'] = get_value('princeton_comon')
74 vals['princeton_comon_running'] = get_value('princeton_comon_running')
75 vals['princeton_comon_procs'] = get_value('princeton_comon_procs')
76
77
78 rows = ""
79 fb = database.dbLoad("findbad")
80 packed_values = []
81 node_count = 0
82 for mynode in fb['nodes'].keys():
83         fbnode = fb['nodes'][mynode]['values']
84         row = []
85         row.append(mynode)
86         add=True
87         if 'readonlyfs' in fbnode:
88                 if 'Read-only file system' in fbnode['readonlyfs']:
89                         fbnode['readonlyfs'] = 'Y'
90                 else:
91                         fbnode['readonlyfs'] = '_'
92
93         if 'dns' in fbnode:
94                 if 'boot.planet-lab.org has address' in fbnode['dns']:
95                         fbnode['dns'] = '_'
96                 else:
97                         fbnode['dns'] = 'N'
98                         
99         for key in ['ssh', 'state', 'plcnode/last_contact', 'readonlyfs', 'dns', 'nm', 'princeton_comon', 'princeton_comon_running', 'princeton_comon_procs', 'comonstats/uptime']:
100                 if get(fbnode, key) is None:
101                         row.append('nokey')
102                 else:
103                         if vals[key] is not None and vals[key] in get(fbnode, key):
104                                 add = True & add
105                         elif vals[key] is None:
106                                 add = True & add
107                         else:
108                                 add = False
109                         
110                         if 'last_contact' in key:
111                                 t = time.time()
112                                 lc = get(fbnode,key)
113                                 diff = ((t - lc) // (60*60*6)) * 6
114                                 row.append(-int(diff))
115                         else:
116                                 row.append(get(fbnode,key))
117         if add:
118                 packed_values.append(row)
119
120
121
122 def rowcmp(x,y):
123         for i in range(1,len(x)):
124                 if x[i] == y[i]: continue
125                 if x[i] < y[i]: return -1
126                 if x[i] > y[i]: return 1
127         return 0
128
129 packed_values.sort(rowcmp)
130
131 t = TABLE(border=1)
132 r = TR()
133 for value in ['num', 'host', 'ssh', 'state', 'last<br>contact', 'readonlyfs', 'dns', 'NM', 'comon<br>dir', 'comon<br>vserver', 'comon<br>procs']:
134         r.append(TD(value))
135 t.append(r)
136
137 i=1
138 for row in packed_values:
139         r = TR()
140         r.append(TD(i))
141         for value in row:
142                 r.append(TD(value))
143         i+=1 
144         t.append(r)
145                 
146 #r = TR()
147 #r.append(TD(node_count))
148 #t.append(r)
149
150 d = Document(t)
151 print d