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 if not fbnode['boot_server']:
40 fbnode['boot_server'] = "unknown"
41 fbnode['pcu'] = color_pcu_state(fbnode)
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'] = ""
49 fbnode['kernel_version'] = fbnode['kernel_version'].split()[2]
51 if fbnode['plc_node_stats'] is not None:
52 fbnode['boot_state'] = fbnode['plc_node_stats']['boot_state']
54 fbnode['boot_state'] = "unknown"
57 if len(fbnode['nodegroups']) > 0:
58 fbnode['category'] = fbnode['nodegroups'][0]
60 #print "ERROR!!!!!!!!!!!!!!!!!!!!!"
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
67 format += "%%(%s)s " % f
72 from monitor import parser as parsermodule
73 parser = parsermodule.getParser()
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.")
94 parser = parsermodule.getParser(['defaults'], parser)
95 config = parsermodule.parse_args(parser)
99 archive = database.SPickle(path)
100 d = datetime_fromstr(config.fromtime)
101 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
104 file = glob.glob(glob_str)[0]
105 #print "loading %s" % file
107 fb = archive.load(file[:-4])
109 #fbnodes = FindbadNodeRecord.select(FindbadNodeRecord.q.hostname, orderBy='date_checked',distinct=True).reversed()
113 nodelist = utilfile.getListFromFile(config.nodelist)
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 ]
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)
131 # for pcu in pculist:
134 print "len: %s" % len(nodelist)
135 for node in nodelist:
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"
147 print traceback.print_exc()
151 print "Primary keys available in the findbad object:"
152 for key in fb_nodeinfo.keys():
161 daysdown_print_nodeinfo(fb_nodeinfo, node)
165 fields = config.fields.split(",")
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)
176 if __name__ == "__main__":