def main_loop(self): rlist = [] rlist.append(self.__pipe.inform) timeout = 10 try: while self.__running: readable, _, _ = select.select(rlist, [], [], timeout) if not readable: continue if self.__pipe.inform in readable: try: message = self.__pipe.read(256) except OSError, exc: logger.warn("[Error %d] appeared at reading pipe" % exc.errno) continue if len(message) == 0: continue self.handle_message(message) except KeyboardInterrupt: logger.error("Break by user.") except Exception, ex: logger.error("{0}: {1}".format(sys._getframe().f_code.co_name, ex))
def send_command(self, cmd): logger.info("Executing command: {0}".format(cmd)) if self.transport is None or self.transport.is_active() is False: self.reconnect() if self.transport is None or self.transport.is_active() is False: logger.error("Connection not established failed.") return -1, None try: session = self.transport.open_session() session.set_combine_stderr(True) session.get_pty() session.invoke_shell() if session.send_ready(): index = 0 input_data = self.__fix_indata(cmd) if index < len(input_data): data = input_data[index] + '\n' index += 1 session.send(data) else: logger.warn("session is not ready for send") except paramiko.SSHException as ex: logger.error("Exception for command '{0}: {1}'".format(cmd, ex)) session.close() return -1, None output = self.poll(session) status = session.recv_exit_status() # send quit to notify server shutdown the channel session.send("quit\n") logger.info("Returned status {0}".format(status)) session.close() return status, output
def main_loop(self): rlist = [] rlist.append(self.__pipe.inform) timeout = 10 print "Total threads: {0}".format(threading.activeCount()) try: while self.__running: readable, _, _ = select.select(rlist, [], [], timeout) if not readable: continue if self.__pipe.inform in readable: try: message = self.__pipe.read(256) except OSError, exc: logger.warn("[Error %d] appeared at reading pipe" % exc.errno) continue if len(message) == 0: continue pdu_id = message.split()[0].split('.')[-2] pdu_index = self.to_index(int(pdu_id)) logger.info("Assign message to pdu {0}".format(pdu_id)) self.__pdus[pdu_index].handle_message(message) except KeyboardInterrupt: logger.error("Break by user.") except Exception, ex: logger.error("{0}: {1}".format(sys._getframe().f_code.co_name, ex))
def handle_outlet(self, args): ''' 1. Get current outlet state 2. Get the current outlet action ''' # self.logger.info("handle outlet {0}/{1}".format(outlet, self.pdu)) outlet = args[0] action = args[1] logger.info("handle outlet {0}/{1}, action: {2}" .format(outlet, self.pdu, self.actions[int(action)])) vmname = self.__node_control_handler.get_node_name(1, int(outlet)) if vmname is None: self.set_outlet_field(self.outlet_action_oid_offset, outlet, 0) logger.error("No virtual node found for outlet {0}".format(outlet)) return datastore = self.__node_control_handler.get_node_datastore(vmname) if datastore is None: self.set_outlet_field(self.outlet_action_oid_offset, outlet, 0) logger.error("No datastore found for virtual node {0}" .format(vmname)) return # action = self.get_outlet_field(self.outlet_action_oid_offset, outlet) state = self.get_outlet_field(self.outlet_state_oid_offset, outlet) if self.actions[int(action)] == 'none' or \ self.actions[int(action)] == self.states[int(state)]: logger.warn("No need to execute the action: {}" .format(self.actions[int(action)])) return # restore the action default to "none" if self.actions[int(action)] == 'on': # 'on' state self.set_outlet_field(self.outlet_state_oid_offset, outlet, 5) status = self.__node_control_handler.power_on_node(datastore, vmname) elif self.actions[int(action)] == 'off': # 'off' state self.set_outlet_field(self.outlet_state_oid_offset, outlet, 4) status = self.__node_control_handler.power_off_node(datastore, vmname) elif self.actions[int(action)] == 'reboot': # 'off' state self.set_outlet_field(self.outlet_state_oid_offset, outlet, 8) status = self.__node_control_handler.reboot_node(datastore, vmname) # 'on' state self.set_outlet_field(self.outlet_state_oid_offset, outlet, 5) else: logger.error("Unknown action: {0}".format(action)) return if status != 0: logger.error("Failed to {0} virtual node." .format(self.actions[int(action)])) return self.set_outlet_field(self.outlet_action_oid_offset, outlet, 0)
def handle_message(self, message): logger.info("Got new message {0}".format(message)) oid = message.split()[0] outlet = oid.split('.')[-1] value = message.split()[1] if oid.startswith(self.outlet_action_oid_offset): self.add_task("handle outlet {0}".format(outlet), self.handle_outlet, int(outlet), value) else: logger.warn("{0} is not handled now.".format(message))
def handle_message(self, message): oid = message.split()[0] outlet = oid.split('.')[-1] value = message.split()[1] logger.info("Handle message {0}".format(message)) if message.startswith(self.pduouton_oid_offset): self.add_task("handle_outlet-{0}".format(outlet), self.handle_outlet, int(outlet), value) elif message.startswith(self.pduoutpwd_oid_offset): self.add_task("handle_password-{0}".format(outlet), self.handle_password, int(outlet), value) else: logger.warn("{0} is not handled now.".format(message))
def handle_outlet(self, args): outlet = args[0] action = args[1] logger.info("handle outlet {0}/{1}, action: {2}". format(outlet, self.pdu, self.actions[int(action)])) on_offset = self.pduouton_oid_offset + "." + str(self.to_pdu(self.pdu)) action_in_oid = self.extract(self.get_outlet_field(on_offset, outlet)) logger.warn("action: {0}, action_in_oid: {1}". format(self.actions[int(action)], self.actions[int(action_in_oid)])) vmname = self.__node_control_handler.get_node_name(int(self.pdu), int(outlet)) if vmname is None: logger.error("No virtual node found for outlet {0}".format(outlet)) return datastore = self.__node_control_handler.get_node_datastore(vmname) if datastore is None: logger.error("No datastore found for virtual node {0}". format(vmname)) return # Make sure the action as the last one logger.info("last action: {0}, current action: {1}". format(self.action_list[int(outlet) - 1], self.actions[int(action)])) if self.action_list[int(outlet) - 1] == self.actions[int(action)]: logger.warn("No need to execute action for {0}/{1}". format(outlet, self.pdu)) return if self.actions[int(action)] == 'on': status = self.__node_control_handler.power_on_node(datastore, vmname) elif self.actions[int(action)] == 'off': status = self.__node_control_handler.power_off_node(datastore, vmname) elif self.actions[int(action)] == 'reboot': status = self.__node_control_handler.reboot(datastore, vmname) else: logger.error("Unknown action: {0}".format(action)) if status != 0: logger.error("Failed to {0} virtual node.". format(self.actions[int(action)])) return self.action_list[int(outlet) - 1] = self.actions[int(action)]
def handle_outlet(self, args): outlet = args[0] action = args[1] logger.info("handle outlet {0}/{1}, action: {2}".format( outlet, self.pdu, self.actions[int(action)])) on_offset = self.pduouton_oid_offset + "." + str(self.to_pdu(self.pdu)) action_in_oid = self.extract(self.get_outlet_field(on_offset, outlet)) logger.warn("action: {0}, action_in_oid: {1}".format( self.actions[int(action)], self.actions[int(action_in_oid)])) vmname = self.__node_control_handler.get_node_name( int(self.pdu), int(outlet)) if vmname is None: logger.error("No virtual node found for outlet {0}".format(outlet)) return datastore = self.__node_control_handler.get_node_datastore(vmname) if datastore is None: logger.error( "No datastore found for virtual node {0}".format(vmname)) return # Make sure the action as the last one logger.info("last action: {0}, current action: {1}".format( self.action_list[int(outlet) - 1], self.actions[int(action)])) if self.action_list[int(outlet) - 1] == self.actions[int(action)]: logger.warn("No need to execute action for {0}/{1}".format( outlet, self.pdu)) return if self.actions[int(action)] == 'on': status = self.__node_control_handler.power_on_node( datastore, vmname) elif self.actions[int(action)] == 'off': status = self.__node_control_handler.power_off_node( datastore, vmname) elif self.actions[int(action)] == 'reboot': status = self.__node_control_handler.reboot(datastore, vmname) else: logger.error("Unknown action: {0}".format(action)) if status != 0: logger.error("Failed to {0} virtual node.".format( self.actions[int(action)])) return self.action_list[int(outlet) - 1] = self.actions[int(action)]
def handle_message(self, message): oid = message.split()[0] outlet = oid.split('.')[-1] value = message.split()[1] logger.info("Handle message {0}".format(message)) if message.startswith(self.pduouton_oid_offset): self.add_task("handle_outlet-{0}". format(outlet), self.handle_outlet, int(outlet), value) elif message.startswith(self.pduoutpwd_oid_offset): self.add_task("handle_password-{0}". format(outlet), self.handle_password, int(outlet), value) else: logger.warn("{0} is not handled now.".format(message))
def get_outlet_mode(self, offset, outlet): oid = '.'.join([offset, str(outlet)]) ret = self.__oid_handler.query_oid_val(oid) if not ret: return "" try: value_settings = {} value_settings = \ dict([self.split(x, '=') for x in self.split(ret, ',')]) if 'mode' in value_settings: return value_settings['mode'] return "" except: logger.warn("Mode is not set!") return ""
def __init__(self): ''' Constructor ''' super(VMwareHandler, self).__init__() self.__host_ip = None self.__username = None self.__password = None self.set_esxi_host_info() if self.__host_ip is not None: self.__ssh = sshclient.SSH(self.__host_ip, self.__username, self.__password) self.__ssh.connect() if self.__ssh.connected() is False: logger.error("Connection error for {0}@{1}". format(self.__username, self.__host_ip)) else: logger.info("Connection ok for {0}@{1}". format(self.__username, self.__host_ip)) else: logger.warn("ESXi is not set in configuration file.")
def __init__(self): ''' Constructor ''' super(VMwareHandler, self).__init__() self.__host_ip = None self.__username = None self.__password = None self.set_esxi_host_info() if self.__host_ip is not None: self.__ssh = sshclient.SSH(self.__host_ip, self.__username, self.__password) self.__ssh.connect() if self.__ssh.connected() is False: logger.error("Connection error for {0}@{1}".format( self.__username, self.__host_ip)) else: logger.info("Connection ok for {0}@{1}".format( self.__username, self.__host_ip)) else: logger.warn("ESXi is not set in configuration file.")