use https by default
[monitor.git] / comonquery.py
1 #!/usr/bin/python
2
3
4 import sys
5 from monitor import database
6 from monitor.common import *
7 from monitor.model import Record
8 import glob
9 import os
10 import traceback
11
12 import time
13 import re
14 import string
15
16 from monitor.wrapper import plc
17 api = plc.getAuthAPI()
18
19 from monitor.util import file
20 from monitor import config
21
22 from monitor.sources import comon
23
24 default_fields="name,resptime,sshstatus,date,uptime,lastcotop,cpuspeed,memsize,disksize"
25
26 class NoKeyException(Exception): pass
27
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)
32
33         print "%(intdaysdown)5s %(hostname)-44s | %(state)10.10s | %(daysdown)s" % co_nodeinfo
34
35 def co_print_nodeinfo(co_nodeinfo, hostname, fields=None):
36         
37         # co_nodeinfo['bootstate'] : unknown pattern
38         co_nodeinfo['name'] = hostname
39
40         if 'uptime' in co_nodeinfo and co_nodeinfo['uptime'] != "null":
41                 co_nodeinfo['uptime'] = diff_time(time.time()-float(co_nodeinfo['uptime']))
42
43         if 'date' in co_nodeinfo and co_nodeinfo['date'] != "null":
44                 co_nodeinfo['date'] = diff_time(float(co_nodeinfo['date']))
45
46         if fields == default_fields.split(','):
47
48                 print "%(name)-40s %(sshstatus)5.5s %(resptime)6.6s %(lastcotop)6.6s %(uptime)s" % co_nodeinfo
49         else:
50                 format = ""
51                 for f in fields:
52                         format += "%%(%s)s " % f
53                 print format % co_nodeinfo
54
55 def main():
56
57         from monitor import parser as parsermodule
58         parser = parsermodule.getParser()
59
60         parser.set_defaults(node=None, 
61                                 select=None, 
62                                 list=None, 
63                                 dns=False,
64                                 listkeys=False,
65                                 pcuselect=None, 
66                                 nodelist=None, 
67                                 daysdown=None, 
68                                 fields=default_fields)
69         parser.add_option("", "--daysdown", dest="daysdown", action="store_true",
70                                                 help="List the node state and days down...")
71
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
83         parser.add_option("", "--dns", dest="dns", action="store_true",
84                                                 help="A convenience query for dns values")
85
86         parser = parsermodule.getParser(['defaults'], parser)
87         config = parsermodule.parse_args(parser)
88         
89         #if config.fromtime:
90         #       fb = None
91         #else:
92         #       fb = None
93
94         # lastcotop measures whether cotop is actually running.  this is a better
95         # metric than sshstatus, or other values from CoMon
96
97         COMON_COTOPURL= "http://summer.cs.princeton.edu/status/tabulator.cgi?" + \
98                                         "table=table_nodeview&formatcsv"
99         if config.dns:
100                 config.fields = "name,dns1udp,dns1tcp,dns2udp,dns2tcp"
101                 config.select = "dns1udp>0||dns1tcp>0||dns2udp>0||dns2tcp>0"
102
103         if config.fields == "all":
104                 cotop_url = COMON_COTOPURL
105         else:
106                 cotop_url = COMON_COTOPURL + "&dumpcols='%s'" % config.fields
107
108         if config.select:
109                 cotop_url = cotop_url + "&select='%s'" % config.select
110
111         if config.listkeys:
112                 cotop_url = COMON_COTOPURL + "&limit=1"
113
114         cotop = comon.Comon()
115         cohash = cotop.coget(cotop_url)
116
117         if config.nodelist:
118                 nodelist = file.getListFromFile(config.nodelist)
119         else:
120                 # NOTE: list of nodes should come from comon query.   
121                 nodelist = cohash.keys()
122
123         print "%(name)-40s %(sshstatus)5.5s %(resptime)6.6s %(lastcotop)6.6s %(uptime)s" % {
124                                         'name' : 'hostname', 
125                                         'sshstatus' : 'sshstatus', 
126                                         'resptime' : 'resptime', 
127                                         'lastcotop' : 'lastcotop', 
128                                         'uptime' : 'uptime'}
129         for node in nodelist:
130                 config.node = node
131
132                 if node not in cohash: continue
133
134                 co_nodeinfo = cohash[node]
135
136                 if config.listkeys:
137                         print "Primary keys available in the comon object:"
138                         for key in co_nodeinfo.keys():
139                                 print "\t",key
140                         sys.exit(0)
141                         
142                 if config.list:
143                         print node
144                 else:
145                         if config.daysdown:
146                                 daysdown_print_nodeinfo(co_nodeinfo, node)
147                         else:
148                                 fields = config.fields.split(",")
149                                 co_print_nodeinfo(co_nodeinfo, node, fields)
150                 
151 if __name__ == "__main__":
152         main()