clearer names for actions, and infer actions better
[monitor.git] / statistics / parse_nodestatus.py
1 #!/usr/bin/python
2
3 import sys
4 import os
5
6 def recent(q, val, length):
7     return [val] + q[:length-1]
8
9 def add(d, path, v):
10     if path not in d:
11         d[path] = []
12     d[path] = recent(d[path], v, 6)
13     #d[path].append(v)
14
15 def stateof(l):
16     if len(set(l)) == 1:
17         return l[0]
18     else:
19         return "BOOT"
20
21 def in_hard_state(l):
22     if len(set(l)) == 1:
23         return True
24     else:
25         return False
26
27 recent_node_ts = {}
28 recent_node_states = {}
29 last_node_state = {}
30 last_node_ts = {}
31 print "ts,host,status,pcustatus,model,length"
32
33 while True:
34     line = sys.stdin.readline()
35     if not line:
36         break
37     line = line.strip()
38     # NOTE assumes ts are ordered.
39     try:
40         (ts, host, state, pcu, pcumodel) = line.split(',')
41     except:
42         print >>sys.stderr, "EXCEPTION:", line
43         while True:
44             line = sys.stdin.readline()
45             if not line:
46                 break
47             line = line.strip()
48             if line[0] == ',':
49                 pcumodel = line[1:]
50                 break
51         #sys.exit(1)
52
53     ts = int(float(ts))
54     add(recent_node_states, host, state)
55     add(recent_node_ts, host, ts)
56
57     if host not in last_node_state:
58         last_node_state[host] = "UNKNOWN"
59         last_node_ts[host] = ts
60         
61     if ( (in_hard_state(recent_node_states[host]) and stateof(recent_node_states[host]) == "DOWN") or \
62         not in_hard_state(recent_node_states[host]) ) and \
63        last_node_state[host] != stateof(recent_node_states[host]):
64
65         if stateof(recent_node_states[host]) == "DOWN":
66             ts = recent_node_ts[host][-1]   # get earliest time down
67         print "%s,%s,%s,%s,%s,%s" % (ts, host, stateof(recent_node_states[host]), pcu, pcumodel, ts-last_node_ts[host] )
68         last_node_state[host] = stateof(recent_node_states[host])
69         last_node_ts[host] = ts
70
71