AM nagios/plc2nagios.py
[monitor.git] / printbadcsv.py
1 #!/usr/bin/python
2 import soltesz
3 from config import config
4 from optparse import OptionParser
5 from www.printbadnodes import *
6
7 def main():
8         global fb
9         db = soltesz.dbLoad(config.dbname)
10         fb = soltesz.dbLoad("findbadpcus")
11         act= soltesz.dbLoad("act_all")
12
13         ## Field widths used for printing
14         maxFieldLengths = { 'nodename' : -45,
15                                                 'ping' : 6, 
16                                                 'ssh' : 6, 
17                                                 'rt' : 10, 
18                                                 'pcu' : 7, 
19                                                 'category' : 9, 
20                                                 'state' : 5, 
21                                                 'kernel' : 10.65, 
22                                                 'comonstats' : 5, 
23                                                 'plcsite' : 12,
24                                                 'bootcd' : 10.65}
25         ## create format string based on config.fields
26         fields = {}
27         format = ""
28         for f in config.fields.split(','):
29                 fields[f] = "%%(%s)%ds" % (f, maxFieldLengths[f])
30         for f in config.fields.split(','):
31                 format += fields[f] + " "
32
33
34         d_n = db['nodes']
35         if config.display:
36                 l_nodes = sys.argv[2:]
37         else:
38                 l_nodes = d_n.keys()
39
40         # d2 was an array of [{node}, {}, ...]
41         # the bysite is a loginbase dict of [{node}, {node}]
42         d2 = []
43         for nodename in l_nodes: 
44                 vals=d_n[nodename]['values'] 
45                 v = {}
46                 v.update(vals)
47                 v['nodename'] = nodename 
48                 if 'plcsite' in vals and 'status' in vals['plcsite'] and vals['plcsite']['status'] == "SUCCESS":
49                         site_string = "<b>%-20s</b> %2s nodes :: %2s of %4s slices" % ( \
50                                                                                                                 vals['plcsite']['login_base'],
51                                                                                                                 vals['plcsite']['num_nodes'], 
52                                                                                                                 vals['plcsite']['num_slices'], 
53                                                                                                                 vals['plcsite']['max_slices'])
54                         v['site_string'] = site_string
55                         d2.append(v)
56                 else:
57                         #print "ERROR: ", nodename, vals, "<br>"
58                         pass
59                         #site_string = "<b>UNKNOWN</b>"
60                         
61
62         if config.cmpping:
63                 d2.sort(cmp=cmpPing)
64         elif config.cmpssh:
65                 d2.sort(cmp=cmpSSH)
66         elif config.cmpcategory:
67                 d2.sort(cmp=cmpCategory)
68         elif config.cmpstate:
69                 d2.sort(cmp=cmpState)
70         elif config.cmpdays:
71                 d2.sort(cmp=cmpDays)
72         elif config.cmpkernel:
73                 d2.sort(cmp=cmpUname)
74         else:
75                 d2.sort(cmp=cmpCategory)
76         
77
78         for row in d2:
79                 site_string = row['site_string']
80                 vals = row
81                 # convert uname values into a single kernel version string
82                 if 'kernel' in vals:
83                         kernel = vals['kernel'].split()
84                         if len(kernel) > 0:
85                                 if kernel[0] == "Linux":
86                                         vals['kernel'] = kernel[2]
87                                 else:
88                                         vals['ssherror'] = vals['kernel']
89                                         vals['kernel'] = ""
90                 else:
91                         vals['ssherror'] = ""
92                         vals['kernel'] = ""
93                         continue
94
95                 if 'pcu' in vals and vals['pcu'] == "PCU":
96                         # check the health of the pcu.
97                         s = pcu_state(vals['plcnode']['pcu_ids'][0])
98                         if s == 0:
99                                 vals['pcu'] = "UP-PCU"
100                         else:
101                                 vals['pcu'] = "DN-PCU"
102
103                 vals['rt'] = " -"
104                 if vals['nodename'] in act:
105                         if len(act[vals['nodename']]) > 0 and 'rt' in act[vals['nodename']][0]:
106                                 if 'Status' in act[vals['nodename']][0]['rt']:
107                                         vals['rt'] = "%s %s" % (act[vals['nodename']][0]['rt']['Status'], 
108                                                                                         act[vals['nodename']][0]['rt']['id'])
109
110                 str = format % vals 
111                 fields = str.split()
112                 #print "<tr>"
113                 s = fields_to_html(fields, vals)
114                 s = ""
115                 if config.display:
116                         print str
117
118         keys = categories.keys()
119         for cat in ['BOOT-ALPHA', 'BOOT-PROD', 'BOOT-OLDBOOTCD', 'DEBUG-ALPHA',
120         'DEBUG-PROD', 'DEBUG-OLDBOOTCD', 'DOWN-ERROR']:
121                 if cat not in keys:
122                         categories[cat] = 0
123         keys = categories.keys()
124         for cat in ['BOOT-ALPHA', 'BOOT-PROD', 'BOOT-OLDBOOTCD', 'DEBUG-ALPHA',
125         'DEBUG-PROD', 'DEBUG-OLDBOOTCD', 'DOWN-ERROR']:
126                 if cat in keys:
127                         print "%d," % categories[cat],
128         print ""
129 import cgi
130 if __name__ == '__main__':
131         parser = OptionParser()
132         parser.set_defaults(cmpdays=False, 
133                                                 comon="sshstatus", 
134                                                 fields="nodename,ping,ssh,pcu,category,state,kernel,bootcd,rt", 
135                                                 dbname="findbad", # -070724-1", 
136                                                 display=False,
137                                                 cmpping=False, 
138                                                 cmpssh=False, 
139                                                 cmpcategory=False,
140                                                 cmpstate=False)
141         parser.add_option("", "--fields",       dest="dbname", help="")
142         parser.add_option("", "--dbname",       dest="dbname", help="")
143         parser.add_option("", "--display",      dest="display", action="store_true")
144         parser.add_option("", "--days",         dest="cmpdays", action="store_true", help="")
145         parser.add_option("", "--ping",         dest="cmpping", action="store_true", help="")
146         parser.add_option("", "--ssh",          dest="cmpssh",  action="store_true", help="")
147         parser.add_option("", "--category",     dest="cmpcategory", action="store_true", help="")
148         parser.add_option("", "--kernel",       dest="cmpkernel", action="store_true", help="")
149         parser.add_option("", "--state",        dest="cmpstate", action="store_true", help="")
150         parser.add_option("", "--comon",        dest="comon",   help="")
151         config = config(parser)
152         config.parse_args()
153         main()