www/printbadnodes.py
[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], 
80                          actnode['msg_format'][:-1])
81         else:
82                 if type(actnode['action']) == type([]):
83                         action = actnode['action'][0]
84                 else:
85                         action = actnode['action']
86                 if 'category' in actnode:
87                         category = actnode['category']
88                 else:
89                         category = "none"
90                         
91                 if 'msg_format' in actnode:
92                         print "\t       %5.5s | %8.8s | %15.15s | %s" % \
93                         (actnode['ticket_id'],
94                          category, action, 
95                          actnode['msg_format'][:-1])
96                 else:
97                         print "\t       %5.5s | %8.8s | %15.15s" % \
98                         (actnode['ticket_id'],
99                          category, action)
100
101 def pcu_print_info(pcuinfo, hostname):
102         print "   Checked: ",
103         if 'checked' in pcuinfo:
104                 print "%11.11s " % diff_time(pcuinfo['checked'])
105         else:
106                 print "Unknown"
107
108         print "\t            user   |          password | port | pcu_id | hostname "
109         print "\t %17s | %17s | %4s | %6s | %30s | %s" % \
110                 (pcuinfo['username'], pcuinfo['password'], 
111                  pcuinfo[hostname], pcuinfo['pcu_id'], reboot.pcu_name(pcuinfo), pcuinfo['model'])
112
113         if 'portstatus' in pcuinfo and pcuinfo['portstatus'] != {} and pcuinfo['portstatus'] != None:
114                 if pcuinfo['portstatus']['22'] == "open":
115                         print "\t ssh -o PasswordAuthentication=yes -o PubkeyAuthentication=no %s@%s" % (pcuinfo['username'], reboot.pcu_name(pcuinfo))
116                 if pcuinfo['portstatus']['23'] == "open":
117                         print "\t telnet %s" % (reboot.pcu_name(pcuinfo))
118                 if pcuinfo['portstatus']['80'] == "open" or \
119                         pcuinfo['portstatus']['443'] == "open":
120                         print "\t https://%s" % (reboot.pcu_name(pcuinfo))
121                         print "\t import %s.png" % (reboot.pcu_name(pcuinfo))
122                         print """\t mutt -s "crash for %s" -a %s.png sapanb@cs.princeton.edu < /dev/null""" % (hostname, reboot.pcu_name(pcuinfo))
123                 if pcuinfo['portstatus']['443'] == "open":
124                         print "\t racadm.py -r %s -u %s -p '%s'" % (pcuinfo['ip'], pcuinfo['username'], pcuinfo['password'])
125                         print "\t cmdhttps/locfg.pl -s %s -f iloxml/Reset_Server.xml -u %s -p '%s' | grep MESSAGE" % \
126                                 (reboot.pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
127                         print "\t cmdhttps/locfg.pl -s %s -f iloxml/License.xml -u %s -p '%s' | grep MESSAGE" % \
128                                 (reboot.pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
129                 if pcuinfo['portstatus']['16992'] == "open":
130                         print "\t ./cmdamt/remoteControl -A -verbose 'http://%s:16992/RemoteControlService' -user admin -pass '%s'" % (reboot.pcu_name(pcuinfo), pcuinfo['password'])
131
132 if config.findbad:
133         # rerun findbad with the nodes in the given nodes.
134         import os
135         file = "findbad.txt"
136         util.file.setFileFromList(file, config.args)
137         os.system("./findbad.py --cachenodes --debug=0 --dbname=findbad --increment --nodelist %s" % file)
138
139 for node in config.args:
140         config.node = node
141
142         fb = database.dbLoad("findbad")
143         plc_nodeinfo = api.GetNodes({'hostname': config.node}, None)[0]
144         fb_nodeinfo  = fb['nodes'][config.node]['values']
145
146         plc_print_nodeinfo(plc_nodeinfo)
147         fb_nodeinfo['hostname'] = node
148         fb_print_nodeinfo(fb_nodeinfo)
149
150         if fb_nodeinfo['pcu'] == "PCU":
151                 pcu = reboot.get_pcu_values(fb_nodeinfo['plcnode']['pcu_ids'][0])
152                 if pcu: pcu_print_info(pcu, config.node)
153
154         try:
155                 act_all = database.dbLoad("act_all")
156         except:
157                 act_all = {}
158         if config.node in act_all and len(act_all[config.node]) > 0:
159                 header = [True]
160
161                 if config.endrecord:
162                         node_end_record(config.node)
163                         #a = Action(config.node, act_all[config.node][0])
164                         #a.delField('rt')
165                         #a.delField('found_rt_ticket')
166                         #a.delField('second-mail-at-oneweek')
167                         #a.delField('second-mail-at-twoweeks')
168                         #a.delField('first-found')
169                         #rec = a.get()
170                         #rec['action'] = ["close_rt"]
171                         #rec['category'] = "UNKNOWN"
172                         #rec['stage'] = "monitor-end-record"
173                         #rec['time'] = time.time() - 7*60*60*24
174                         #act_all[config.node].insert(0,rec)
175                         #database.dbDump("act_all", act_all)
176
177                 for act_nodeinfo in act_all[config.node]:
178                         act_print_nodeinfo(act_nodeinfo, header)
179         else: act_nodeinfo = None
180
181         print ""
182
183         if config.bootcd:
184                 print """
185 If you need a new bootcd, the steps are very simple:
186
187 Visit:
188  * https://www.planet-lab.org/db/nodes/index.php?nodepattern=%s
189  * Select Download -> Download ISO image for %s
190  * Save the ISO, and burn it to a writable CD-ROM.
191  * Replace the old CD and reboot the machine.
192
193 Please let me know if you have any additional questions.
194 """ % (config.node, config.node)
195