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