def __registerWithAutoDetectionDaemon(self): log.info("Trying to connect with autodetection deamon from " + self.__management_ip) if self.__debug: HOST = "192.168.0.2" PORT = DaemonLocations.autodetectDaemon[1] + 100000 print "TARGET ", HOST, PORT else: HOST = DaemonLocations.autodetectDaemon[0] PORT = DaemonLocations.autodetectDaemon[1] client = ReservationClient() success = False command = "register_target" payload = str(self.__management_ip) retries = 0 while (retries < 3 and not success): try: retries += 1 client.connect(HOST, PORT) reply = client.sendRequest(command, payload) if reply == "success": success = True except Exception, e: print e log.warning("Retry " + str(retries) + " sleeping for 10 seconds") time.sleep(10)
def __registerWithAutoDetectionDaemon(self): log.info("Trying to connect with autodetection deamon from "+self.__management_ip) if self.__debug: HOST = "192.168.0.2" PORT = DaemonLocations.autodetectDaemon[1] + 100000 print "TARGET ",HOST,PORT else: HOST = DaemonLocations.autodetectDaemon[0] PORT = DaemonLocations.autodetectDaemon[1] client = ReservationClient() success = False command = "register_target" payload = str(self.__management_ip) retries = 0 while (retries < 3 and not success): try: retries += 1 client.connect(HOST, PORT) reply = client.sendRequest(command,payload) if reply == "success": success = True except Exception,e: print e log.warning("Retry "+str(retries)+" sleeping for 10 seconds") time.sleep(10)
def __init__(self, ip, node, debug): self.__node = node self.__toGiga = 1.0 / 10 ** 9 self.__toGigaBinary = 1.0 / 2 ** 30 self.__processor_count = 0 self.__processor_core_count = {} self.__interfaces = {} self.__next_interface_id = 0 self.__ip = ip self.__debug = debug self.__target = None self.__lshw = None self.__target = ReservationClient() # if not self.__debug: self.__switchd = ReservationClient() self.__if_list = [] log.info("Detecting " + str(ip)) self.finished = False self.start()
def __init__(self, ip, node, debug): self.__node = node self.__toGiga = (1.0 / 10**9) self.__toGigaBinary = (1.0 / 2**30) self.__processor_count = 0 self.__processor_core_count = {} self.__interfaces = {} self.__next_interface_id = 0 self.__ip = ip self.__debug = debug self.__target = None self.__lshw = None self.__target = ReservationClient() #if not self.__debug: self.__switchd = ReservationClient() self.__if_list = [] log.info("Detecting " + str(ip)) self.finished = False self.start()
class ComputerDetector: def __init__(self, ip, node, debug): self.__node = node self.__toGiga = 1.0 / 10 ** 9 self.__toGigaBinary = 1.0 / 2 ** 30 self.__processor_count = 0 self.__processor_core_count = {} self.__interfaces = {} self.__next_interface_id = 0 self.__ip = ip self.__debug = debug self.__target = None self.__lshw = None self.__target = ReservationClient() # if not self.__debug: self.__switchd = ReservationClient() self.__if_list = [] log.info("Detecting " + str(ip)) self.finished = False self.start() def getNode(self): return self.__node def getLshw(self): return self.__lshw def getInterfaceXML(self): string = "" for interface_type in self.__node.getInterfaces(): for interface in self.__node.getInterfaces(interface_type): if interface.getTdrSuccess() == False: string += ( '<interface type="' + str(interface_type) + '" mac="' + str(interface.getMAC()) + '" switch="' + str(interface.getSwitch()) + '" port="' + str(interface.getPort()) + '" model="' + str(interface.getModel()) + '" speed="' + str(interface.getSpeed()) + '" id="' + str(interface.getInterfaceID()) + '" tdrSuccess="' + str(interface.getTdrSuccess()) + '" tdrOutput="' + str(interface.getTdrOutput()) + '" />\n' ) return string def __getText(self, nodelist): rc = "" for node in nodelist: if node.nodeType == node.TEXT_NODE: rc = rc + node.data return rc def __handleLshw(self, lshwdoc): nodes = lshwdoc.getElementsByTagName("node") self.__handleLshwNodes(nodes) def __handleLshwNodes(self, nodes): global processor_count, processor_core_count, processor_vendor, processor_version, processor_speed, usb_version, memory_count for node in nodes: if node.hasAttributes(): if self.__getText(node.attributes["class"].childNodes) == "system": self.__handleSystemNode(node) if self.__getText(node.attributes["class"].childNodes) == "processor": self.__handleProcessorNode(node) if self.__getText(node.attributes["class"].childNodes) == "network": self.__handleNetworkNode(node) if self.__getText(node.attributes["class"].childNodes) == "bus": self.__handleBusNode(node) if self.__getText(node.attributes["class"].childNodes) == "memory": self.__handleMemoryNode(node) self.__node.setSingleAttribute("numbercorespercpu", 0) for cpu_id in self.__processor_core_count: self.__node.setSingleAttribute( "numbercorespercpu", max(self.__node.getSingleAttribute("numbercorespercpu"), self.__processor_core_count[cpu_id]), ) self.__node.setSingleAttribute("numbercpus", self.__processor_count) self.__node.setSystemMemory(memory_count * self.__toGigaBinary) self.__node.setInterfaces(self.__interfaces) def __handleSystemNode(self, node): if self.__getText(node.getElementsByTagName("description")[0].childNodes) == "Rack Mount Chassis": self.__node.setMotherboard(self.__getText(node.getElementsByTagName("product")[0].childNodes)) self.__node.setModel(self.__getText(node.getElementsByTagName("product")[0].childNodes).replace(" ", "")) self.__node.setVendor(self.__getText(node.getElementsByTagName("vendor")[0].childNodes)) try: self.__node.setSingleAttribute( "serialnumber", self.__getText(node.getElementsByTagName("serial")[0].childNodes) ) except: pass def __handleProcessorNode(self, node): id = self.__getText(node.attributes["id"].childNodes) if id.startswith("logical"): parent = self.__getText(node.parentNode.attributes["id"].childNodes) if self.__processor_core_count.has_key(parent): self.__processor_core_count[parent] = self.__processor_core_count[parent] + 1 else: self.__processor_core_count[parent] = 1 else: self.__processor_count = self.__processor_count + 1 try: self.__node.setSingleAttribute( "cputype", " ".join(str(self.__getText(node.getElementsByTagName("product")[0].childNodes)).split()) ) except: pass try: self.__node.setSingleAttribute( "cpuspeed", int(self.__getText(node.getElementsByTagName("size")[0].childNodes)) * self.__toGiga ) except: pass def __handleNetworkNode(self, node): interface = Interface() try: for n in node.getElementsByTagName("configuration")[0].getElementsByTagName("setting"): if self.__getText(n.attributes["id"].childNodes) == "ip": interface.setIP(self.__getText(n.attributes["value"].childNodes)) if self.__getText(n.attributes["id"].childNodes) == "subnet": interface.setSubnet(self.__getText(n.attributes["value"].childNodes)) if self.__getText(n.attributes["id"].childNodes) == "module": if interface.getModel() == None: interface.setModel(self.__getText(n.attributes["value"].childNodes)) if self.__getText(n.attributes["id"].childNodes) == "speed": interface.setSpeed(self.__getText(n.attributes["value"].childNodes)) except: pass try: interface.setMAC(self.__getText(node.getElementsByTagName("serial")[0].childNodes).upper()) self.__if_list.append(self.__getText(node.getElementsByTagName("serial")[0].childNodes)) except: interface.setMAC("00:00:00:00:00:00") try: if interface.getSpeed() == None: if self.__getText(node.getElementsByTagName("capacity")[0].childNodes) == "1000000000": interface.setSpeed("1GB/s") if self.__getText(node.getElementsByTagName("capacity")[0].childNodes) == "100000000": interface.setSpeed("100MB/s") self.__if_list.append(self.__getText(node.getElementsByTagName("capacity")[0].childNodes)) except: pass try: interface.setModel(self.__getText(node.getElementsByTagName("product")[0].childNodes)) except: pass interface.setIfaceType("experimental") if interface.getIP() != None: if interface.getIP().startswith("192.168.0."): interface.setIfaceType("management") elif interface.getIP().startswith("192.168.1."): interface.setIfaceType("infrastructure") elif interface.getIP().startswith("128.16."): interface.setIfaceType("external") if not self.__interfaces.has_key(interface.getIfaceType()): self.__interfaces[interface.getIfaceType()] = [] interface.setInterfaceID("interface" + str(self.__next_interface_id)) self.__next_interface_id = self.__next_interface_id + 1 self.__interfaces[interface.getIfaceType()].append(interface) def __handleBusNode(self, node): try: if self.__getText(node.getElementsByTagName("description")[0].childNodes) == "USB Controller": usbVersionMap = {"01": "1.0", "0b": "1.1", "02": "2.0", "09": "2.0"} id = self.__getText(node.attributes["id"].childNodes) if id.startswith("usb"): self.__node.setSingleAttribute( "usbversion", usbVersionMap[self.__getText(node.getElementsByTagName("version")[0].childNodes)] ) elif self.__getText(node.getElementsByTagName("description")[0].childNodes) == "Motherboard": # self.__node.setMotherboard(self.__getText(node.getElementsByTagName("product")[0].childNodes)) pass except: pass def __handleMemoryNode(self, node): global memory_count id = self.__getText(node.attributes["id"].childNodes) if id.startswith("bank"): try: memory_count = memory_count + int(self.__getText(node.getElementsByTagName("size")[0].childNodes)) except: pass def lshw(self): command = "lshw" payload = "" if not self.__debug: PORT = DaemonLocations.autodetectClientDaemon[1] else: PORT = DaemonLocations.autodetectClientDaemon[1] + 100000 log.info("trying to connect to " + str(self.__ip) + " on " + str(PORT)) self.__target.connect(self.__ip, PORT) # if not self.__debug: self.__lshw = self.__target.sendRequest(command, payload) # print lshw # else: # self.__lshw = "/home/arkell/u0/adam/development/svn/hen_scripts/computer100-lshw.xml" # self.__lshw = "computer100-lshw.xml" # if self.__debug: # dom = xml.dom.minidom.parse(self.__lshw) # else: # # write lshw to file dom = xml.dom.minidom.parseString(self.__lshw) self.__handleLshw(dom) def interface_find(self): print "INTERFACE FIND" self.__switchd.connect("192.168.1.2", 90000) self.__target.sendRequest("interface_up", pickle.dumps(self.__if_list)) self.__target.sendRequest("interface_test", pickle.dumps(self.__if_list)) time.sleep(20) reply = self.__switchd.sendRequest("port_find_mac", pickle.dumps(self.__if_list)) for i in range(0, len(reply)): for interface_type in self.__node.getInterfaces(): for interface in self.__node.getInterfaces(interface_type): if interface.getMAC().upper() == reply[i][0].upper(): mac_list = reply[i][1].getMacs() found = [] for m in mac_list: if found.count(m) == 0: found.append(m) if len(found) == 1: interface.setPort(reply[i][1].getName()) interface.setName(reply[i][1].getName()) interface.setSwitch(reply[i][1].getSwitch()) self.__switchd.close() def interface_tdr(self): self.__switchd.connect("192.168.1.2", 90000) for interface_type in self.__node.getInterfaces(): for interface in self.__node.getInterfaces(interface_type): if interface.getSwitch() == None or interface.getName() == None: pass dev = [interface.getMAC()] new_payload = pickle.dumps(dev) self.__target.sendRequest("interface_down", new_payload) dev = (interface.getSwitch(), interface.getName()) new_payload = pickle.dumps(dev) try: tdr_res = pickle.loads(self.__switchd.sendRequest("port_tdr", new_payload)) if tdr_res[0]: interface.setTdrSuccess(True) # print str(interface.getSwitch()),str(interface.getName())+" tdr ok" else: interface.setTdrSuccess(False) interface.setTdrOutput(tdr_res[1]) # print str(interface.getSwitch()),str(interface.getName())+" tdr failed : \n"+str(tdr_res[1]) except: print "problem running tdr for ", str(interface.getSwitch()), str(interface.getName()) pass print interface payload = pickle.dumps(()) self.__target.sendRequest("interface_down", payload) self.__switchd.close() def start(self): self.lshw() self.interface_find() if not self.__debug: # self.interface_find() self.interface_tdr() time.sleep(5) payload = pickle.dumps(("")) self.__target.sendAsyncRequest("stop", payload) print "finished detecting", self.__ip self.finished = True
print message + "\n" help() os._exit(1) def convertToDate(string): dates = string.split("/") return datetime.date(int(dates[2]), int(dates[1]), int(dates[0])) if (len(sys.argv) < 2): error("missing parameter(s)") HOST = DaemonLocations.reservationDaemon[0] PORT = DaemonLocations.reservationDaemon[1] + 100000 client = ReservationClient() client.connect(HOST, PORT) username = os.environ['USER'] command = sys.argv[1] payload = None #command = "cleanexpired" #payload = pickle.dumps(()) #client.sendRequest(command, payload) #os._exit(1) #command = "reloadhendb" #payload = pickle.dumps(()) #client.sendRequest(command, payload) #os._exit(1)
class ComputerDetector(): def __init__(self, ip, node, debug): self.__node = node self.__toGiga = (1.0 / 10**9) self.__toGigaBinary = (1.0 / 2**30) self.__processor_count = 0 self.__processor_core_count = {} self.__interfaces = {} self.__next_interface_id = 0 self.__ip = ip self.__debug = debug self.__target = None self.__lshw = None self.__target = ReservationClient() #if not self.__debug: self.__switchd = ReservationClient() self.__if_list = [] log.info("Detecting " + str(ip)) self.finished = False self.start() def getNode(self): return self.__node def getLshw(self): return self.__lshw def getInterfaceXML(self): string = "" for interface_type in self.__node.getInterfaces(): for interface in self.__node.getInterfaces(interface_type): if interface.getTdrSuccess() == False: string += '<interface type="' + str(interface_type) + '" mac="' + str(interface.getMAC()) + \ '" switch="' + str(interface.getSwitch()) + '" port="' + str(interface.getPort()) + \ '" model="' + str(interface.getModel()) + '" speed="' + str(interface.getSpeed()) + \ '" id="' + str(interface.getInterfaceID()) +'" tdrSuccess="' + str(interface.getTdrSuccess()) + \ '" tdrOutput="' + str(interface.getTdrOutput()) +'" />\n' return string def __getText(self, nodelist): rc = "" for node in nodelist: if node.nodeType == node.TEXT_NODE: rc = rc + node.data return rc def __handleLshw(self, lshwdoc): nodes = lshwdoc.getElementsByTagName("node") self.__handleLshwNodes(nodes) def __handleLshwNodes(self, nodes): global processor_count, processor_core_count, processor_vendor, processor_version, processor_speed, usb_version, memory_count for node in nodes: if node.hasAttributes(): if self.__getText( node.attributes['class'].childNodes) == "system": self.__handleSystemNode(node) if self.__getText( node.attributes['class'].childNodes) == "processor": self.__handleProcessorNode(node) if self.__getText( node.attributes['class'].childNodes) == "network": self.__handleNetworkNode(node) if self.__getText( node.attributes['class'].childNodes) == "bus": self.__handleBusNode(node) if self.__getText( node.attributes['class'].childNodes) == "memory": self.__handleMemoryNode(node) self.__node.setSingleAttribute("numbercorespercpu", 0) for cpu_id in self.__processor_core_count: self.__node.setSingleAttribute( "numbercorespercpu", max(self.__node.getSingleAttribute("numbercorespercpu"), self.__processor_core_count[cpu_id])) self.__node.setSingleAttribute("numbercpus", self.__processor_count) self.__node.setSystemMemory(memory_count * self.__toGigaBinary) self.__node.setInterfaces(self.__interfaces) def __handleSystemNode(self, node): if (self.__getText( node.getElementsByTagName("description")[0].childNodes) == "Rack Mount Chassis"): self.__node.setMotherboard( self.__getText( node.getElementsByTagName("product")[0].childNodes)) self.__node.setModel( self.__getText( node.getElementsByTagName("product") [0].childNodes).replace(' ', '')) self.__node.setVendor( self.__getText( node.getElementsByTagName("vendor")[0].childNodes)) try: self.__node.setSingleAttribute( "serialnumber", self.__getText( node.getElementsByTagName("serial")[0].childNodes)) except: pass def __handleProcessorNode(self, node): id = self.__getText(node.attributes['id'].childNodes) if (id.startswith("logical")): parent = self.__getText( node.parentNode.attributes['id'].childNodes) if self.__processor_core_count.has_key(parent): self.__processor_core_count[ parent] = self.__processor_core_count[parent] + 1 else: self.__processor_core_count[parent] = 1 else: self.__processor_count = self.__processor_count + 1 try: self.__node.setSingleAttribute( "cputype", ' '.join( str( self.__getText( node.getElementsByTagName("product") [0].childNodes)).split())) except: pass try: self.__node.setSingleAttribute( "cpuspeed", int( self.__getText( node.getElementsByTagName("size")[0].childNodes)) * self.__toGiga) except: pass def __handleNetworkNode(self, node): interface = Interface() try: for n in node.getElementsByTagName( "configuration")[0].getElementsByTagName("setting"): if (self.__getText(n.attributes['id'].childNodes) == "ip"): interface.setIP( self.__getText(n.attributes['value'].childNodes)) if (self.__getText(n.attributes['id'].childNodes) == "subnet"): interface.setSubnet( self.__getText(n.attributes['value'].childNodes)) if (self.__getText(n.attributes['id'].childNodes) == "module"): if interface.getModel() == None: interface.setModel( self.__getText(n.attributes['value'].childNodes)) if (self.__getText(n.attributes['id'].childNodes) == "speed"): interface.setSpeed( self.__getText(n.attributes['value'].childNodes)) except: pass try: interface.setMAC( self.__getText( node.getElementsByTagName("serial")[0].childNodes).upper()) self.__if_list.append( self.__getText( node.getElementsByTagName("serial")[0].childNodes)) except: interface.setMAC("00:00:00:00:00:00") try: if interface.getSpeed() == None: if self.__getText( node.getElementsByTagName("capacity") [0].childNodes) == "1000000000": interface.setSpeed("1GB/s") if self.__getText( node.getElementsByTagName("capacity") [0].childNodes) == "100000000": interface.setSpeed("100MB/s") self.__if_list.append( self.__getText( node.getElementsByTagName("capacity")[0].childNodes)) except: pass try: interface.setModel( self.__getText( node.getElementsByTagName("product")[0].childNodes)) except: pass interface.setIfaceType("experimental") if interface.getIP() != None: if interface.getIP().startswith("192.168.0."): interface.setIfaceType("management") elif interface.getIP().startswith("192.168.1."): interface.setIfaceType("infrastructure") elif interface.getIP().startswith("128.16."): interface.setIfaceType("external") if not self.__interfaces.has_key(interface.getIfaceType()): self.__interfaces[interface.getIfaceType()] = [] interface.setInterfaceID("interface" + str(self.__next_interface_id)) self.__next_interface_id = self.__next_interface_id + 1 self.__interfaces[interface.getIfaceType()].append(interface) def __handleBusNode(self, node): try: if (self.__getText( node.getElementsByTagName("description")[0].childNodes) == "USB Controller"): usbVersionMap = { "01": "1.0", "0b": "1.1", "02": "2.0", "09": "2.0" } id = self.__getText(node.attributes['id'].childNodes) if (id.startswith("usb")): self.__node.setSingleAttribute( "usbversion", usbVersionMap[self.__getText( node.getElementsByTagName("version") [0].childNodes)]) elif (self.__getText( node.getElementsByTagName("description")[0].childNodes) == "Motherboard"): #self.__node.setMotherboard(self.__getText(node.getElementsByTagName("product")[0].childNodes)) pass except: pass def __handleMemoryNode(self, node): global memory_count id = self.__getText(node.attributes['id'].childNodes) if (id.startswith("bank")): try: memory_count = memory_count + int( self.__getText( node.getElementsByTagName("size")[0].childNodes)) except: pass def lshw(self): command = "lshw" payload = "" if not self.__debug: PORT = DaemonLocations.autodetectClientDaemon[1] else: PORT = DaemonLocations.autodetectClientDaemon[1] + 100000 log.info("trying to connect to " + str(self.__ip) + " on " + str(PORT)) self.__target.connect(self.__ip, PORT) #if not self.__debug: self.__lshw = self.__target.sendRequest(command, payload) #print lshw #else: # self.__lshw = "/home/arkell/u0/adam/development/svn/hen_scripts/computer100-lshw.xml" # self.__lshw = "computer100-lshw.xml" # if self.__debug: # dom = xml.dom.minidom.parse(self.__lshw) # else: # # write lshw to file dom = xml.dom.minidom.parseString(self.__lshw) self.__handleLshw(dom) def interface_find(self): print "INTERFACE FIND" self.__switchd.connect("192.168.1.2", 90000) self.__target.sendRequest("interface_up", pickle.dumps(self.__if_list)) self.__target.sendRequest("interface_test", pickle.dumps(self.__if_list)) time.sleep(20) reply = self.__switchd.sendRequest("port_find_mac", pickle.dumps(self.__if_list)) for i in range(0, len(reply)): for interface_type in self.__node.getInterfaces(): for interface in self.__node.getInterfaces(interface_type): if interface.getMAC().upper() == reply[i][0].upper(): mac_list = reply[i][1].getMacs() found = [] for m in mac_list: if found.count(m) == 0: found.append(m) if len(found) == 1: interface.setPort(reply[i][1].getName()) interface.setName(reply[i][1].getName()) interface.setSwitch(reply[i][1].getSwitch()) self.__switchd.close() def interface_tdr(self): self.__switchd.connect("192.168.1.2", 90000) for interface_type in self.__node.getInterfaces(): for interface in self.__node.getInterfaces(interface_type): if interface.getSwitch() == None or interface.getName( ) == None: pass dev = ([interface.getMAC()]) new_payload = pickle.dumps(dev) self.__target.sendRequest("interface_down", new_payload) dev = (interface.getSwitch(), interface.getName()) new_payload = pickle.dumps(dev) try: tdr_res = pickle.loads( self.__switchd.sendRequest("port_tdr", new_payload)) if tdr_res[0]: interface.setTdrSuccess(True) #print str(interface.getSwitch()),str(interface.getName())+" tdr ok" else: interface.setTdrSuccess(False) interface.setTdrOutput(tdr_res[1]) #print str(interface.getSwitch()),str(interface.getName())+" tdr failed : \n"+str(tdr_res[1]) except: print "problem running tdr for ", str( interface.getSwitch()), str(interface.getName()) pass print interface payload = pickle.dumps(()) self.__target.sendRequest("interface_down", payload) self.__switchd.close() def start(self): self.lshw() self.interface_find() if not self.__debug: #self.interface_find() self.interface_tdr() time.sleep(5) payload = pickle.dumps(("")) self.__target.sendAsyncRequest("stop", payload) print "finished detecting", self.__ip self.finished = True