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