Add an option to end a monitor record for a node. This results in the
[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 from model import *
15
16 from config import config
17 from optparse import OptionParser
18
19 parser = OptionParser()
20 parser.set_defaults(node=None, endrecord=False)
21 parser.add_option("", "--node", dest="node", metavar="nodename.edu", 
22                                         help="A single node name to add to the nodegroup")
23 parser.add_option("", "--endrecord", dest="endrecord", action="store_true",
24                                         help="Force an end to the action record; to prompt Montior to start messaging again.")
25 config = config(parser)
26 config.parse_args()
27
28 def diff_time(timestamp):
29         now = time.time()
30         diff = now - timestamp
31         # return the number of seconds as a difference from current time.
32         t_str = ""
33         if diff < 60: # sec in min.
34                 t = diff
35                 t_str = "%s sec ago" % t
36         elif diff < 60*60: # sec in hour
37                 t = diff // (60)
38                 t_str = "%s min ago" % int(t)
39         elif diff < 60*60*24: # sec in day
40                 t = diff // (60*60)
41                 t_str = "%s hours ago" % int(t)
42         elif diff < 60*60*24*7: # sec in week
43                 t = diff // (60*60*24)
44                 t_str = "%s days ago" % int(t)
45         elif diff < 60*60*24*30: # approx sec in month
46                 t = diff // (60*60*24*7)
47                 t_str = "%s weeks ago" % int(t)
48         elif diff > 60*60*24*30: # approx sec in month
49                 t = diff // (60*60*24*7*30)
50                 t_str = "%s months ago" % int(t)
51         return t_str
52
53 def plc_print_nodeinfo(plcnode):
54         url = "https://www.planet-lab.org/db/nodes/index.php?nodepattern="
55         plcnode['url'] = url + plcnode['hostname']
56
57         print "%(hostname)s %(url)s" % plcnode
58         print "   Checked: %s" % time.ctime()
59
60         print "\t boot_state |   created   |   updated   | last_contact"
61         print "\t %10.10s | %11.11s | %11.11s | %12s" % \
62                 (plcnode['boot_state'], diff_time(plcnode['date_created']),
63                  diff_time(plcnode['last_updated']), 
64                  diff_time(plcnode['last_contact']))
65
66 def fb_print_nodeinfo(fbnode):
67         print "   Checked: ",
68         if 'checked' in fbnode:
69                 print "%11.11s " % diff_time(fbnode['checked'])
70         else:
71                 print "Unknown"
72         print "\t      state |  ssh  |  pcu  | bootcd | category | kernel"
73         if fbnode['bootcd']:
74                 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
75         else:
76                 fbnode['bootcd'] = "unknown"
77         fbnode['kernel'] = fbnode['kernel'].split()[2]
78         print "\t %(state)10.10s | %(ssh)5.5s | %(pcu)5.5s | %(bootcd)6.6s | %(category)8.8s | %(kernel)s" % fbnode
79
80 def act_print_nodeinfo(actnode, header):
81         if header[0]:
82                 print "   Created: %11.11s" % diff_time(actnode['date_created'])
83                 print "   LastTime %11.11s" % diff_time(actnode['time'])
84                 print "\t      RT     | category | action          | msg"
85                 header[0] = False
86
87         if 'rt' in actnode:
88                 print "\t %5.5s %5.5s | %8.8s | %15.15s | %s" % \
89                         (actnode['rt']['Status'], actnode['rt']['id'][7:],
90                          actnode['category'], actnode['action'][0], 
91                          actnode['msg_format'][:-1])
92         else:
93                 if type(actnode['action']) == type([]):
94                         action = actnode['action'][0]
95                 else:
96                         action = actnode['action']
97                 if 'category' in actnode:
98                         category = actnode['category']
99                 else:
100                         category = "none"
101                         
102                 print "\t       %5.5s | %8.8s | %15.15s | %s" % \
103                         (actnode['ticket_id'],
104                          category, action, 
105                          actnode['msg_format'][:-1])
106
107 def pcu_print_info(pcuinfo, hostname):
108         print "   Checked: ",
109         if 'checked' in pcuinfo:
110                 print "%11.11s " % diff_time(pcuinfo['checked'])
111         else:
112                 print "Unknown"
113
114         print "\t            user   |          password | port | hostname "
115         print "\t %17s | %17s | %4s | %30s | %s" % \
116                 (pcuinfo['username'], pcuinfo['password'], 
117                  pcuinfo[hostname], reboot.pcu_name(pcuinfo), pcuinfo['model'])
118
119         if pcuinfo['portstatus']['22'] == "open":
120                 print "\t ssh -o PasswordAuthentication=yes -o PubkeyAuthentication=no %s@%s" % (pcuinfo['username'], reboot.pcu_name(pcuinfo))
121         if pcuinfo['portstatus']['23'] == "open":
122                 print "\t telnet %s" % (reboot.pcu_name(pcuinfo))
123         if pcuinfo['portstatus']['80'] == "open" or \
124                 pcuinfo['portstatus']['443'] == "open":
125                 print "\t http://%s" % (reboot.pcu_name(pcuinfo))
126         if pcuinfo['portstatus']['443'] == "open":
127                 print "\t racadm.py -r %s -u %s -p '%s'" % (pcuinfo['ip'], pcuinfo['username'], pcuinfo['password'])
128                 print "\t cmdhttps/locfg.pl -s %s -f iloxml/Reset_Server.xml -u %s -p %s | grep MESSAGE" % \
129                         (reboot.pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
130
131 for node in config.args:
132         config.node = node
133
134         plc_nodeinfo = api.GetNodes({'hostname': config.node}, None)[0]
135         fb_nodeinfo  = fb['nodes'][config.node]['values']
136
137         plc_print_nodeinfo(plc_nodeinfo)
138         fb_print_nodeinfo(fb_nodeinfo)
139
140         if fb_nodeinfo['pcu'] == "PCU":
141                 pcu = reboot.get_pcu_values(fb_nodeinfo['plcnode']['pcu_ids'][0])
142                 pcu_print_info(pcu, config.node)
143
144         if config.node in act_all and len(act_all[config.node]) > 0:
145                 header = [True]
146
147                 if config.endrecord:
148                         a = Action(config.node, act_all[config.node][0])
149                         a.delField('rt')
150                         a.delField('second-mail-at-oneweek')
151                         a.delField('second-mail-at-twoweeks')
152                         a.delField('first-found')
153                         rec = a.get()
154                         rec['action'] = ["close_rt"]
155                         rec['category'] = "UNKNOWN"
156                         rec['stage'] = "monitor-end-record"
157                         rec['time'] = time.time() - 7*60*60*24
158                         act_all[config.node].insert(0,rec)
159
160                 for act_nodeinfo in act_all[config.node]:
161                         act_print_nodeinfo(act_nodeinfo, header)
162         else: act_nodeinfo = None
163