Changed 'import auth' statements to use plc.py or monitorconfig.py
[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 config as configmodule
14
15 from config import config as cfg
16 from optparse import OptionParser
17
18 parser = OptionParser()
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 = cfg(parser)
31 config.parse_args()
32
33 def diff_time(timestamp):
34         now = time.time()
35         if timestamp == None:
36                 return "unknown"
37         diff = now - timestamp
38         # return the number of seconds as a difference from current time.
39         t_str = ""
40         if diff < 60: # sec in min.
41                 t = diff
42                 t_str = "%s sec ago" % t
43         elif diff < 60*60: # sec in hour
44                 t = diff // (60)
45                 t_str = "%s min ago" % int(t)
46         elif diff < 60*60*24: # sec in day
47                 t = diff // (60*60)
48                 t_str = "%s hours ago" % int(t)
49         elif diff < 60*60*24*7: # sec in week
50                 t = diff // (60*60*24)
51                 t_str = "%s days ago" % int(t)
52         elif diff < 60*60*24*30: # approx sec in month
53                 t = diff // (60*60*24*7)
54                 t_str = "%s weeks ago" % int(t)
55         elif diff > 60*60*24*30: # approx sec in month
56                 t = diff // (60*60*24*7*30)
57                 t_str = "%s months ago" % int(t)
58         return t_str
59
60 def plc_print_nodeinfo(plcnode):
61         url = "https://www.planet-lab.org/db/nodes/index.php?nodepattern="
62         plcnode['url'] = url + plcnode['hostname']
63
64         print "%(hostname)s %(url)s" % plcnode
65         print "   Checked: %s" % time.ctime()
66
67         print "\t boot_state |   created   |   updated   | last_contact | key"
68         print "\t       %5s | %11.11s | %11.11s | %12s | %s" % \
69                 (color_boot_state(plcnode['boot_state']), diff_time(plcnode['date_created']),
70                  diff_time(plcnode['last_updated']), 
71                  diff_time(plcnode['last_contact']), plcnode['key'])
72
73 def fb_print_nodeinfo(fbnode):
74         print "   Checked: ",
75         if 'checked' in fbnode:
76                 print "%11.11s " % diff_time(fbnode['checked'])
77         else:
78                 print "Unknown"
79         print "\t      state |  ssh  |  pcu  | bootcd | category | kernel"
80         if fbnode['bootcd']:
81                 fbnode['bootcd'] = fbnode['bootcd'].split()[-1]
82         else:
83                 fbnode['bootcd'] = "unknown"
84         if 'state' in fbnode:
85                 fbnode['state'] = color_boot_state(get_current_state(fbnode))
86         else:
87                 fbnode['state'] = "none"
88         if len(fbnode['kernel'].split()) > 2:
89                 fbnode['kernel'] = fbnode['kernel'].split()[2]
90         print "\t       %(state)5s | %(ssh)5.5s | %(pcu)5.5s | %(bootcd)6.6s | %(category)8.8s | %(kernel)s" % fbnode
91
92 def act_print_nodeinfo(actnode, header):
93         if header[0]:
94                 if 'date_created' in actnode:
95                         print "   Created: %11.11s" % diff_time(actnode['date_created'])
96                 print "   LastTime %11.11s" % diff_time(actnode['time'])
97                 print "\t      RT     | category | action          | msg"
98                 header[0] = False
99
100         if 'rt' in actnode and 'Status' in actnode['rt']:
101                 print "\t %5.5s %5.5s | %8.8s | %15.15s | %s" % \
102                         (actnode['rt']['Status'], actnode['rt']['id'][7:],
103                          actnode['category'], actnode['action'][0], 
104                          actnode['msg_format'][:-1])
105         else:
106                 if type(actnode['action']) == type([]):
107                         action = actnode['action'][0]
108                 else:
109                         action = actnode['action']
110                 if 'category' in actnode:
111                         category = actnode['category']
112                 else:
113                         category = "none"
114                         
115                 if 'msg_format' in actnode:
116                         print "\t       %5.5s | %8.8s | %15.15s | %s" % \
117                         (actnode['ticket_id'],
118                          category, action, 
119                          actnode['msg_format'][:-1])
120                 else:
121                         print "\t       %5.5s | %8.8s | %15.15s" % \
122                         (actnode['ticket_id'],
123                          category, action)
124
125 def pcu_print_info(pcuinfo, hostname):
126         print "   Checked: ",
127         if 'checked' in pcuinfo:
128                 print "%11.11s " % diff_time(pcuinfo['checked'])
129         else:
130                 print "Unknown"
131
132         print "\t            user   |          password | port | pcu_id | hostname "
133         print "\t %17s | %17s | %4s | %6s | %30s | %s" % \
134                 (pcuinfo['username'], pcuinfo['password'], 
135                  pcuinfo[hostname], pcuinfo['pcu_id'], reboot.pcu_name(pcuinfo), pcuinfo['model'])
136
137         if 'portstatus' in pcuinfo and pcuinfo['portstatus'] != {} and pcuinfo['portstatus'] != None:
138                 if pcuinfo['portstatus']['22'] == "open":
139                         print "\t ssh -o PasswordAuthentication=yes -o PubkeyAuthentication=no %s@%s" % (pcuinfo['username'], reboot.pcu_name(pcuinfo))
140                 if pcuinfo['portstatus']['23'] == "open":
141                         print "\t telnet %s" % (reboot.pcu_name(pcuinfo))
142                 if pcuinfo['portstatus']['80'] == "open" or \
143                         pcuinfo['portstatus']['443'] == "open":
144                         print "\t https://%s" % (reboot.pcu_name(pcuinfo))
145                         print "\t import %s.png" % (reboot.pcu_name(pcuinfo))
146                         print """\t mutt -s "crash for %s" -a %s.png sapanb@cs.princeton.edu < /dev/null""" % (hostname, reboot.pcu_name(pcuinfo))
147                 if pcuinfo['portstatus']['443'] == "open":
148                         print "\t racadm.py -r %s -u %s -p '%s'" % (pcuinfo['ip'], pcuinfo['username'], pcuinfo['password'])
149                         print "\t cmdhttps/locfg.pl -s %s -f iloxml/Reset_Server.xml -u %s -p '%s' | grep MESSAGE" % \
150                                 (reboot.pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
151                         print "\t cmdhttps/locfg.pl -s %s -f iloxml/License.xml -u %s -p '%s' | grep MESSAGE" % \
152                                 (reboot.pcu_name(pcuinfo), pcuinfo['username'], pcuinfo['password'])
153                 if pcuinfo['portstatus']['16992'] == "open":
154                         print "\t ./cmdamt/remoteControl -A -verbose 'http://%s:16992/RemoteControlService' -user admin -pass '%s'" % (reboot.pcu_name(pcuinfo), pcuinfo['password'])
155
156 if config.findbad:
157         # rerun findbad with the nodes in the given nodes.
158         import os
159         file = "findbad.txt"
160         configmodule.setFileFromList(file, config.args)
161         os.system("./findbad.py --cachenodes --debug=0 --dbname=findbad --increment --nodelist %s" % file)
162
163 fb = database.dbLoad("findbad")
164 act_all = database.dbLoad("act_all")
165
166 for node in config.args:
167         config.node = node
168
169         plc_nodeinfo = api.GetNodes({'hostname': config.node}, None)[0]
170         fb_nodeinfo  = fb['nodes'][config.node]['values']
171
172         plc_print_nodeinfo(plc_nodeinfo)
173         fb_print_nodeinfo(fb_nodeinfo)
174
175         if fb_nodeinfo['pcu'] == "PCU":
176                 pcu = reboot.get_pcu_values(fb_nodeinfo['plcnode']['pcu_ids'][0])
177                 pcu_print_info(pcu, config.node)
178
179         if config.node in act_all and len(act_all[config.node]) > 0:
180                 header = [True]
181
182                 if config.endrecord:
183                         node_end_record(config.node)
184                         #a = Action(config.node, act_all[config.node][0])
185                         #a.delField('rt')
186                         #a.delField('found_rt_ticket')
187                         #a.delField('second-mail-at-oneweek')
188                         #a.delField('second-mail-at-twoweeks')
189                         #a.delField('first-found')
190                         #rec = a.get()
191                         #rec['action'] = ["close_rt"]
192                         #rec['category'] = "UNKNOWN"
193                         #rec['stage'] = "monitor-end-record"
194                         #rec['time'] = time.time() - 7*60*60*24
195                         #act_all[config.node].insert(0,rec)
196                         #database.dbDump("act_all", act_all)
197
198                 for act_nodeinfo in act_all[config.node]:
199                         act_print_nodeinfo(act_nodeinfo, header)
200         else: act_nodeinfo = None
201
202         print ""
203
204         if config.bootcd:
205                 print """
206 If you need a new bootcd, the steps are very simple:
207
208 Visit:
209  * https://www.planet-lab.org/db/nodes/index.php?nodepattern=%s
210  * Select Download -> Download ISO image for %s
211  * Save the ISO, and burn it to a writable CD-ROM.
212  * Replace the old CD and reboot the machine.
213
214 Please let me know if you have any additional questions.
215 """ % (config.node, config.node)
216