clearer names for actions, and infer actions better
[monitor.git] / statistics / harvest_nodestatus.py
1 #!/usr/bin/python
2
3 # Collect statistics from myops db on node downtimes.
4 # For every node that goes down we need:
5 #    * node down time
6 #    * node reboot time
7 #    * node notice time
8 #    * node up time
9
10 # then for each node, order events by time
11 #    for each event sequence extract sub-sequences like:
12 #        down xx up         
13 #    for each such sub-sequence extract
14 #        time between down and up
15
16 from monitor.database.info.model import *
17 from math import *
18 import sys
19 from datetime import datetime, timedelta
20 from monitor.common import *
21
22 if len(sys.argv) > 1 and sys.argv[1] == "--pcu":
23     args = sys.argv[2:]
24     find_pcu = True
25 else:
26     args = sys.argv[1:]
27     find_pcu = False
28
29
30 for index,node in enumerate(FindbadNodeRecord.query.all()):
31
32     if find_pcu:
33         if not node.plc_pcuid: 
34             # NOTE: if we're looking for nodes with pcus, then skip the ones that don't have them.
35             continue
36     else:
37         if node.plc_pcuid:
38             # NOTE: if we're not looking for nodes with pcus, then skip those that have them
39             continue
40
41     print >>sys.stderr, index, node.hostname
42
43     for v in node.versions:
44         if find_pcu:
45             # we should be guaranteed that the node has pcuids here, but it could be deleted or added later
46             pcu = FindbadPCURecord.get_by(plc_pcuid=v.plc_pcuid)
47             if pcu:
48                 v_pcu = pcu.get_as_of(v.timestamp)
49                 if v_pcu:
50                     print "%s,%s,%s,%s,%s" % (Time.dt_to_ts(v.timestamp), v.hostname, v.observed_status, v_pcu.reboot_trial_status, v_pcu.plc_pcu_stats['model'])
51             else:
52                 print "%s,%s,%s,%s,%s" % (Time.dt_to_ts(v.timestamp), v.hostname, v.observed_status, "NOPCU", "NONE")
53                 
54         else:
55             print "%s,%s,%s,%s,%s" % (Time.dt_to_ts(v.timestamp), v.hostname, v.observed_status, "NOPCU", "NONE")