add simple command line tools for manipulating node groups, and for querying
[monitor.git] / nodeinfo.py
1 #!/usr/bin/python
2
3 import plc
4 import auth
5 api = plc.PLC(auth.auth, auth.plc)
6
7 import soltesz
8 fb = soltesz.dbLoad("findbad")
9 act_all = soltesz.dbLoad("act_all")
10
11 import reboot
12
13 import time
14
15 from config import config
16 from optparse import OptionParser
17
18 parser = OptionParser()
19 parser.set_defaults(node=None)
20 parser.add_option("", "--node", dest="node", metavar="nodename.edu", 
21                                         help="A single node name to add to the nodegroup")
22 config = config(parser)
23 config.parse_args()
24
25 def diff_time(timestamp):
26         now = time.time()
27         diff = now - timestamp
28         # return the number of seconds as a difference from current time.
29         t_str = ""
30         if diff < 60: # sec in min.
31                 t = diff
32                 t_str = "%s sec ago" % t
33         elif diff < 60*60: # sec in hour
34                 t = diff // (60)
35                 t_str = "%s min ago" % int(t)
36         elif diff < 60*60*24: # sec in day
37                 t = diff // (60*60)
38                 t_str = "%s hours ago" % int(t)
39         elif diff < 60*60*24*7: # sec in week
40                 t = diff // (60*60*24)
41                 t_str = "%s days ago" % int(t)
42         elif diff < 60*60*24*30: # approx sec in month
43                 t = diff // (60*60*24*7)
44                 t_str = "%s weeks ago" % int(t)
45         elif diff > 60*60*24*30: # approx sec in month
46                 t = diff // (60*60*24*7*30)
47                 t_str = "%s months ago" % int(t)
48         return t_str
49
50 def plc_print_nodeinfo(plcnode):
51         url = "https://www.planet-lab.org/db/nodes/index.php?nodepattern="
52         plcnode['url'] = url + plcnode['hostname']
53
54         print "%(hostname)s %(url)s" % plcnode
55         print "   Checked: %s" % time.ctime()
56
57         print "\t boot_state |   created   |   updated   | last_contact"
58         print "\t %10.10s | %11.11s | %11.11s | %12s" % \
59                 (plcnode['boot_state'], diff_time(plcnode['date_created']),
60                  diff_time(plcnode['last_updated']), 
61                  diff_time(plcnode['last_contact']))
62
63 def fb_print_nodeinfo(fbnode):
64         print "   Checked: ",
65         if 'checked' in fbnode:
66                 print "%11.11s " % diff_time(fbnode['checked'])
67         else:
68                 print "Unknown"
69         print "\t      state |  ssh  |  pcu  | bootcd | category | kernel"
70         if fbnode['bootcd']:
71                 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
72         else:
73                 fbnode['bootcd'] = "unknown"
74         fbnode['kernel'] = fbnode['kernel'].split()[2]
75         print "\t %(state)10.10s | %(ssh)5.5s | %(pcu)5.5s | %(bootcd)6.6s | %(category)8.8s | %(kernel)s" % fbnode
76
77 def act_print_nodeinfo(actnode, header):
78         if header[0]:
79                 print "   Created: %11.11s" % diff_time(actnode['date_created'])
80                 print "   LastTime %11.11s" % diff_time(actnode['time'])
81                 print "\t      RT     | category | action          | msg"
82                 header[0] = False
83
84         if 'rt' in actnode:
85                 print "\t %5.5s %5.5s | %8.8s | %15.15s | %s" % \
86                         (actnode['rt']['Status'], actnode['rt']['id'][7:],
87                          actnode['category'], actnode['action'][0], 
88                          actnode['msg_format'][:-1])
89         else:
90                 print "\t       %5.5s | %8.8s | %15.15s | %s" % \
91                         (actnode['ticket_id'],
92                          actnode['category'], actnode['action'][0], 
93                          actnode['msg_format'][:-1])
94
95 def pcu_print_info(pcuinfo):
96         print "   Checked: ",
97         if 'checked' in pcuinfo:
98                 print "%11.11s " % diff_time(pcuinfo['checked'])
99         else:
100                 print "Unknown"
101
102         print "\t            user   |          password |   hostname "
103         print "\t %17s | %17s | %30s | %s" % \
104                 (pcuinfo['username'], pcuinfo['password'], 
105                  reboot.pcu_name(pcuinfo), pcuinfo['model'])
106
107         if pcuinfo['portstatus']['22'] == "open":
108                 print "\t ssh -o PasswordAuthentication=yes -o PubkeyAuthentication=no %s@%s" % (pcuinfo['username'], reboot.pcu_name(pcuinfo))
109         if pcuinfo['portstatus']['23'] == "open":
110                 print "\t telnet %s" % (reboot.pcu_name(pcuinfo))
111         if pcuinfo['portstatus']['80'] == "open" or \
112                 pcuinfo['portstatus']['443'] == "open":
113                 print "\t http://%s" % (reboot.pcu_name(pcuinfo))
114
115 if config.node:
116         plc_nodeinfo = api.GetNodes({'hostname': config.node}, None)[0]
117         fb_nodeinfo  = fb['nodes'][config.node]['values']
118
119         plc_print_nodeinfo(plc_nodeinfo)
120         fb_print_nodeinfo(fb_nodeinfo)
121
122         if fb_nodeinfo['pcu'] == "PCU":
123                 pcu = reboot.get_pcu_values(fb_nodeinfo['plcnode']['pcu_ids'][0])
124                 pcu_print_info(pcu)
125
126         if config.node in act_all and len(act_all[config.node]) > 0:
127                 header = [True]
128                 for act_nodeinfo in act_all[config.node]:
129                         act_print_nodeinfo(act_nodeinfo, header)
130         else: act_nodeinfo = None
131