def monitoring(self, ip, mainPojo, mainDBRRD, indexInterface, indexCPU): snmp = SNMP() hostname = mainPojo.getHostname(str(ip)) community = mainPojo.getCommunity(str(hostname)) version_snmp = 0 if str(mainPojo.getVersionSNMP( str(hostname))) == "v1" else 1 port_snmp = int(mainPojo.getPortSNMP(str(hostname))) telegram = Telegram("712644612:AAHauTnCsKgno4fOh3z8p9B5fJuOnGC9-tk") idChat = "-343597492" flag = True while True: try: if os.system("ping -c 1 " + str(ip) + " >/dev/null 2>&1 < /dev/null &" ) == 0 and mainPojo.getStatus( str(ip)) == "up" and len( str(mainPojo.getStatus(str(ip)))) > 0: inoctets = int( snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.2.2.1.10.1") ) #OID para obtener el total de inoctets mainDBRRD.insert(inoctets) #######Notificacion de Fallas####### try: fallas = mainDBRRD.check_aberration() if int(fallas) == 1 and flag: flag = False graphHW(ip) mensaje = "Falla detectada en el Host :" + str( ip) + "(" + str(time.strftime("%c")) + ")" telegram.sendMessage(mensaje, idChat) telegram.sendImage(str(ip) + "HW.png", idChat) if int(fallas) == 2 and not flag: flag = True graphHW(ip) mensaje = "La falla en el Host :" + str( ip) + " ha sido mitigada (" + str( time.strftime("%c")) + ")" telegram.sendMessage(mensaje, idChat) telegram.sendImage(str(ip) + "HW.png", idChat) except Exception as a: print(a) time.sleep(60) else: mainPojo.setStatus(str(hostname), "down") print("Server is down") break except Exception as e: pa = PojoAgent("localhost", "root", "", "snmp") result = pa.verifyHost(str(ip)) if len(result) == 0: break else: print(e)
def graphPing(ip): pa = PojoAgent("localhost", "root", "", "snmp") #Conexion con la base de datos start = pa.getStart(ip) ret = rrdtool.graph( ip + "Ping.png", "--start", str(start), # "--end","N", "--vertical-label=Bytes/s", "DEF:inping=" + ip + ".rrd:inping:AVERAGE", "DEF:outping=" + ip + ".rrd:outping:AVERAGE", "AREA:inping#00FF00:In traffic", "LINE1:outping#0000FF:Out traffic\r") return ret
def graphNetInt(ip): pa = PojoAgent("localhost", "root", "", "snmp") #Conexion con la base de datos start = pa.getStart(ip) ret = rrdtool.graph( ip + "TraficoInterfazRed.png", "--start", str(start), # "--end","N", "--vertical-label=Bytes/s", "DEF:innetworkinterface=" + ip + ".rrd:innetworkinterface:AVERAGE", "DEF:outnetworkinterface=" + ip + ".rrd:outnetworkinterface:AVERAGE", "AREA:innetworkinterface#00FF00:In traffic", "LINE1:outnetworkinterface#0000FF:Out traffic\r") return ret
def graficas(): while True: menuGraficas() opcionMenu = raw_input("Selecciona una opcion >> ") if str(opcionMenu)=="1": print("Graficas Hot Wheels") hostname = raw_input("Introduce el hostname del agente: ") pa = PojoAgent("localhost","root","","snmp")#Conexion con la base de datos while True: if str(hostname) == "q" or str(hostname) == "Q": break else: #result = pa.verifyHost(str(hostname)) result = 1 if result == 0: print("El hostname no existe.") hostname = raw_input("Introduce el hostname del agente: ") else: #ip = pa.getIP(str(hostname)) graphHW(hostname) print("Grafica creada") raw_input("Presiona una tecla para regresar al menu >>") break elif (str(opcionMenu)=="q" or str(opcionMenu)=="Q"): print("") break else: raw_input("Opcion incorrecta...pulsa una tecla para continuar >>")
def eliminarAgente(): hostname = raw_input("Introduce el hostname del agente: ") pa = PojoAgent("localhost","root","","snmp")#Conexion con la base de datos while True: if str(hostname) == "q" or str(hostname) == "Q": break else: result = pa.verifyHost(str(hostname)) if len(result[0]) == 0: print("El hostname no existe.") hostname = raw_input("Introduce el hostname del agente: ") else: ip = pa.getIP(str(hostname)) pa.deleteAgent(str(hostname), str(ip)) pa.closeConnection() raw_input("El agente ha sido eliminado....presiona una tecla para regresar al Menu >>") break
def estadoDispositivo(): hostname = raw_input("Introduce el hostname del agente: ") pa = PojoAgent("localhost","root","","snmp")#Conexion con la base de datos while True: if str(hostname) == "q" or str(hostname) == "Q": break else: result = pa.verifyHost(str(hostname)) if len(result) == 0: print("El hostname no existe.") hostname = raw_input("Introduce el hostname del agente: ") else: ip = pa.getIP(str(hostname)) print("Hostname: "+ str(hostname) +"\nIP: " + str(pa.getIP(str(hostname))) +"\nVersion SO: "+ str(pa.getVersionSO(str(hostname),str(ip))) +"\nNumero Interfaces: "+ str(pa.getInterfaces(str(hostname),str(ip))) +"\nUltimo Reinicio: "+ str(pa.getLastReboot(str(hostname),str(ip))) +"\nUbicacion Fisica: "+ str(pa.getMac(str(hostname),str(ip))) +"\nInfo. Admin: "+ str(pa.getInfoAdmin(str(hostname),str(ip)))) pa.closeConnection() raw_input("Presiona una tecla para regresar al menu >>") break
def agregarAgente(): os.system('clear') hostname = "" status = "" version_snmp = "" port_snmp = "" community = "" index = "" pa = PojoAgent("localhost","root","","snmp")#Conexion con la base de datos paux = PojoAgent("localhost","root","","snmp")#Conexion con la base de datos snmp = SNMP() agent = "y" while agent == "y" or agent == "Y": hostname = raw_input("Introduce el hostname del agente: ") if str(hostname) == "q" or str(hostname) == "Q": break else: result = pa.verifyHost(str(hostname)) if len(result) > 0: print("El hostname ya se encuentra registrado.") hostname = raw_input("Introduce el hostname del agente: ") else: status = "up" if os.system("ping -c 1 " + str(hostname) +" >/dev/null 2>&1 < /dev/null &") == 0 else "down" version_snmp = raw_input("Introduce la version de snmp ('v1' o 'v2c'): ") if str(version_snmp) == "q" or str(version_snmp) == "Q": break else: port_snmp = raw_input("Introduce el puerto snmp: ") if str(port_snmp) == "q" or str(port_snmp) == "Q": break else: community = raw_input("Introduce la comunidad: ") if str(community) == "q" or str(community) == "Q": break else: index = raw_input("Introduce el index de la interfaz de red del agente: ") if str(index) == "q" or str(index) == "Q": break else: indexCPU = raw_input("Index del procesador del agente: ") if str(indexCPU) == "q" or str(indexCPU) == "Q": break version_snmp = "0" if str(version_snmp) == "v1" else "1" ip = str(subprocess.check_output("cat /etc/hosts | grep \""+ str(hostname) +"\" | awk '{print $1}'",shell=True)).split("\n")[0] if len(str(ip)) == 0: ip = str(subprocess.check_output("dig '"+ str(hostname) +"' +short",shell=True)).split("\n")[0] ip = str(hostname) oid = "1.3.6.1.2.1.1.1.0" #Oid para obtener la version de SO version_so = snmp.get(str(community),int(version_snmp),str(hostname),str(port_snmp),str(oid)) oid = "1.3.6.1.2.1.2.1.0" #Oid para obtener el numero de interfaces interfaces = snmp.get(str(community),int(version_snmp),str(hostname),str(port_snmp),str(oid)) oid = "1.3.6.1.2.1.2.2.1.9.1" #Oid para obtener el ultimo reinicio # last_reboot = snmp.get("variation/virtualtable",1,"10.100.71.230","1024","1.3.6.1.2.1.2.2.1.9.1") last_reboot = snmp.get(str(community),int(version_snmp),str(hostname),str(port_snmp),str(oid)) oid = "1.3.6.1.2.1.1.6.0" #Oid para obtener la direccion fisica mac = snmp.get(str(community),int(version_snmp),str(hostname),str(port_snmp),str(oid)) oid = "1.3.6.1.2.1.1.4.0" #Oid para obtener informacion del administrador info_admin = snmp.get(str(community),int(version_snmp),str(hostname),str(port_snmp),str(oid)) pa.insertAgent(str(hostname),str(version_snmp),port_snmp,str(community),str(status),str(ip),str(version_so),str(interfaces),str(last_reboot),str(mac),str(info_admin),str(index),str(indexCPU)) if pa.ifWindows(str(hostname)): print("Dispositivo Windows.") indexRAM = raw_input("Introduce el Index de la RAM: ") indexHDD = raw_input("Introduce el Index del HDD: ") pa.updateIndex(str(hostname),str(indexRAM),str(indexHDD)) thread = ThreadSNMP() thread.startMonitoring(str(hostname),paux,str(index),str(indexCPU)) pa.closeConnection() raw_input("El agente "+ str(hostname) +" ha sido agregado...Presiona una tecla para regresar al menu") break
def dispositivosRegistrados(flag): print("Dispositivos\n###############################################################################") pa = PojoAgent("localhost","root","","snmp")#Conexion con la base de datos paux = PojoAgent("localhost","root","","snmp")#Conexion con la base de datos hosts = pa.getAllHosts() for i in range(0,len(hosts)): if flag and os.system("ping -c 1 " + str(hosts[i][0]) +" >/dev/null 2>&1 < /dev/null &") == 0: ip = str(pa.getIP(str(hosts[i][0]))) index = str(pa.getIndex(str(ip))) indexCPU = str(pa.getIndexCPU(str(ip))) pa.setStatus(str(hosts[i][0]),"up") thread = ThreadSNMP() thread.startMonitoring(str(ip),paux,str(index),str(indexCPU)) else: ip = str(pa.getIP(str(hosts[i][0]))) status = str(pa.getStatus(str(ip))) if status == "down" and os.system("ping -c 1 " + str(hosts[i][0]) +" >/dev/null 2>&1 < /dev/null &") == 0: index = str(pa.getIndex(str(ip))) indexCPU = str(pa.getIndexCPU(str(ip))) thread = ThreadSNMP() thread.startMonitoring(str(ip),paux,str(index),str(indexCPU)) elif os.system("ping -c 1 " + str(hosts[i][0]) +" >/dev/null 2>&1 < /dev/null &") == 1: pa.setStatus(str(hosts[i][0]),"down") for i in range(0,len(hosts)): ip = str(pa.getIP(str(hosts[i][0]))) status = str(pa.getStatus(str(ip))) print("Dispositivo: "+ str(i+1)) print("Hostname: "+ str(hosts[i][0])) print("Status: "+ str(status)) print("Numero de Interfaces: "+str(pa.getInterfaces(str(hosts[i][0]),pa.getIP(str(hosts[i][0]))))) print("###############################################################################")
def graficas(): while True: menuGraficas() opcionMenu = raw_input("Selecciona una opcion >> ") if str(opcionMenu) == "1": print("Grafica Trafico de Interfaz de red") hostname = raw_input("Introduce el hostname del agente: ") pa = PojoAgent("localhost", "root", "", "snmp") #Conexion con la base de datos while True: if str(hostname) == "q" or str(hostname) == "Q": break else: result = pa.verifyHost(str(hostname)) if len(result[0]) == 0: print("El hostname no existe.") hostname = raw_input( "Introduce el hostname del agente: ") else: #ip = pa.getIP(str(hostname)) graphNetInt(hostname) print("Grafica creada") raw_input( "Presiona una tecla para regresar al menu >>") break elif str(opcionMenu) == "2": print("Grafica ICMP") hostname = raw_input("Introduce el hostname del agente: ") pa = PojoAgent("localhost", "root", "", "snmp") #Conexion con la base de datos while True: if str(hostname) == "q" or str(hostname) == "Q": break else: result = pa.verifyHost(str(hostname)) if len(result[0]) == 0: print("El hostname no existe.") hostname = raw_input( "Introduce el hostname del agente: ") else: #ip = pa.getIP(str(hostname)) graphICMP(hostname) print("Grafica creada") raw_input( "Presiona una tecla para regresar al menu >>") break elif str(opcionMenu) == "3": print("Grafica TCP") hostname = raw_input("Introduce el hostname del agente: ") pa = PojoAgent("localhost", "root", "", "snmp") #Conexion con la base de datos while True: if str(hostname) == "q" or str(hostname) == "Q": break else: result = pa.verifyHost(str(hostname)) if len(result[0]) == 0: print("El hostname no existe.") hostname = raw_input( "Introduce el hostname del agente: ") else: #ip = pa.getIP(str(hostname)) graphTCP(hostname) print("Grafica creada") raw_input( "Presiona una tecla para regresar al menu >>") break elif str(opcionMenu) == "4": print("Graficas UDP") hostname = raw_input("Introduce el hostname del agente: ") pa = PojoAgent("localhost", "root", "", "snmp") #Conexion con la base de datos while True: if str(hostname) == "q" or str(hostname) == "Q": break else: result = pa.verifyHost(str(hostname)) if len(result[0]) == 0: print("El hostname no existe.") hostname = raw_input( "Introduce el hostname del agente: ") else: #ip = pa.getIP(str(hostname)) graphUDP(hostname) print("Grafica creada") raw_input( "Presiona una tecla para regresar al menu >>") break elif str(opcionMenu) == "5": print("Graficas Ping") hostname = raw_input("Introduce el hostname del agente: ") pa = PojoAgent("localhost", "root", "", "snmp") #Conexion con la base de datos while True: if str(hostname) == "q" or str(hostname) == "Q": break else: result = pa.verifyHost(str(hostname)) if len(result[0]) == 0: print("El hostname no existe.") hostname = raw_input( "Introduce el hostname del agente: ") else: #ip = pa.getIP(str(hostname)) graphPing(hostname) print("Grafica creada") raw_input( "Presiona una tecla para regresar al menu >>") break elif (str(opcionMenu) == "q" or str(opcionMenu) == "Q"): print("") break else: raw_input("Opcion incorrecta...pulsa una tecla para continuar >>")
def monitoring(self, ip, mainPojo, mainDBRRD, indexInterface, indexCPU): snmp = SNMP() hostname = mainPojo.getHostname(str(ip)) community = mainPojo.getCommunity(str(hostname)) version_snmp = 0 if str(mainPojo.getVersionSNMP( str(hostname))) == "v1" else 1 port_snmp = int(mainPojo.getPortSNMP(str(hostname))) telegram = Telegram("712644612:AAHauTnCsKgno4fOh3z8p9B5fJuOnGC9-tk") #telegram.sendMessage(mensaje,"-343597492") idChat = "-343597492" umbralRAM = 80 umbralHDD = 90 umbralCPU = 6 while True: try: if mainPojo.ifWindows(ip): if os.system("ping -c 1 " + str(ip) + " >/dev/null 2>&1 < /dev/null &" ) == 0 and mainPojo.getStatus( str(ip)) == "up" and len( str(mainPojo.getStatus(str(ip)))) > 0: indexRAM = mainPojo.getIndexRAM(str(ip)) indexHDD = mainPojo.getIndexHDD(str(ip)) cpuload = snmp.get( community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.25.3.3.1.2." + str(indexCPU) ) #OID para calcular promedio del ultimo minuto del porcentaje que el procesador no estuvo inactivo freeram = int( snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.25.2.3.1.6." + str(indexRAM)) ) #OID para obtener la memoria RAM disponible en kB usehdd = int( snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.25.2.3.1.6." + str(indexHDD)) ) #OID para obtener la cantidad de Disco Duro usado. totalram = int( snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.25.2.3.1.5." + str(indexRAM)) ) #OID para obtener el total de la RAM totalhdd = int( snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.25.2.3.1.5." + str(indexHDD)) ) #OID para obtener el total del HDD hddload = ( usehdd * 100 ) / totalhdd #Cantidad de Disco Duro en Porcentaje ramload = ( (totalram - freeram) * 100) / totalram #Cantidad de RAM en porcentaje mainDBRRD.insert(cpuload, ramload, hddload) #######Establecer umbrales####### if int(ramload) > umbralRAM: graphRAM(ip) mensaje = "Porcentaje de RAM utilizada por el Host " + str( ip) + ": " + str(ramload) + " % (" + str( time.strftime("%c")) + ")" telegram.sendMessage(mensaje, idChat) telegram.sendImage(str(ip) + "RAM.png", idChat) if int(hddload) > umbralHDD: graphHDD(ip) mensaje = "Porcentaje de HDD utilizado por el Host " + str( ip) + ": " + str(hddload) + " % (" + str( time.strftime("%c")) + ")" telegram.sendMessage(mensaje, idChat) telegram.sendImage(str(ip) + "HDD.png", idChat) if int(cpuload) > umbralCPU: graphCPU(ip) mensaje = "Porcentaje de CPU utilizado por el Hots " + str( ip) + ": " + str(cpuload) + " % (" + str( time.strftime("%c")) + ")" telegram.sendMessage(mensaje, idChat) telegram.sendImage(str(ip) + "CPU.png", idChat) time.sleep(5) else: mainPojo.setStatus(str(hostname), "down") print("Server is down") break else: if os.system("ping -c 1 " + str(ip) + " >/dev/null 2>&1 < /dev/null &" ) == 0 and mainPojo.getStatus( str(ip)) == "up" and len( str(mainPojo.getStatus(str(ip)))) > 0: cpuload = snmp.get( community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.25.3.3.1.2." + str(indexCPU) ) #OID para calcular promedio del ultimo minuto del porcentaje que el procesador no estuvo inactivo freeram = int( snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.4.1.2021.4.6.0") ) #OID para obtener la memoria RAM disponible en kB usehdd = int( snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.4.1.2021.9.1.8.1") ) #OID para obtener la cantidad de Disco Duro usado. En linux agregar disk / 1 en snmpd.conf totalram = int( snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.25.2.2.0") ) #OID para obtener el total de la RAM totalhdd = int( snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.4.1.2021.9.1.11.1") ) #OID para obtener el total del HDD hddload = ( usehdd * 100 ) / totalhdd #Cantidad de Disco Duro en Porcentaje ramload = ( (totalram - freeram) * 100) / totalram #Cantidad de RAM en porcentaje mainDBRRD.insert(cpuload, ramload, hddload) #######Establecer umbrales####### if int(ramload) > umbralRAM: graphRAM(ip) mensaje = "Porcentaje de RAM utilizada por el Host " + str( ip) + ": " + str(ramload) + " % (" + str( time.strftime("%c")) + ")" telegram.sendMessage(mensaje, idChat) telegram.sendImage(str(ip) + "RAM.png", idChat) if int(hddload) > umbralHDD: graphHDD(ip) mensaje = "Porcentaje de HDD utilizado por el Host " + str( ip) + ": " + str(hddload) + " % (" + str( time.strftime("%c")) + ")" telegram.sendMessage(mensaje, idChat) telegram.sendImage(str(ip) + "HDD.png", idChat) if int(cpuload) > umbralCPU: graphCPU(ip) mensaje = "Porcentaje de CPU utilizado por el Hots " + str( ip) + ": " + str(cpuload) + " % (" + str( time.strftime("%c")) + ")" telegram.sendMessage(mensaje, idChat) telegram.sendImage(str(ip) + "CPU.png", idChat) time.sleep(5) else: mainPojo.setStatus(str(hostname), "down") print("Server is down") break except Exception as e: pa = PojoAgent("localhost", "root", "", "snmp") result = pa.verifyHost(str(ip)) if len(result) == 0: break else: print(e) print("Ocurrio un error")