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