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 if not fbnode['install_date']:
42 fbnode['install_date'] = "unknown"
43 fbnode['pcu'] = color_pcu_state(fbnode)
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'] = ""
51 fbnode['kernel_version'] = fbnode['kernel_version'].split()[2]
53 if fbnode['plc_node_stats'] is not None:
54 fbnode['boot_state'] = fbnode['plc_node_stats']['boot_state']
56 fbnode['boot_state'] = "unknown"
59 if len(fbnode['nodegroups']) > 0:
60 fbnode['category'] = fbnode['nodegroups'][0]
62 #print "ERROR!!!!!!!!!!!!!!!!!!!!!"
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
69 format += "%%(%s)s " % f
74 from monitor import parser as parsermodule
75 parser = parsermodule.getParser()
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.")
96 parser = parsermodule.getParser(['defaults'], parser)
97 config = parsermodule.parse_args(parser)
101 archive = database.SPickle(path)
102 d = datetime_fromstr(config.fromtime)
103 glob_str = "%s*.production.findbad.pkl" % d.strftime("%Y-%m-%d")
106 file = glob.glob(glob_str)[0]
107 #print "loading %s" % file
109 fb = archive.load(file[:-4])
111 #fbnodes = FindbadNodeRecord.select(FindbadNodeRecord.q.hostname, orderBy='date_checked',distinct=True).reversed()
115 nodelist = utilfile.getListFromFile(config.nodelist)
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 ]
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)
133 # for pcu in pculist:
136 print >>sys.stderr, "len: %s" % len(nodelist)
137 for node in nodelist:
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"
149 print traceback.print_exc()
153 print "Primary keys available in the findbad object:"
154 for key in fb_nodeinfo.keys():
163 daysdown_print_nodeinfo(fb_nodeinfo, node)
167 fields = config.fields.split(",")
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)
178 if __name__ == "__main__":