5 from monitor import database
6 from monitor.common import *
7 from monitor.model import Record
16 from monitor.wrapper import plc
17 api = plc.getAuthAPI()
19 from monitor.util import file
20 from monitor import config
22 from monitor.sources import comon
24 default_fields="name,resptime,sshstatus,date,uptime,lastcotop,cpuspeed,memsize,disksize"
26 class NoKeyException(Exception): pass
28 def daysdown_print_nodeinfo(co_nodeinfo, hostname):
29 co_nodeinfo['hostname'] = hostname
30 co_nodeinfo['daysdown'] = Record.getStrDaysDown(co_nodeinfo)
31 co_nodeinfo['intdaysdown'] = Record.getDaysDown(co_nodeinfo)
33 print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % co_nodeinfo
35 def co_print_nodeinfo(co_nodeinfo, hostname, fields=None):
37 co_nodeinfo['name'] = hostname
39 if 'uptime' in co_nodeinfo and co_nodeinfo['uptime'] != "null":
40 co_nodeinfo['uptime'] = diff_time(time.time()-float(co_nodeinfo['uptime']))
42 if 'date' in co_nodeinfo and co_nodeinfo['date'] != "null":
43 co_nodeinfo['date'] = diff_time(float(co_nodeinfo['date']))
45 if fields == default_fields.split(','):
47 print "%(name)-40s %(sshstatus)5.5s %(resptime)6.6s %(lastcotop)6.6s %(uptime)s" % co_nodeinfo
51 format += "%%(%s)s," % f
52 print format % co_nodeinfo
56 from monitor import parser as parsermodule
57 parser = parsermodule.getParser()
59 parser.set_defaults(node=None,
68 fields=default_fields)
69 parser.add_option("", "--daysdown", dest="daysdown", action="store_true",
70 help="List the node state and days down...")
72 parser.add_option("", "--select", dest="select", metavar="key=value",
73 help="List all nodes with the given key=value pattern")
74 parser.add_option("", "--fields", dest="fields", metavar="key,list,...",
75 help="a list of keys to display for each entry.")
76 parser.add_option("", "--list", dest="list", action="store_true",
77 help="Write only the hostnames as output.")
78 parser.add_option("", "--nodelist", dest="nodelist", metavar="nodelist.txt",
79 help="A list of nodes to bring out of debug mode.")
80 parser.add_option("", "--listkeys", dest="listkeys", action="store_true",
81 help="A list of nodes to bring out of debug mode.")
82 parser.add_option("", "--cache", dest="cache", action="store_true",
83 help="Specify whether the command should perform a live query or save/use a cached values.")
85 parser.add_option("", "--dns", dest="dns", action="store_true",
86 help="A convenience query for dns values")
88 parser = parsermodule.getParser(['defaults'], parser)
89 config = parsermodule.parse_args(parser)
91 # lastcotop measures whether cotop is actually running. this is a better
92 # metric than sshstatus, or other values from CoMon
94 COMON_COTOPURL= "http://comon.cs.princeton.edu/status/tabulator.cgi?" + \
95 "table=table_nodeview&formatcsv"
97 config.fields = "name,dns1udp,dns1tcp,dns2udp,dns2tcp"
98 config.select = "dns1udp>0||dns1tcp>0||dns2udp>0||dns2tcp>0"
100 if config.fields == "all":
101 cotop_url = COMON_COTOPURL
103 cotop_url = COMON_COTOPURL + "&dumpcols='%s'" % config.fields
106 cotop_url = cotop_url + "&select='%s'" % config.select
109 cotop_url = COMON_COTOPURL + "&limit=1"
111 cotop = comon.Comon()
112 if database.cachedRecently(cotop_url):
113 print >>sys.stderr, "loading cached query :%s" % cotop_url
114 cohash = database.dbLoad(cotop_url)
116 cohash = cotop.coget(cotop_url)
118 print >>sys.stderr, "saving query :%s" % cotop_url
119 database.dbDump(cotop_url, cohash)
122 nodelist = file.getListFromFile(config.nodelist)
124 # NOTE: list of nodes should come from comon query.
125 nodelist = cohash.keys()
127 fields = config.fields.split(",")
129 for f in fields: f_info[f]=f
130 co_print_nodeinfo(f_info, "hostname", fields)
132 for node in nodelist:
135 if node not in cohash: continue
137 co_nodeinfo = cohash[node]
140 print >>sys.stderr, "Primary keys available in the comon object:"
141 for key in co_nodeinfo.keys():
149 daysdown_print_nodeinfo(co_nodeinfo, node)
151 fields = config.fields.split(",")
152 co_print_nodeinfo(co_nodeinfo, node, fields)
154 if __name__ == "__main__":