5 from monitor import database
6 from monitor.common import *
7 from monitor.query import *
8 from monitor.model import Record
17 from monitor.wrapper import plc
18 api = plc.getAuthAPI()
20 from monitor.database.info.model import HistoryNodeRecord, FindbadNodeRecord, FindbadPCURecord, session
21 from monitor.util import file as utilfile
22 from monitor import config
25 def daysdown_print_nodeinfo(fbnode, hostname):
26 fbnode['hostname'] = hostname
27 fbnode['daysdown'] = Record.getStrDaysDown(fbnode)
28 fbnode['intdaysdown'] = Record.getDaysDown(fbnode)
30 print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % fbnode
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]
38 fbnode['bootcd_version'] = "unknown"
39 fbnode['pcu'] = color_pcu_state(fbnode)
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'] = ""
47 fbnode['kernel_version'] = fbnode['kernel_version'].split()[2]
49 if fbnode['plc_node_stats'] is not None:
50 fbnode['boot_state'] = fbnode['plc_node_stats']['boot_state']
52 fbnode['boot_state'] = "unknown"
55 if len(fbnode['nodegroups']) > 0:
56 fbnode['category'] = fbnode['nodegroups'][0]
58 #print "ERROR!!!!!!!!!!!!!!!!!!!!!"
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
65 format += "%%(%s)s " % f
70 from monitor import parser as parsermodule
71 parser = parsermodule.getParser()
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.")
92 parser = parsermodule.getParser(['defaults'], parser)
93 config = parsermodule.parse_args(parser)
97 archive = database.SPickle(path)
98 d = datetime_fromstr(config.fromtime)
99 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
102 file = glob.glob(glob_str)[0]
103 #print "loading %s" % file
105 fb = archive.load(file[:-4])
107 #fbnodes = FindbadNodeRecord.select(FindbadNodeRecord.q.hostname, orderBy='date_checked',distinct=True).reversed()
111 nodelist = utilfile.getListFromFile(config.nodelist)
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 ]
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)
129 # for pcu in pculist:
132 print "len: %s" % len(nodelist)
133 for node in nodelist:
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"
145 print traceback.print_exc()
149 print "Primary keys available in the findbad object:"
150 for key in fb_nodeinfo.keys():
159 daysdown_print_nodeinfo(fb_nodeinfo, node)
163 fields = config.fields.split(",")
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)
174 if __name__ == "__main__":