fe44423cacbdf925e1a2c8701140c770abeca665
[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['plcnode/last_contact'] = None
70 vals['comonstats/uptime'] = None
71 vals['princeton_comon'] = get_value('princeton_comon')
72 vals['princeton_comon_running'] = get_value('princeton_comon_running')
73 vals['princeton_comon_procs'] = get_value('princeton_comon_procs')
74
75
76 rows = ""
77 fb = database.dbLoad("findbad")
78 packed_values = []
79 node_count = 0
80 for mynode in fb['nodes'].keys():
81         fbnode = fb['nodes'][mynode]['values']
82         row = []
83         row.append(mynode)
84         add=True
85         for key in ['ssh', 'state', 'plcnode/last_contact', 'nm', 'princeton_comon', 'princeton_comon_running', 'princeton_comon_procs', 'comonstats/uptime']:
86                 if get(fbnode, key) is None:
87                         row.append('nokey')
88                 else:
89                         if vals[key] is not None and vals[key] in get(fbnode, key):
90                                 add = True & add
91                         elif vals[key] is None:
92                                 add = True & add
93                         else:
94                                 add = False
95                         
96                         if 'last_contact' in key:
97                                 t = time.time()
98                                 lc = get(fbnode,key)
99                                 diff = ((t - lc) // (60*60*6)) * 6
100                                 row.append(-int(diff))
101                         else:
102                                 row.append(get(fbnode,key))
103         if add:
104                 packed_values.append(row)
105
106
107
108 def rowcmp(x,y):
109         for i in range(1,len(x)):
110                 if x[i] == y[i]: continue
111                 if x[i] < y[i]: return -1
112                 if x[i] > y[i]: return 1
113         return 0
114
115 packed_values.sort(rowcmp)
116
117 t = TABLE(border=1)
118 r = TR()
119 for value in ['num', 'host', 'ssh', 'state', 'last<br>contact', 'NM', 'comon<br>dir', 'comon<br>vserver', 'comon<br>procs']:
120         r.append(TD(value))
121 t.append(r)
122
123 i=1
124 for row in packed_values:
125         r = TR()
126         r.append(TD(i))
127         for value in row:
128                 r.append(TD(value))
129         i+=1 
130         t.append(r)
131                 
132 #r = TR()
133 #r.append(TD(node_count))
134 #t.append(r)
135
136 d = Document(t)
137 print d