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