add new scripts
[monitor.git] / statistics / nodediff-graph-better.py
1 #!/usr/bin/python
2
3
4 from monitor import config
5 from monitor.wrapper import plc
6 from monitor import parser as parsermodule
7 #from monitor.model import *
8 from monitorstats import *
9 from monitor import database
10
11 import sys
12 import time
13 import calendar
14 from datetime import datetime, timedelta
15
16 from nodequeryold import verify,query_to_dict,node_select
17
18 api = plc.getAuthAPI()
19
20 def nodes_from_time(arch, file, select=None):
21         fb = arch.load(file)
22
23         nodelist = fb['nodes'].keys()
24         nodelist = node_select(select, nodelist, fb)
25         return nodelist
26
27 def print_nodelist(nodelist, file):
28         for node in nodelist:
29                 if file:
30                         print >>file, node
31                 else:
32                         print node
33         
34
35 def main():
36         parser = parsermodule.getParser()
37         parser.set_defaults(archivedir='archive-pdb', begin=None, end=None, printnodes=False, select=None, select2=None)
38         parser.add_option("", "--archivedir", dest="archivedir", metavar="filename",
39                                                 help="Pickle file aggregate output.")
40         parser.add_option("", "--select", dest="select", metavar="key",
41                                                 help="Select .")
42         parser.add_option("", "--select2", dest="select2", metavar="key",
43                                                 help="Select .")
44         parser.add_option("", "--print", dest="printnodes", action="store_true",
45                                                 help="print the nodes that have come up or down.")
46         parser.add_option("", "--begin", dest="begin", metavar="YYYY-MM-DD",
47                                                 help="Specify a starting date from which to begin the query.")
48         parser.add_option("", "--end", dest="end", metavar="YYYY-MM-DD",
49                                                 help="Specify a ending date at which queries end.")
50         config = parsermodule.parse_args(parser)
51         archive = get_archive(config.archivedir)
52
53         if not config.begin or not config.end:
54                 print parsermodule.usage(parser)
55                 sys.exit(1)
56
57         tdelta = timedelta(1)
58         d_s1 = datetime_fromstr(config.begin)
59         d_s2 = datetime_fromstr(config.begin) + tdelta
60         d_end = datetime_fromstr(config.end)
61
62         print d_s1
63         print d_s2
64         print d_end
65
66         data = []
67         while d_end > d_s2:
68
69                 try:
70                         f_s1 = get_filefromglob(d_s1, "production.findbad", config.archivedir)
71                         f_s2 = get_filefromglob(d_s2, "production.findbad", config.archivedir)
72                 except:
73                         timestr = d_s2.strftime("%Y-%m-%d")
74                         print timestr, ",", 0, ",", 0
75                         d_s1 = d_s2
76                         d_s2 = d_s1 + tdelta
77                         continue
78
79                 s1 = set(nodes_from_time(archive, f_s1, config.select))
80                 s2 = set(nodes_from_time(archive, f_s2, config.select))
81                 s3 = set(nodes_from_time(archive, f_s2, config.select2))
82
83
84                 timestr = d_s2.strftime("%Y-%m-%d")
85                 print timestr, ",", len(s2),",",  len(s3)
86                 if not config.printnodes:
87                 #       f_up = open("up-%s" % timestr, 'w')
88                 #       f_down = open("dn-%s" % timestr, 'w')
89                         f_up = None
90                         f_down = None
91                         pass
92                 else:
93                         print "%s nodes up" % len(s2-s1)
94                         print "Nodes s2 minus s1: len(s2-s1) = %s" % len(s2-s1)
95                         f_up = None
96                         f_down = None
97
98                 #print_nodelist(s2-s1, f_up)
99
100                 if config.printnodes:
101                         print ""
102                         print "%s nodes down" % len(s1-s2)
103                         print "Nodes s1 minus s2: len(s1-s2) = %s" % len(s1-s2)
104
105                 #print_nodelist(s1-s2, f_down)
106                 if not config.printnodes:
107                         if f_up: f_up.close()
108                         if f_up: f_down.close()
109
110                 d_s1 = d_s2
111                 d_s2 = d_s1 + tdelta
112         
113         s1=[]
114         s2=[]
115         s3=[]
116         for row in data:
117                 s1.append(row[0])
118                 s2.append(row[1])
119                 s3.append(row[2])
120         
121         s1 = map(lambda x: x-500, s1)
122         rlow= zip(s1,s3)
123         rhigh = zip(s1,s2)
124         diff_low  = map(lambda x: x[0]-x[1], rlow)
125         diff_high = map(lambda x: x[0]+x[1], rhigh)
126         s1 = map(lambda x: str(x), s1)
127         diff_low = map(lambda x: str(x), diff_low)
128         diff_high = map(lambda x: str(x), diff_high)
129         print s1
130         print diff_low
131         print diff_high
132         print "http://chart.apis.google.com/chart?cht=lc&chds=40,100&chxt=x,y&chxl=0:|Oct|Nov|Dec|Jan|Feb|1:|540|580|600&chs=700x200&chm=F,aaaaaa,1,-1,2&chd=t1:%s" % ",".join(s1) + "|" + ",".join(diff_low) + "|" + ",".join(s1) + "|" + ",".join(s1) +"|" + ",".join(diff_high)
133
134 # takes two arguments as dates, comparing the number of up nodes from one and
135 # the other.
136
137 if __name__ == "__main__":
138         main()