From e245bbdfabfa71cc5a9701d38ae605200d16a7fd Mon Sep 17 00:00:00 2001 From: mbruzon Date: Mon, 23 Mar 2026 14:56:59 +0100 Subject: [PATCH] #wip: Python fixed and VBS in doing. --- JSON/NucelarMonitor.settings.json | 6 ++- Python/Controllers/AgentsController.py | 18 +++++++- Python/Dispatchers/AgentsDispatcher.py | 13 ++++++ Python/Drivers/WebServerDriver.py | 1 - Python/Managers/DispatchersManager.py | 12 ++--- Python/run.py | 4 +- Tools/NucelarMonitor.bat | 9 ++-- VisualBasicScripts/NucelarMonitor.bat | 6 +++ VisualBasicScripts/NucelarMonitor.vbs | 17 +++---- VisualBasicScripts/tests.vbs | 63 ++++++++++++++++++++++++++ 10 files changed, 125 insertions(+), 24 deletions(-) create mode 100644 Python/Dispatchers/AgentsDispatcher.py create mode 100644 VisualBasicScripts/NucelarMonitor.bat create mode 100644 VisualBasicScripts/tests.vbs diff --git a/JSON/NucelarMonitor.settings.json b/JSON/NucelarMonitor.settings.json index f9ccac5..6d03c4e 100644 --- a/JSON/NucelarMonitor.settings.json +++ b/JSON/NucelarMonitor.settings.json @@ -48,7 +48,11 @@ }, "default_routes" : [ "post:/agents/debian/{key} debian@agents", + "post:/agents/windows/{key} windows@agents", "get:/agents/test/{key} test@agents", "get:/ /Public" - ] + ], + "dispatchers" : { + "agents" : "agents_dispatcher" + } } \ No newline at end of file diff --git a/Python/Controllers/AgentsController.py b/Python/Controllers/AgentsController.py index 16f9b16..dfbd579 100644 --- a/Python/Controllers/AgentsController.py +++ b/Python/Controllers/AgentsController.py @@ -29,7 +29,23 @@ class AgentsController(ControllerAbstract): print([hostnames, domain, interfaces, disks, iterations, candle_times, cpu, memory, net_use]) def windows(self:Self, request:RequestModel, response:ResponseModel) -> None: - pass + + key:str = request.get("key") + hostnames:list[str] + domain:str|None + interfaces:list[str] + disks:list[list[str, int, int]] + iterations:int + candle_times:list[int, int] + cpu:list[float, float, float, float, float] + memory:list[int, int, int, int, int, float] + net_use:list[list[list[str, int, int, int, int, int, int]]] = [] + + print(Utils.json_decode(request.body)) + + hostnames, domain, interfaces, disks, candle_times, iterations, cpu, memory = Utils.json_decode(request.body) + + print([hostnames, domain, interfaces, disks, iterations, candle_times, cpu, memory, net_use]) def test(self:Self, request:RequestModel, response:ResponseModel) -> None: response.set_data({ diff --git a/Python/Dispatchers/AgentsDispatcher.py b/Python/Dispatchers/AgentsDispatcher.py new file mode 100644 index 0000000..2d43587 --- /dev/null +++ b/Python/Dispatchers/AgentsDispatcher.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from typing import Self +from Interfaces.Application.NucelarMonitorInterface import NucelarMonitorInterface + +class AgentsDispatcher: + + def __init__(self:Self, nucelar_monitor:NucelarMonitorInterface) -> None: + super().__init__(nucelar_monitor) + + def save(self:Self) -> None: + pass \ No newline at end of file diff --git a/Python/Drivers/WebServerDriver.py b/Python/Drivers/WebServerDriver.py index d905653..c5143c2 100644 --- a/Python/Drivers/WebServerDriver.py +++ b/Python/Drivers/WebServerDriver.py @@ -4,7 +4,6 @@ from typing import Any, Self, Optional, Sequence from threading import Thread from socket import socket as Socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR, SHUT_RDWR -from requests import get as get from Interfaces.Application.NucelarMonitorInterface import NucelarMonitorInterface from Abstracts.WebServerAbstract import WebServerAbstract from Models.ResponseModel import ResponseModel diff --git a/Python/Managers/DispatchersManager.py b/Python/Managers/DispatchersManager.py index a2494a4..23ef42b 100644 --- a/Python/Managers/DispatchersManager.py +++ b/Python/Managers/DispatchersManager.py @@ -30,12 +30,12 @@ class DispatchersManager: for subinputs in self.nucelar_monitor.files.load_json(inputs): key:str - controller:DispatcherAbstract|str + dispatcher:DispatcherAbstract|str - for key, controller in subinputs.items(): - if isinstance(controller, str): - controller = self.nucelar_monitor.models.get(DispatcherAbstract, controller)(self.nucelar_monitor) - if controller is not None and isinstance(controller, DispatcherAbstract) and ( + for key, dispatcher in subinputs.items(): + if isinstance(dispatcher, str): + dispatcher = self.nucelar_monitor.models.get(DispatcherAbstract, dispatcher)(self.nucelar_monitor) + if dispatcher is not None and isinstance(dispatcher, DispatcherAbstract) and ( overwrite or key not in self.__dispatcher ): - self.__dispatcher[key] = controller \ No newline at end of file + self.__dispatcher[key] = dispatcher \ No newline at end of file diff --git a/Python/run.py b/Python/run.py index 60d2494..e31ce30 100644 --- a/Python/run.py +++ b/Python/run.py @@ -6,12 +6,14 @@ from Application.NucelarMonitor import NucelarMonitor from Controllers.AgentsController import AgentsController from Drivers.SQLServerDriver import SQLServerDriver from Drivers.WebServerDriver import WebServerDriver +from Dispatchers.AgentsDispatcher import AgentsDispatcher inputs:dict[str, dict[str, Any|None]] = { "default_models" : { "agents" : AgentsController, "sql_server" : SQLServerDriver, - "web_server" : WebServerDriver + "web_server" : WebServerDriver, + "agents_dispatcher" : AgentsDispatcher, } } diff --git a/Tools/NucelarMonitor.bat b/Tools/NucelarMonitor.bat index d1a504d..a705e19 100644 --- a/Tools/NucelarMonitor.bat +++ b/Tools/NucelarMonitor.bat @@ -1,6 +1,3 @@ -@echo off - -:bucle -cscript //nologo ..\VisualBasicScripts\NucelarMonitor.vbs -@ping 127.255.255.255 -n 1 -w 5000 >nul -goto bucle \ No newline at end of file +@rem @echo off +cd %~dp0..\Python +python3 run.py \ No newline at end of file diff --git a/VisualBasicScripts/NucelarMonitor.bat b/VisualBasicScripts/NucelarMonitor.bat new file mode 100644 index 0000000..524cafb --- /dev/null +++ b/VisualBasicScripts/NucelarMonitor.bat @@ -0,0 +1,6 @@ +@echo off + +:bucle +cscript //nologo .\NucelarMonitor.vbs +@ping 127.255.255.255 -n 1 -w 5000 >nul +goto bucle \ No newline at end of file diff --git a/VisualBasicScripts/NucelarMonitor.vbs b/VisualBasicScripts/NucelarMonitor.vbs index ae81d52..2e4325c 100644 --- a/VisualBasicScripts/NucelarMonitor.vbs +++ b/VisualBasicScripts/NucelarMonitor.vbs @@ -10,7 +10,7 @@ Dim memory_maximum, memory_minimum, memory_average, memory_in, memory_out, memor key = "pc_miguel" print_json = True send_data = False -url_target = "" +url_target = "http://127.0.0.1:13000/agents/windows/" & key ips = "" disks_info = "" start_date = DateDiff("s", #1/1/1970#, Now) @@ -34,12 +34,13 @@ For Each item In wmi_service.ExecQuery("SELECT Name, Domain FROM Win32_ComputerS hostnames = hostnames & """" & item.Name & """" Next -For Each item In wmi_service.ExecQuery("SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") +For Each item In wmi_service.ExecQuery("SELECT NetConnectionID, IPAddress FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") If Not IsNull(item.IPAddress) Then If ips <> "" Then ips = ips & "," End If - ips = ips & """" & item.IPAddress(0) & """" + ips = ips & "[""" & item.NetConnectionID(0) & """, """ & item.IPAddress(0) & """]" End If Next +ips = "[" & ips & "]" For Each item In wmi_service.ExecQuery("SELECT DeviceID, Size, FreeSpace FROM Win32_LogicalDisk where DriveType = 3") @@ -99,12 +100,12 @@ if cpu_summatory <> 0 and iterations <> 0 Then cpu_average = Replace(CStr(cpu_su If memory_summatory <> 0 and iterations <> 0 Then memory_average = Round(memory_summatory / iterations) End If json = "[" -json = json & """" & key & """," +' json = json & """" & key & """," json = json & "[" & hostnames & "],""" & domain & """,[" & ips & "],[" & disks_info & "]," -json = json & "[" & start_date & "," & DateDiff("s", #1/1/1970#, Now) & "]," -json = json & iterations & "," -json = json & "[" & cpu_in & "," & cpu_out & "," & cpu_minimum & "," & cpu_maximum & "," & cpu_average & "]" -json = json & "[" & total_memory & "," & memory_in & "," & memory_out & "," & memory_minimum & "," & memory_maximum & "," & memory_average & "]" +'json = json & "[" & start_date & "," & DateDiff("s", #1/1/1970#, Now) & "]," +'json = json & iterations & "," +'json = json & "[" & cpu_in & "," & cpu_out & "," & cpu_minimum & "," & cpu_maximum & "," & cpu_average & "]" +'json = json & "[" & total_memory & "," & memory_in & "," & memory_out & "," & memory_minimum & "," & memory_maximum & "," & memory_average & "]" json = json & "]" if print_json Then diff --git a/VisualBasicScripts/tests.vbs b/VisualBasicScripts/tests.vbs new file mode 100644 index 0000000..214c09e --- /dev/null +++ b/VisualBasicScripts/tests.vbs @@ -0,0 +1,63 @@ +Option Explicit +On Error Resume Next + +Dim strComputer, objWMIService, colConfigs, objConfig, colAdapters, objAdapter, strNombre, strIP, strSubnet, strTipo, strMascaraBits, i + +strComputer = "." +Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") + +' Consultamos las configuraciones que tienen IP habilitada +Set colConfigs = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True") + +For Each objConfig In colConfigs + ' 1. Obtener el Nombre de la Interfaz (NetConnectionID) + Set colAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter Where DeviceID = " & objConfig.Index) + strNombre = "N/A" + For Each objAdapter In colAdapters + strNombre = objAdapter.NetConnectionID + Next + + ' 2. Procesar cada dirección IP del adaptador + If Not IsNull(objConfig.IPAddress) Then + For i = 0 To UBound(objConfig.IPAddress) + strIP = objConfig.IPAddress(i) + strSubnet = objConfig.IPSubnet(i) + + ' 3. Verificar si es IPv6 (buscando el símbolo ":") + If InStr(strIP, ":") > 0 Then + strTipo = "IPv6" + strMascaraBits = strSubnet ' En IPv6, WMI ya devuelve el prefijo (ej: 64) + Else + strTipo = "IPv4" + strMascaraBits = MascaraABits(strSubnet) + End If + + ' Salida de datos + WScript.Echo "Interfaz: " & strNombre + WScript.Echo "Tipo: " & strTipo + WScript.Echo "IP: " & strIP + WScript.Echo "Máscara: /" & strMascaraBits + WScript.Echo "------------------------------------------" + Next + End If +Next + +' Función para convertir máscara decimal (255.255.255.0) a bits (24) +Function MascaraABits(strMask) + Dim arrOctetos, intBits, octeto + intBits = 0 + arrOctetos = Split(strMask, ".") + For Each octeto In arrOctetos + Select Case Int(octeto) + Case 255: intBits = intBits + 8 + Case 254: intBits = intBits + 7 + Case 252: intBits = intBits + 6 + Case 248: intBits = intBits + 5 + Case 240: intBits = intBits + 4 + Case 224: intBits = intBits + 3 + Case 192: intBits = intBits + 2 + Case 128: intBits = intBits + 1 + End Select + Next + MascaraABits = intBits +End Function \ No newline at end of file