def MakeTreeDevice(): #create list whit elements shows list_elements_shows = [] #generate connection database = Comunication.BeginComunication() #get list elements fathers query = "SELECT serialp from conecta" list_serialesp = [] #generate list whit information serialesp = Comunication.MakeQueryDB(query, database[1]) #manage the query #storage list of serials for element in serialesp: list_serialesp.append(element[0]) #delete elements repeats in list list_serialesp = list(set(list_serialesp)) #for each element in list for element in list_serialesp: #check if exist in elements already show if ExisteInLista(list_elements_shows, element) == 0: list_parcial = Query.CreateTreeBySerial(element, list_elements_shows, '') #create tree of device #append element to list show. Query.AddElement(list_parcial, list_elements_shows)
def ShowElementsinDataBase(query, title): #start connection whit data base database = Comunication.BeginComunication() response = Comunication.MakeQueryDB (query, database[1])#get the response in base of query makye #create file for send element in response file_response = open("temp", 'w') var = "%s\n"%title file_response.write(var) #shows the results whit format -> for element in response: file_response.write("->") cont=0#for ask if is the first element or no for element_in_response in element: if cont < len(element)-1: var = "%s;"%element_in_response file_response.write(var) else: file_response.write(element_in_response) cont+=1 file_response.write("\n") #close file... file_response.close() #show file whit less option... os.system("less temp")
def GetNameModel(id_model): query = "select nom_modelo from modelo where id_modelo = %s" % id_model database_elements = Comunication.BeginComunication()#init connection response = Comunication.MakeQueryDB(query, database_elements[1])#manage query return response[0][0]
def GetSerialDevice(ip_device): query = "select serial from dispositivo where ip_disp = '%s'" % ip_device print query database_elements = Comunication.BeginComunication() #init connection response = Comunication.MakeQueryDB(query, database_elements[1]) #manage query return response[0][0]
def DeleteDB_WithOutMessage(query): #start connection whit data base database = Comunication.BeginComunication() try:#error and validation database[1].execute(query)#execute query database[0].commit()#commit the data base except psycopg2.DatabaseError, e: print 'Error %s', e sys.exit(1)
def GenerateCSVOfTable(table, query): database_elements = Comunication.BeginComunication()#init connection response = Comunication.MakeQueryDB(query, database_elements[1])#manage query #create file whit information name_faile = "%s.csv" % table file_write = open(name_faile, 'w') WriteInFile(file_write, response)#print element in file csv format... file_write.close()
def InsertDBWithOutMessage(query): #start connection whit data base database = Comunication.BeginComunication() try:#error and validation database[1].execute(query)#execute the query database[0].commit()#commit the data base whit new information inserted. except psycopg2.DatabaseError, e: print 'Error %s', e sys.exit(1)
def UpdateDB(query): #start connection whit data base database = Comunication.BeginComunication() try:#error and validation database[1].execute(query)#execute query database[0].commit()#commit the data base print "Update of element is ok!" except psycopg2.DatabaseError, e: print 'Error %s', e sys.exit(1)
def GetSerialbyIP(query): #start connection with data base database = Comunication.BeginComunication() try:#error and validation database[1].execute(query)#execute the query for row in database[1]: return row[0]#return the serial getting break except psycopg2.DatabaseError, e: print 'Error %s', e sys.exit(1)
def GetMaxIDinTable(table, id_table): #star connection whit data base database = Comunication.BeginComunication() try:#error and validation query = "select MAX(%s) from %s" % (id_table, table);#create query for get max value database[1].execute(query) for row in database[1]: return row[0]#return of max value finded break#break of iteration except psycopg2.DatabaseError, e: print 'Error %s', e sys.exit(1)
def IPisUnica(query): cantidad =0#will store the number of repeat ip #start connection with data base database = Comunication.BeginComunication() try:#error and validation database[1].execute(query)#execute the query for row in database[1]: cantidad = row[0]#get number of repeat return cantidad#return number of repeat except psycopg2.DatabaseError, e: print 'Error %s', e sys.exit(1)
def ExistElementInDB(query): #start conecction whit data base database = Comunication.BeginComunication() existe =1#value of init search try: database[1].execute(query)#execute the query for row in database[1]:#get values existe = 0#set value of indicator break#break of iteration return existe#return of indicator except psycopg2.DatabaseError, e: print 'Error %s', e sys.exit(1)
def IsPapa(serial): #start connection whit data base database = Comunication.BeginComunication() existe = 0 query = "select serialp from conecta where serialp = '%s'" % serial try: database[1].execute(query)#execute the query for row in database[1]: existe = 1#set value if existe element break#break of iteration return existe#return value of response query except psycopg2.DatabaseError, e: print 'Error %s', e sys.exit(1)
def GetHijosOfDevice(serial): #start connection with data base database = Comunication.BeginComunication() hijos_device = []#will store of list serials #make the query query = "select serialh from conecta where serialp = '%s'" % serial try:#error and validation database[1].execute(query)#execute the query for row in database[1]:#get all results hijos_device.append(row[0])#add a list return hijos_device#erturn list with results query except psycopg2.DatabaseError, e: print 'Error %s', e sys.exit(1)
def MakeQuery(query): #start connection whit data base database = Comunication.BeginComunication() response = Comunication.MakeQueryDB (query, database[1])#get the response in base of query makye #shows the results whit format -> for element in response: print "->", cont=0#for ask if is the first element or no for element_in_response in element: if cont < len(element)-1: print element_in_response, ";", else: print element_in_response cont+=1 print
def GetInforDeviceBySerial(serial): #start connection whit data base database = Comunication.BeginComunication() information_device = []#list that will store information of device #make the query query = "select dispositivo.serial, dispositivo.nom_disp, dispositivo.ip_disp from dispositivo where dispositivo.serial= '%s'" %serial try:#error and validation database[1].execute(query)#execute the query for row in database[1]:#get results for element in row:#get by row of information information_device.append(element)#add element in list break#break iteration for not make a lot of iteration whitout sense return information_device except psycopg2.DatabaseError, e: print 'Error %s', e sys.exit(1)
def UpdateModelDevice( model_device, serial): #Falta el id de la marca y del tipo de dispositivo!!! #evaluate if exists the model in data base... query = "select * from modelo where nom_modelo = '%s'" % model_device if Query.ExistElementInDB(query) == 0: #exists in db... #get the id of model query = "select id_modelo from modelo where nom_modelo = '%s'" % model_device database_elements = Comunication.BeginComunication() #init connection response = Comunication.MakeQueryDB( query, database_elements[1]) #manage query #update the id of model device... query = "update dispositivo set id_modelo = %d where serial = '%s'" % ( int(response[0][0]), serial) Query.UpdateDBWithOutMessage(query) else: #the model device not exist in data base... #insert model in data base... id_model = Query.GetMaxIDinTable( 'modelo', 'id_modelo') #falta agregar el tipo de dispositivo y la marca... if id_model == None: id_model = 0 query = "insert into modelo values (%d, %d, %d, '%s')" % ( id_model, 0, 0, model_device) Query.InsertDBWithOutMessage(query) query = "update dispositivo set id_modelo = %d where serial = '%s'" % ( id_model, serial) Query.UpdateDBWithOutMessage(query) else: id_model += 1 query = "insert into modelo values (%d, %d, %d, '%s')" % ( id_model, 0, 0, model_device) Query.InsertDBWithOutMessage(query) query = "update dispositivo set id_modelo = %d where serial = '%s'" % ( id_model, serial) Query.UpdateDBWithOutMessage(query)
def MakeTree(): #establecemos la comunicacion database = Comunication.BeginComunication() #obtenemos en una lista aparte la informacion de los niveles... query = "SELECT * FROM nivel" niveles = Comunication.MakeQueryDB(query, database[1]) #gestionamos la consulta #creamos una consulta... query = "SELECT * FROM campus" info_campus = Comunication.MakeQueryDB( query, database[1]) #gestionamos la consulta for element in info_campus: #No mostrare los campus si no tienen edificios... #se obtiene la informacion de los edificios... query = "SELECT * FROM edificio where id_campus = %d" % element[0] if Query.ExistElementInDB(query) == 0: info_edificios = Comunication.MakeQueryDB( query, database[1]) #gestionamos la consulta print "├─Campus: %s" % element[1] for edificios in info_edificios: #no mostrare los edificios si no tiene racks asociados query = "select * from edificio join rack on (edificio.id_edif = rack.id_edif) where edificio.id_edif=%d" % int( edificios[0]) if Query.ExistElementInDB(query) == 0: print "│ ├─Edificio: '%s'" % edificios[2] #por cada nivel del edificio obtenemos los rack... for nivel in niveles: query = "select * from rack join edificio on (rack.id_edif = edificio.id_edif) join nivel on (nivel.id_nivel = rack.id_nivel) where rack.id_nivel = %d AND rack.id_edif = %d" % ( nivel[0], edificios[0]) #evaluamos si el edificio tiene los niveles indicados... if Query.ExistElementInDB(query) == 0: racks_niveles = Comunication.MakeQueryDB( query, database[1]) print "│ │ ├─Nivel : ", nivel[1] for rack in racks_niveles: #informacion de los rack print "│ │ │ ├─Rack: ", rack[3] #por cada rack obtenemos la informacion de los dispositivos existentes en el... query = "select dispositivo.serial, dispositivo.nom_disp, dispositivo.ip_disp, modelo.nom_modelo, tipo_disp.nom_tipo_disp, marca.nom_marca from dispositivo join rack on (rack.id_rack = dispositivo.id_rack) join modelo on (dispositivo.id_modelo = modelo.id_modelo) join tipo_disp on (tipo_disp.id_tipo_disp = modelo.id_tipo_disp) join marca on (marca.id_marca = modelo.id_marca) where dispositivo.id_rack= %d" % rack[ 0] dispositivo_rack = Comunication.MakeQueryDB( query, database[1]) #evaluamos aquellos que son papas y con ellos formamos el arbol dispositivo_rack_papa = [] for dispositivo in dispositivo_rack: if Query.IsPapa(dispositivo[0]) == 1: dispositivo_rack_papa.append( dispositivo[0]) #ahora hacemos el arbol en base a la lista obtenida... #creamos una lista con los elementos que ya han sido buscados... list_elements_shows = [] #recorremos la lista de seriales que son papas... for element in dispositivo_rack_papa: #evaluamos si ya mostramos a el y sus hijos... if MakeTreeDispositivos.ExisteInLista( list_elements_shows, element) == 0: list_parcial = Query.CreateTreeBySerial( element, list_elements_shows, '│ │ │ │ ' ) #creamos el arbol para ese dispositivo... #agregamos los elementos a la lista de elementos mostrados... Query.AddElement( list_parcial, list_elements_shows) return 0