www/database.php
[monitor.git] / reboot.py
index 6a3b9bb..337b0b4 100755 (executable)
--- a/reboot.py
+++ b/reboot.py
@@ -8,16 +8,16 @@ import os, sys
 import xml, xmlrpclib
 import errno, time, traceback
 import urllib2
 import xml, xmlrpclib
 import errno, time, traceback
 import urllib2
+import urllib
 import threading, popen2
 import array, struct
 import threading, popen2
 import array, struct
-#from socket import *
-import socket
 import plc
 import base64
 from subprocess import PIPE, Popen
 import ssh.pxssh as pxssh
 import ssh.pexpect as pexpect
 import socket
 import plc
 import base64
 from subprocess import PIPE, Popen
 import ssh.pxssh as pxssh
 import ssh.pexpect as pexpect
 import socket
+import moncommands 
 
 # Use our versions of telnetlib and pyssh
 sys.path.insert(0, os.path.dirname(sys.argv[0]))
 
 # Use our versions of telnetlib and pyssh
 sys.path.insert(0, os.path.dirname(sys.argv[0]))
@@ -296,7 +296,7 @@ class IPAL(PCUControl):
                        ret = s.recv(count, socket.MSG_DONTWAIT)
                except socket.error, e:
                        if e[0] == errno.EAGAIN:
                        ret = s.recv(count, socket.MSG_DONTWAIT)
                except socket.error, e:
                        if e[0] == errno.EAGAIN:
-                               return Exception(e[1])
+                               raise Exception(e[1])
                        else:
                                # TODO: not other exceptions.
                                raise Exception(e)
                        else:
                                # TODO: not other exceptions.
                                raise Exception(e)
@@ -316,7 +316,7 @@ class IPAL(PCUControl):
                        s.close()
                        if e[0] == errno.ECONNREFUSED:
                                # cannot connect to remote host
                        s.close()
                        if e[0] == errno.ECONNREFUSED:
                                # cannot connect to remote host
-                               return Exception(e[1])
+                               raise Exception(e[1])
                        else:
                                # TODO: what other conditions are there?
                                raise Exception(e)
                        else:
                                # TODO: what other conditions are there?
                                raise Exception(e)
@@ -326,6 +326,10 @@ class IPAL(PCUControl):
                s.send(self.format_msg("", 'O'))
                ret = self.recv_noblock(s, 8)
                print "Current status is '%s'" % ret
                s.send(self.format_msg("", 'O'))
                ret = self.recv_noblock(s, 8)
                print "Current status is '%s'" % ret
+
+               if ret == '':
+                       raise Exception("Status returned 'another session already open' %s : %s" % (node_port, ret))
+                       
                                
                if node_port < len(ret):
                        status = ret[node_port]
                                
                if node_port < len(ret):
                        status = ret[node_port]
@@ -342,14 +346,14 @@ class IPAL(PCUControl):
                        
 
                if not dryrun:
                        
 
                if not dryrun:
-                       print "Pulsing %s" % node_port
                        if power_on:
                        if power_on:
+                               print "Pulsing %s" % node_port
                                s.send(self.format_msg("%s" % node_port, 'P'))
                        else:
                                s.send(self.format_msg("%s" % node_port, 'P'))
                        else:
-                               # NOTE: turn power on before pulsing the port.
-                               print "power was off, so turning on then pulsing..."
+                               # NOTE: turn power on ; do not pulse the port.
+                               print "Power was off, so turning on ..."
                                s.send(self.format_msg("%s" % node_port, 'E'))
                                s.send(self.format_msg("%s" % node_port, 'E'))
-                               s.send(self.format_msg("%s" % node_port, 'P'))
+                               #s.send(self.format_msg("%s" % node_port, 'P'))
 
                        print "Receiving response."
                        ret = self.recv_noblock(s, 8)
 
                        print "Receiving response."
                        ret = self.recv_noblock(s, 8)
@@ -554,10 +558,10 @@ class APC(PCUControl):
 
 class IntelAMT(PCUControl):
        def run(self, node_port, dryrun):
 
 class IntelAMT(PCUControl):
        def run(self, node_port, dryrun):
-               import soltesz
 
 
-               cmd = soltesz.CMD()
-               cmd_str = "IntelAMTSDK/Samples/RemoteControl/remoteControl"
+               cmd = moncommands.CMD()
+               #[cmd_str = "IntelAMTSDK/Samples/RemoteControl/remoteControl"
+               cmd_str = "cmdamt/remoteControl"
 
                if dryrun:
                        # NOTE: -p checks the power state of the host.
 
                if dryrun:
                        # NOTE: -p checks the power state of the host.
@@ -619,9 +623,8 @@ class HPiLO(PCUControl):
                
 class HPiLOHttps(PCUControl):
        def run(self, node_port, dryrun):
                
 class HPiLOHttps(PCUControl):
        def run(self, node_port, dryrun):
-               import soltesz
 
 
-               locfg = soltesz.CMD()
+               locfg = moncommands.CMD()
                cmd = "cmdhttps/locfg.pl -s %s -f %s -u %s -p '%s' | grep 'MESSAGE' | grep -v 'No error'" % (
                                        self.host, "iloxml/Get_Network.xml", 
                                        self.username, self.password)
                cmd = "cmdhttps/locfg.pl -s %s -f %s -u %s -p '%s' | grep 'MESSAGE' | grep -v 'No error'" % (
                                        self.host, "iloxml/Get_Network.xml", 
                                        self.username, self.password)
@@ -632,7 +635,7 @@ class HPiLOHttps(PCUControl):
                        return sout.strip()
 
                if not dryrun:
                        return sout.strip()
 
                if not dryrun:
-                       locfg = soltesz.CMD()
+                       locfg = moncommands.CMD()
                        cmd = "cmdhttps/locfg.pl -s %s -f %s -u %s -p '%s' | grep 'MESSAGE' | grep -v 'No error'" % (
                                                self.host, "iloxml/Reset_Server.xml", 
                                                self.username, self.password)
                        cmd = "cmdhttps/locfg.pl -s %s -f %s -u %s -p '%s' | grep 'MESSAGE' | grep -v 'No error'" % (
                                                self.host, "iloxml/Reset_Server.xml", 
                                                self.username, self.password)
@@ -701,7 +704,9 @@ class BayTechCtrlCUnibe(PCUControl):
 
                # Control Outlets  (5 ,1).........5
                try:
 
                # Control Outlets  (5 ,1).........5
                try:
-                       index = s.expect(["Enter Request :"])
+                       print s
+                       print "Enter Request" in s.before
+                       index = s.expect("Enter Request")
 
                        if index == 0:
                                print "3"
 
                        if index == 0:
                                print "3"
@@ -781,7 +786,10 @@ class BayTechCtrlC(PCUControl):
                                                        print "sending Y"
                                                        s.send("Y\r\n")
 
                                                        print "sending Y"
                                                        s.send("Y\r\n")
 
-                               index = s.expect(["DS-RPC>"])
+                               # NOTE: for some reason, the script times out with the
+                               # following line.  In manual tests, it works correctly, but
+                               # with automated tests, evidently it fails.
+                               #index = s.expect(["DS-RPC>"])
                                #print "got prompt back"
 
                        s.close()
                                #print "got prompt back"
 
                        s.close()
@@ -878,19 +886,41 @@ class ePowerSwitchGood(PCUControl):
                        # failing here means the User/passwd is wrong (hopefully)
                        raise ExceptionPassword("Incorrect username/password")
 
                        # failing here means the User/passwd is wrong (hopefully)
                        raise ExceptionPassword("Incorrect username/password")
 
-               # TODO: after verifying that the user/password is correct, we should
-               # actually reboot the given node.
-
+               # NOTE: after verifying that the user/password is correct, 
+               #               actually reboot the given node.
                if not dryrun:
                if not dryrun:
-                       # add data to handler,
-                       # fetch url one more time on cmd.html, econtrol.html or whatever.
-                       pass
+                       try:
+                               data = urllib.urlencode({'P%d' % node_port : "r"})
+                               req = urllib2.Request(self.url + "cmd.html")
+                               req.add_header("Authorization", authheader)
+                               # add data to handler,
+                               f = urllib2.urlopen(req, data)
+                               if self.verbose: print f.read()
+                       except:
+                               import traceback; traceback.print_exc()
 
 
-               if self.verbose: print f.read()
+                               # fetch url one more time on cmd.html, econtrol.html or whatever.
+                               # pass
+               else:
+                       if self.verbose: print f.read()
 
                self.close()
                return 0
 
 
                self.close()
                return 0
 
+class CustomPCU(PCUControl):
+       def run(self, node_port, dryrun):
+               url = "https://www-itec.uni-klu.ac.at/plab-pcu/index.php" 
+
+               if not dryrun:
+                       # Turn host off, then on
+                       formstr = "plab%s=off" % node_port
+                       os.system("curl --user %s:%s --form '%s' --insecure %s" % (self.username, self.password, formstr, url))
+                       time.sleep(5)
+                       formstr = "plab%s=on" % node_port
+                       os.system("curl --user %s:%s --form '%s' --insecure %s" % (self.username, self.password, formstr, url))
+               else:
+                       os.system("curl --user %s:%s --insecure %s" % (self.username, self.password, url))
+
 
 class ePowerSwitchOld(PCUControl):
        def run(self, node_port, dryrun):
 
 class ePowerSwitchOld(PCUControl):
        def run(self, node_port, dryrun):
@@ -1135,12 +1165,16 @@ def pcu_name(pcu):
        else:
                return None
 
        else:
                return None
 
-import soltesz
-fb =soltesz.dbLoad("findbadpcus")
+#import database
+from monitor import database
+fb = None
 
 def get_pcu_values(pcu_id):
 
 def get_pcu_values(pcu_id):
-       # TODO: obviously, this shouldn't be loaded each time...
-
+       global fb
+       if fb == None:
+               # this shouldn't be loaded each time...
+               fb = database.dbLoad("findbadpcus")
+               
        try:
                values = fb['nodes']["id_%s" % pcu_id]['values']
        except:
        try:
                values = fb['nodes']["id_%s" % pcu_id]['values']
        except:
@@ -1183,12 +1217,13 @@ def reboot_test(nodename, values, continue_probe, verbose, dryrun):
 
        try:
                # DataProbe iPal (many sites)
 
        try:
                # DataProbe iPal (many sites)
-               if  continue_probe and values['model'].find("Dataprobe IP-41x/IP-81x") >= 0:
+               if  continue_probe and values['model'].find("IP-41x_IP-81x") >= 0:
                        ipal = IPAL(values, verbose, ['23', '80', '9100'])
                        rb_ret = ipal.reboot(values[nodename], dryrun)
                                
                # APC Masterswitch (Berkeley)
                        ipal = IPAL(values, verbose, ['23', '80', '9100'])
                        rb_ret = ipal.reboot(values[nodename], dryrun)
                                
                # APC Masterswitch (Berkeley)
-               elif continue_probe and values['model'].find("APC AP79xx/Masterswitch") >= 0:
+               elif continue_probe and ( values['model'].find("AP79xx") >= 0 or \
+                                                                 values['model'].find("Masterswitch") >= 0 ):
                        print values
 
                        # TODO: make a more robust version of APC
                        print values
 
                        # TODO: make a more robust version of APC
@@ -1213,7 +1248,7 @@ def reboot_test(nodename, values, continue_probe, verbose, dryrun):
                                rb_ret = apc.reboot(values[nodename], dryrun)
 
                # BayTech DS4-RPC
                                rb_ret = apc.reboot(values[nodename], dryrun)
 
                # BayTech DS4-RPC
-               elif continue_probe and values['model'].find("Baytech DS4-RPC") >= 0:
+               elif continue_probe and values['model'].find("DS4-RPC") >= 0:
                        if values['pcu_id'] in [1237,1052,1209,1002,1008,1041,1013,1022]:
                                # These  require a 'ctrl-c' to be sent... 
                                baytech = BayTechCtrlC(values, verbose, ['22', '23'])
                        if values['pcu_id'] in [1237,1052,1209,1002,1008,1041,1013,1022]:
                                # These  require a 'ctrl-c' to be sent... 
                                baytech = BayTechCtrlC(values, verbose, ['22', '23'])
@@ -1242,7 +1277,7 @@ def reboot_test(nodename, values, continue_probe, verbose, dryrun):
                                rb_ret = baytech.reboot(values[nodename], dryrun)
 
                # iLO
                                rb_ret = baytech.reboot(values[nodename], dryrun)
 
                # iLO
-               elif continue_probe and values['model'].find("HP iLO") >= 0:
+               elif continue_probe and values['model'].find("ilo") >= 0:
                        try:
                                hpilo = HPiLO(values, verbose, ['22'])
                                rb_ret = hpilo.reboot(0, dryrun)
                        try:
                                hpilo = HPiLO(values, verbose, ['22'])
                                rb_ret = hpilo.reboot(0, dryrun)
@@ -1254,7 +1289,7 @@ def reboot_test(nodename, values, continue_probe, verbose, dryrun):
                                rb_ret = hpilo.reboot(0, dryrun)
 
                # DRAC ssh
                                rb_ret = hpilo.reboot(0, dryrun)
 
                # DRAC ssh
-               elif continue_probe and values['model'].find("Dell RAC") >= 0:
+               elif continue_probe and values['model'].find("DRAC") >= 0:
                        # TODO: I don't think DRACRacAdm will throw an exception for the
                        # default method to catch...
                        try:
                        # TODO: I don't think DRACRacAdm will throw an exception for the
                        # default method to catch...
                        try:
@@ -1268,24 +1303,26 @@ def reboot_test(nodename, values, continue_probe, verbose, dryrun):
                                wti = WTIIPS4(values, verbose, ['23'])
                                rb_ret = wti.reboot(values[nodename], dryrun)
 
                                wti = WTIIPS4(values, verbose, ['23'])
                                rb_ret = wti.reboot(values[nodename], dryrun)
 
-               elif continue_probe and values['model'].find("Intel AMT") >= 0:
+               elif continue_probe and values['model'].find("AMT") >= 0:
                                amt = IntelAMT(values, verbose, ['16992'])
                                rb_ret = amt.reboot(values[nodename], dryrun)
 
                # BlackBox PSExxx-xx (e.g. PSE505-FR)
                                amt = IntelAMT(values, verbose, ['16992'])
                                rb_ret = amt.reboot(values[nodename], dryrun)
 
                # BlackBox PSExxx-xx (e.g. PSE505-FR)
-               elif continue_probe and \
-                       (values['model'].find("BlackBox PS5xx") >= 0 or
-                        values['model'].find("ePowerSwitch 1/4/8x") >=0 ):
-
+               elif continue_probe and values['model'].find("ePowerSwitch") >=0:
                        # TODO: allow a different port than http 80.
                        if values['pcu_id'] in [1089, 1071, 1046, 1035, 1118]:
                                eps = ePowerSwitchGood(values, verbose, ['80'])
                        elif values['pcu_id'] in [1003]:
                        # TODO: allow a different port than http 80.
                        if values['pcu_id'] in [1089, 1071, 1046, 1035, 1118]:
                                eps = ePowerSwitchGood(values, verbose, ['80'])
                        elif values['pcu_id'] in [1003]:
+                               # OLD EPOWER
+                               print "OLD EPOWER"
                                eps = ePowerSwitch(values, verbose, ['80'])
                        else:
                                eps = ePowerSwitchGood(values, verbose, ['80'])
 
                        rb_ret = eps.reboot(values[nodename], dryrun)
                                eps = ePowerSwitch(values, verbose, ['80'])
                        else:
                                eps = ePowerSwitchGood(values, verbose, ['80'])
 
                        rb_ret = eps.reboot(values[nodename], dryrun)
+               elif continue_probe and values['pcu_id'] in [1122]:
+                       custom = CustomPCU(values, verbose, ['80', '443'])
+                       custom.reboot(values[nodename], dryrun)
 
                elif continue_probe:
                        rb_ret = "Unsupported_PCU"
 
                elif continue_probe:
                        rb_ret = "Unsupported_PCU"