- fix UP/DOWN summary on nodes page.
[monitor.git] / nodequery.py
1 #!/usr/bin/python
2
3
4 import sys
5 from monitor import database
6 from monitor.common import *
7 from monitor.query import *
8 from monitor.model import Record
9 import glob
10 import os
11 import traceback
12
13 import time
14 import re
15 import string
16
17 from monitor.wrapper import plc
18 api = plc.getAuthAPI()
19
20 from monitor.database.info.model import HistoryNodeRecord, FindbadNodeRecord, FindbadPCURecord, session
21 from monitor.util import file as utilfile
22 from monitor import config
23
24
25 def daysdown_print_nodeinfo(fbnode, hostname):
26         fbnode['hostname'] = hostname
27         fbnode['daysdown'] = Record.getStrDaysDown(fbnode)
28         fbnode['intdaysdown'] = Record.getDaysDown(fbnode)
29
30         print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % fbnode
31
32 def fb_print_nodeinfo(fbnode, hostname, fields=None):
33         #fbnode['hostname'] = hostname
34         #fbnode['checked'] = diff_time(fbnode['checked'])
35         if fbnode['bootcd_version']:
36                 fbnode['bootcd_version'] = fbnode['bootcd_version'].split()[-1]
37         else:
38                 fbnode['bootcd_version'] = "unknown"
39         if not fbnode['boot_server']:
40                 fbnode['boot_server'] = "unknown"
41         fbnode['pcu'] = color_pcu_state(fbnode)
42
43         if not fields:
44                 if ( fbnode['observed_status'] is not None and \
45                    'DOWN' in fbnode['observed_status'] ) or \
46                    fbnode['kernel_version'] is None:
47                         fbnode['kernel_version'] = ""
48                 else:
49                         fbnode['kernel_version'] = fbnode['kernel_version'].split()[2]
50
51                 if fbnode['plc_node_stats'] is not None:
52                         fbnode['boot_state'] = fbnode['plc_node_stats']['boot_state']
53                 else:
54                         fbnode['boot_state'] = "unknown"
55
56                 try:
57                         if len(fbnode['nodegroups']) > 0:
58                                 fbnode['category'] = fbnode['nodegroups'][0]
59                 except:
60                         #print "ERROR!!!!!!!!!!!!!!!!!!!!!"
61                         pass
62
63                 print "%(hostname)-45s | %(date_checked)11.11s | %(boot_state)5.5s| %(observed_status)8.8s | %(ssh_status)5.5s | %(pcu)6.6s | %(bootcd_version)6.6s | %(boot_server)s | %(kernel_version)s" % fbnode
64         else:
65                 format = ""
66                 for f in fields:
67                         format += "%%(%s)s " % f
68                 print format % fbnode
69
70 def main():
71
72         from monitor import parser as parsermodule
73         parser = parsermodule.getParser()
74
75         parser.set_defaults(node=None, fromtime=None, select=None, list=None, listkeys=False,
76                                                 pcuselect=None, nodelist=None, daysdown=None, fields=None)
77         parser.add_option("", "--daysdown", dest="daysdown", action="store_true",
78                                                 help="List the node state and days down...")
79         parser.add_option("", "--select", dest="select", metavar="key=value", 
80                                                 help="List all nodes with the given key=value pattern")
81         parser.add_option("", "--fields", dest="fields", metavar="key,list,...", 
82                                                 help="a list of keys to display for each entry.")
83         parser.add_option("", "--list", dest="list", action="store_true", 
84                                                 help="Write only the hostnames as output.")
85         parser.add_option("", "--pcuselect", dest="pcuselect", metavar="key=value", 
86                                                 help="List all nodes with the given key=value pattern")
87         parser.add_option("", "--nodelist", dest="nodelist", metavar="nodelist.txt", 
88                                                 help="A list of nodes to bring out of debug mode.")
89         parser.add_option("", "--listkeys", dest="listkeys", action="store_true",
90                                                 help="A list of nodes to bring out of debug mode.")
91         parser.add_option("", "--fromtime", dest="fromtime", metavar="YYYY-MM-DD",
92                                         help="Specify a starting date from which to begin the query.")
93
94         parser = parsermodule.getParser(['defaults'], parser)
95         config = parsermodule.parse_args(parser)
96         
97         if config.fromtime:
98                 path = "archive-pdb"
99                 archive = database.SPickle(path)
100                 d = datetime_fromstr(config.fromtime)
101                 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
102                 os.chdir(path)
103                 #print glob_str
104                 file = glob.glob(glob_str)[0]
105                 #print "loading %s" % file
106                 os.chdir("..")
107                 fb = archive.load(file[:-4])
108         else:
109                 #fbnodes = FindbadNodeRecord.select(FindbadNodeRecord.q.hostname, orderBy='date_checked',distinct=True).reversed()
110                 fb = None
111
112         if config.nodelist:
113                 nodelist = utilfile.getListFromFile(config.nodelist)
114         else:
115                 # NOTE: list of nodes should come from findbad db.   Otherwise, we
116                 # don't know for sure that there's a record in the db..
117                 fbquery = HistoryNodeRecord.query.all()
118                 nodelist = [ n.hostname for n in fbquery ]
119
120         pculist = None
121         if config.select is not None and config.pcuselect is not None:
122                 nodelist = node_select(config.select, nodelist, fb)
123                 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
124         elif config.select is not None:
125                 nodelist = node_select(config.select, nodelist, fb)
126         elif config.pcuselect is not None:
127                 print "thirhd node select"
128                 nodelist, pculist = pcu_select(config.pcuselect, nodelist)
129
130         #if pculist:
131         #       for pcu in pculist:
132         #               print pcu
133
134         print "len: %s" % len(nodelist)
135         for node in nodelist:
136                 config.node = node
137
138                 try:
139                         # Find the most recent record
140                         fb_noderec = FindbadNodeRecord.get_latest_by(hostname=node) 
141                         if not fb_noderec: continue
142                         fb_nodeinfo = fb_noderec.to_dict()
143                 except KeyboardInterrupt:
144                         print "Exiting at user request: Ctrl-C"
145                         sys.exit(1)
146                 except:
147                         print traceback.print_exc()
148                         continue
149
150                 if config.listkeys:
151                         print "Primary keys available in the findbad object:"
152                         for key in fb_nodeinfo.keys():
153                                 print "\t",key
154                         sys.exit(0)
155                         
156
157                 if config.list:
158                         print node
159                 else:
160                         if config.daysdown:
161                                 daysdown_print_nodeinfo(fb_nodeinfo, node)
162                         else:
163                                 if config.select:
164                                         if config.fields:
165                                                 fields = config.fields.split(",")
166                                         else:
167                                                 fields = None
168
169                                         fb_print_nodeinfo(fb_nodeinfo, node, fields)
170                                 elif not config.select and 'observed_status' in fb_nodeinfo:
171                                         fb_print_nodeinfo(fb_nodeinfo, node)
172                                 else:
173                                         print "passing..."
174                                         pass
175                 
176 if __name__ == "__main__":
177         main()