AM nagios/plc2nagios.py
[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 soltesz
12 import comon
13 from nodecommon import color_pcu_state, datetime_fromstr
14 from nodehistory import get_filefromglob
15 import time
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 = soltesz.if_cached_else(1, "cmhardware", lambda : comon.comonget(cm_url))
25
26 def gethardwarequality(nodename, fb):
27         if nodename in fb['nodes']:
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.4:
33                         return "BAD" # "cpu_slow",
34                 if cstat['memsize'] != "null" and float(cstat['memsize']) < 2.9:
35                         return "BAD" # "mem_small",
36                 if cstat['disksize'] != "null" and float(cstat['disksize']) < 320.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.4 and \
46                                 float(cstat['memsize']) >= 2.9 and \
47                                 (cstat['disksize'] == "null" or float(cstat['disksize']) >= 320.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 = soltesz.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 = soltesz.dbLoad("l_plcnodes")
90         l_plcsites = soltesz.dbLoad("l_plcsites")
91         lb2hn = soltesz.dbLoad("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                                         args = {'cc': CC, 
142                                                 'site' : site['login_base'],
143                                                 'host' : hostname,
144                                                 'status' : fb['nodes'][hostname]['values']['state'].lower(),
145                                                 'hardware' : gethardwarequality(hostname, fb),
146                                                 'pcuok' : color_pcu_state(fb['nodes'][hostname]['values']) }
147                                         results.append("%(cc)7s %(status)8s %(hardware)8s %(pcuok)8s %(site)15s %(host)42s " % args)
148                                         addtostats(stats, args)
149                 else:
150                         site['latitude'] = -2
151                         site['longitude'] = -2
152
153                 #print "%4s %20s %8s %8s" % (CC, site['login_base'], site['latitude'], site['longitude'])
154
155         regions = { 'mideast'   : ['cy', 'gr', 'il', 'in', 'lb', 'pk'],
156                                 'ca'        : ['ca'],
157                                 'usa'           : ['pr','us', 'uscom', 'usedu', 'usorg'],
158                                 'europe'        : ['at','ch','cz','be', 'de', 'dk', 
159                                                            'es','fi', 'fr', 'hu', 'ie', 'is', 'it','nl',
160                                                            'no', 'pl', 'pt', 'se', 'tr', 'uk'],
161                                 'asia'          : ['cn','hk','jp','kr', 'ru', 'sg', 'si','tw',],
162                                 'australia': ['au', 'nz',],
163                                 'southam'       : ['ar','br','southamerica','uy', 've'],
164                                 }
165         # fold stats
166         statsfold = {}
167         for key in regions.keys():
168                 statsfold[key] = {'total' : 0, 'up' : 0, 
169                                                 'goodhw': 0, 'pcuok' : 0}
170
171         totaltotal = {  'total' : 0, 'up' : 0, 
172                                         'goodhw': 0, 'pcuok' : 0}
173         # for all of the cc stats
174         for cc in stats.keys():
175                 # search for the cc in the regions dict
176                 for region in regions:
177                         # if the cc is assigned to a region
178                         if cc in regions[region]:
179                                 # add all values in cc stats to that region
180                                 for key in statsfold[region]:
181                                         statsfold[region][key] += stats[cc][key]
182                                         totaltotal[key] += stats[cc][key]
183
184         # print folded stats
185         print "       REGION | total | up  |& goodhw |& pcuok "
186         for region in statsfold.keys():
187                 statsfold[region]['region'] = region
188                 print "%(region)13s | %(total)5s | %(up)3s | %(goodhw)7s | %(pcuok)3s" % statsfold[region]
189         print "       totals | %(total)5s | %(up)3s | %(goodhw)7s | %(pcuok)3s" % totaltotal
190
191
192         print "      Region  | total | up  |& goodhw |& pcuok "
193         for region in stats.keys():
194                 stats[region]['region'] = region
195                 print "%(region)13s | %(total)5s | %(up)3s | %(goodhw)7s | %(pcuok)3s" % stats[region]
196
197         for line in results:
198                 print line
199                 
200 if __name__ == "__main__":
201         try:
202                 main()
203         except IOError:
204                 pass