Improvements for older records. Consolidated code related to ending a
[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 from nodecommon import *
16
17 from config import config
18 from optparse import OptionParser
19
20 parser = OptionParser()
21 parser.set_defaults(node=None, endrecord=False)
22 parser.add_option("", "--node", dest="node", metavar="nodename.edu", 
23                                         help="A single node name to add to the nodegroup")
24 parser.add_option("", "--endrecord", dest="endrecord", action="store_true",
25                                         help="Force an end to the action record; to prompt Montior to start messaging again.")
26 parser.add_option("", "--bootcd", dest="bootcd", action="store_true",
27                                         help="A stock help message for fetching a new BootCD from the PLC GUI.")
28 config = config(parser)
29 config.parse_args()
30
31 def diff_time(timestamp):
32         now = time.time()
33         if timestamp == None:
34                 return "unknown"
35         diff = now - timestamp
36         # return the number of seconds as a difference from current time.
37         t_str = ""
38         if diff < 60: # sec in min.
39                 t = diff
40                 t_str = "%s sec ago" % t
41         elif diff < 60*60: # sec in hour
42                 t = diff // (60)
43                 t_str = "%s min ago" % int(t)
44         elif diff < 60*60*24: # sec in day
45                 t = diff // (60*60)
46                 t_str = "%s hours ago" % int(t)
47         elif diff < 60*60*24*7: # sec in week
48                 t = diff // (60*60*24)
49                 t_str = "%s days ago" % int(t)
50         elif diff < 60*60*24*30: # approx sec in month
51                 t = diff // (60*60*24*7)
52                 t_str = "%s weeks ago" % int(t)
53         elif diff > 60*60*24*30: # approx sec in month
54                 t = diff // (60*60*24*7*30)
55                 t_str = "%s months ago" % int(t)
56         return t_str
57
58 def plc_print_nodeinfo(plcnode):
59         url = "https://www.planet-lab.org/db/nodes/index.php?nodepattern="
60         plcnode['url'] = url + plcnode['hostname']
61
62         print "%(hostname)s %(url)s" % plcnode
63         print "   Checked: %s" % time.ctime()
64
65         print "\t boot_state |   created   |   updated   | last_contact | key"
66         print "\t       %5s | %11.11s | %11.11s | %12s | %s" % \
67                 (color_boot_state(plcnode['boot_state']), diff_time(plcnode['date_created']),
68                  diff_time(plcnode['last_updated']), 
69                  diff_time(plcnode['last_contact']), plcnode['key'])
70
71 def fb_print_nodeinfo(fbnode):
72         print "   Checked: ",
73         if 'checked' in fbnode:
74                 print "%11.11s " % diff_time(fbnode['checked'])
75         else:
76                 print "Unknown"
77         print "\t      state |  ssh  |  pcu  | bootcd | category | kernel"
78         if fbnode['bootcd']:
79                 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
80         else:
81                 fbnode['bootcd'] = "unknown"
82         if 'state' in fbnode:
83                 fbnode['state'] = color_boot_state(get_current_state(fbnode))
84         else:
85                 fbnode['state'] = "none"
86         fbnode['kernel'] = fbnode['kernel'].split()[2]
87         print "\t       %(state)5s | %(ssh)5.5s | %(pcu)5.5s | %(bootcd)6.6s | %(category)8.8s | %(kernel)s" % fbnode
88
89 def act_print_nodeinfo(actnode, header):
90         if header[0]:
91                 if 'date_created' in actnode:
92                         print "   Created: %11.11s" % diff_time(actnode['date_created'])
93                 print "   LastTime %11.11s" % diff_time(actnode['time'])
94                 print "\t      RT     | category | action          | msg"
95                 header[0] = False
96
97         if 'rt' in actnode and 'Status' in actnode['rt']:
98                 print "\t %5.5s %5.5s | %8.8s | %15.15s | %s" % \
99                         (actnode['rt']['Status'], actnode['rt']['id'][7:],
100                          actnode['category'], actnode['action'][0], 
101                          actnode['msg_format'][:-1])
102         else:
103                 if type(actnode['action']) == type([]):
104                         action = actnode['action'][0]
105                 else:
106                         action = actnode['action']
107                 if 'category' in actnode:
108                         category = actnode['category']
109                 else:
110                         category = "none"
111                         
112                 if 'msg_format' in actnode:
113                         print "\t       %5.5s | %8.8s | %15.15s | %s" % \
114                         (actnode['ticket_id'],
115                          category, action, 
116                          actnode['msg_format'][:-1])
117                 else:
118                         print "\t       %5.5s | %8.8s | %15.15s" % \
119                         (actnode['ticket_id'],
120                          category, action)
121
122 def pcu_print_info(pcuinfo, hostname):
123         print "   Checked: ",
124         if 'checked' in pcuinfo:
125                 print "%11.11s " % diff_time(pcuinfo['checked'])
126         else:
127                 print "Unknown"
128
129         print "\t            user   |          password | port | pcu_id | hostname "
130         print "\t %17s | %17s | %4s | %6s | %30s | %s" % \
131                 (pcuinfo['username'], pcuinfo['password'], 
132                  pcuinfo[hostname], pcuinfo['pcu_id'], reboot.pcu_name(pcuinfo), pcuinfo['model'])
133
134         if 'portstatus' in pcuinfo and pcuinfo['portstatus'] != {} and pcuinfo['portstatus'] != None:
135                 if pcuinfo['portstatus']['22'] == "open":
136                         print "\t ssh -o PasswordAuthentication=yes -o PubkeyAuthentication=no %s@%s" % (pcuinfo['username'], reboot.pcu_name(pcuinfo))
137                 if pcuinfo['portstatus']['23'] == "open":
138                         print "\t telnet %s" % (reboot.pcu_name(pcuinfo))
139                 if pcuinfo['portstatus']['80'] == "open" or \
140                         pcuinfo['portstatus']['443'] == "open":
141                         print "\t http://%s" % (reboot.pcu_name(pcuinfo))
142                 if pcuinfo['portstatus']['443'] == "open":
143                         print "\t racadm.py -r %s -u %s -p '%s'" % (pcuinfo['ip'], pcuinfo['username'], pcuinfo['password'])
144                         print "\t cmdhttps/locfg.pl -s %s -f iloxml/Reset_Server.xml -u %s -p '%s' | grep MESSAGE" % \
145                                 (reboot.pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
146
147 for node in config.args:
148         config.node = node
149
150         plc_nodeinfo = api.GetNodes({'hostname': config.node}, None)[0]
151         fb_nodeinfo  = fb['nodes'][config.node]['values']
152
153         plc_print_nodeinfo(plc_nodeinfo)
154         fb_print_nodeinfo(fb_nodeinfo)
155
156         if fb_nodeinfo['pcu'] == "PCU":
157                 pcu = reboot.get_pcu_values(fb_nodeinfo['plcnode']['pcu_ids'][0])
158                 pcu_print_info(pcu, config.node)
159
160         if config.node in act_all and len(act_all[config.node]) > 0:
161                 header = [True]
162
163                 if config.endrecord:
164                         node_end_record(config.node)
165                         #a = Action(config.node, act_all[config.node][0])
166                         #a.delField('rt')
167                         #a.delField('found_rt_ticket')
168                         #a.delField('second-mail-at-oneweek')
169                         #a.delField('second-mail-at-twoweeks')
170                         #a.delField('first-found')
171                         #rec = a.get()
172                         #rec['action'] = ["close_rt"]
173                         #rec['category'] = "UNKNOWN"
174                         #rec['stage'] = "monitor-end-record"
175                         #rec['time'] = time.time() - 7*60*60*24
176                         #act_all[config.node].insert(0,rec)
177                         #soltesz.dbDump("act_all", act_all)
178
179                 for act_nodeinfo in act_all[config.node]:
180                         act_print_nodeinfo(act_nodeinfo, header)
181         else: act_nodeinfo = None
182
183         print ""
184
185         if config.bootcd:
186                 print """
187 If you need a new bootcd, the steps are very simple:
188
189 Visit:
190  * https://www.planet-lab.org/db/nodes/index.php?nodepattern=%s
191  * Select Download -> Download ISO image for %s
192  * Save the ISO, and burn it to a writable CD-ROM.
193  * Replace the old CD and reboot the machine.
194
195 Please let me know if you have any additional questions.
196 """ % (config.node, config.node)
197