6 from optparse import OptionParser
10 # everything in the onelab.eu domain
12 testmaster = 'testmaster'
13 build_boxes = [ "mirror", "liquid", "reed", "velvet", ]
14 plc_boxes = [ "testplc" ]
16 [ "testqemu%d"%i for i in range (1,4) ] + \
17 [ "testqemu32-%d"%i for i in range (1,6) ]
18 test_boxes = plc_boxes + qemu_boxes
23 self.do_tracker_qemus = False
24 self.do_tracker_plcs = False
27 return "%s.%s"%(box,self.domain)
29 def root (box): return "root@%s"%box
31 def header (self,message):
32 print "===============",message
34 def run (self,argv,message, trash_err=False):
35 if self.options.dry_run:
39 if message: self.header(message)
43 subprocess.call(argv,stderr=file('/dev/null','w'))
45 def backquote (self, argv, trash_err=False):
47 return subprocess.Popen(argv,stdout=subprocess.PIPE).communicate()[0]
49 null = open('/dev/null','w')
50 result = subprocess.Popen(argv,stdout=subprocess.PIPE,stderr=null).communicate()[0]
54 def reboot (self,box):
55 command=['ssh',self.root(box),'shutdown','-r','now']
56 self.run (command,"Rebooting %s"%box)
58 def handle_tracker_plcs (self):
59 box = self.fqdn (self.testmaster)
60 filename="tracker-plcs"
61 if not self.options.probe:
62 command=['ssh',self.root(box),"rm","-rf",filename]
63 self.run(command,"Cleaning up %s on %s"%(filename,box))
65 self.header ("++++++++++ Inspecting %s on %s"%(filename,box))
66 read_command = ['ssh',self.root(box),"cat",filename]
67 trackers=self.backquote(read_command)
68 for tracker in trackers.split('\n'):
69 if not tracker: continue
71 tracker=tracker.strip()
72 [hostname,buildname]=tracker.split('@')
73 [left,plcname]=buildname.rsplit('-',1)
74 print self.margin_outline(plcname),tracker
76 print self.margin(""),tracker
78 def handle_tracker_qemus (self):
79 box = self.fqdn (self.testmaster)
80 filename="tracker-qemus"
81 if not self.options.probe:
82 command=['ssh',self.root(box),"rm","-rf",filename]
83 self.run(command,"Cleaning up %s on %s"%(filename,box))
85 self.header ("++++++++++ Inspecting %s on %s"%(filename,box))
86 read_command = ['ssh',self.root(box),"cat",filename]
87 trackers=self.backquote(read_command)
88 for tracker in trackers.split('\n'):
89 if not tracker: continue
91 tracker=tracker.strip()
92 [hostname,buildname,nodename]=tracker.split('@')
93 nodename=nodename.split('.')[0]
94 print self.margin_outline(nodename),tracker
96 print self.margin(""),tracker
98 def handle_build_box (self,box):
99 if not self.options.probe:
102 command=['ssh',self.root(box),'uptime']
103 uptime=self.backquote(command,True).strip()
105 command=['ssh',self.root(box),'pgrep','build']
106 if self.options.dry_run:
107 self.run(command,None)
109 pids=self.backquote(command,True)
111 self.header ('No build process on %s (%s)'%(box,uptime))
113 command=['ssh',self.root(box),'ps','-o','pid,command'] + [ pid for pid in pids.split("\n") if pid]
114 self.run(command,"Active build processes on %s (%s)"%(box,uptime),True)
116 vplc_matcher = re.compile(".*(vplc[0-9]+$)")
117 def vplcname (self, vservername):
118 match = self.vplc_matcher.match(vservername)
119 if match: return match.groups(0)
122 margin_format="%-14s"
123 def margin(self,string): return self.margin_format%string
124 def outline (self, string): return '== %s =='%string
125 def margin_outline (self, string): return self.margin(self.outline(string))
127 def handle_plc_box (self,box):
128 if not self.options.probe:
131 command=['ssh',self.root(box),'vserver-stat']
132 if self.options.dry_run:
133 self.run(command,"Active vservers on %s"%box)
135 # try to find fullname (vserver_stat truncates to a ridiculously short name)
137 self.header ("vserver map on %s"%box)
138 # fetch the contexts for all vservers on that box
139 map_command=['ssh',self.root(box),'grep','.','/etc/vservers/*/context','/dev/null',]
140 context_map=self.backquote (map_command)
141 # at this point we have a set of lines like
142 # /etc/vservers/2010.01.20--k27-f12-32-vplc03/context:40144
144 for map_line in context_map.split("\n"):
145 if not map_line: continue
146 [path,xid] = map_line.split(':')
147 ctx_dict[xid]=os.path.basename(os.path.dirname(path))
148 # at this point ctx_id maps context id to vservername
150 vserver_stat = self.backquote (command)
151 for vserver_line in vserver_stat.split("\n"):
152 if not vserver_line: continue
153 context=vserver_line.split()[0]
155 print self.margin(""),vserver_line
157 longname=ctx_dict[context]
158 print self.margin_outline(self.vplcname(longname)),"%(vserver_line)s [=%(longname)s]"%locals()
160 self.run(command,"Fine-grained method failed - fallback to plain vserver-stat")
162 vnode_matcher = re.compile(".*(vnode[0-9]+)")
163 def vnodename (self, ps_line):
164 match = self.vnode_matcher.match(ps_line)
165 if match: return match.groups(0)
169 def handle_qemu_box (self,box):
170 if not self.options.probe:
173 command=['ssh',self.root(box),'lsmod']
174 modules=self.backquote(command).split('\n')
175 kqemu_msg='*NO kqemu MODULE LOADED*'
176 for module in modules:
177 if module.find('kqemu')==0:
180 command=['ssh',self.root(box),'pgrep','qemu']
181 if self.options.dry_run:
182 self.run(command,None)
184 pids=self.backquote(command)
186 self.header ('No qemu process on %s'%box)
188 self.header ("Active qemu processes on %s (%s)"%(box,kqemu_msg))
189 command=['ssh',self.root(box),'ps','-o','pid,command'] + [ pid for pid in pids.split("\n") if pid]
190 ps_lines = self.backquote (command).split("\n")
191 for ps_line in ps_lines:
192 if not ps_line or ps_line.find('PID') >=0 : continue
193 print self.margin_outline(self.vnodename(ps_line)), ps_line
195 def handle_box(self,box,type):
196 if box in self.qemu_boxes:
197 if type=="qemu": self.handle_qemu_box(self.fqdn(box))
198 elif box in self.plc_boxes:
199 if type=="plc": self.handle_plc_box(self.fqdn(box))
201 self.handle_build_box(self.fqdn(box))
204 usage="""%prog [options] [hostname..(s)]
205 Default is to act on test boxes only (with trackers clean)"""
206 parser = OptionParser (usage=usage)
207 parser.add_option ("-n","--dry-run",action="store_true",dest="dry_run",default=False,
209 parser.add_option ("-r","--reboot", action="store_false",dest="probe",default=True,
210 help="Actually reset/reboot stuff instead of just probing it")
211 # no need for -p = probe, as this is the default
212 parser.add_option ("-p","--plc", action="store_true",dest="plc_only",default=False,
213 help="Acts on the plc box only")
215 parser.add_option ("-a","--all",action="store_true",dest="all_boxes",default=False,
216 help="Acts on build and test boxes")
217 parser.add_option ("-b","--build",action="store_true",dest="build_only",default=False,
218 help="Acts on build boxes only")
219 parser.add_option ("-q","--qemu",action="store_true",dest="qemu_only",default=False,
220 help="Only acts on the qemu boxes")
221 parser.add_option ("-t","--trackers",action="store_true",dest="trackers_only",default=False,
222 help="Only wipes trackers")
224 (self.options,args) = parser.parse_args()
226 # use given hostnames if provided
229 # if hostnames are specified, let's stay on the safe side and don't reset trackers
230 self.do_tracker_plcs = False
231 self.do_tracker_qemus = False
232 elif self.options.all_boxes:
233 self.boxes=self.test_boxes + self.build_boxes
234 self.do_tracker_plcs = True
235 self.do_tracker_qemus = True
236 elif self.options.build_only:
237 self.boxes=self.build_boxes
238 self.do_tracker_plcs = False
239 self.do_tracker_qemus = False
240 elif self.options.qemu_only:
241 self.boxes=self.qemu_boxes
242 self.do_tracker_plcs = False
243 self.do_tracker_qemus = True
244 elif self.options.plc_only:
245 self.boxes=self.plc_boxes
246 self.do_tracker_plcs = True
247 self.do_tracker_qemus = False
248 elif self.options.trackers_only:
250 self.do_tracker_plcs = True
251 self.do_tracker_qemus = True
254 self.boxes = self.test_boxes
255 self.do_tracker_plcs = True
256 self.do_tracker_qemus = True
259 for box in self.boxes: self.handle_box (box,"build")
261 if self.do_tracker_plcs:self.handle_tracker_plcs ()
262 for box in self.boxes: self.handle_box (box,"plc")
264 if self.do_tracker_qemus:self.handle_tracker_qemus ()
265 for box in self.boxes: self.handle_box (box,"qemu")
267 if __name__ == "__main__":