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 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 monitoring(self, ip, mainPojo, mainDBRRD, index): 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))) while True: 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: in_network_interface = snmp.get( community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.2.2.1.10." + str(index)) out_network_interface = snmp.get( community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.2.2.1.16." + str(index)) in_icmp = snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.5.1.0") out_icmp = snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.5.14.0") in_tcp = snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.6.10.0") out_tcp = snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.6.11.0") in_udp = snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.7.1.0") out_udp = snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.7.4.0") in_ping = int( snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.5.8.0") ) #The number of ICMP Echo (request) messages received. in_ping += int( snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.5.9.0") ) #The number of ICMP Echo Reply messages received. out_ping = int( snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.5.21.0") ) #The number of ICMP Echo (request) messages sent. out_ping += int( snmp.get(community, version_snmp, ip, port_snmp, "1.3.6.1.2.1.5.22.0") ) #The number of ICMP Echo Reply messages sent. mainDBRRD.insert(in_network_interface, out_network_interface, in_icmp, out_icmp, in_tcp, out_tcp, in_udp, out_udp, in_ping, out_ping) time.sleep(1) else: mainPojo.setStatus(str(hostname), "down") print("") break
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")