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