Beispiel #1
0
    def _lock(self):
        self.info('Acquiring lock %s' % (LOCK_PATH))

        self.apc_lock = FilesystemLock(LOCK_PATH)

        count = 0
        while not self.apc_lock.lock():
            time.sleep(1)
            count += 1
            if count >= LOCK_TIMEOUT:
                raise SystemError('Cannot acquire %s\n' % (LOCK_PATH))
Beispiel #2
0
    def _lock(self):
        self.info('Acquiring lock %s' % (LOCK_PATH))

        self.apc_lock = FilesystemLock(LOCK_PATH)

        count = 0
        while not self.apc_lock.lock():
            time.sleep(1)
            count += 1
            if count >= LOCK_TIMEOUT:
                raise SystemError('Cannot acquire %s\n' % (LOCK_PATH))
Beispiel #3
0
class APC:
    def __init__(self, options):
        self.host = options.host
        self.user = options.user
        self.password = options.password
        self.verbose = options.verbose
        self.quiet = options.quiet
        self.connect()

    def info(self, msg):
        if not self.quiet:
            print msg

    def notify(self, outlet_name, state):
        print 'APC %s: %s %s' % (self.host, outlet_name, state)

    def sendnl(self, a):
        self.child.send(a + '\r\n')
        if self.verbose:
            print self.child.before

    def _lock(self):
        self.info('Acquiring lock %s' % (LOCK_PATH))

        self.apc_lock = FilesystemLock(LOCK_PATH)

        count = 0
        while not self.apc_lock.lock():
            time.sleep(1)
            count += 1
            if count >= LOCK_TIMEOUT:
                raise SystemError('Cannot acquire %s\n' % (LOCK_PATH))

    def _unlock(self):
        self.apc_lock.unlock()

    def connect(self):
        self._lock()

        self.info('Connecting to APC @ %s' % self.host)
        self.child = pexpect.spawn('telnet %s' % self.host)

        self.child.timeout = 10
        self.child.setecho(True)

        self.child.expect('User Name : ')
        self.child.send(self.user + '\r\n')
        self.child.before
        self.child.expect('Password  : '******'\r\n')

        self.child.expect('Communication Established')

        header = self.child.before

        match = APC_VERSION_PATTERN.search(header)

        if not match:
            raise Exception, 'Could not parse APC version'

        self.version = match.group(1)
        self.is_new_version = (self.version[0] == '3')

        self.info('Logged in as user %s, version %s' % (self.user, self.version))

    def get_outlet(self, outlet):
        if str(outlet) in ['*', '+', '9']:
            return (9, 'ALL outlets')
        else:
            # Assume integer outlet
            try:
                outlet = int(outlet)
                return (outlet, 'Outlet #%d' % outlet)

            except:
                raise SystemExit('Bad outlet: [%s]' % outlet)

    def configure_outlet(self, outlet):
        if self.is_new_version:
            self.sendnl('1')
            self.sendnl('2')
            self.sendnl('1')
            self.sendnl(str(outlet))

            self.sendnl('1')

        else:
            self.sendnl('1')
            self.sendnl('1')
            self.sendnl(str(outlet))

            self.sendnl('1')

        self.child.before

    def get_command_result(self):
        if self.is_new_version:
            self.child.expect('Command successfully issued')
        else:
            self.child.expect('Outlet State')

    def _escape_to_main(self):
        for i in range(6):
            self.child.send(APC_ESCAPE)

    def reboot(self, outlet):
        (outlet, outlet_name) = self.get_outlet(outlet)

        self.configure_outlet(outlet)

        self.sendnl(APC_IMMEDIATE_REBOOT[self.is_new_version])

        self.child.expect('Immediate Reboot')
        self.sendnl(APC_YES)
        self.sendnl('')

        self.get_command_result()

        self.notify(outlet_name, 'Rebooted')

        self._escape_to_main()

    def on_off(self, outlet, on):
        (outlet, outlet_name) = self.get_outlet(outlet)

        self.configure_outlet(outlet)

        if on:
            cmd = APC_IMMEDIATE_ON[self.is_new_version]
            str_cmd = 'On'
        else:
            cmd = APC_IMMEDIATE_OFF[self.is_new_version]
            str_cmd = 'Off'

        self.sendnl(cmd)
        self.sendnl(APC_YES)
        self.sendnl('')

        self.get_command_result()

        self.notify(outlet_name, str_cmd)

        self._escape_to_main()

    def on(self, outlet):
        self.on_off(outlet, True)

    def off(self, outlet):
        self.on_off(outlet, False)

    def debug(self):
        self.child.interact()

    def disconnect(self):
        #self._escape_to_main()

        self.sendnl(APC_LOGOUT)
        self.child.sendeof()
        if not self.quiet:
            print 'DISCONNECTED from %s' % self.host

        if self.verbose:
            print '[%s]' % ''.join(self.child.readlines())

        self.child.close()
        self._unlock()
Beispiel #4
0
class APC:
    def __init__(self, options):
        self.host = options.host
        self.user = options.user
        self.password = options.password
        self.verbose = options.verbose
        self.quiet = options.quiet
        self.connect()

    def info(self, msg):
        if not self.quiet:
            print msg

    def notify(self, outlet_name, state):
        print 'APC %s: %s %s' % (self.host, outlet_name, state)

    def sendnl(self, a):
        self.child.send(a + '\r\n')
        if self.verbose:
            print self.child.before

    def _lock(self):
        self.info('Acquiring lock %s' % (LOCK_PATH))

        self.apc_lock = FilesystemLock(LOCK_PATH)

        count = 0
        while not self.apc_lock.lock():
            time.sleep(1)
            count += 1
            if count >= LOCK_TIMEOUT:
                raise SystemError('Cannot acquire %s\n' % (LOCK_PATH))

    def _unlock(self):
        self.apc_lock.unlock()

    def connect(self):
        self._lock()

        self.info('Connecting to APC @ %s' % self.host)
        self.child = pexpect.spawn('telnet %s' % self.host)

        self.child.timeout = 10
        self.child.setecho(True)

        self.child.expect('User Name : ')
        self.child.send(self.user + '\r\n')
        self.child.before
        self.child.expect('Password  : '******'\r\n')

        self.child.expect('Communication Established')

        header = self.child.before

        match = APC_VERSION_PATTERN.search(header)

        if not match:
            raise Exception, 'Could not parse APC version'

        self.version = match.group(1)
        self.is_new_version = (self.version[0] == '3')

        self.info('Logged in as user %s, version %s' %
                  (self.user, self.version))

    def get_outlet(self, outlet):
        if str(outlet) in ['*', '+', '9']:
            return (9, 'ALL outlets')
        else:
            # Assume integer outlet
            try:
                outlet = int(outlet)
                return (outlet, 'Outlet #%d' % outlet)

            except:
                raise SystemExit('Bad outlet: [%s]' % outlet)

    def configure_outlet(self, outlet):
        if self.is_new_version:
            self.sendnl('1')
            self.sendnl('2')
            self.sendnl('1')
            self.sendnl(str(outlet))

            self.sendnl('1')

        else:
            self.sendnl('1')
            self.sendnl('1')
            self.sendnl(str(outlet))

            self.sendnl('1')

        self.child.before

    def get_command_result(self):
        if self.is_new_version:
            self.child.expect('Command successfully issued')
        else:
            self.child.expect('Outlet State')

    def _escape_to_main(self):
        for i in range(6):
            self.child.send(APC_ESCAPE)

    def reboot(self, outlet):
        (outlet, outlet_name) = self.get_outlet(outlet)

        self.configure_outlet(outlet)

        self.sendnl(APC_IMMEDIATE_REBOOT[self.is_new_version])

        self.child.expect('Immediate Reboot')
        self.sendnl(APC_YES)
        self.sendnl('')

        self.get_command_result()

        self.notify(outlet_name, 'Rebooted')

        self._escape_to_main()

    def on_off(self, outlet, on):
        (outlet, outlet_name) = self.get_outlet(outlet)

        self.configure_outlet(outlet)

        if on:
            cmd = APC_IMMEDIATE_ON[self.is_new_version]
            str_cmd = 'On'
        else:
            cmd = APC_IMMEDIATE_OFF[self.is_new_version]
            str_cmd = 'Off'

        self.sendnl(cmd)
        self.sendnl(APC_YES)
        self.sendnl('')

        self.get_command_result()

        self.notify(outlet_name, str_cmd)

        self._escape_to_main()

    def on(self, outlet):
        self.on_off(outlet, True)

    def off(self, outlet):
        self.on_off(outlet, False)

    def debug(self):
        self.child.interact()

    def disconnect(self):
        #self._escape_to_main()

        self.sendnl(APC_LOGOUT)
        self.child.sendeof()
        if not self.quiet:
            print 'DISCONNECTED from %s' % self.host

        if self.verbose:
            print '[%s]' % ''.join(self.child.readlines())

        self.child.close()
        self._unlock()
Beispiel #5
0
class APC:
    def __init__(self, options):
        self.host = options.host
        self.user = options.user
        self.password = options.password
        self.verbose = options.verbose
        self.quiet = options.quiet
        self.connect()

    def info(self, msg):
        if not self.quiet:
            print(msg)

    def notify(self, outlet_name, state):
        print('APC %s: %s %s' % (self.host, outlet_name, state))

    def sendnl(self, a):
        self.child.send(a + '\r')
        if self.verbose:
            print(self.child.before)

    def _lock(self):
        self.info('Acquiring lock %s' % (LOCK_PATH))

        self.apc_lock = FilesystemLock(LOCK_PATH)

        count = 0
        while not self.apc_lock.lock():
            time.sleep(1)
            count += 1
            if count >= LOCK_TIMEOUT:
                raise SystemError('Cannot acquire %s\n' % (LOCK_PATH))

    def _unlock(self):
        self.apc_lock.unlock()

    def connect(self):
        self._lock()

        self.info('Connecting to APC @ %s' % self.host)
        self.child = pexpect.spawn('telnet %s' % self.host)

        self.child.timeout = 10
        self.child.setecho(True)

        self.child.expect('User Name : ')
        time.sleep(1)
        self.child.send(self.user + '\r')
        time.sleep(1)
        self.child.expect('Password  : '******'\r')
        self.child.send('\r\n')
        time.sleep(2)
        self.child.expect('apc>')
        header = self.child.before
        match = APC_VERSION_PATTERN.search(str(header))

    def get_outlet(self, outlet):
        if str(outlet) in ['*', '+', 'all']:
            return ('all', 'ALL outlets')
        else:
            # Assume integer outlet
            try:
                outlet = int(outlet)
                return (outlet, 'Outlet #%d' % outlet)

            except:
                raise SystemExit('Bad outlet: [%s]' % outlet)

    def get_command_result(self):
        self.child.expect('E000: Success')
        print(self.child.after)

    def get_result(self, outlet):
        try:
            self.child.logfile = sys.stdout
            self.child.expect('%d:' % outlet)
        except:
            self.child.expect('E102:')
            raise SystemExit('Bad outlet: [%s]' % outlet)

    def _escape_to_main(self):
        for i in range(6):
            self.child.send(APC_ESCAPE)

    def delay(self, outlet):
        #if secs in range(5, 61):
        cmd1 = 'olRbootTime %d 10' % outlet
        self.sendnl(cmd1)
        #else:
        #print("Enter time delay in seconds between 5 and 60")
        #raise SystemExit(1)

    def reboot(self, outlet):
        (outlet, outlet_name) = self.get_outlet(outlet)

        cmd = 'olReboot %d' % outlet

        self.delay(outlet)

        self.sendnl(cmd)

        self.get_command_result()

        self.notify(outlet_name, 'Rebooted')

    def on_off(self, outlet, on):
        (outlet, outlet_name) = self.get_outlet(outlet)

        if on:
            cmd = 'olOn %d' % outlet
            str_cmd = 'On'
        else:
            cmd = 'olOff %d' % outlet
            str_cmd = 'Off'

        self.sendnl(cmd)

        self.get_command_result()

        self.notify(outlet_name, str_cmd)

    def get(self, outlet):
        (outlet, outlet_name) = self.get_outlet(outlet)

        if outlet == 'all':
            cmd = 'olStatus %s' % outlet
            self.child.logfile = sys.stdout
            self.child.send(cmd + '\r')
            time.sleep(2)
            self.child.expect('apc>')
            print(self.child.readline())
        else:
            cmd = 'olStatus %d' % outlet
            self.sendnl(cmd)
            self.get_result(outlet)

    def on(self, outlet):
        self.on_off(outlet, True)

    def off(self, outlet):
        self.on_off(outlet, False)

    def debug(self):
        self.child.interact()

    def disconnect(self):
        # self._escape_to_main()

        self.sendnl(APC_LOGOUT)
        self.child.sendeof()
        if not self.quiet:
            print('DISCONNECTED from %s' % self.host)

        if self.verbose:
            print('[%s]' % ''.join(self.child.readlines()))

        self.child.close()
        self._unlock()