4cccdf0a3499dec0702a5c6f5d9db5d242c10976
[monitor.git] / reboot.py
1 #!/usr/bin/python
2 #
3 # Reboot specified nodes
4 #
5
6 import getpass, getopt
7 import os, sys
8 import xml, xmlrpclib
9 import errno, time, traceback
10 import urllib2
11 import urllib
12 import threading, popen2
13 import array, struct
14 #from socket import *
15 import socket
16 import plc
17 import base64
18 from subprocess import PIPE, Popen
19 import ssh.pxssh as pxssh
20 import ssh.pexpect as pexpect
21 import socket
22
23 # Use our versions of telnetlib and pyssh
24 sys.path.insert(0, os.path.dirname(sys.argv[0]))
25 import telnetlib
26 sys.path.insert(0, os.path.dirname(sys.argv[0]) + "/pyssh")    
27 import pyssh
28
29 # Timeouts in seconds
30 TELNET_TIMEOUT = 45
31
32 # Event class ID from pcu events
33 #NODE_POWER_CONTROL = 3
34
35 # Monitor user ID
36 #MONITOR_USER_ID = 11142
37
38 import logging
39 logger = logging.getLogger("monitor")
40 verbose = 1
41 #dryrun = 0;
42
43 class ExceptionNoTransport(Exception): pass
44 class ExceptionNotFound(Exception): pass
45 class ExceptionPassword(Exception): pass
46 class ExceptionTimeout(Exception): pass
47 class ExceptionPrompt(Exception): pass
48 class ExceptionSequence(Exception): pass
49 class ExceptionReset(Exception): pass
50 class ExceptionPort(Exception): pass
51 class ExceptionUsername(Exception): pass
52
53 def telnet_answer(telnet, expected, buffer):
54         global verbose
55
56         output = telnet.read_until(expected, TELNET_TIMEOUT)
57         #if verbose:
58         #       logger.debug(output)
59         if output.find(expected) == -1:
60                 raise ExceptionNotFound, "'%s' not found" % expected
61         else:
62                 telnet.write(buffer + "\r\n")
63
64
65 # PCU has model, host, preferred-port, user, passwd, 
66
67 # This is an object derived directly form the PLCAPI DB fields
68 class PCU(object):
69         def __init__(self, plc_pcu_dict):
70                 for field in ['username', 'password', 'site_id', 
71                                                 'hostname', 'ip', 
72                                                 'pcu_id', 'model', 
73                                                 'node_ids', 'ports', ]:
74                         if field in plc_pcu_dict:
75                                 self.__setattr__(field, plc_pcu_dict[field])
76                         else:
77                                 raise Exception("No such field %s in PCU object" % field)
78
79 # These are the convenience functions build around the PCU object.
80 class PCUModel(PCU):
81         def __init__(self, plc_pcu_dict):
82                 PCU.__init__(self, plc_pcu_dict)
83                 self.host = self.pcu_name()
84
85         def pcu_name(self):
86                 if self.hostname is not None and self.hostname is not "":
87                         return self.hostname
88                 elif self.ip is not None and self.ip is not "":
89                         return self.ip
90                 else:
91                         return None
92
93         def nodeidToPort(self, node_id):
94                 if node_id in self.node_ids:
95                         for i in range(0, len(self.node_ids)):
96                                 if node_id == self.node_ids[i]:
97                                         return self.ports[i]
98
99                 raise Exception("No such Node ID: %d" % node_id)
100
101 # This class captures the observed pcu records from FindBadPCUs.py
102 class PCURecord:
103         def __init__(self, pcu_record_dict):
104                 for field in ['nodenames', 'portstatus', 
105                                                 'dnsmatch', 
106                                                 'complete_entry', ]:
107                         if field in pcu_record_dict:
108                                 if field == "reboot":
109                                         self.__setattr__("reboot_str", pcu_record_dict[field])
110                                 else:
111                                         self.__setattr__(field, pcu_record_dict[field])
112                         else:
113                                 raise Exception("No such field %s in pcu record dict" % field)
114
115 class Transport:
116         TELNET = 1
117         SSH    = 2
118         HTTP   = 3
119         IPAL   = 4
120
121         TELNET_TIMEOUT = 60
122
123         def __init__(self, type, verbose):
124                 self.type = type
125                 self.verbose = verbose
126                 self.transport = None
127
128         def open(self, host, username=None, password=None, prompt="User Name"):
129                 transport = None
130
131                 if self.type == self.TELNET:
132                         transport = telnetlib.Telnet(host, timeout=self.TELNET_TIMEOUT)
133                         transport.set_debuglevel(self.verbose)
134                         if username is not None:
135                                 self.transport = transport
136                                 self.ifThenSend(prompt, username, ExceptionUsername)
137
138                 elif self.type == self.SSH:
139                         if username is not None:
140                                 transport = pyssh.Ssh(username, host)
141                                 transport.set_debuglevel(self.verbose)
142                                 transport.open()
143                                 # TODO: have an ssh set_debuglevel() also...
144                         else:
145                                 raise Exception("Username cannot be None for ssh transport.")
146                 elif self.type == self.HTTP:
147                         self.url = "http://%s:%d/" % (host,80)
148                         uri = "%s:%d" % (host,80)
149
150                         # create authinfo
151                         authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm()
152                         authinfo.add_password (None, uri, username, password)
153                         authhandler = urllib2.HTTPBasicAuthHandler( authinfo )
154
155                         transport = urllib2.build_opener(authhandler)
156
157                 else:
158                         raise Exception("Unknown transport type: %s" % self.type)
159
160                 self.transport = transport
161                 return True
162
163         def close(self):
164                 if self.type == self.TELNET:
165                         self.transport.close() 
166                 elif self.type == self.SSH:
167                         self.transport.close() 
168                 elif self.type == self.HTTP:
169                         pass
170                 else:
171                         raise Exception("Unknown transport type %s" % self.type)
172                 self.transport = None
173
174         def sendHTTP(self, resource, data):
175                 if self.verbose:
176                         print "POSTing '%s' to %s" % (data,self.url + resource)
177
178                 try:
179                         f = self.transport.open(self.url + resource ,data)
180                         r = f.read()
181                         if self.verbose:
182                                 print r
183
184                 except urllib2.URLError,err:
185                         logger.info('Could not open http connection', err)
186                         return "http transport error"
187
188                 return 0
189
190         def sendPassword(self, password, prompt=None):
191                 if self.type == self.TELNET:
192                         if prompt == None:
193                                 self.ifThenSend("Password", password, ExceptionPassword)
194                         else:
195                                 self.ifThenSend(prompt, password, ExceptionPassword)
196                 elif self.type == self.SSH:
197                         self.ifThenSend("password:", password, ExceptionPassword)
198                 elif self.type == self.HTTP:
199                         pass
200                 else:
201                         raise Exception("Unknown transport type: %s" % self.type)
202
203         def ifThenSend(self, expected, buffer, ErrorClass=ExceptionPrompt):
204
205                 if self.transport != None:
206                         output = self.transport.read_until(expected, self.TELNET_TIMEOUT)
207                         if output.find(expected) == -1:
208                                 raise ErrorClass, "'%s' not found" % expected
209                         else:
210                                 self.transport.write(buffer + "\r\n")
211                 else:
212                         raise ExceptionNoTransport("transport object is type None")
213
214         def ifElse(self, expected, ErrorClass):
215                 try:
216                         self.transport.read_until(expected, self.TELNET_TIMEOUT)
217                 except:
218                         raise ErrorClass("Could not find '%s' within timeout" % expected)
219                         
220
221 class PCUControl(Transport,PCUModel,PCURecord):
222         def __init__(self, plc_pcu_record, verbose, supported_ports=[]):
223                 PCUModel.__init__(self, plc_pcu_record)
224                 PCURecord.__init__(self, plc_pcu_record)
225                 type = None
226                 if self.portstatus:
227                         if '22' in supported_ports and self.portstatus['22'] == "open":
228                                 type = Transport.SSH
229                         elif '23' in supported_ports and self.portstatus['23'] == "open":
230                                 type = Transport.TELNET
231                         elif '80' in supported_ports and self.portstatus['80'] == "open":
232                                 type = Transport.HTTP
233                         elif '443' in supported_ports and self.portstatus['443'] == "open":
234                                 type = Transport.HTTP
235                         elif '5869' in supported_ports and self.portstatus['5869'] == "open":
236                                 # For DRAC cards. Racadm opens this port.
237                                 type = Transport.HTTP
238                         elif '9100' in supported_ports and self.portstatus['9100'] == "open":
239                                 type = Transport.IPAL
240                         elif '16992' in supported_ports and self.portstatus['16992'] == "open":
241                                 type = Transport.HTTP
242                         else:
243                                 raise ExceptionPort("Unsupported Port: No transport from open ports")
244                 else:
245                         raise Exception("No Portstatus: No transport because no open ports")
246                 Transport.__init__(self, type, verbose)
247
248         def run(self, node_port, dryrun):
249                 """ This function is to be defined by the specific PCU instance.  """
250                 pass
251                 
252         def reboot(self, node_port, dryrun):
253                 try:
254                         return self.run(node_port, dryrun)
255                 except ExceptionNotFound, err:
256                         return "error: " + str(err)
257                 except ExceptionPassword, err:
258                         return "password exception: " + str(err)
259                 except ExceptionTimeout, err:
260                         return "timeout exception: " + str(err)
261                 except ExceptionUsername, err:
262                         return "exception: no username prompt: " + str(err)
263                 except ExceptionSequence, err:
264                         return "sequence error: " + str(err)
265                 except ExceptionPrompt, err:
266                         return "prompt exception: " + str(err)
267                 except ExceptionPort, err:
268                         return "no ports exception: " + str(err)
269                 except socket.error, err:
270                         return "socket error: timeout: " + str(err)
271                 except EOFError, err:
272                         if self.verbose:
273                                 logger.debug("reboot: EOF")
274                                 logger.debug(err)
275                         self.transport.close()
276                         import traceback
277                         traceback.print_exc()
278                         return "EOF connection reset" + str(err)
279                 
280 class IPAL(PCUControl):
281         """ 
282                 This now uses a proprietary format for communicating with the PCU.  I
283                 prefer it to Telnet, and Web access, since it's much lighter weight
284                 and, more importantly, IT WORKS!! HHAHHHAHAHAHAHAHA!
285         """
286
287         def format_msg(self, data, cmd):
288                 esc = chr(int('1b',16))
289                 return "%c%s%c%s%c" % (esc, self.password, esc, data, cmd) # esc, 'q', chr(4))
290         
291         def recv_noblock(self, s, count):
292                 import errno
293
294                 try:
295                         # TODO: make sleep backoff, before stopping.
296                         time.sleep(4)
297                         ret = s.recv(count, socket.MSG_DONTWAIT)
298                 except socket.error, e:
299                         if e[0] == errno.EAGAIN:
300                                 raise Exception(e[1])
301                         else:
302                                 # TODO: not other exceptions.
303                                 raise Exception(e)
304                 return ret
305
306         def run(self, node_port, dryrun):
307                 import errno
308
309                 power_on = False
310
311                 print "open socket"
312                 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
313                 try:
314                         print "connect"
315                         s.connect((self.host, 9100))
316                 except socket.error, e:
317                         s.close()
318                         if e[0] == errno.ECONNREFUSED:
319                                 # cannot connect to remote host
320                                 raise Exception(e[1])
321                         else:
322                                 # TODO: what other conditions are there?
323                                 raise Exception(e)
324                                 
325                 # get current status
326                 print "Checking status"
327                 s.send(self.format_msg("", 'O'))
328                 ret = self.recv_noblock(s, 8)
329                 print "Current status is '%s'" % ret
330
331                 if ret == '':
332                         raise Exception("Status returned 'another session already open' %s : %s" % (node_port, ret))
333                         
334                                 
335                 if node_port < len(ret):
336                         status = ret[node_port]
337                         if status == '1':
338                                 # up
339                                 power_on = True
340                         elif status == '0':
341                                 # down
342                                 power_on = False
343                         else:
344                                 raise Exception("Unknown status for PCU socket %s : %s" % (node_port, ret))
345                 else:
346                         raise Exception("Mismatch between configured port and PCU status: %s %s" % (node_port, ret))
347                         
348
349                 if not dryrun:
350                         if power_on:
351                                 print "Pulsing %s" % node_port
352                                 s.send(self.format_msg("%s" % node_port, 'P'))
353                         else:
354                                 # NOTE: turn power on ; do not pulse the port.
355                                 print "Power was off, so turning on ..."
356                                 s.send(self.format_msg("%s" % node_port, 'E'))
357                                 #s.send(self.format_msg("%s" % node_port, 'P'))
358
359                         print "Receiving response."
360                         ret = self.recv_noblock(s, 8)
361                         print "Current status is '%s'" % ret
362
363                         if node_port < len(ret):
364                                 status = ret[node_port]
365                                 if status == '1':
366                                         # up
367                                         power_on = True
368                                 elif status == '0':
369                                         # down
370                                         power_on = False
371                                 else:
372                                         raise Exception("Unknown status for PCU socket %s : %s" % (node_port, ret))
373                         else:
374                                 raise Exception("Mismatch between configured port and PCU status: %s %s" % (node_port, ret))
375
376                         if power_on:
377                                 return 0
378                         else:
379                                 return "Failed Power On"
380
381                 s.close()
382                 return 0
383
384 # TELNET version of protocol...
385 #               #self.open(self.host)
386 #               ## XXX Some iPals require you to hit Enter a few times first
387 #               #self.ifThenSend("Password >", "\r\n\r\n", ExceptionNotFound)
388 #               # Login
389 #               self.ifThenSend("Password >", self.password, ExceptionPassword)
390 #               self.transport.write("\r\n\r\n")
391 #               if not dryrun: # P# - Pulse relay
392 #                       print "node_port %s" % node_port
393 #                       self.ifThenSend("Enter >", 
394 #                                                       "P7", # % node_port, 
395 #                                                       ExceptionNotFound)
396 #                       print "send newlines"
397 #                       self.transport.write("\r\n\r\n")
398 #                       print "after new lines"
399 #               # Get the next prompt
400 #               print "wait for enter"
401 #               self.ifElse("Enter >", ExceptionTimeout)
402 #               print "closing "
403 #               self.close()
404 #               return 0
405
406 class APCEurope(PCUControl):
407         def run(self, node_port, dryrun):
408                 self.open(self.host, self.username)
409                 self.sendPassword(self.password)
410
411                 self.ifThenSend("\r\n> ", "1", ExceptionPassword)
412                 self.ifThenSend("\r\n> ", "2")
413                 self.ifThenSend("\r\n> ", str(node_port))
414                 # 3- Immediate Reboot             
415                 self.ifThenSend("\r\n> ", "3")
416
417                 if not dryrun:
418                         self.ifThenSend("Enter 'YES' to continue or <ENTER> to cancel", 
419                                                         "YES\r\n",
420                                                         ExceptionSequence)
421                 else:
422                         self.ifThenSend("Enter 'YES' to continue or <ENTER> to cancel", 
423                                                         "", ExceptionSequence)
424                 self.ifThenSend("Press <ENTER> to continue...", "", ExceptionSequence)
425
426                 self.close()
427                 return 0
428
429 class APCBrazil(PCUControl):
430         def run(self, node_port, dryrun):
431                 self.open(self.host, self.username)
432                 self.sendPassword(self.password)
433
434                 self.ifThenSend("\r\n> ", "1", ExceptionPassword)
435                 self.ifThenSend("\r\n> ", str(node_port))
436                 # 4- Immediate Reboot             
437                 self.ifThenSend("\r\n> ", "4")
438
439                 if not dryrun:
440                         self.ifThenSend("Enter 'YES' to continue or <ENTER> to cancel", 
441                                                         "YES\r\n",
442                                                         ExceptionSequence)
443                 else:
444                         self.ifThenSend("Enter 'YES' to continue or <ENTER> to cancel", 
445                                                         "", ExceptionSequence)
446                 self.ifThenSend("Press <ENTER> to continue...", "", ExceptionSequence)
447
448                 self.close()
449                 return 0
450
451 class APCBerlin(PCUControl):
452         def run(self, node_port, dryrun):
453                 self.open(self.host, self.username)
454                 self.sendPassword(self.password)
455
456                 self.ifThenSend("\r\n> ", "1", ExceptionPassword)
457                 self.ifThenSend("\r\n> ", "2")
458                 self.ifThenSend("\r\n> ", "1")
459                 self.ifThenSend("\r\n> ", str(node_port))
460                 # 3- Immediate Reboot             
461                 self.ifThenSend("\r\n> ", "3")
462
463                 if not dryrun:
464                         self.ifThenSend("Enter 'YES' to continue or <ENTER> to cancel", 
465                                                         "YES\r\n",
466                                                         ExceptionSequence)
467                 else:
468                         self.ifThenSend("Enter 'YES' to continue or <ENTER> to cancel", 
469                                                         "", ExceptionSequence)
470                 self.ifThenSend("Press <ENTER> to continue...", "", ExceptionSequence)
471
472                 self.close()
473                 return 0
474
475 class APCFolsom(PCUControl):
476         def run(self, node_port, dryrun):
477                 self.open(self.host, self.username)
478                 self.sendPassword(self.password)
479
480                 self.ifThenSend("\r\n> ", "1", ExceptionPassword)
481                 self.ifThenSend("\r\n> ", "2")
482                 self.ifThenSend("\r\n> ", "1")
483                 self.ifThenSend("\r\n> ", str(node_port))
484                 self.ifThenSend("\r\n> ", "1")
485
486                 # 3- Immediate Reboot             
487                 self.ifThenSend("\r\n> ", "3")
488
489                 if not dryrun:
490                         self.ifThenSend("Enter 'YES' to continue or <ENTER> to cancel", 
491                                                         "YES\r\n",
492                                                         ExceptionSequence)
493                 else:
494                         self.ifThenSend("Enter 'YES' to continue or <ENTER> to cancel", 
495                                                         "", ExceptionSequence)
496                 self.ifThenSend("Press <ENTER> to continue...", "", ExceptionSequence)
497
498                 self.close()
499                 return 0
500
501 class APCMaster(PCUControl):
502         def run(self, node_port, dryrun):
503                 print "Rebooting %s" % self.host
504                 self.open(self.host, self.username)
505                 self.sendPassword(self.password)
506
507                 # 1- Device Manager
508                 self.ifThenSend("\r\n> ", "1", ExceptionPassword)
509                 # 3- Outlet Control/Config
510                 self.ifThenSend("\r\n> ", "3")
511                 # n- Outlet n
512                 self.ifThenSend("\r\n> ", str(node_port))
513                 # 1- Control Outlet
514                 self.ifThenSend("\r\n> ", "1")
515                 # 3- Immediate Reboot             
516                 self.ifThenSend("\r\n> ", "3")
517
518                 if not dryrun:
519                         self.ifThenSend("Enter 'YES' to continue or <ENTER> to cancel", 
520                                                         "YES\r\n",
521                                                         ExceptionSequence)
522                 else:
523                         self.ifThenSend("Enter 'YES' to continue or <ENTER> to cancel", 
524                                                         "", ExceptionSequence)
525                 self.ifThenSend("Press <ENTER> to continue...", "", ExceptionSequence)
526
527                 self.close()
528                 return 0
529
530 class APC(PCUControl):
531         def __init__(self, plc_pcu_record, verbose):
532                 PCUControl.__init__(self, plc_pcu_record, verbose)
533
534                 self.master = APCMaster(plc_pcu_record, verbose)
535                 self.folsom = APCFolsom(plc_pcu_record, verbose)
536                 self.europe = APCEurope(plc_pcu_record, verbose)
537
538         def run(self, node_port, dryrun):
539                 try_again = True
540                 sleep_time = 1
541
542                 for pcu in [self.master, self.europe, self.folsom]:
543                         if try_again:
544                                 try:
545                                         print "-*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*"
546                                         try_again = False
547                                         print "sleeping 5"
548                                         time.sleep(sleep_time)
549                                         ret = pcu.reboot(node_port, dryrun)
550                                 except ExceptionSequence, err:
551                                         del pcu
552                                         sleep_time = 130
553                                         try_again = True
554
555                 if try_again:
556                         return "Unknown reboot sequence for APC PCU"
557                 else:
558                         return ret
559
560 class IntelAMT(PCUControl):
561         def run(self, node_port, dryrun):
562                 import soltesz
563
564                 cmd = soltesz.CMD()
565                 cmd_str = "IntelAMTSDK/Samples/RemoteControl/remoteControl"
566
567                 if dryrun:
568                         # NOTE: -p checks the power state of the host.
569                         # TODO: parse the output to find out if it's ok or not.
570                         cmd_str += " -p http://%s:16992/RemoteControlService  -user admin -pass '%s' " % (self.host, self.password )
571                 else:
572                         cmd_str += " -A http://%s:16992/RemoteControlService -user admin -pass '%s' " % (self.host, self.password )
573                         
574                 print cmd_str
575                 return cmd.system(cmd_str, self.TELNET_TIMEOUT)
576
577 class DRACRacAdm(PCUControl):
578         def run(self, node_port, dryrun):
579
580                 print "trying racadm_reboot..."
581                 racadm_reboot(self.host, self.username, self.password, node_port, dryrun)
582
583                 return 0
584
585 class DRAC(PCUControl):
586         def run(self, node_port, dryrun):
587                 self.open(self.host, self.username)
588                 self.sendPassword(self.password)
589
590                 print "logging in..."
591                 self.transport.write("\r\n")
592                 # Testing Reboot ?
593                 if dryrun:
594                         self.ifThenSend("[%s]#" % self.username, "getsysinfo")
595                 else:
596                         # Reset this machine
597                         self.ifThenSend("[%s]#" % self.username, "serveraction powercycle")
598
599                 self.ifThenSend("[%s]#" % self.username, "exit")
600
601                 self.close()
602                 return 0
603
604 class HPiLO(PCUControl):
605         def run(self, node_port, dryrun):
606                 self.open(self.host, self.username)
607                 self.sendPassword(self.password)
608
609                 # </>hpiLO-> 
610                 self.ifThenSend("</>hpiLO->", "cd system1")
611
612                 # Reboot Outlet  N        (Y/N)?
613                 if dryrun:
614                         self.ifThenSend("</system1>hpiLO->", "POWER")
615                 else:
616                         # Reset this machine
617                         self.ifThenSend("</system1>hpiLO->", "reset")
618
619                 self.ifThenSend("</system1>hpiLO->", "exit")
620
621                 self.close()
622                 return 0
623
624                 
625 class HPiLOHttps(PCUControl):
626         def run(self, node_port, dryrun):
627                 import soltesz
628
629                 locfg = soltesz.CMD()
630                 cmd = "cmdhttps/locfg.pl -s %s -f %s -u %s -p '%s' | grep 'MESSAGE' | grep -v 'No error'" % (
631                                         self.host, "iloxml/Get_Network.xml", 
632                                         self.username, self.password)
633                 sout, serr = locfg.run_noexcept(cmd)
634
635                 if sout.strip() != "":
636                         print "sout: %s" % sout.strip()
637                         return sout.strip()
638
639                 if not dryrun:
640                         locfg = soltesz.CMD()
641                         cmd = "cmdhttps/locfg.pl -s %s -f %s -u %s -p '%s' | grep 'MESSAGE' | grep -v 'No error'" % (
642                                                 self.host, "iloxml/Reset_Server.xml", 
643                                                 self.username, self.password)
644                         sout, serr = locfg.run_noexcept(cmd)
645
646                         if sout.strip() != "":
647                                 print "sout: %s" % sout.strip()
648                                 #return sout.strip()
649                 return 0
650
651 class BayTechAU(PCUControl):
652         def run(self, node_port, dryrun):
653                 self.open(self.host, self.username, None, "Enter user name:")
654                 self.sendPassword(self.password, "Enter Password:")
655
656                 #self.ifThenSend("RPC-16>", "Status")
657                 self.ifThenSend("RPC3-NC>", "Reboot %d" % node_port)
658
659                 # Reboot Outlet  N        (Y/N)?
660                 if dryrun:
661                         self.ifThenSend("(Y/N)?", "N")
662                 else:
663                         self.ifThenSend("(Y/N)?", "Y")
664                 self.ifThenSend("RPC3-NC>", "")
665
666                 self.close()
667                 return 0
668
669 class BayTechGeorgeTown(PCUControl):
670         def run(self, node_port, dryrun):
671                 self.open(self.host, self.username, None, "Enter user name:")
672                 self.sendPassword(self.password, "Enter Password:")
673
674                 #self.ifThenSend("RPC-16>", "Status")
675
676                 self.ifThenSend("RPC-16>", "Reboot %d" % node_port)
677
678                 # Reboot Outlet  N        (Y/N)?
679                 if dryrun:
680                         self.ifThenSend("(Y/N)?", "N")
681                 else:
682                         self.ifThenSend("(Y/N)?", "Y")
683                 self.ifThenSend("RPC-16>", "")
684
685                 self.close()
686                 return 0
687
688 class BayTechCtrlCUnibe(PCUControl):
689         """
690                 For some reason, these units let you log in fine, but they hang
691                 indefinitely, unless you send a Ctrl-C after the password.  No idea
692                 why.
693         """
694         def run(self, node_port, dryrun):
695                 print "BayTechCtrlC %s" % self.host
696
697                 ssh_options="-o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no"
698                 s = pxssh.pxssh()
699                 if not s.login(self.host, self.username, self.password, ssh_options):
700                         raise ExceptionPassword("Invalid Password")
701                 # Otherwise, the login succeeded.
702
703                 # Send a ctrl-c to the remote process.
704                 print "sending ctrl-c"
705                 s.send(chr(3))
706
707                 # Control Outlets  (5 ,1).........5
708                 try:
709                         index = s.expect(["Enter Request :"])
710
711                         if index == 0:
712                                 print "3"
713                                 s.send("3\r\n")
714                                 index = s.expect(["DS-RPC>", "Enter user name:"])
715                                 if index == 1:
716                                         s.send(self.username + "\r\n")
717                                         index = s.expect(["DS-RPC>"])
718
719                                 if index == 0:
720                                         print "Reboot %d" % node_port
721                                         s.send("Reboot %d\r\n" % node_port)
722
723                                         index = s.expect(["(Y/N)?"])
724                                         if index == 0:
725                                                 if dryrun:
726                                                         print "sending N"
727                                                         s.send("N\r\n")
728                                                 else:
729                                                         print "sending Y"
730                                                         s.send("Y\r\n")
731
732                                 #index = s.expect(["DS-RPC>"])
733                                 #print "got prompt back"
734
735                         s.close()
736
737                 except pexpect.EOF:
738                         raise ExceptionPrompt("EOF before 'Enter Request' Prompt")
739                 except pexpect.TIMEOUT:
740                         raise ExceptionPrompt("Timeout before 'Enter Request' Prompt")
741
742                 return 0
743
744 class BayTechCtrlC(PCUControl):
745         """
746                 For some reason, these units let you log in fine, but they hang
747                 indefinitely, unless you send a Ctrl-C after the password.  No idea
748                 why.
749         """
750         def run(self, node_port, dryrun):
751                 print "BayTechCtrlC %s" % self.host
752
753                 ssh_options="-o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no"
754                 s = pxssh.pxssh()
755                 if not s.login(self.host, self.username, self.password, ssh_options):
756                         raise ExceptionPassword("Invalid Password")
757                 # Otherwise, the login succeeded.
758
759                 # Send a ctrl-c to the remote process.
760                 print "sending ctrl-c"
761                 s.send(chr(3))
762
763                 # Control Outlets  (5 ,1).........5
764                 try:
765                         index = s.expect(["Enter Request :"])
766
767                         if index == 0:
768                                 print "5"
769                                 s.send("5\r\n")
770                                 index = s.expect(["DS-RPC>", "Enter user name:"])
771                                 if index == 1:
772                                         print "sending username"
773                                         s.send(self.username + "\r\n")
774                                         index = s.expect(["DS-RPC>"])
775
776                                 if index == 0:
777                                         print "Reboot %d" % node_port
778                                         s.send("Reboot %d\r\n" % node_port)
779
780                                         index = s.expect(["(Y/N)?"])
781                                         if index == 0:
782                                                 if dryrun:
783                                                         print "sending N"
784                                                         s.send("N\r\n")
785                                                 else:
786                                                         print "sending Y"
787                                                         s.send("Y\r\n")
788
789                                 index = s.expect(["DS-RPC>"])
790                                 #print "got prompt back"
791
792                         s.close()
793
794                 except pexpect.EOF:
795                         raise ExceptionPrompt("EOF before 'Enter Request' Prompt")
796                 except pexpect.TIMEOUT:
797                         raise ExceptionPrompt("Timeout before Prompt")
798
799                 return 0
800
801 class BayTech(PCUControl):
802         def run(self, node_port, dryrun):
803                 self.open(self.host, self.username)
804                 self.sendPassword(self.password)
805
806                 # Control Outlets  (5 ,1).........5
807                 self.ifThenSend("Enter Request :", "5")
808
809                 # Reboot N
810                 try:
811                         self.ifThenSend("DS-RPC>", "Reboot %d" % node_port, ExceptionNotFound)
812                 except ExceptionNotFound, msg:
813                         # one machine is configured to ask for a username,
814                         # even after login...
815                         print "msg: %s" % msg
816                         self.transport.write(self.username + "\r\n")
817                         self.ifThenSend("DS-RPC>", "Reboot %d" % node_port)
818
819                 # Reboot Outlet  N        (Y/N)?
820                 if dryrun:
821                         self.ifThenSend("(Y/N)?", "N")
822                 else:
823                         self.ifThenSend("(Y/N)?", "Y")
824                 self.ifThenSend("DS-RPC>", "")
825
826                 self.close()
827                 return 0
828
829 class WTIIPS4(PCUControl):
830         def run(self, node_port, dryrun):
831                 self.open(self.host)
832                 self.sendPassword(self.password, "Enter Password:")
833
834                 self.ifThenSend("IPS> ", "/Boot %s" % node_port)
835                 if not dryrun:
836                         self.ifThenSend("Sure? (Y/N): ", "N")
837                 else:
838                         self.ifThenSend("Sure? (Y/N): ", "Y")
839
840                 self.ifThenSend("IPS> ", "")
841
842                 self.close()
843                 return 0
844
845 class ePowerSwitchGood(PCUControl):
846         # NOTE:
847         #               The old code used Python's HTTPPasswordMgrWithDefaultRealm()
848         #               For some reason this both doesn't work and in some cases, actually
849         #               hangs the PCU.  Definitely not what we want.
850         #               
851         #               The code below is much simpler.  Just letting things fail first,
852         #               and then, trying again with authentication string in the header.
853         #               
854         def run(self, node_port, dryrun):
855                 self.transport = None
856                 self.url = "http://%s:%d/" % (self.host,80)
857                 uri = "%s:%d" % (self.host,80)
858
859                 req = urllib2.Request(self.url)
860                 try:
861                         handle = urllib2.urlopen(req)
862                 except IOError, e:
863                         # NOTE: this is expected to fail initially
864                         pass
865                 else:
866                         print self.url
867                         print "-----------"
868                         print handle.read()
869                         print "-----------"
870                         return "ERROR: not protected by HTTP authentication"
871
872                 if not hasattr(e, 'code') or e.code != 401:
873                         return "ERROR: failed for: %s" % str(e)
874
875                 base64data = base64.encodestring("%s:%s" % (self.username, self.password))[:-1]
876                 # NOTE: assuming basic realm authentication.
877                 authheader = "Basic %s" % base64data
878                 req.add_header("Authorization", authheader)
879
880                 try:
881                         f = urllib2.urlopen(req)
882                 except IOError, e:
883                         # failing here means the User/passwd is wrong (hopefully)
884                         raise ExceptionPassword("Incorrect username/password")
885
886                 # NOTE: after verifying that the user/password is correct, 
887                 #               actually reboot the given node.
888                 if not dryrun:
889                         try:
890                                 data = urllib.urlencode({'P%d' % node_port : "r"})
891                                 req = urllib2.Request(self.url + "cmd.html")
892                                 req.add_header("Authorization", authheader)
893                                 # add data to handler,
894                                 f = urllib2.urlopen(req, data)
895                                 if self.verbose: print f.read()
896                         except:
897                                 import traceback; traceback.print_exc()
898
899                                 # fetch url one more time on cmd.html, econtrol.html or whatever.
900                                 # pass
901                 else:
902                         if self.verbose: print f.read()
903
904                 self.close()
905                 return 0
906
907
908 class ePowerSwitchOld(PCUControl):
909         def run(self, node_port, dryrun):
910                 self.url = "http://%s:%d/" % (self.host,80)
911                 uri = "%s:%d" % (self.host,80)
912
913                 # create authinfo
914                 authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm()
915                 authinfo.add_password (None, uri, self.username, self.password)
916                 authhandler = urllib2.HTTPBasicAuthHandler( authinfo )
917
918                 # NOTE: it doesn't seem to matter whether this authinfo is here or not.
919                 transport = urllib2.build_opener(authinfo)
920                 f = transport.open(self.url)
921                 if self.verbose: print f.read()
922
923                 if not dryrun:
924                         transport = urllib2.build_opener(authhandler)
925                         f = transport.open(self.url + "cmd.html", "P%d=r" % node_port)
926                         if self.verbose: print f.read()
927
928                 self.close()
929                 return 0
930
931 class ePowerSwitch(PCUControl):
932         def run(self, node_port, dryrun):
933                 self.url = "http://%s:%d/" % (self.host,80)
934                 uri = "%s:%d" % (self.host,80)
935
936                 # TODO: I'm still not sure what the deal is here.
937                 #               two independent calls appear to need to be made before the
938                 #               reboot will succeed.  It doesn't seem to be possible to do
939                 #               this with a single call.  I have no idea why.
940
941                 # create authinfo
942                 authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm()
943                 authinfo.add_password (None, uri, self.username, self.password)
944                 authhandler = urllib2.HTTPBasicAuthHandler( authinfo )
945
946                 # NOTE: it doesn't seem to matter whether this authinfo is here or not.
947                 transport = urllib2.build_opener()
948                 f = transport.open(self.url + "elogin.html", "pwd=%s" % self.password)
949                 if self.verbose: print f.read()
950
951                 if not dryrun:
952                         transport = urllib2.build_opener(authhandler)
953                         f = transport.open(self.url + "econtrol.html", "P%d=r" % node_port)
954                         if self.verbose: print f.read()
955
956                 #       data= "P%d=r" % node_port
957                 #self.open(self.host, self.username, self.password)
958                 #self.sendHTTP("elogin.html", "pwd=%s" % self.password)
959                 #self.sendHTTP("econtrol.html", data)
960                 #self.sendHTTP("cmd.html", data)
961
962                 self.close()
963                 return 0
964                 
965
966 ### rebooting european BlackBox PSE boxes
967 # Thierry Parmentelat - May 11 2005
968 # tested on 4-ports models known as PSE505-FR
969 # uses http to POST a data 'P<port>=r'
970 # relies on basic authentication within http1.0
971 # first curl-based script was
972 # curl --http1.0 --basic --user <username>:<password> --data P<port>=r \
973 #       http://<hostname>:<http_port>/cmd.html && echo OK
974
975 def bbpse_reboot (pcu_ip,username,password,port_in_pcu,http_port, dryrun):
976
977         global verbose
978
979         url = "http://%s:%d/cmd.html" % (pcu_ip,http_port)
980         data= "P%d=r" % port_in_pcu
981         if verbose:
982                 logger.debug("POSTing '%s' on %s" % (data,url))
983
984         authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm()
985         uri = "%s:%d" % (pcu_ip,http_port)
986         authinfo.add_password (None, uri, username, password)
987         authhandler = urllib2.HTTPBasicAuthHandler( authinfo )
988
989         opener = urllib2.build_opener(authhandler)
990         urllib2.install_opener(opener)
991
992         if (dryrun):
993                 return 0
994
995         try:
996                 f = urllib2.urlopen(url,data)
997
998                 r= f.read()
999                 if verbose:
1000                         logger.debug(r)
1001                 return 0
1002
1003         except urllib2.URLError,err:
1004                 logger.info('Could not open http connection', err)
1005                 return "bbpse error"
1006
1007 ### rebooting x10toggle based systems addressed by port
1008 # Marc E. Fiuczynski - May 31 2005
1009 # tested on 4-ports models known as PSE505-FR
1010 # uses ssh and password to login to an account
1011 # that will cause the system to be powercycled.
1012
1013 def x10toggle_reboot(ip, username, password, port, dryrun):
1014         global verbose
1015
1016         ssh = None
1017         try:
1018                 ssh = pyssh.Ssh(username, ip)
1019                 ssh.open()
1020
1021                 # Login
1022                 telnet_answer(ssh, "password:", password)
1023
1024                 if not dryrun:
1025                         # Reboot
1026                         telnet_answer(ssh, "x10toggle>", "A%d" % port)
1027
1028                 # Close
1029                 output = ssh.close()
1030                 if verbose:
1031                         logger.debug(output)
1032                 return 0
1033
1034         except Exception, err:
1035                 if verbose:
1036                         logger.debug(err)
1037                 if ssh:
1038                         output = ssh.close()
1039                         if verbose:
1040                                 logger.debug(output)
1041                 return errno.ETIMEDOUT
1042
1043 ### rebooting Dell systems via RAC card
1044 # Marc E. Fiuczynski - June 01 2005
1045 # tested with David Lowenthal's itchy/scratchy nodes at UGA
1046 #
1047
1048 def runcmd(command, args, username, password, timeout = None):
1049
1050         result = [None]
1051         result_ready = threading.Condition()
1052
1053         def set_result(x):
1054
1055                 result_ready.acquire()
1056                 try:
1057                         result[0] = x
1058                 finally:
1059                         result_ready.notify()
1060                         result_ready.release()
1061
1062         def do_command(command, username, password):
1063
1064                 try:
1065                         # Popen4 is a popen-type class that combines stdout and stderr
1066                         p = popen2.Popen4(command)
1067
1068                         # read all output data
1069                         p.tochild.write("%s\n" % username)
1070                         p.tochild.write("%s\n" % password)
1071                         p.tochild.close()
1072                         data = p.fromchild.read()
1073
1074                         while True:
1075                                 # might get interrupted by a signal in poll() or waitpid()
1076                                 try:
1077                                         retval = p.wait()
1078                                         set_result((retval, data))
1079                                         break
1080                                 except OSError, ex:
1081                                         if ex.errno == errno.EINTR:
1082                                                 continue
1083                                         raise ex
1084                 except Exception, ex:
1085                         set_result(ex)
1086
1087         if args:
1088                 command = " ".join([command] + args)
1089
1090         worker = threading.Thread(target = do_command, args = (command, username, password, ))
1091         worker.setDaemon(True)
1092         result_ready.acquire()
1093         worker.start()
1094         result_ready.wait(timeout)
1095         try:
1096                 if result == [None]:
1097                         raise Exception, "command timed-out: '%s'" % command
1098         finally:
1099                 result_ready.release()
1100         result = result[0]
1101
1102         if isinstance(result, Exception):
1103                 raise result
1104         else:
1105                 (retval, data) = result
1106                 if os.WIFEXITED(retval) and os.WEXITSTATUS(retval) == 0:
1107                         return data
1108                 else:
1109                         out = "system command ('%s') " % command
1110                         if os.WIFEXITED(retval):
1111                                 out += "failed, rc = %d" % os.WEXITSTATUS(retval)
1112                         else:
1113                                 out += "killed by signal %d" % os.WTERMSIG(retval)
1114                         if data:
1115                                 out += "; output follows:\n" + data
1116                         raise Exception, out
1117
1118 def racadm_reboot(host, username, password, port, dryrun):
1119         global verbose
1120
1121         ip = socket.gethostbyname(host)
1122         try:
1123                 cmd = "/usr/sbin/racadm"
1124                 os.stat(cmd)
1125                 if not dryrun:
1126                         output = runcmd(cmd, ["-r %s -i serveraction powercycle" % ip],
1127                                 username, password)
1128                 else:
1129                         output = runcmd(cmd, ["-r %s -i getsysinfo" % ip],
1130                                 username, password)
1131
1132                 print "RUNCMD: %s" % output
1133                 if verbose:
1134                         logger.debug(output)
1135                 return 0
1136
1137         except Exception, err:
1138                 logger.debug("runcmd raised exception %s" % err)
1139                 if verbose:
1140                         logger.debug(err)
1141                 return -1
1142
1143 def pcu_name(pcu):
1144         if pcu['hostname'] is not None and pcu['hostname'] is not "":
1145                 return pcu['hostname']
1146         elif pcu['ip'] is not None and pcu['ip'] is not "":
1147                 return pcu['ip']
1148         else:
1149                 return None
1150
1151 import soltesz
1152 fb =soltesz.dbLoad("findbadpcus")
1153
1154 def get_pcu_values(pcu_id):
1155         # TODO: obviously, this shouldn't be loaded each time...
1156
1157         try:
1158                 values = fb['nodes']["id_%s" % pcu_id]['values']
1159         except:
1160                 values = None
1161
1162         return values
1163
1164 def reboot(nodename):
1165         return reboot_policy(nodename, True, False)
1166         
1167 def reboot_policy(nodename, continue_probe, dryrun):
1168         global verbose
1169
1170         pcu = plc.getpcu(nodename)
1171         if not pcu:
1172                 logger.debug("no pcu for %s" % hostname)
1173                 print "no pcu for %s" % hostname
1174                 return False # "%s has no pcu" % nodename
1175
1176         values = get_pcu_values(pcu['pcu_id'])
1177         if values == None:
1178                 logger.debug("No values for pcu probe %s" % hostname)
1179                 print "No values for pcu probe %s" % hostname
1180                 return False #"no info for pcu_id %s" % pcu['pcu_id']
1181         
1182         # Try the PCU first
1183         logger.debug("Trying PCU %s %s" % (pcu['hostname'], pcu['model']))
1184
1185         ret = reboot_test(nodename, values, continue_probe, verbose, dryrun)
1186
1187         if ret != 0:
1188                 print ret
1189                 return False
1190         else:
1191                 print "return true"
1192                 return True
1193
1194 def reboot_test(nodename, values, continue_probe, verbose, dryrun):
1195         rb_ret = ""
1196
1197         try:
1198                 # DataProbe iPal (many sites)
1199                 if  continue_probe and values['model'].find("IP-41x_IP-81x") >= 0:
1200                         ipal = IPAL(values, verbose, ['23', '80', '9100'])
1201                         rb_ret = ipal.reboot(values[nodename], dryrun)
1202                                 
1203                 # APC Masterswitch (Berkeley)
1204                 elif continue_probe and ( values['model'].find("AP79xx") >= 0 or \
1205                                                                   values['model'].find("Masterswitch") >= 0 ):
1206                         print values
1207
1208                         # TODO: make a more robust version of APC
1209                         if values['pcu_id'] in [1163,1055,1111,1231,1113,1127,1128,1148]:
1210                                 apc = APCEurope(values, verbose, ['22', '23'])
1211                                 rb_ret = apc.reboot(values[nodename], dryrun)
1212
1213                         elif values['pcu_id'] in [1110,86]:
1214                                 apc = APCBrazil(values, verbose, ['22', '23'])
1215                                 rb_ret = apc.reboot(values[nodename], dryrun)
1216
1217                         elif values['pcu_id'] in [1221,1225]:
1218                                 apc = APCBerlin(values, verbose, ['22', '23'])
1219                                 rb_ret = apc.reboot(values[nodename], dryrun)
1220
1221                         elif values['pcu_id'] in [1173,1221,1220]:
1222                                 apc = APCFolsom(values, verbose, ['22', '23'])
1223                                 rb_ret = apc.reboot(values[nodename], dryrun)
1224
1225                         else:
1226                                 apc = APCMaster(values, verbose, ['22', '23'])
1227                                 rb_ret = apc.reboot(values[nodename], dryrun)
1228
1229                 # BayTech DS4-RPC
1230                 elif continue_probe and values['model'].find("DS4-RPC") >= 0:
1231                         if values['pcu_id'] in [1237,1052,1209,1002,1008,1041,1013,1022]:
1232                                 # These  require a 'ctrl-c' to be sent... 
1233                                 baytech = BayTechCtrlC(values, verbose, ['22', '23'])
1234                                 rb_ret = baytech.reboot(values[nodename], dryrun)
1235
1236                         elif values['pcu_id'] in [93]:
1237                                 baytech = BayTechAU(values, verbose, ['22', '23'])
1238                                 rb_ret = baytech.reboot(values[nodename], dryrun)
1239
1240                         elif values['pcu_id'] in [1057]:
1241                                 # These  require a 'ctrl-c' to be sent... 
1242                                 baytech = BayTechCtrlCUnibe(values, verbose, ['22', '23'])
1243                                 rb_ret = baytech.reboot(values[nodename], dryrun)
1244
1245                         elif values['pcu_id'] in [1012]:
1246                                 # This pcu sometimes doesn't present the 'Username' prompt,
1247                                 # unless you immediately try again...
1248                                 try:
1249                                         baytech = BayTechGeorgeTown(values, verbose, ['22', '23'])
1250                                         rb_ret = baytech.reboot(values[nodename], dryrun)
1251                                 except:
1252                                         baytech = BayTechGeorgeTown(values, verbose, ['22', '23'])
1253                                         rb_ret = baytech.reboot(values[nodename], dryrun)
1254                         else:
1255                                 baytech = BayTech(values, verbose, ['22', '23'])
1256                                 rb_ret = baytech.reboot(values[nodename], dryrun)
1257
1258                 # iLO
1259                 elif continue_probe and values['model'].find("ilo") >= 0:
1260                         try:
1261                                 hpilo = HPiLO(values, verbose, ['22'])
1262                                 rb_ret = hpilo.reboot(0, dryrun)
1263                                 if rb_ret != 0:
1264                                         hpilo = HPiLOHttps(values, verbose, ['443'])
1265                                         rb_ret = hpilo.reboot(0, dryrun)
1266                         except:
1267                                 hpilo = HPiLOHttps(values, verbose, ['443'])
1268                                 rb_ret = hpilo.reboot(0, dryrun)
1269
1270                 # DRAC ssh
1271                 elif continue_probe and values['model'].find("DRAC") >= 0:
1272                         # TODO: I don't think DRACRacAdm will throw an exception for the
1273                         # default method to catch...
1274                         try:
1275                                 drac = DRACRacAdm(values, verbose, ['443', '5869'])
1276                                 rb_ret = drac.reboot(0, dryrun)
1277                         except:
1278                                 drac = DRAC(values, verbose, ['22'])
1279                                 rb_ret = drac.reboot(0, dryrun)
1280
1281                 elif continue_probe and values['model'].find("WTI IPS-4") >= 0:
1282                                 wti = WTIIPS4(values, verbose, ['23'])
1283                                 rb_ret = wti.reboot(values[nodename], dryrun)
1284
1285                 elif continue_probe and values['model'].find("AMT") >= 0:
1286                                 amt = IntelAMT(values, verbose, ['16992'])
1287                                 rb_ret = amt.reboot(values[nodename], dryrun)
1288
1289                 # BlackBox PSExxx-xx (e.g. PSE505-FR)
1290                 elif continue_probe and values['model'].find("ePowerSwitch") >=0:
1291                         # TODO: allow a different port than http 80.
1292                         if values['pcu_id'] in [1089, 1071, 1046, 1035, 1118]:
1293                                 eps = ePowerSwitchGood(values, verbose, ['80'])
1294                         elif values['pcu_id'] in [1003]:
1295                                 # OLD EPOWER
1296                                 print "OLD EPOWER"
1297                                 eps = ePowerSwitch(values, verbose, ['80'])
1298                         else:
1299                                 eps = ePowerSwitchGood(values, verbose, ['80'])
1300
1301                         rb_ret = eps.reboot(values[nodename], dryrun)
1302
1303                 elif continue_probe:
1304                         rb_ret = "Unsupported_PCU"
1305
1306                 elif continue_probe == False:
1307                         if 'portstatus' in values:
1308                                 rb_ret = "NetDown"
1309                         else:
1310                                 rb_ret = "Not_Run"
1311                 else:
1312                         rb_ret = -1
1313
1314         except ExceptionPort, err:
1315                 rb_ret = str(err)
1316
1317         return rb_ret
1318         # ????
1319         #elif continue_probe and values['protocol'] == "racadm" and \
1320         #               values['model'] == "RAC":
1321         #       rb_ret = racadm_reboot(pcu_name(values),
1322         #                                                                 values['username'],
1323         #                                                                 values['password'],
1324         #                                                                 pcu[nodename],
1325         #                                                                 dryrun)
1326
1327 def main():
1328         logger.setLevel(logging.DEBUG)
1329         ch = logging.StreamHandler()
1330         ch.setLevel(logging.DEBUG)
1331         formatter = logging.Formatter('LOGGER - %(message)s')
1332         ch.setFormatter(formatter)
1333         logger.addHandler(ch)
1334
1335         try:
1336                 if "test" in sys.argv:
1337                         dryrun = True
1338                 else:
1339                         dryrun = False
1340
1341                 for node in sys.argv[1:]:
1342                         if node == "test": continue
1343
1344                         print "Rebooting %s" % node
1345                         if reboot_policy(node, True, dryrun):
1346                                 print "success"
1347                         else:
1348                                 print "failed"
1349         except Exception, err:
1350                 import traceback; traceback.print_exc()
1351                 print err
1352
1353 if __name__ == '__main__':
1354         import plc
1355         logger = logging.getLogger("monitor")
1356         main()