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