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