1d0659404e4e3294fea425f37f0d88c3cc96c4a0
[monitor.git] / showlatlon.py
1 #!/usr/bin/python
2
3 from monitor.wrapper import plc, plccache
4 api = plc.getAuthAPI()
5
6 import sys
7 import reboot
8 from datetime import datetime, timedelta
9
10 import database
11 import comon
12 from monitor.common import color_pcu_state, datetime_fromstr
13 from nodehistory import get_filefromglob
14 import time
15 import traceback
16
17 # region
18 # total
19 # up
20 # up with good hardware
21 # up with good hardware & functional pcu
22
23 #cm_url="http://summer.cs.princeton.edu/status/tabulator.cgi?table=table_nodeviewshort&format=formatcsv&dumpcols='name,cpuspeed,memsize,disksize'"
24 #cm = database.if_cached_else(1, "cmhardware", lambda : comon.comonget(cm_url))
25
26 def gethardwarequality(nodename, fb):
27         if nodename in fb['nodes'] and 'comonstats' in fb['nodes'][nodename]['values']:
28                 cstat = fb['nodes'][nodename]['values']['comonstats']
29                 for field in ['cpuspeed', 'memsize', 'disksize']:
30                         if field not in cstat: cstat[field] = "null"
31
32                 if cstat['cpuspeed'] != "null" and float(cstat['cpuspeed']) < 2.2:
33                         return "BAD" # "cpu_slow",
34                 if cstat['memsize'] != "null" and float(cstat['memsize']) < 2.8:
35                         return "BAD" # "mem_small",
36                 if cstat['disksize'] != "null" and float(cstat['disksize']) < 300.0:
37                         return "BAD" # "disk_small",
38
39                 if cstat['disksize'] == "null" and \
40                    cstat['cpuspeed'] == "null" and \
41                    cstat['memsize'] == "null":
42                         return "N/A"
43
44                 try:
45                         if  float(cstat['cpuspeed']) >= 2.2 and \
46                                 float(cstat['memsize']) >= 2.8 and \
47                                 (cstat['disksize'] == "null" or float(cstat['disksize']) >= 300.0):
48                                 return "A-OK"
49                 except:
50                         print cstat
51
52                 return "ZOO"
53         else:
54                 return "N/A"
55
56 def addtostats(stats, a):
57         if a['cc'] not in stats:
58                 stats[a['cc']] = {'total' : 0,
59                                   'up' : 0,
60                                                   'goodhw': 0,
61                                                   'pcuok' : 0}
62         
63         stats[a['cc']]['total'] += 1
64         if a['status'] == "boot":
65                 stats[a['cc']]['up'] += 1  
66                 if a['hardware'] == "A-OK":
67                         stats[a['cc']]['goodhw'] += 1 
68                         if a['pcuok'] == "PCUOK  " or a['pcuok'] == "PCUA-OK":
69                                 stats[a['cc']]['pcuok'] += 1
70
71 def main():
72
73         stats = {}
74         path = "archive-pdb"
75         archive = database.SPickle(path)
76
77         if len(sys.argv) > 2:
78                 timestr = sys.argv[1]
79                 format = sys.argv[2]
80                 begin = timestr
81         else:
82                 format = "%Y-%m-%d"
83                 begin = time.strftime(format)
84
85         d = datetime_fromstr(begin)
86         fbstr = get_filefromglob(d, "production.findbad")
87         fbpcustr = get_filefromglob(d, "production.findbadpcus")
88
89         l_plcnodes = plccache.l_nodes
90         l_plcsites = plccache.l_sites
91         lb2hn = plccache.plcdb_lb2hn
92         fb = archive.load(fbstr) 
93         fbpcu = archive.load(fbpcustr)
94         reboot.fb = fbpcu
95
96         results = []
97         # COLLECT nodegroups, nodes and node lists
98         for site in l_plcsites:
99                 CC="none"
100                 if site['login_base'] in lb2hn:
101                         nodes = lb2hn[site['login_base']]
102                         for node in nodes:
103                                 hostname = node['hostname']
104                                 fields = hostname.split(".")
105                                 if len(fields[-1]) == 2:
106                                         CC=fields[-1]
107                                 elif fields[-1] == "edu":
108                                         CC="usedu"
109                                 elif site['login_base'] == "ft":
110                                         CC="fr"
111                                 elif site['login_base'] == "ntu":
112                                         CC="tw"
113                                 elif site['login_base'] in ["mcgill", "canarieottawa", 'canariecalgary',
114                                                                                         'canariehalifax', 'canariemontreal',
115                                                                                         'canarietoronto', 'canariewinnipeg']:
116                                         CC="ca"
117                                 elif site['login_base'] in ["plcoloclarasanti", "plcoloclarasaopa",
118                                                                                         "plcoloclarabueno", "plcoloclaratijua", 
119                                                                                         "plcoloclarapanam"]:
120                                         CC="southamerica"
121                                 elif site['login_base'] in ["plcoloamst", 'cwi']:
122                                         CC="nl"
123                                 elif site['login_base'] == "urv":
124                                         CC="es"
125                                 elif site['login_base'] == "ncl":
126                                         CC="uk"
127                                 elif site['login_base'] == "waterford":
128                                         CC="ie"
129                                 elif site['login_base'] in ["kisti", "snummlab"]:
130                                         CC="kr"
131                                 elif site['login_base'] == "astri":
132                                         CC="cn"
133                                 elif fields[-1] in [ "org", "net" ]:
134                                         CC="usorg"
135                                 elif fields[-1] == "com":
136                                         CC="uscom"
137                                 else:
138                                         CC=fields[-1]
139
140                                 if hostname in fb['nodes']:
141                                         if 'state' in fb['nodes'][hostname]['values']:
142                                                 state = fb['nodes'][hostname]['values']['state'].lower()
143                                         else:
144                                                 state = "unknown"
145
146                                         args = {'cc': CC, 
147                                                 'site' : site['login_base'],
148                                                 'host' : hostname,
149                                                 'status' : state,
150                                                 'hardware' : gethardwarequality(hostname, fb),
151                                                 'pcuok' : color_pcu_state(fb['nodes'][hostname]['values']) }
152                                         #except:
153                                         #       print traceback.print_exc()
154                                         #       print args
155                                         #       print fb['nodes'][hostname]['values']
156                                         results.append("%(cc)7s %(status)8s %(hardware)8s %(pcuok)8s %(site)15s %(host)42s " % args)
157                                         addtostats(stats, args)
158                 else:
159                         site['latitude'] = -2
160                         site['longitude'] = -2
161
162                 #print "%4s %20s %8s %8s" % (CC, site['login_base'], site['latitude'], site['longitude'])
163
164         regions = { 'mideast'   : ['cy', 'gr', 'il', 'in', 'lb', 'pk'],
165                                 'ca'        : ['ca'],
166                                 'usa'           : ['pr','us', 'uscom', 'usedu', 'usorg'],
167                                 'europe'        : ['at','ch','cz','be', 'de', 'dk', 
168                                                            'es','fi', 'fr', 'hu', 'ie', 'is', 'it','nl',
169                                                            'no', 'pl', 'pt', 'se', 'tr', 'uk'],
170                                 'asia'          : ['cn','hk','jp','kr', 'ru', 'sg', 'si','tw',],
171                                 'australia': ['au', 'nz',],
172                                 'southam'       : ['ar','br','southamerica','uy', 've'],
173                                 }
174         # fold stats
175         statsfold = {}
176         for key in regions.keys():
177                 statsfold[key] = {'total' : 0, 'up' : 0, 
178                                                 'goodhw': 0, 'pcuok' : 0}
179
180         totaltotal = {  'total' : 0, 'up' : 0, 
181                                         'goodhw': 0, 'pcuok' : 0}
182         # for all of the cc stats
183         for cc in stats.keys():
184                 # search for the cc in the regions dict
185                 for region in regions:
186                         # if the cc is assigned to a region
187                         if cc in regions[region]:
188                                 # add all values in cc stats to that region
189                                 for key in statsfold[region]:
190                                         statsfold[region][key] += stats[cc][key]
191                                         totaltotal[key] += stats[cc][key]
192
193         # print folded stats
194         print "       REGION | total | up  |& goodhw |& pcuok "
195         for region in statsfold.keys():
196                 statsfold[region]['region'] = region
197                 print "%(region)13s | %(total)5s | %(up)3s | %(goodhw)7s | %(pcuok)3s" % statsfold[region]
198         print "       totals | %(total)5s | %(up)3s | %(goodhw)7s | %(pcuok)3s" % totaltotal
199
200
201         print "      Region  | total | up  |& goodhw |& pcuok "
202         for region in stats.keys():
203                 stats[region]['region'] = region
204                 print "%(region)13s | %(total)5s | %(up)3s | %(goodhw)7s | %(pcuok)3s" % stats[region]
205
206         for line in results:
207                 print line
208                 
209 if __name__ == "__main__":
210         try:
211                 main()
212         except IOError:
213                 pass