changes for 3.0
[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         try:
50                 fbnode['last_change'] = diff_time(pf.last_changed)
51         except:
52                 fbnode['last_change'] = diff_time(time.time())
53         print "   Checked: ",
54         if 'checked' in fbnode:
55                 print "%11.11s " % diff_time(fbnode['checked'])
56         else:
57                 print "Unknown"
58         print "\t      state |  ssh  |  pcu  | bootcd | category | last change | kernel"
59         if fbnode['bootcd']:
60                 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
61         else:
62                 fbnode['bootcd'] = "unknown"
63         if 'state' in fbnode:
64                 fbnode['state'] = color_boot_state(get_current_state(fbnode))
65         else:
66                 fbnode['state'] = "none"
67         if len(fbnode['kernel'].split()) > 2:
68                 fbnode['kernel'] = fbnode['kernel'].split()[2]
69         print "\t       %(state)5s | %(ssh)5.5s | %(pcu)5.5s | %(bootcd)6.6s | %(category)8.8s | %(last_change)11s | %(kernel)s" % fbnode
70
71 def act_print_nodeinfo(actnode, header):
72         if header[0]:
73                 if 'date_created' in actnode:
74                         print "   Created: %11.11s" % diff_time(actnode['date_created'])
75                 print "   LastTime %11.11s" % diff_time(actnode['time'])
76                 print "\t      RT     | category | action          | msg"
77                 header[0] = False
78
79         if 'rt' in actnode and 'Status' in actnode['rt']:
80                 print "\t %5.5s %5.5s | %8.8s | %15.15s | %s" % \
81                         (actnode['rt']['Status'], actnode['rt']['id'][7:],
82                          actnode['category'], actnode['action'][0], actnode['info'][1:])
83         else:
84                 if type(actnode['action']) == type([]):
85                         action = actnode['action'][0]
86                 else:
87                         action = actnode['action']
88                 if 'category' in actnode:
89                         category = actnode['category']
90                 else:
91                         category = "none"
92                         
93                 if 'msg_format' in actnode:
94                         print "\t       %5.5s | %8.8s | %15.15s | %s" % \
95                         (actnode['ticket_id'],
96                          category, action, 
97                          actnode['msg_format'][:-1])
98                 else:
99                         print "\t       %5.5s | %8.8s | %15.15s" % \
100                         (actnode['ticket_id'],
101                          category, action)
102
103 def pcu_print_info(pcuinfo, hostname):
104         print "   Checked: ",
105         if 'checked' in pcuinfo:
106                 print "%11.11s " % diff_time(pcuinfo['checked'])
107         else:
108                 print "Unknown"
109
110         print "\t            user   |          password | port | pcu_id | hostname "
111         print "\t %17s | %17s | %4s | %6s | %30s | %s" % \
112                 (pcuinfo['username'], pcuinfo['password'], 
113                  pcuinfo[hostname], pcuinfo['pcu_id'], reboot.pcu_name(pcuinfo), pcuinfo['model'])
114
115         if 'portstatus' in pcuinfo and pcuinfo['portstatus'] != {} and pcuinfo['portstatus'] != None:
116                 if pcuinfo['portstatus']['22'] == "open":
117                         print "\t ssh -o PasswordAuthentication=yes -o PubkeyAuthentication=no %s@%s" % (pcuinfo['username'], reboot.pcu_name(pcuinfo))
118                 if pcuinfo['portstatus']['23'] == "open":
119                         print "\t telnet %s" % (reboot.pcu_name(pcuinfo))
120                 if pcuinfo['portstatus']['80'] == "open" or \
121                         pcuinfo['portstatus']['443'] == "open":
122                         print "\t https://%s" % (reboot.pcu_name(pcuinfo))
123                         print "\t import %s.png" % (reboot.pcu_name(pcuinfo))
124                         print """\t mutt -s "crash for %s" -a %s.png sapanb@cs.princeton.edu < /dev/null""" % (hostname, reboot.pcu_name(pcuinfo))
125                 if pcuinfo['portstatus']['443'] == "open":
126                         print "\t racadm.py -r %s -u %s -p '%s'" % (pcuinfo['ip'], pcuinfo['username'], pcuinfo['password'])
127                         print "\t cmdhttps/locfg.pl -s %s -f iloxml/Reset_Server.xml -u %s -p '%s' | grep MESSAGE" % \
128                                 (reboot.pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
129                         print "\t cmdhttps/locfg.pl -s %s -f iloxml/License.xml -u %s -p '%s' | grep MESSAGE" % \
130                                 (reboot.pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
131                 if pcuinfo['portstatus']['16992'] == "open":
132                         print "\t ./cmdamt/remoteControl -A -verbose 'http://%s:16992/RemoteControlService' -user admin -pass '%s'" % (reboot.pcu_name(pcuinfo), pcuinfo['password'])
133
134 if config.findbad:
135         # rerun findbad with the nodes in the given nodes.
136         import os
137         file = "findbad.txt"
138         util.file.setFileFromList(file, config.args)
139         os.system("./findbad.py --cachenodes --debug=0 --dbname=findbad --increment --nodelist %s" % file)
140
141 for node in config.args:
142         config.node = node
143
144         fb = database.dbLoad("findbad")
145         plc_nodeinfo = api.GetNodes({'hostname': config.node}, None)[0]
146         fb_nodeinfo  = fb['nodes'][config.node]['values']
147
148         plc_print_nodeinfo(plc_nodeinfo)
149         fb_nodeinfo['hostname'] = node
150         fb_print_nodeinfo(fb_nodeinfo)
151
152         if fb_nodeinfo['pcu'] == "PCU":
153                 pcu = reboot.get_pcu_values(fb_nodeinfo['plcnode']['pcu_ids'][0])
154                 if pcu: pcu_print_info(pcu, config.node)
155
156         try:
157                 act_all = database.dbLoad("act_all")
158         except:
159                 act_all = {}
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                         #database.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