update of all changes in the last week that fine-tuned the behavior of Monitor
[monitor.git] / nodeinfo.py
1 #!/usr/bin/python
2
3 import plc
4 api = plc.getAuthAPI()
5
6 from monitor import *
7 #import database
8 import reboot
9
10 import time
11 from model import *
12 from nodecommon import *
13 from unified_model import node_end_record, PersistFlags
14
15 import util.file
16
17 import parser as parsermodule
18
19 parser = parsermodule.getParser()
20 parser.set_defaults(node=None, 
21                                         findbad=False,
22                                         endrecord=False)
23 parser.add_option("", "--node", dest="node", metavar="nodename.edu", 
24                                         help="A single node name to add to the nodegroup")
25 parser.add_option("", "--endrecord", dest="endrecord", action="store_true",
26                                         help="Force an end to the action record; to prompt Montior to start messaging again.")
27 parser.add_option("", "--findbad", dest="findbad", action="store_true", 
28                                         help="Re-run findbad on the nodes we're going to check before acting.")
29 parser.add_option("", "--bootcd", dest="bootcd", action="store_true",
30                                         help="A stock help message for fetching a new BootCD from the PLC GUI.")
31 config = parsermodule.parse_args(parser)
32
33
34 def plc_print_nodeinfo(plcnode):
35         url = "https://www.planet-lab.org/db/nodes/index.php?nodepattern="
36         plcnode['url'] = url + plcnode['hostname']
37
38         print "%(hostname)s %(url)s" % plcnode
39         print "   Checked: %s" % time.ctime()
40
41         print "\t boot_state |   created   |   updated   | last_contact | key"
42         print "\t       %5s | %11.11s | %11.11s | %12s | %s" % \
43                 (color_boot_state(plcnode['boot_state']), diff_time(plcnode['date_created']),
44                  diff_time(plcnode['last_updated']), 
45                  diff_time(plcnode['last_contact']), plcnode['key'])
46
47 def fb_print_nodeinfo(fbnode):
48         pf = PersistFlags(fbnode['hostname'], 1, db='node_persistflags')
49         fbnode['last_change'] = diff_time(pf.last_changed)
50         print "   Checked: ",
51         if 'checked' in fbnode:
52                 print "%11.11s " % diff_time(fbnode['checked'])
53         else:
54                 print "Unknown"
55         print "\t      state |  ssh  |  pcu  | bootcd | category | last change | kernel"
56         if fbnode['bootcd']:
57                 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
58         else:
59                 fbnode['bootcd'] = "unknown"
60         if 'state' in fbnode:
61                 fbnode['state'] = color_boot_state(get_current_state(fbnode))
62         else:
63                 fbnode['state'] = "none"
64         if len(fbnode['kernel'].split()) > 2:
65                 fbnode['kernel'] = fbnode['kernel'].split()[2]
66         print "\t       %(state)5s | %(ssh)5.5s | %(pcu)5.5s | %(bootcd)6.6s | %(category)8.8s | %(last_change)11s | %(kernel)s" % fbnode
67
68 def act_print_nodeinfo(actnode, header):
69         if header[0]:
70                 if 'date_created' in actnode:
71                         print "   Created: %11.11s" % diff_time(actnode['date_created'])
72                 print "   LastTime %11.11s" % diff_time(actnode['time'])
73                 print "\t      RT     | category | action          | msg"
74                 header[0] = False
75
76         if 'rt' in actnode and 'Status' in actnode['rt']:
77                 print "\t %5.5s %5.5s | %8.8s | %15.15s | %s" % \
78                         (actnode['rt']['Status'], actnode['rt']['id'][7:],
79                          actnode['category'], actnode['action'][0], actnode['info'][1:])
80         else:
81                 if type(actnode['action']) == type([]):
82                         action = actnode['action'][0]
83                 else:
84                         action = actnode['action']
85                 if 'category' in actnode:
86                         category = actnode['category']
87                 else:
88                         category = "none"
89                         
90                 if 'msg_format' in actnode:
91                         print "\t       %5.5s | %8.8s | %15.15s | %s" % \
92                         (actnode['ticket_id'],
93                          category, action, 
94                          actnode['msg_format'][:-1])
95                 else:
96                         print "\t       %5.5s | %8.8s | %15.15s" % \
97                         (actnode['ticket_id'],
98                          category, action)
99
100 def pcu_print_info(pcuinfo, hostname):
101         print "   Checked: ",
102         if 'checked' in pcuinfo:
103                 print "%11.11s " % diff_time(pcuinfo['checked'])
104         else:
105                 print "Unknown"
106
107         print "\t            user   |          password | port | pcu_id | hostname "
108         print "\t %17s | %17s | %4s | %6s | %30s | %s" % \
109                 (pcuinfo['username'], pcuinfo['password'], 
110                  pcuinfo[hostname], pcuinfo['pcu_id'], reboot.pcu_name(pcuinfo), pcuinfo['model'])
111
112         if 'portstatus' in pcuinfo and pcuinfo['portstatus'] != {} and pcuinfo['portstatus'] != None:
113                 if pcuinfo['portstatus']['22'] == "open":
114                         print "\t ssh -o PasswordAuthentication=yes -o PubkeyAuthentication=no %s@%s" % (pcuinfo['username'], reboot.pcu_name(pcuinfo))
115                 if pcuinfo['portstatus']['23'] == "open":
116                         print "\t telnet %s" % (reboot.pcu_name(pcuinfo))
117                 if pcuinfo['portstatus']['80'] == "open" or \
118                         pcuinfo['portstatus']['443'] == "open":
119                         print "\t https://%s" % (reboot.pcu_name(pcuinfo))
120                         print "\t import %s.png" % (reboot.pcu_name(pcuinfo))
121                         print """\t mutt -s "crash for %s" -a %s.png sapanb@cs.princeton.edu < /dev/null""" % (hostname, reboot.pcu_name(pcuinfo))
122                 if pcuinfo['portstatus']['443'] == "open":
123                         print "\t racadm.py -r %s -u %s -p '%s'" % (pcuinfo['ip'], pcuinfo['username'], pcuinfo['password'])
124                         print "\t cmdhttps/locfg.pl -s %s -f iloxml/Reset_Server.xml -u %s -p '%s' | grep MESSAGE" % \
125                                 (reboot.pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
126                         print "\t cmdhttps/locfg.pl -s %s -f iloxml/License.xml -u %s -p '%s' | grep MESSAGE" % \
127                                 (reboot.pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
128                 if pcuinfo['portstatus']['16992'] == "open":
129                         print "\t ./cmdamt/remoteControl -A -verbose 'http://%s:16992/RemoteControlService' -user admin -pass '%s'" % (reboot.pcu_name(pcuinfo), pcuinfo['password'])
130
131 if config.findbad:
132         # rerun findbad with the nodes in the given nodes.
133         import os
134         file = "findbad.txt"
135         util.file.setFileFromList(file, config.args)
136         os.system("./findbad.py --cachenodes --debug=0 --dbname=findbad --increment --nodelist %s" % file)
137
138 for node in config.args:
139         config.node = node
140
141         fb = database.dbLoad("findbad")
142         plc_nodeinfo = api.GetNodes({'hostname': config.node}, None)[0]
143         fb_nodeinfo  = fb['nodes'][config.node]['values']
144
145         plc_print_nodeinfo(plc_nodeinfo)
146         fb_nodeinfo['hostname'] = node
147         fb_print_nodeinfo(fb_nodeinfo)
148
149         if fb_nodeinfo['pcu'] == "PCU":
150                 pcu = reboot.get_pcu_values(fb_nodeinfo['plcnode']['pcu_ids'][0])
151                 if pcu: pcu_print_info(pcu, config.node)
152
153         try:
154                 act_all = database.dbLoad("act_all")
155         except:
156                 act_all = {}
157         if config.node in act_all and len(act_all[config.node]) > 0:
158                 header = [True]
159
160                 if config.endrecord:
161                         node_end_record(config.node)
162                         #a = Action(config.node, act_all[config.node][0])
163                         #a.delField('rt')
164                         #a.delField('found_rt_ticket')
165                         #a.delField('second-mail-at-oneweek')
166                         #a.delField('second-mail-at-twoweeks')
167                         #a.delField('first-found')
168                         #rec = a.get()
169                         #rec['action'] = ["close_rt"]
170                         #rec['category'] = "UNKNOWN"
171                         #rec['stage'] = "monitor-end-record"
172                         #rec['time'] = time.time() - 7*60*60*24
173                         #act_all[config.node].insert(0,rec)
174                         #database.dbDump("act_all", act_all)
175
176                 for act_nodeinfo in act_all[config.node]:
177                         act_print_nodeinfo(act_nodeinfo, header)
178         else: act_nodeinfo = None
179
180         print ""
181
182         if config.bootcd:
183                 print """
184 If you need a new bootcd, the steps are very simple:
185
186 Visit:
187  * https://www.planet-lab.org/db/nodes/index.php?nodepattern=%s
188  * Select Download -> Download ISO image for %s
189  * Save the ISO, and burn it to a writable CD-ROM.
190  * Replace the old CD and reboot the machine.
191
192 Please let me know if you have any additional questions.
193 """ % (config.node, config.node)
194