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