diff --git a/.gitignore b/.gitignore index 7110aeb..a282772 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,6 @@ __pycache__ /Python/Assets /SQLServer/data /SQLServer/temporary -/SQLServer/scripts \ No newline at end of file +/SQLServer/scripts +/Python/pyodbc.* +/Python/.venv \ No newline at end of file diff --git a/Bash/NucelarMonitor.debian.script.sh b/Bash/NucelarMonitor.debian.script.sh index 136af70..b921e73 100755 --- a/Bash/NucelarMonitor.debian.script.sh +++ b/Bash/NucelarMonitor.debian.script.sh @@ -6,7 +6,7 @@ candle_seconds=10 candle_sleep_seconds=1 execute_sleep_seconds=0 show_json=true -send_json=true +send_json=false url_server="http://192.168.1.131:13000/agents/debian" # Settings. @@ -179,16 +179,18 @@ function execute(){ net_data="$net_data,$(get_net_data)]" candle_end=$(date +%s) - json="$json$hostnames,$domain,$ips,$disks,$iterations" - json="$json,[$candle_start,$candle_end]" + # json="$json$hostnames,$domain,$ips,$disks,$iterations" + # json="$json,[$candle_start,$candle_end]" json="$json,[${cpu_in//,/.},${cpu_out//,/.},${cpu_minimum//,/.},${cpu_maximum//,/.},${cpu_average//,/.}]" - json="$json,[$memory_total,$memory_in,$memory_out,$memory_minimum,$memory_maximum,${memory_average//,/.}]" - json="$json,$net_data" + # json="$json,[$memory_total,$memory_in,$memory_out,$memory_minimum,$memory_maximum,${memory_average//,/.}]" + # json="$json,$net_data" json="$json]" if [ "$show_json" = true ]; then echo "$json";fi if [ "$send_json" = true ]; then - local response=$(echo "$json"|curl -s -X POST -H "Content-Type: application/json" -d @- "$url_server/$key") + # local response=$(echo "$json"|curl -s -X POST -H "Content-Type: application/json" -d @- "$url_server/$key") + echo "$json"|curl -s -X POST -H "Content-Type: application/json" -d @- "$url_server/$key" + echo Sent fi } diff --git a/JSON/NucelarMonitor.settings.json b/JSON/NucelarMonitor.settings.json index 6d03c4e..2ca44af 100644 --- a/JSON/NucelarMonitor.settings.json +++ b/JSON/NucelarMonitor.settings.json @@ -11,6 +11,12 @@ [" ok ", "ok", "success", "succeed", "yes"], ["test", "debug"] ], + "default_settings_files" : [ + "/JSON/NucelarMonitor.settings.json" + ], + "default_secrets_files" : [ + "/JSON/NucelarMonitor.secrets.json" + ], "web_servers" : { "main" : { "type" : "web_server", @@ -32,17 +38,6 @@ "index_files" : ["index.html", "index.htm"] } }, - "databases" : { - "sql_server" : { - "type" : "sql_server", - "driver" : "{ODBC Driver 17 for SQL Server}", - "host" : "127.0.0.1", - "port" : 1433, - "user" : "sa", - "password" : "password", - "database" : "NucelarMonitor" - } - }, "default_controllers" : { "agents" : "agents" }, @@ -53,6 +48,6 @@ "get:/ /Public" ], "dispatchers" : { - "agents" : "agents_dispatcher" + "sql_server_agents" : "sql_server_agents" } } \ No newline at end of file diff --git a/Python/Abstracts/DatabaseAbstract.py b/Python/Abstracts/DatabaseAbstract.py index b6ef994..80abea7 100644 --- a/Python/Abstracts/DatabaseAbstract.py +++ b/Python/Abstracts/DatabaseAbstract.py @@ -2,23 +2,13 @@ # -*- coding: utf-8 -*- from typing import Self, Any, Optional, Sequence -from abc import ABC, abstractmethod from Interfaces.Application.NucelarMonitorInterface import NucelarMonitorInterface -from Models.QueryResponseModel import QueryResponseModel +from Interfaces.Owners.DatabasesInterface import DatabasesInterface -class DatabaseAbstract(ABC): +class DatabaseAbstract(DatabasesInterface): def __init__(self:Self, nucelar_monitor:NucelarMonitorInterface, inputs:Optional[dict[str, Any|None]|Sequence[Any|None]] = None ) -> None: - self.nucelar_monitor:NucelarMonitorInterface = nucelar_monitor - - @abstractmethod - def close(self:Self) -> bool:pass - - @abstractmethod - def query(self:Self, - query:str, - parameters:Optional[dict[str, Any|None]|Sequence[Any|None]] = None - ) -> QueryResponseModel:pass \ No newline at end of file + self.nucelar_monitor:NucelarMonitorInterface = nucelar_monitor \ No newline at end of file diff --git a/Python/Abstracts/DispatcherAbstract.py b/Python/Abstracts/DispatcherAbstract.py index fc9c771..625b5a6 100644 --- a/Python/Abstracts/DispatcherAbstract.py +++ b/Python/Abstracts/DispatcherAbstract.py @@ -3,8 +3,9 @@ from typing import Self from Interfaces.Application.NucelarMonitorInterface import NucelarMonitorInterface +from Interfaces.Owners.DispatchersInterface import DispatchersInterface -class DispatcherAbstract: +class DispatcherAbstract(DispatchersInterface): def __init__(self:Self, nucelar_monitor:NucelarMonitorInterface) -> None: self.nucelar_monitor:NucelarMonitorInterface = nucelar_monitor \ No newline at end of file diff --git a/Python/Application/NucelarMonitor.py b/Python/Application/NucelarMonitor.py index 57c5a4f..b2dcc45 100644 --- a/Python/Application/NucelarMonitor.py +++ b/Python/Application/NucelarMonitor.py @@ -14,6 +14,7 @@ from Managers.ModelsManager import ModelsManager from Managers.TerminalManager import TerminalManager from Managers.RoutesManager import RoutesManager from Managers.ControllersManager import ControllersManager +from Managers.DispatchersManager import DispatchersManager from Managers.DatabasesManager import DatabasesManager from Managers.WebServersManager import WebServersManager @@ -46,6 +47,7 @@ class NucelarMonitor: self.models:ModelsManager = ModelsManager(self) self.terminal:TerminalManager = TerminalManager(self) self.controllers:ControllersManager = ControllersManager(self) + self.dispatchers:DispatchersManager = DispatchersManager(self) self.routes:RoutesManager = RoutesManager(self) self.databases:DatabasesManager = DatabasesManager(self) self.web_servers:WebServersManager = WebServersManager(self) diff --git a/Python/Controllers/AgentsController.py b/Python/Controllers/AgentsController.py index 4d013ec..9f38ff1 100644 --- a/Python/Controllers/AgentsController.py +++ b/Python/Controllers/AgentsController.py @@ -10,6 +10,7 @@ from Models.IterationModel import ( CandleTimeModel, IterationModel ) from Dispatchers.AgentsDispatcher import AgentsDispatcher +from Abstracts.DatabaseAbstract import DatabaseAbstract from Utils.Utils import Utils class AgentsController(ControllerAbstract): @@ -26,8 +27,10 @@ class AgentsController(ControllerAbstract): 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]]] - received:list[NetUseModel] = [] - transmitted:list[NetUseModel] = [] + received:dict[str, NetUseModel] = {} + transmitted:dict[str, NetUseModel] = {} + i:int + net:list[str, int, int, int, int, int, int] ( hostnames, domain, interfaces_data, disks, @@ -36,10 +39,23 @@ class AgentsController(ControllerAbstract): print(Utils.json_decode(request.body)) - received = {net[0] : NetUseModel(*net[1:]) for i, net in enumerate(net_use[0])} - transmitted = {net[0] : NetUseModel(*net[1:]) for i, net in enumerate(net_use[1])} + # 'enp3s0', 12489583046, 9909705, 0, 774527051, 4221200, 0 + # 'enp3s0', 12486039059, 9906802, 0, 774272948, 4220106, 0 + # 3543987 2903, 0 - self.nucelar_monitor.dispatchers.get(AgentsDispatcher, "sql_server").save(IterationModel( + for i, net in enumerate(net_use[0]): + print([net[0], net_use[1][i][0]]) + received[net[0]] = NetUseModel(net_use[1][i][1] - net[1], net_use[1][i][2] - net[2], net_use[1][i][3] - net[3]) + transmitted[net[0]] = NetUseModel(net_use[1][i][4] - net[4], net_use[1][i][5] - net[5], net_use[1][i][6] - net[6]) + + for key, net in received.items(): + if net.bytes < 0 or net.packets < 0 or net.errors < 0: + print(["received", key, net.bytes, net.packets, net.errors]) + for key, net in transmitted.items(): + if net.bytes < 0 or net.packets < 0 or net.errors < 0: + print(["transmitted", key, net.bytes, net.packets, net.errors]) + + self.nucelar_monitor.dispatchers.get(AgentsDispatcher, "sql_server_agents").save(IterationModel( key, hostnames, domain, [InterfaceModel( i, name, is_ipv6, ip, mask, received[name], transmitted[name] @@ -51,6 +67,11 @@ class AgentsController(ControllerAbstract): MemoryModel(*memory) )) + response.set_data({ + "message": "Data saved successfully", + "key" : key + }, "application/json") + def windows(self:Self, request:RequestModel, response:ResponseModel) -> None: key:str = request.get("key") @@ -71,7 +92,11 @@ class AgentsController(ControllerAbstract): print([hostnames, domain, interfaces, disks, iterations, candle_times, cpu, memory, net_use]) def test(self:Self, request:RequestModel, response:ResponseModel) -> None: - response.set({ + + database:DatabaseAbstract = self.nucelar_monitor.databases.get("sql_server") + + response.set_data({ "message": "Test successful", - "key" : request.get("key") + "key" : request.get("key"), + "data" : database.query("select top 10 * from information_schema.tables", {}).tables }, "application/json") \ No newline at end of file diff --git a/Python/Dispatchers/AgentsDispatcher.py b/Python/Dispatchers/AgentsDispatcher.py index 8f3087c..0b74db7 100644 --- a/Python/Dispatchers/AgentsDispatcher.py +++ b/Python/Dispatchers/AgentsDispatcher.py @@ -38,20 +38,32 @@ class AgentsDispatcher(DispatcherAbstract): }) for interface in iteration.interfaces: - database.query(( + + id:int = int(database.query(( "execute dbo.set_machine_interface_data " + - "{key}, {candle_start}, {candle_end}, {candle_interations}, " + - "{name}, {bytes}, {packages}, {errors}, {is_ipv6}, {ip}, {mask}" + "{key}, {name}, {is_ipv6}, {ip}, {mask}, {mounted}, @id output" ), { "key" : iteration.key, - "candle_start" : iteration.candle_time.start, - "candle_end" : iteration.candle_time.end, - "candle_interations" : iteration.iterations, "name" : interface.name, - "bytes" : interface.received.bytes + interface.transmitted.bytes, - "packages" : interface.received.packets + interface.transmitted.packets, - "errors" : interface.received.errors + interface.transmitted.errors, "is_ipv6" : interface.is_ipv6, "ip" : interface.ip, - "mask" : interface.mask - }) \ No newline at end of file + "mask" : interface.mask, + "mounted" : True + }).variables["id"]) + _type:str + + for _type in ("received", "transmitted"): + database.query(( + "execute dbo.set_machine_interface_traffic " + + "{id}, {type}, {candle_start}, {candle_end}, " + + "{candle_interations}, {bytes}, {packets}, {errors}" + ), { + "id" : id, + "type" : _type, + "candle_start" : iteration.candle_time.start, + "candle_end" : iteration.candle_time.end, + "candle_interations" : iteration.iterations, + "bytes" : getattr(interface, _type).bytes, + "packets" : getattr(interface, _type).packets, + "errors" : getattr(interface, _type).errors + }) \ No newline at end of file diff --git a/Python/Drivers/SQLServerDriver.py b/Python/Drivers/SQLServerDriver.py index bed748f..ee1687e 100644 --- a/Python/Drivers/SQLServerDriver.py +++ b/Python/Drivers/SQLServerDriver.py @@ -3,7 +3,8 @@ from typing import Self, Any, Optional, Sequence from re import Match as REMatch -from Assets.pyodbc import Connection as Connection, connect as connect, Cursor as Cursor +import datetime +from pyodbc import Connection as Connection, connect as connect, Cursor as Cursor from Interfaces.Application.NucelarMonitorInterface import NucelarMonitorInterface from Abstracts.DatabaseAbstract import DatabaseAbstract from Models.QueryResponseModel import QueryResponseModel @@ -19,14 +20,14 @@ class SQLServerDriver(DatabaseAbstract): super().__init__(nucelar_monitor, inputs) self.__connection:Connection|None = None self.__string_connection:str = Utils.string_variables(self.nucelar_monitor.settings.get(( - "odbc_string_connection", "sql_string_connection", "string_connection" + "odbc_string_connection", "sql_server_string_connection", "string_connection" ), inputs, "DRIVER={driver};SERVER={host},{port};UID={user};PWD={password};DATABASE={database}"), { - "driver" : self.nucelar_monitor.settings.get("sql_driver", inputs, "{ODBC Driver 17 for SQL Server}"), - "host" : self.nucelar_monitor.settings.get(("sql_host", "odbc_host"), inputs, "localhost"), - "port" : self.nucelar_monitor.settings.get(("sql_port", "odbc_port"), inputs, 1433), - "user" : self.nucelar_monitor.settings.get(("sql_user", "odbc_user"), inputs, "sa"), - "password" : self.nucelar_monitor.settings.get(("sql_password", "odbc_password"), inputs, "password"), - "database" : self.nucelar_monitor.settings.get(("sql_database", "odbc_database"), inputs, "NucelarMonitor") + "driver" : self.nucelar_monitor.settings.get(("sql_driver", "driver"), inputs, "{ODBC Driver 17 for SQL Server}"), + "host" : self.nucelar_monitor.settings.get(("sql_host", "odbc_host", "host"), inputs, "localhost"), + "port" : self.nucelar_monitor.settings.get(("sql_port", "odbc_port", "port"), inputs, 1433), + "user" : self.nucelar_monitor.settings.get(("sql_user", "odbc_user", "user"), inputs, "sa"), + "password" : self.nucelar_monitor.settings.get(("sql_password", "odbc_password", "password"), inputs, "password"), + "database" : self.nucelar_monitor.settings.get(("sql_database", "odbc_database", "database"), inputs, "NucelarMonitor") }) def close(self:Self) -> bool: @@ -58,16 +59,22 @@ class SQLServerDriver(DatabaseAbstract): if key: return ( "'" + str(parameters[key]).replace("'","''") + "'" if isinstance(parameters[key], str) else + "'" + parameters[key].strftime("%Y-%m-%d %H:%M:%S") + "'" if isinstance(parameters[key], datetime.datetime) else + ("1" if parameters[key] else "0") if isinstance(parameters[key], bool) else str(parameters[key]) if key in parameters else matches.group(0)) key = matches.group(2) - variables.append(key) + print(key) + + key in variables or variables.append(key) return matches.group(0) query = RE.ODBC_STRING_VARIABLE.sub(callback, query) + print(variables) + return ( "".join("declare @" + variable + " varchar(max)\n" for variable in variables) + query + @@ -90,6 +97,7 @@ class SQLServerDriver(DatabaseAbstract): self.__string_connection, autocommit = True ) + cursor = self.__connection.cursor() query, variables = self.format_query(query, parameters) @@ -108,6 +116,7 @@ class SQLServerDriver(DatabaseAbstract): self.nucelar_monitor.exception(exception, "connection_exception", { "string_connection" : self.__string_connection }) + print(self.format_query(query, parameters)) finally: if cursor is not None: cursor.close() diff --git a/Python/Interfaces/Managers/DatabasesManagerInterface.py b/Python/Interfaces/Managers/DatabasesManagerInterface.py index cbcf32d..815bd4f 100644 --- a/Python/Interfaces/Managers/DatabasesManagerInterface.py +++ b/Python/Interfaces/Managers/DatabasesManagerInterface.py @@ -3,12 +3,12 @@ from typing import Self, Any from abc import ABC, abstractmethod -from Abstracts.DatabaseAbstract import DatabaseAbstract +from Interfaces.Owners.DatabasesInterface import DatabasesInterface class DatabasesManagerInterface(ABC): @abstractmethod - def get(self:Self, key:str) -> type[DatabaseAbstract]|None:pass + def get(self:Self, key:str) -> type[DatabasesInterface]|None:pass @abstractmethod def add(self:Self, inputs:Any|None, overwrite:bool = False) -> None:pass diff --git a/Python/Interfaces/Managers/DispatchersManagerInterface.py b/Python/Interfaces/Managers/DispatchersManagerInterface.py index 60d2558..dd28db4 100644 --- a/Python/Interfaces/Managers/DispatchersManagerInterface.py +++ b/Python/Interfaces/Managers/DispatchersManagerInterface.py @@ -3,9 +3,9 @@ from typing import Any, Self, TypeVar from abc import ABC, abstractmethod -from Abstracts.DispatcherAbstract import DispatcherAbstract +from Interfaces.Owners.DispatchersInterface import DispatchersInterface -T = TypeVar("T", bound = DispatcherAbstract) +T = TypeVar("T", bound = DispatchersInterface) class DispatchersManagerInterface(ABC): diff --git a/Python/Interfaces/Owners/DatabasesInterface.py b/Python/Interfaces/Owners/DatabasesInterface.py new file mode 100644 index 0000000..7b7af04 --- /dev/null +++ b/Python/Interfaces/Owners/DatabasesInterface.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from abc import ABC, abstractmethod +from typing import Self, Any, Optional, Sequence +from Models.QueryResponseModel import QueryResponseModel + +class DatabasesInterface(ABC): + + @abstractmethod + def close(self:Self) -> bool:pass + + @abstractmethod + def query(self:Self, + query:str, + parameters:Optional[dict[str, Any|None]|Sequence[Any|None]] = None + ) -> QueryResponseModel:pass \ No newline at end of file diff --git a/Python/Interfaces/Owners/DispatchersInterface.py b/Python/Interfaces/Owners/DispatchersInterface.py new file mode 100644 index 0000000..217988b --- /dev/null +++ b/Python/Interfaces/Owners/DispatchersInterface.py @@ -0,0 +1,7 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from abc import ABC, abstractmethod + +class DispatchersInterface(ABC): + pass \ No newline at end of file diff --git a/Python/Managers/DatabasesManager.py b/Python/Managers/DatabasesManager.py index b9c1a71..9baed95 100644 --- a/Python/Managers/DatabasesManager.py +++ b/Python/Managers/DatabasesManager.py @@ -3,7 +3,7 @@ from typing import Self, Any from Interfaces.Application.NucelarMonitorInterface import NucelarMonitorInterface -from Abstracts.DatabaseAbstract import DatabaseAbstract +from Interfaces.Owners.DatabasesInterface import DatabasesInterface class DatabasesManager: @@ -12,7 +12,7 @@ class DatabasesManager: key:str - self.__databases:dict[str, DatabaseAbstract] = {} + self.__databases:dict[str, DatabasesInterface] = {} for key in ( "default_databases_files", "databases_files", @@ -20,34 +20,34 @@ class DatabasesManager: ): self.add(self.nucelar_monitor.settings.get(key, None, []), True) - def get(self:Self, key:str) -> type[DatabaseAbstract]|None: + def get(self:Self, key:str) -> type[DatabasesInterface]|None: return self.__databases.get(key, None) def add(self:Self, inputs:Any|None, overwrite:bool = False) -> None: - subinputs:dict[str, type[DatabaseAbstract]|dict[str, Any|None]] + subinputs:dict[str, type[DatabasesInterface]|dict[str, Any|None]] for subinputs in self.nucelar_monitor.files.load_json(inputs): key:str - database:DatabaseAbstract|dict[str, Any|None] + database:DatabasesInterface|dict[str, Any|None] for key, database in subinputs.items(): if isinstance(database, dict): database = self.nucelar_monitor.models.get( - DatabaseAbstract, + DatabasesInterface, self.nucelar_monitor.settings.get(("database_type", "type"), database, "sql_server") )(self.nucelar_monitor, database) if database is None: continue - if database is not None and isinstance(database, DatabaseAbstract) and ( + if database is not None and isinstance(database, DatabasesInterface) and ( overwrite or key not in self.__databases ): self.__databases[key] = database def close(self:Self) -> None: - database:type[DatabaseAbstract] + database:type[DatabasesInterface] for database in self.__databases.values(): database.close() \ No newline at end of file diff --git a/Python/Managers/DispatchersManager.py b/Python/Managers/DispatchersManager.py index 50c5aee..1518aff 100644 --- a/Python/Managers/DispatchersManager.py +++ b/Python/Managers/DispatchersManager.py @@ -3,9 +3,9 @@ from typing import Any, Self, TypeVar from Interfaces.Application.NucelarMonitorInterface import NucelarMonitorInterface -from Abstracts.DispatcherAbstract import DispatcherAbstract +from Interfaces.Owners.DispatchersInterface import DispatchersInterface -T = TypeVar("T", bound = DispatcherAbstract) +T = TypeVar("T", bound = DispatchersInterface) class DispatchersManager: @@ -14,32 +14,32 @@ class DispatchersManager: key:str - self.__dispatcher:dict[str, DispatcherAbstract] = {} + self.__dispatchers:dict[str, DispatchersInterface] = {} for key in ( - "default_dispatcher_files", "dispatcher_files", - "default_dispatcher", "dispatcher" + "default_dispatchers_files", "dispatchers_files", + "default_dispatchers", "dispatchers" ): self.add(self.nucelar_monitor.settings.get(key, None, []), True) def get(self:Self, Type:type[T], key:str) -> T|None: - if key in self.__dispatcher and isinstance(self.__dispatcher[key], Type): - return self.__dispatcher[key] + if key in self.__dispatchers and isinstance(self.__dispatchers[key], Type): + return self.__dispatchers[key] return None def add(self:Self, inputs:Any|None, overwrite:bool = False) -> None: - subinputs:dict[str, str|DispatcherAbstract] + subinputs:dict[str, str|DispatchersInterface] for subinputs in self.nucelar_monitor.files.load_json(inputs): key:str - dispatcher:DispatcherAbstract|str + dispatcher:DispatchersInterface|str 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 + dispatcher = self.nucelar_monitor.models.get(DispatchersInterface, dispatcher)(self.nucelar_monitor) + if dispatcher is not None and isinstance(dispatcher, DispatchersInterface) and ( + overwrite or key not in self.__dispatchers ): - self.__dispatcher[key] = dispatcher \ No newline at end of file + self.__dispatchers[key] = dispatcher \ No newline at end of file diff --git a/Python/Managers/SettingsManager.py b/Python/Managers/SettingsManager.py index 617e0f9..ca82e8c 100644 --- a/Python/Managers/SettingsManager.py +++ b/Python/Managers/SettingsManager.py @@ -59,8 +59,6 @@ class SettingsManager: def add(self:Self, inputs:Any|None, overwrite:bool = False) -> None: subinputs:dict[str, Any|None] - - print(inputs) for subinputs in self.nucelar_monitor.files.load_json(inputs): diff --git a/Python/Models/IterationModel.py b/Python/Models/IterationModel.py index 2628128..c6f0a9b 100644 --- a/Python/Models/IterationModel.py +++ b/Python/Models/IterationModel.py @@ -1,20 +1,21 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +import datetime from typing import Self class CPUModel: def __init__(self:Self, _in:float, _out:float, - maximum:float, minimum:float, + maximum:float, average:float ) -> None: self._in:float = _in self.out:float = _out - self.maximum:float = maximum self.minimum:float = minimum + self.maximum:float = maximum self.average:float = average class MemoryModel: @@ -22,16 +23,16 @@ class MemoryModel: total:int, _in:int, _out:int, - maximum:int, minimum:int, - average:float + maximum:int, + average:int ) -> None: self.total:int = total self._in:int = _in self.out:int = _out - self.maximum:int = maximum self.minimum:int = minimum - self.average:float = average + self.maximum:int = maximum + self.average:int = int(average) class NetUseModel: def __init__(self:Self, _bytes:int, packets:int, errors:int) -> None: @@ -67,8 +68,8 @@ class DiskModel: class CandleTimeModel: def __init__(self:Self, start:int, end:int) -> None: - self.start:int = start - self.end:int = end + self.start:datetime.datetime = datetime.datetime.fromtimestamp(start) + self.end:datetime.datetime = datetime.datetime.fromtimestamp(end) class IterationModel: def __init__(self:Self, diff --git a/Python/requirements.txt b/Python/requirements.txt new file mode 100644 index 0000000..6033c8f --- /dev/null +++ b/Python/requirements.txt @@ -0,0 +1 @@ +pyodbc==5.3.0 \ No newline at end of file diff --git a/Python/run.py b/Python/run.py index e31ce30..8c91bec 100644 --- a/Python/run.py +++ b/Python/run.py @@ -13,7 +13,7 @@ inputs:dict[str, dict[str, Any|None]] = { "agents" : AgentsController, "sql_server" : SQLServerDriver, "web_server" : WebServerDriver, - "agents_dispatcher" : AgentsDispatcher, + "sql_server_agents" : AgentsDispatcher, } } diff --git a/README.md b/README.md index 4449d1b..291d521 100644 --- a/README.md +++ b/README.md @@ -45,4 +45,16 @@ Datos de envío Debian: 5. `int`: Paquetes enviados actualmente. 6. `int`: Errores en envío actualmente. -La idea es procesar el uso de red en Python. \ No newline at end of file +La idea es procesar el uso de red en Python. + +Para instalar el Driver cliente de SQL Server para usar con el Python ODBC sobre Debian. + +```bash +#!/bin/bash + +curl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc +sudo add-apt-repository "$(curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list)" +sudo apt update +sudo apt -y install msodbcsql18 mssql-tools18 unixodbc-dev + +``` \ No newline at end of file diff --git a/SQLServer/NucelarMonitor.server.01.remove.server.sql b/SQLServer/NucelarMonitor.server.01.remove.server.sql index 8dd8e84..0450f94 100644 --- a/SQLServer/NucelarMonitor.server.01.remove.server.sql +++ b/SQLServer/NucelarMonitor.server.01.remove.server.sql @@ -4,18 +4,19 @@ use NucelarMonitor if object_id(N'dbo.tables_drop', N'P') is not null drop procedure dbo.tables_drop go -create procedure dbo.tables_drop as begin +create procedure dbo.tables_drop as begin set nocount on - set nocount on - - -- Level Plains. + -- Plain 2. if object_id(N'dbo.MachineInterfacesTrafficPlain', N'U') is not null drop table dbo.MachineInterfacesTrafficPlain + + -- Plains 1. if object_id(N'dbo.MachineInterfacesPlain', N'U') is not null drop table dbo.MachineInterfacesPlain if object_id(N'dbo.MachineDisksPlain', N'U') is not null drop table dbo.MachineDisksPlain + + -- Plains 0. if object_id(N'dbo.MachinePlain', N'U') is not null drop table dbo.MachinePlain -- Level 2. - if object_id(N'dbo.MachineInterfacesData', N'U') is not null drop table dbo.MachineInterfacesData if object_id(N'dbo.MachineInterfacesTraffic', N'U') is not null drop table dbo.MachineInterfacesTraffic if object_id(N'dbo.MachineDisksSpace', N'U') is not null drop table dbo.MachineDisksSpace if object_id(N'dbo.Exceptions', N'U') is not null drop table dbo.Exceptions diff --git a/SQLServer/NucelarMonitor.server.02.create.server.sql b/SQLServer/NucelarMonitor.server.02.create.server.sql index 3c139d7..5e17f6a 100644 --- a/SQLServer/NucelarMonitor.server.02.create.server.sql +++ b/SQLServer/NucelarMonitor.server.02.create.server.sql @@ -4,9 +4,7 @@ use NucelarMonitor if object_id(N'dbo.tables_create', N'P') is not null drop procedure dbo.tables_create go -create procedure dbo.tables_create as begin - - set nocount on +create procedure dbo.tables_create as begin set nocount on -- Level 0. if object_id(N'dbo.Machines', N'U') is null create table dbo.Machines( @@ -16,8 +14,8 @@ create procedure dbo.tables_create as begin date_in datetime not null constraint machines_df_date_in default getdate(), date_out datetime, constraint machines_pk primary key clustered (id), - constraint machines_uk_key unique ([key]), - constraint machines_ck_key check ([key] like '[a-zA-Z0-9][a-zA-Z0-9_]{0,31}') + constraint machines_uk_key unique nonclustered ([key] asc) with (fillfactor = 90), + constraint machines_ck_key check ([key] not like '%[^a-zA-Z0-9_]%' and [key] like '[a-zA-Z_]%') ) if object_id(N'dbo.Types', N'U') is null create table dbo.Types( @@ -28,7 +26,7 @@ create procedure dbo.tables_create as begin date_out datetime, constraint types_pk primary key clustered (id), constraint types_uk_name unique nonclustered ([name] asc) with (fillfactor = 90), - constraint types_ck_name check ([name] like '[a-zA-Z_][a-zA-Z0-9_]{0,31}') + constraint types_ck_name check ([name] not like '%[^a-zA-Z0-9_]%' and [name] like '[a-zA-Z_]%') ) if object_id(N'dbo.Domains', N'U') is null create table dbo.Domains( @@ -39,9 +37,8 @@ create procedure dbo.tables_create as begin date_out datetime, constraint domains_pk primary key clustered (id), constraint domains_uk_domain unique nonclustered (domain asc) with (fillfactor = 90), - constraint domains_ck_domain check ([domain] like '[a-zA-Z0-9_-.]{1,64}') + constraint domains_ck_domain check (len(domain) > 0 and domain not like '%[^a-zA-Z0-9_-.]%') ) - if object_id(N'dbo.Hostnames', N'U') is null create table dbo.Hostnames( id integer not null identity(1, 1), @@ -51,7 +48,7 @@ create procedure dbo.tables_create as begin date_out datetime, constraint hostnames_pk primary key clustered (id), constraint hostnames_uk_name unique nonclustered ([name] asc) with (fillfactor = 90), - constraint hostnames_ck_name check ([name] like '[a-zA-Z0-9_-]{1,32}') + constraint hostnames_ck_name check (len([name]) > 0 and [name] not like '%[^a-zA-Z0-9_-]%') ) if object_id(N'dbo.Disks', N'U') is null create table dbo.Disks( @@ -64,11 +61,10 @@ create procedure dbo.tables_create as begin date_out datetime, constraint disks_pk primary key clustered (id), -- constraint disks_uk_name unique nonclustered ([name] asc, mountpoint asc) with (fillfactor = 90), - constraint disks_ck_name check ( - [name] like '[a-zA-Z0-9_-]{1,32}' or - mountpoint like '[A-Z]:' - ), - constraint disks_ck_size check (size >= 0 and size < power(2, 53)), + constraint disks_ck_name check (( + len([name]) > 0 and [name] not like '%[^a-zA-Z0-9_-]%' + ) or [name] like '[A-Z]:'), + constraint disks_ck_size check (size >= 0 and size < power(cast(2 as bigint), 53)), constraint disks_ck_mountpoint check ( mountpoint like '/%' or mountpoint like '[a-zA-Z]:\%' or @@ -84,7 +80,7 @@ create procedure dbo.tables_create as begin date_out datetime, constraint interfaces_pk primary key clustered (id), -- constraint interfaces_uk_machine_name unique nonclustered ([name] asc) with (fillfactor = 90), - constraint interfaces_ck_name check ([name] like '[a-zA-Z0-9_-]{1,32}') + constraint interfaces_ck_name check (len([name]) > 0 and [name] not like '%[^a-zA-Z0-9_-]%') ) if object_id(N'dbo.CandlesTimes', N'U') is null create table dbo.CandlesTimes( @@ -96,7 +92,8 @@ create procedure dbo.tables_create as begin date_out datetime, constraint candles_times_pk primary key clustered (id), constraint candles_times_uk_from_to unique nonclustered ([from] asc, [to] asc) with (fillfactor = 90), - constraint candles_times_ck_from_to check ([from] < [to]) + constraint candles_times_ck_from_to check ([from] < [to]), + constraint candles_times_ck_iterations check (iterations > 0) ) if object_id(N'dbo.BigData', N'U') is null create table dbo.BigData( @@ -115,7 +112,8 @@ create procedure dbo.tables_create as begin date_in datetime not null constraint messages_df_date_in default getdate(), date_out datetime, constraint messages_pk primary key clustered (id), - constraint messages_ck_key check ([key] like '[a-zA-Z_][a-zA-Z0-9_]{0,127}') + constraint messages_uk_key unique nonclustered ([key] asc) with (fillfactor = 90), + constraint messages_ck_key check ([key] not like '%[^a-zA-Z0-9_]%' and [key] like '[a-zA-Z_]%') ) if object_id(N'dbo.Databases', N'U') is null create table dbo.Databases( @@ -125,7 +123,7 @@ create procedure dbo.tables_create as begin date_out datetime, constraint databases_pk primary key clustered (id), constraint databases_uk_name unique nonclustered ([name] asc) with (fillfactor = 90), - constraint databases_ck_name check ([name] like '[a-zA-Z_][a-zA-Z0-9_]{0,63}') + constraint databases_ck_name check ([name] not like '%[^a-zA-Z0-9_]%' and [name] like '[a-zA-Z_]%') ) -- Level 1. @@ -189,29 +187,29 @@ create procedure dbo.tables_create as begin on update no action on delete no action, constraint machine_ram_ck_memory_total check ( - total between 0 and power(2, 53) - 1 + total between 0 and power(cast(2 as bigint), 53) - 1 ), constraint machine_ram_ck_memory_in check ( - [in] between 0 and power(2, 53) - 1 and + [in] between 0 and power(cast(2 as bigint), 53) - 1 and [in] between minimum and maximum and [in] <= total ), constraint machine_ram_ck_memory_out check ( - [out] between 0 and power(2, 53) - 1 and + [out] between 0 and power(cast(2 as bigint), 53) - 1 and [out] between minimum and maximum and [out] <= total ), constraint machine_ram_ck_memory_minimum check ( - minimum between 0 and power(2, 53) - 1 and + minimum between 0 and power(cast(2 as bigint), 53) - 1 and minimum <= total and minimum <= maximum ), constraint machine_ram_ck_memory_maximum check ( - maximum between 0 and power(2, 53) - 1 and - maximum between total and minimum + maximum between 0 and power(cast(2 as bigint), 53) - 1 and + maximum between minimum and total ), constraint machine_ram_ck_memory_average check ( - average between 0 and power(2, 53) - 1 and + average between 0 and power(cast(2 as bigint), 53) - 1 and average between minimum and maximum and average <= total ) @@ -237,9 +235,12 @@ create procedure dbo.tables_create as begin if object_id(N'dbo.MachineInterfaces', N'U') is null create table dbo.MachineInterfaces( id integer not null identity(1, 1), machine integer not null, - [interface] integer not null, + [interface] integer not null, belongs bit not null constraint machine_interfaces_df_belongs default 1, mounted bit not null constraint machine_interfaces_df_mounted default 1, + is_ipv6 bit not null constraint machine_interfaces_data_df_is_ipv6 default 0, + [address] varchar(45) not null, + mask tinyint not null, date_in datetime not null constraint machine_interfaces_df_date_in default getdate(), date_out datetime, constraint machine_interfaces_pk primary key clustered (id), @@ -262,7 +263,7 @@ create procedure dbo.tables_create as begin on update no action on delete no action, constraint procedures_uk_name unique nonclustered ([database] asc, [name] asc) with (fillfactor = 90), - constraint procedures_ck_name check ([name] like '[a-zA-Z_][a-zA-Z0-9_]{0,63}') + constraint procedures_ck_name check ([name] not like '%[^a-zA-Z0-9_]%' and [name] like '[a-zA-Z_]%') ) -- Level 2. @@ -281,7 +282,7 @@ create procedure dbo.tables_create as begin on update no action on delete no action, constraint machine_disks_space_ck_free check ( - free between 0 and power(2, 53) - 1 -- and + free between 0 and power(cast(2 as bigint), 53) - 1 -- and -- free <= isnull(( -- select top 1 disks.[size] -- from dbo.Disks disks @@ -301,7 +302,7 @@ create procedure dbo.tables_create as begin candle_time integer not null, [type] integer not null, bytes bigint not null, - packages integer not null, + packets integer not null, errors integer not null, date_in datetime not null constraint machine_interfaces_traffic_df_date_in default getdate(), date_out datetime, @@ -316,40 +317,40 @@ create procedure dbo.tables_create as begin on update no action on delete no action, constraint machine_interfaces_traffic_ck_bytes check ( - bytes between 0 and power(2, 53) - 1 + bytes between 0 and power(cast(2 as bigint), 53) - 1 ), - constraint machine_interfaces_traffic_ck_packages check ( - packages between 0 and power(2, 31) - 1 + constraint machine_interfaces_traffic_ck_packets check ( + packets between 0 and power(2, 30) - 1 ), constraint machine_interfaces_traffic_ck_errors check ( - errors between 0 and power(2, 31) - 1 + errors between 0 and power(2, 30) - 1 ) ) - if object_id(N'dbo.MachineInterfacesData', N'U') is null create table dbo.MachineInterfacesData( - id integer not null identity(1, 1), - machine_interface integer not null, - is_ipv6 bit not null constraint machine_interfaces_data_df_is_ipv6 default 0, - [address] varchar(45) not null, - mask tinyint not null, - date_in datetime not null constraint machine_interfaces_data_df_date_in default getdate(), - date_out datetime, - constraint machine_interfaces_data_pk primary key clustered (id), - constraint machine_interfaces_data_fk_machine_interface foreign key (machine_interface) references dbo.MachineInterfaces(id) - on update no action - on delete no action, - constraint machine_interfaces_data_ck_is_ipv6 check ( - (is_ipv6 = 0 and [address] like '[0-9]%.%.%.%') or - (is_ipv6 = 1 and [address] like '%:%') - ), - constraint machine_interfaces_data_ck_address check ( - ([address] like '[0-9]%.%.%.%' and mask between 0 and 32) or - ([address] like '%:%' and mask between 0 and 128) - ), - constraint machine_interfaces_data_ck_mask check ( - (mask between 0 and (case when is_ipv6 = 1 then 128 else 32 end)) - ) - ) + -- if object_id(N'dbo.MachineInterfacesData', N'U') is null create table dbo.MachineInterfacesData( + -- id integer not null identity(1, 1), + -- machine_interface integer not null, + -- is_ipv6 bit not null constraint machine_interfaces_data_df_is_ipv6 default 0, + -- [address] varchar(45) not null, + -- mask tinyint not null, + -- date_in datetime not null constraint machine_interfaces_data_df_date_in default getdate(), + -- date_out datetime, + -- constraint machine_interfaces_data_pk primary key clustered (id), + -- constraint machine_interfaces_data_fk_machine_interface foreign key (machine_interface) references dbo.MachineInterfaces(id) + -- on update no action + -- on delete no action, + -- constraint machine_interfaces_data_ck_is_ipv6 check ( + -- (is_ipv6 = 0 and [address] like '[0-9]%.%.%.%') or + -- (is_ipv6 = 1 and [address] like '%:%') + -- ), + -- constraint machine_interfaces_data_ck_address check ( + -- ([address] like '[0-9]%.%.%.%' and mask between 0 and 32) or + -- ([address] like '%:%' and mask between 0 and 128) + -- ), + -- constraint machine_interfaces_data_ck_mask check ( + -- (mask between 0 and (case when is_ipv6 = 1 then 128 else 32 end)) + -- ) + -- ) if object_id(N'dbo.Exceptions', N'U') is null create table dbo.Exceptions( id integer not null identity(1, 1), @@ -376,7 +377,7 @@ create procedure dbo.tables_create as begin on delete no action ) - -- Level Plains 1. + -- Level Plains 0. if object_id(N'dbo.MachinePlain', N'U') is null create table dbo.MachinePlain( id integer not null identity(1, 1), [key] varchar(32) not null, @@ -410,32 +411,32 @@ create procedure dbo.tables_create as begin constraint machine_plain_fk_machine_cpu foreign key (machine_cpu) references dbo.MachineCPU(id) on update no action on delete no action, - constraint machine_plain_uk_key unique ([key]), - constraint machine_plain_ck_key check ([key] like '[a-zA-Z0-9][a-zA-Z0-9_]{0,31}'), + constraint machine_plain_uk_key unique nonclustered ([key] asc) with (fillfactor = 90), + constraint machine_plain_ck_key check ([key] not like '%[^a-zA-Z0-9_]%' and [key] like '[a-zA-Z_]%'), constraint machine_plain_ck_memory_total check ( - ram_total between 0 and power(2, 53) - 1 + ram_total between 0 and power(cast(2 as bigint), 53) - 1 ), constraint machine_plain_ck_memory_in check ( - ram_in between 0 and power(2, 53) - 1 and + ram_in between 0 and power(cast(2 as bigint), 53) - 1 and ram_in between ram_minimum and ram_maximum and ram_in <= ram_total ), constraint machine_plain_ck_memory_out check ( - ram_out between 0 and power(2, 53) - 1 and + ram_out between 0 and power(cast(2 as bigint), 53) - 1 and ram_out between ram_minimum and ram_maximum and ram_out <= ram_total ), constraint machine_plain_ck_memory_minimum check ( - ram_minimum between 0 and power(2, 53) - 1 and + ram_minimum between 0 and power(cast(2 as bigint), 53) - 1 and ram_minimum <= ram_total and ram_minimum <= ram_maximum ), constraint machine_plain_ck_memory_maximum check ( - ram_maximum between 0 and power(2, 53) - 1 and - ram_maximum between ram_total and ram_minimum + ram_maximum between 0 and power(cast(2 as bigint), 53) - 1 and + ram_maximum between ram_minimum and ram_total ), constraint machine_plain_ck_memory_average check ( - ram_average between 0 and power(2, 53) - 1 and + ram_average between 0 and power(cast(2 as bigint), 53) - 1 and ram_average between ram_minimum and ram_maximum and ram_average <= ram_total ), @@ -464,10 +465,11 @@ create procedure dbo.tables_create as begin -- Level Plains 1. if object_id(N'dbo.MachineInterfacesPlain', N'U') is null create table dbo.MachineInterfacesPlain( id integer not null identity(1, 1), + plain integer not null, machine integer not null, interface integer not null, - machine_interface integer not null, - [name] varchar(32) not null, + machine_interface integer not null, + mounted bit not null constraint machine_interfaces_plain_df_mounted default 1, is_ipv6 bit not null constraint machine_interfaces_plain_df_is_ipv6 default 0, [address] varchar(45) not null, mask tinyint not null, @@ -475,41 +477,20 @@ create procedure dbo.tables_create as begin date_in datetime not null constraint machine_interfaces_plain_df_date_in default getdate(), date_out datetime, constraint machine_interfaces_plain_pk primary key clustered (id), + constraint machine_interfaces_plain_fk_plain foreign key (plain) references dbo.MachinePlain(id) + on update no action + on delete no action, constraint machine_interfaces_plain_fk_machine foreign key (machine) references dbo.Machines(id) on update no action on delete no action, constraint machine_interfaces_plain_fk_interface foreign key (interface) references dbo.Interfaces(id) on update no action on delete no action, + constraint machine_interfaces_plain_fk_machine_interface foreign key (machine_interface) references dbo.MachineInterfaces(id) + on update no action + on delete no action, constraint machine_interfaces_plain_uk_machine_interface unique nonclustered (machine asc, [interface] asc) with (fillfactor = 90), - -- constraint machine_interfaces_plain_uk_machine_name unique nonclustered ([name] asc) with (fillfactor = 90), - constraint machine_interfaces_plain_ck_name check ([name] like '[a-zA-Z0-9_-]{1,32}'), - constraint machine_interfaces_plain_ck_interface check ([interface] like '[a-zA-Z0-9_-]{1,32}') - ) - - if object_id(N'dbo.MachineInterfacesTrafficPlain', N'U') is null create table dbo.MachineInterfacesTrafficPlain( - id integer not null identity(1, 1), - machine_interface integer not null, - candle_time integer not null, - [type] integer not null, - bytes bigint not null, - packages integer not null, - errors integer not null, - date_in datetime not null constraint machine_interfaces_traffic_plain_df_date_in default getdate(), - date_out datetime, - constraint machine_interfaces_traffic_plain_pk primary key clustered (id), - constraint machine_interfaces_traffic_plain_fk_machine_interface foreign key (machine_interface) references dbo.MachineInterfacesPlain(id) - on update no action - on delete no action, - constraint machine_interfaces_traffic_plain_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) - on update no action - on delete no action, - constraint machine_interfaces_traffic_plain_fk_type foreign key ([type]) references dbo.Types(id) - on update no action - on delete no action, - constraint machine_interfaces_traffic_plain_ck_bytes check (bytes between 0 and power(2, 53) - 1), - constraint machine_interfaces_traffic_plain_ck_packages check (packages between 0 and power(2, 31) - 1), - constraint machine_interfaces_traffic_plain_ck_errors check (errors between 0 and power(2, 31) - 1) + -- constraint machine_interfaces_plain_uk_machine_name unique nonclustered ([name] asc) with (fillfactor = 90) ) if object_id(N'dbo.MachineDisksPlain', N'U') is null create table dbo.MachineDisksPlain( @@ -538,22 +519,47 @@ create procedure dbo.tables_create as begin on delete no action, constraint machine_disks_plain_uk_machine_disk unique nonclustered (machine asc, [disk] asc) with (fillfactor = 90), -- constraint machine_disks_plain_uk_name unique nonclustered ([name] asc, mountpoint asc) with (fillfactor = 90), - constraint machine_disks_plain_ck_name check ( - [name] like '[a-zA-Z0-9_-]{1,32}' or - mountpoint like '[A-Z]:' - ), - constraint machine_disks_plain_ck_size check (size >= 0 and size < power(2, 53)), + constraint machine_disks_plain_ck_name check (( + len([name]) > 0 and [name] not like '%[^a-zA-Z0-9_-]%' + ) or [name] like '[A-Z]:'), + constraint machine_disks_plain_ck_size check (size >= 0 and size < power(cast(2 as bigint), 53)), constraint machine_disks_plain_ck_mountpoint check ( mountpoint like '/%' or mountpoint like '[a-zA-Z]:\%' or mountpoint like '\\%' ), constraint machine_disks_plain_ck_free check ( - free between 0 and power(2, 53) - 1 and + free between 0 and power(cast(2 as bigint), 53) - 1 and free <= [size] ) ) + -- Plain 2. + if object_id(N'dbo.MachineInterfacesTrafficPlain', N'U') is null create table dbo.MachineInterfacesTrafficPlain( + id integer not null identity(1, 1), + plain integer not null, + candle_time integer not null, + [type] integer not null, + bytes bigint not null, + packets integer not null, + errors integer not null, + date_in datetime not null constraint machine_interfaces_traffic_plain_df_date_in default getdate(), + date_out datetime, + constraint machine_interfaces_traffic_plain_pk primary key clustered (id), + constraint machine_interfaces_traffic_plain_fk_plain foreign key (plain) references dbo.MachineInterfacesPlain(id) + on update no action + on delete no action, + constraint machine_interfaces_traffic_plain_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) + on update no action + on delete no action, + constraint machine_interfaces_traffic_plain_fk_type foreign key ([type]) references dbo.Types(id) + on update no action + on delete no action, + constraint machine_interfaces_traffic_plain_ck_bytes check (bytes between 0 and power(cast(2 as bigint), 53) - 1), + constraint machine_interfaces_traffic_plain_ck_packets check (packets between 0 and power(2, 30) - 1), + constraint machine_interfaces_traffic_plain_ck_errors check (errors between 0 and power(2, 30) - 1) + ) + end go diff --git a/SQLServer/NucelarMonitor.server.03.update.server.sql b/SQLServer/NucelarMonitor.server.03.update.server.sql index 240cdf0..bf044eb 100644 --- a/SQLServer/NucelarMonitor.server.03.update.server.sql +++ b/SQLServer/NucelarMonitor.server.03.update.server.sql @@ -4,9 +4,581 @@ use NucelarMonitor if object_id(N'dbo.tables_update', N'P') is not null drop procedure dbo.tables_update go -create procedure dbo.tables_update as begin +create procedure dbo.tables_update as begin set nocount on - set nocount on + declare @database varchar(64) = 'NucelarMonitor' + + -- Level 0. + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Machines' and column_name = 'key') is null + alter table dbo.Machines add + [key] varchar(32) not null, + constraint machines_uk_key unique nonclustered ([key] asc) with (fillfactor = 90), + constraint machines_ck_key check ([key] not like '%[^a-zA-Z0-9_]%' and [key] like '[a-zA-Z_]%') + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Machines' and column_name = 'description') is null + alter table dbo.Machines add [description] varchar(512) + + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Types' and column_name = 'name') is null + alter table dbo.Types add + [name] varchar(32) not null, + constraint types_uk_name unique nonclustered ([name] asc) with (fillfactor = 90), + constraint types_ck_name check ([name] not like '%[^a-zA-Z0-9_]%' and [name] like '[a-zA-Z_]%') + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Types' and column_name = 'description') is null + alter table dbo.Types add [description] varchar(512) + + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Domains' and column_name = 'domain') is null + alter table dbo.Domains add + domain varchar(64) not null, + constraint domains_uk_domain unique nonclustered (domain asc) with (fillfactor = 90), + constraint domains_ck_domain check (len(domain) > 0 and domain not like '%[^a-zA-Z0-9_-.]%') + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Domains' and column_name = 'description') is null + alter table dbo.Domains add [description] varchar(512) + + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Hostnames' and column_name = 'name') is null + alter table dbo.Hostnames add + [name] varchar(32) not null, + constraint hostnames_uk_name unique nonclustered ([name] asc) with (fillfactor = 90), + constraint hostnames_ck_name check (len([name]) > 0 and [name] not like '%[^a-zA-Z0-9_-]%') + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Hostnames' and column_name = 'description') is null + alter table dbo.Hostnames add [description] varchar(512) + + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Disks' and column_name = 'name') is null + alter table dbo.Disks add + [name] varchar(32) not null, + constraint disks_ck_name check (( + len([name]) > 0 and [name] not like '%[^a-zA-Z0-9_-]%' + ) or [name] like '[A-Z]:') + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Disks' and column_name = 'size') is null + alter table dbo.Disks add + [size] bigint not null, + constraint disks_ck_size check (size >= 0 and size < power(cast(2 as bigint), 53)) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Disks' and column_name = 'mountpoint') is null + alter table dbo.Disks add + mountpoint varchar(128) not null, + constraint disks_ck_mountpoint check ( + mountpoint like '/%' or + mountpoint like '[a-zA-Z]:\%' or + mountpoint like '\\%' + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Disks' and column_name = 'description') is null + alter table dbo.Disks add [description] varchar(512) + + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Interfaces' and column_name = 'name') is null + alter table dbo.Interfaces add + [name] varchar(32) not null, + constraint interfaces_ck_name check (len([name]) > 0 and [name] not like '%[^a-zA-Z0-9_-]%') + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Interfaces' and column_name = 'description') is null + alter table dbo.Interfaces add [description] varchar(512) + + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'CandlesTimes' and column_name = 'from') is null + alter table dbo.CandlesTimes add + [from] datetime not null, + constraint candles_times_ck_from check ([from] < [to]) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'CandlesTimes' and column_name = 'to') is null + alter table dbo.CandlesTimes add + [to] datetime not null, + constraint candles_times_ck_to check ([from] < [to]) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'CandlesTimes' and column_name = 'iterations') is null + alter table dbo.CandlesTimes add + iterations integer not null, + constraint candles_times_ck_iterations check (iterations > 0) + + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'BigData' and column_name = 'hash') is null + alter table dbo.BigData add + [hash] binary(64) not null, + constraint big_data_uk_hash unique nonclustered ([hash] asc) with (fillfactor = 90) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'BigData' and column_name = 'value') is null + alter table dbo.BigData add [value] varchar(max) not null + + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Messages' and column_name = 'key') is null + alter table dbo.Messages add + [key] varchar(128) not null, + constraint messages_uk_key unique nonclustered ([key] asc) with (fillfactor = 90), + constraint messages_ck_key check ([key] not like '%[^a-zA-Z0-9_]%' and [key] like '[a-zA-Z_]%') + + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Databases' and column_name = 'name') is null + alter table dbo.Databases add + [name] varchar(64) not null, + constraint databases_uk_name unique nonclustered ([name] asc) with (fillfactor = 90), + constraint databases_ck_name check ([name] not like '%[^a-zA-Z0-9_]%' and [name] like '[a-zA-Z_]%') + + -- Level 1. + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineCPU' and column_name = 'machine') is null + alter table dbo.MachineCPU add + machine integer not null, + constraint machine_cpu_fk_machine foreign key (machine) references dbo.Machines(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineCPU' and column_name = 'candle_time') is null + alter table dbo.MachineCPU add + candle_time integer not null, + constraint machine_cpu_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineCPU' and column_name = 'in') is null + alter table dbo.MachineCPU add + [in] float not null, + constraint machine_cpu_ck_cpu_in check ( + [in] between 0 and 100 and + [in] between minimum and maximum + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineCPU' and column_name = 'out') is null + alter table dbo.MachineCPU add + [out] float not null, + constraint machine_cpu_ck_cpu_out check ( + [out] between 0 and 100 and + [out] between minimum and maximum + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineCPU' and column_name = 'minimum') is null + alter table dbo.MachineCPU add + minimum float not null, + constraint machine_cpu_ck_cpu_minimum check ( + minimum between 0 and 100 and + minimum <= maximum + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineCPU' and column_name = 'maximum') is null + alter table dbo.MachineCPU add + maximum float not null, + constraint machine_cpu_ck_cpu_maximum check ( + maximum between 0 and 100 and + maximum >= minimum + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineCPU' and column_name = 'average') is null + alter table dbo.MachineCPU add + average float not null, + constraint machine_cpu_ck_cpu_average check ( + average between 0 and 100 and + average between minimum and maximum + ) + + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineRAM' and column_name = 'machine') is null + alter table dbo.MachineRAM add + machine integer not null, + constraint machine_ram_fk_machine foreign key (machine) references dbo.Machines(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineRAM' and column_name = 'candle_time') is null + alter table dbo.MachineRAM add + candle_time integer not null, + constraint machine_ram_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineRAM' and column_name = 'total') is null + alter table dbo.MachineRAM add + total float not null, + constraint machine_ram_ck_ram_total check ( + total between 0 and power(cast(2 as bigint), 53) - 1 + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineRAM' and column_name = 'in') is null + alter table dbo.MachineRAM add + [in] float not null, + constraint machine_ram_ck_ram_in check ( + [in] between 0 and power(cast(2 as bigint), 53) - 1 and + [in] between minimum and maximum and + [in] <= total + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineRAM' and column_name = 'out') is null + alter table dbo.MachineRAM add + [out] float not null, + constraint machine_ram_ck_ram_out check ( + [out] between 0 and power(cast(2 as bigint), 53) - 1 and + [out] between minimum and maximum and + [out] <= total + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineRAM' and column_name = 'minimum') is null + alter table dbo.MachineRAM add + minimum float not null, + constraint machine_ram_ck_ram_minimum check ( + minimum between 0 and power(cast(2 as bigint), 53) - 1 and + minimum <= total and + minimum <= maximum + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineRAM' and column_name = 'maximum') is null + alter table dbo.MachineRAM add + maximum float not null, + constraint machine_ram_ck_ram_maximum check ( + maximum between 0 and power(cast(2 as bigint), 53) - 1 and + maximum between minimum and total + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineRAM' and column_name = 'average') is null + alter table dbo.MachineRAM add + average float not null, + constraint machine_ram_ck_ram_average check ( + average between 0 and power(cast(2 as bigint), 53) - 1 and + average between minimum and maximum and + average <= total + ) + + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineDisks' and column_name = 'machine') is null + alter table dbo.MachineDisks add + machine integer not null, + constraint machine_disks_fk_machine foreign key (machine) references dbo.Machines(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineDisks' and column_name = 'disk') is null + alter table dbo.MachineDisks add + [disk] integer not null, + constraint machine_disks_fk_disk foreign key ([disk]) references dbo.Disks(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineDisks' and column_name = 'belongs') is null + alter table dbo.MachineDisks add belongs bit not null constraint machine_disks_df_belongs default 1 + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineDisks' and column_name = 'mounted') is null + alter table dbo.MachineDisks add mounted bit not null constraint machine_disks_df_mounted default 1 + + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfaces' and column_name = 'machine') is null + alter table dbo.MachineInterfaces add + machine integer not null, + constraint machine_interfaces_fk_machine foreign key (machine) references dbo.Machines(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfaces' and column_name = 'interface') is null + alter table dbo.MachineInterfaces add + [interface] integer not null, + constraint machine_interfaces_fk_interface foreign key ([interface]) references dbo.Interfaces(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfaces' and column_name = 'belongs') is null + alter table dbo.MachineInterfaces add belongs bit not null constraint machine_interfaces_df_belongs default 1 + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfaces' and column_name = 'mounted') is null + alter table dbo.MachineInterfaces add mounted bit not null constraint machine_interfaces_df_mounted default 1 + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfaces' and column_name = 'is_ipv6') is null + alter table dbo.MachineInterfaces add is_ipv6 bit not null constraint machine_interfaces_df_is_ipv6 default 0 + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfaces' and column_name = 'address') is null + alter table dbo.MachineInterfaces add [address] varchar(45) not null + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfaces' and column_name = 'mask') is null + alter table dbo.MachineInterfaces add mask tinyint not null + + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Procedures' and column_name = 'database') is null + alter table dbo.Procedures add + [database] integer not null, + constraint procedures_fk_database foreign key ([database]) references dbo.Databases(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Procedures' and column_name = 'name') is null + alter table dbo.Procedures add + [name] varchar(64) not null, + constraint procedures_uk_name unique nonclustered ([database] asc, [name] asc) with (fillfactor = 90), + constraint procedures_ck_name check ([name] not like '%[^a-zA-Z0-9_]%' and [name] like '[a-zA-Z_]%') + + -- Level 2. + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineDisksSpace' and column_name = 'machine_disk') is null + alter table dbo.MachineDisksSpace add + machine_disk integer not null, + constraint machine_disks_space_fk_machine_disk foreign key (machine_disk) references dbo.MachineDisks(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineDisksSpace' and column_name = 'candle_time') is null + alter table dbo.MachineDisksSpace add + candle_time integer not null, + constraint machine_disks_space_fk_candle_time foreign key (candle_time) references dbo.CandleTimes(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineDisksSpace' and column_name = 'candle_time') is null + alter table dbo.MachineDisksSpace add + free bigint not null, + constraint machine_disks_space_ck_free check (free between 0 and power(cast(2 as bigint), 53) - 1) + + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesTraffic' and column_name = 'machine_interface') is null + alter table dbo.MachineInterfacesTraffic add + machine_interface integer not null, + constraint machine_interfaces_traffic_fk_machine_interface foreign key (machine_interface) references dbo.MachineInterfaces(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesTraffic' and column_name = 'candle_time') is null + alter table dbo.MachineInterfacesTraffic add + candle_time integer not null, + constraint machine_interfaces_traffic_fk_candle_time foreign key (candle_time) references dbo.CandleTimes(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesTraffic' and column_name = 'type') is null + alter table dbo.MachineInterfacesTraffic add + [type] integer not null, + constraint machine_interfaces_traffic_fk_type foreign key ([type]) references dbo.Types(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesTraffic' and column_name = 'bytes') is null + alter table dbo.MachineInterfacesTraffic add + bytes bigint not null, + constraint machine_interfaces_traffic_ck_bytes check ( + bytes between 0 and power(cast(2 as bigint), 53) - 1 + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesTraffic' and column_name = 'packets') is null + alter table dbo.MachineInterfacesTraffic add + packets integer not null, + constraint machine_interfaces_traffic_ck_packets check ( + packets between 0 and power(2, 30) - 1 + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesTraffic' and column_name = 'errors') is null + alter table dbo.MachineInterfacesTraffic add + errors integer not null, + constraint machine_interfaces_traffic_ck_errors check ( + errors between 0 and power(2, 30) - 1 + ) + + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Exceptions' and column_name = 'procedure') is null + alter table dbo.Exceptions add + [procedure] integer not null, + constraint exceptions_fk_procedure foreign key ([procedure]) references dbo.Procedures(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Exceptions' and column_name = 'message') is null + alter table dbo.Exceptions add + [message] integer not null, + constraint exceptions_fk_message foreign key ([message]) references dbo.Messages(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Exceptions' and column_name = 'parameters') is null + alter table dbo.Exceptions add + parameters integer not null, + constraint exceptions_fk_parameters foreign key (parameters) references dbo.BigData(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Exceptions' and column_name = 'exception') is null + alter table dbo.Exceptions add + exception integer not null, + constraint exceptions_fk_exception foreign key (exception) references dbo.BigData(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Exceptions' and column_name = 'status') is null + alter table dbo.Exceptions add [status] varchar(16) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'Exceptions' and column_name = 'code') is null + alter table dbo.Exceptions add [code] integer + + -- Plain 0. + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachinePlain' and column_name = 'key') is null + alter table dbo.MachinePlain add + [key] varchar(32) not null, + constraint machine_plain_uk_key unique nonclustered ([key] asc) with (fillfactor = 90), + constraint machine_plain_ck_key check ([key] not like '%[^a-zA-Z0-9_]%' and [key] like '[a-zA-Z_]%') + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachinePlain' and column_name = 'machine') is null + alter table dbo.MachinePlain add + machine integer not null, + constraint machine_plain_fk_machine foreign key (machine) references dbo.Machines(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachinePlain' and column_name = 'candle_time') is null + alter table dbo.MachinePlain add + candle_time integer not null, + constraint machine_plain_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachinePlain' and column_name = 'machine_ram') is null + alter table dbo.MachinePlain add + machine_ram integer not null, + constraint machine_plain_fk_machine_ram foreign key (machine_ram) references dbo.MachinesRAM(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachinePlain' and column_name = 'machine_cpu') is null + alter table dbo.MachinePlain add + machine_cpu integer not null, + constraint machine_plain_fk_machine_cpu foreign key (machine_cpu) references dbo.MachinesCPU(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachinePlain' and column_name = 'ram_total') is null + alter table dbo.MachinePlain add + ram_total float not null, + constraint machine_plain_ck_ram_total check ( + ram_total between 0 and power(cast(2 as bigint), 53) - 1 + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachinePlain' and column_name = 'ram_in') is null + alter table dbo.MachinePlain add + ram_in float not null, + constraint machine_plain_ck_ram_in check ( + ram_in between 0 and power(cast(2 as bigint), 53) - 1 and + ram_in between ram_minimum and ram_maximum and + ram_in <= ram_total + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachinePlain' and column_name = 'ram_out') is null + alter table dbo.MachinePlain add + ram_out float not null, + constraint machine_plain_ck_ram_out check ( + ram_out between 0 and power(cast(2 as bigint), 53) - 1 and + ram_out between ram_minimum and ram_maximum and + ram_out <= ram_total + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachinePlain' and column_name = 'ram_minimum') is null + alter table dbo.MachinePlain add + ram_minimum float not null, + constraint machine_plain_ck_ram_minimum check ( + ram_minimum between 0 and power(cast(2 as bigint), 53) - 1 and + ram_minimum <= ram_total and + ram_minimum <= ram_maximum + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachinePlain' and column_name = 'ram_maximum') is null + alter table dbo.MachinePlain add + ram_maximum float not null, + constraint machine_plain_ck_ram_maximum check ( + ram_maximum between 0 and power(cast(2 as bigint), 53) - 1 and + ram_maximum between ram_minimum and ram_total + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachinePlain' and column_name = 'ram_average') is null + alter table dbo.MachinePlain add + ram_average float not null, + constraint machine_plain_ck_ram_average check ( + ram_average between 0 and power(cast(2 as bigint), 53) - 1 and + ram_average between ram_minimum and ram_maximum and + ram_average <= ram_total + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachinePlain' and column_name = 'cpu_in') is null + alter table dbo.MachinePlain add + cpu_in float not null, + constraint machine_plain_ck_cpu_in check ( + cpu_in between 0 and 100 and + cpu_in between cpu_minimum and cpu_maximum + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachinePlain' and column_name = 'cpu_out') is null + alter table dbo.MachinePlain add + cpu_out float not null, + constraint machine_plain_ck_cpu_out check ( + cpu_out between 0 and 100 and + cpu_out between cpu_minimum and cpu_maximum + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachinePlain' and column_name = 'cpu_minimum') is null + alter table dbo.MachinePlain add + cpu_minimum float not null, + constraint machine_plain_ck_cpu_minimum check ( + cpu_minimum between 0 and 100 and + cpu_minimum <= cpu_maximum + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachinePlain' and column_name = 'cpu_maximum') is null + alter table dbo.MachinePlain add + cpu_maximum float not null, + constraint machine_plain_ck_cpu_maximum check ( + cpu_maximum between 0 and 100 and + cpu_maximum >= cpu_minimum + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachinePlain' and column_name = 'cpu_average') is null + alter table dbo.MachinePlain add + cpu_average float not null, + constraint machine_plain_ck_cpu_average check ( + cpu_average between 0 and 100 and + cpu_average between cpu_minimum and cpu_maximum + ) + + -- Plain 1. + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesPlain' and column_name = 'plain') is null + alter table dbo.MachineInterfacesPlain add + plain integer not null, + constraint machine_interfaces_plain_fk_plain foreign key (plain) references dbo.MachinePlain(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesPlain' and column_name = 'machine') is null + alter table dbo.MachineInterfacesPlain add + machine integer not null, + constraint machine_interfaces_plain_fk_machine foreign key (machine) references dbo.Machines(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesPlain' and column_name = 'interface') is null + alter table dbo.MachineInterfacesPlain add + interface integer not null, + constraint machine_interfaces_plain_fk_interface foreign key (interface) references dbo.Interfaces(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesPlain' and column_name = 'machine_interface') is null + alter table dbo.MachineInterfacesPlain add + machine_interface integer not null, + constraint machine_interfaces_plain_fk_machine_interface foreign key (machine_interface) references dbo.MachineInterfaces(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesPlain' and column_name = 'mounted') is null + alter table dbo.MachineInterfacesPlain add mounted bit not null constraint machine_interfaces_plain_df_mounted default 1 + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesPlain' and column_name = 'is_ipv6') is null + alter table dbo.MachineInterfacesPlain add is_ipv6 bit not null constraint machine_interfaces_plain_df_is_ipv6 default 0 + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesPlain' and column_name = 'address') is null + alter table dbo.MachineInterfacesPlain add [address] varchar(45) not null + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesPlain' and column_name = 'mask') is null + alter table dbo.MachineInterfacesPlain add mask tinyint not null + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesPlain' and column_name = 'belongs') is null + alter table dbo.MachineInterfacesPlain add belongs bit not null constraint machine_interfaces_plain_df_belongs default 1 + + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineDisksPlain' and column_name = 'plain') is null + alter table dbo.MachineDisksPlain add + plain integer not null, + constraint machine_disks_plain_fk_plain foreign key (plain) references dbo.MachinePlain(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineDisksPlain' and column_name = 'machine') is null + alter table dbo.MachineDisksPlain add + machine integer not null, + constraint machine_disks_plain_fk_machine foreign key (machine) references dbo.Machines(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineDisksPlain' and column_name = 'disk') is null + alter table dbo.MachineDisksPlain add + disk integer not null, + constraint machine_disks_plain_fk_disk foreign key (disk) references dbo.Disks(id) + on update no action + on delete no action, + constraint machine_disks_plain_uk_machine_disk unique nonclustered (machine asc, [disk] asc) with (fillfactor = 90) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineDisksPlain' and column_name = 'candle_time') is null + alter table dbo.MachineDisksPlain add + candle_time integer not null, + constraint machine_disks_plain_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineDisksPlain' and column_name = 'name') is null + alter table dbo.MachineDisksPlain add + [name] varchar(32) not null, + constraint machine_disks_plain_ck_name check (( + len([name]) > 0 and [name] not like '%[^a-zA-Z0-9_-]%' + ) or [name] like '[A-Z]:') + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineDisksPlain' and column_name = 'size') is null + alter table dbo.MachineDisksPlain add + [size] bigint not null, + constraint machine_disks_plain_ck_size check (size >= 0 and size < power(cast(2 as bigint), 53)) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineDisksPlain' and column_name = 'mountpoint') is null + alter table dbo.MachineDisksPlain add + mountpoint varchar(128) not null, + constraint machine_disks_plain_ck_mountpoint check ( + mountpoint like '/%' or + mountpoint like '[a-zA-Z]:\%' or + mountpoint like '\\%' + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineDisksPlain' and column_name = 'free') is null + alter table dbo.MachineDisksPlain add + free bigint not null, + constraint machine_disks_plain_ck_free check ( + free between 0 and power(cast(2 as bigint), 53) - 1 and + free <= [size] + ) + + -- Plain 2. + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesTrafficPlain' and column_name = 'plain') is null + alter table dbo.MachineInterfacesTrafficPlain add + plain integer not null, + constraint machine_interfaces_traffic_plain_fk_plain foreign key (plain) references dbo.MachineInterfacesPlain(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesTrafficPlain' and column_name = 'candle_time') is null + alter table dbo.MachineInterfacesTrafficPlain add + candle_time integer not null, + constraint machine_interfaces_traffic_plain_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesTrafficPlain' and column_name = 'type') is null + alter table dbo.MachineInterfacesTrafficPlain add + [type] integer not null, + constraint machine_interfaces_traffic_plain_fk_type foreign key ([type]) references dbo.CandlesTimes(id) + on update no action + on delete no action + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesTrafficPlain' and column_name = 'bytes') is null + alter table dbo.MachineInterfacesTrafficPlain add + bytes bigint not null, + constraint machine_interfaces_traffic_plain_ck_bytes check ( + bytes between 0 and power(cast(2 as bigint), 53) - 1 + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesTrafficPlain' and column_name = 'packets') is null + alter table dbo.MachineInterfacesTrafficPlain add + packets bigint not null, + constraint machine_interfaces_traffic_plain_ck_packets check ( + packets between 0 and power(2, 30) - 1 + ) + if (select top 1 0 from information_schema.columns where table_catalog = @database and table_name = 'MachineInterfacesTrafficPlain' and column_name = 'errors') is null + alter table dbo.MachineInterfacesTrafficPlain add + errors bigint not null, + constraint machine_interfaces_traffic_plain_ck_errors check ( + errors between 0 and power(2, 30) - 1 + ) end go diff --git a/SQLServer/NucelarMonitor.server.04.fill.server.sql b/SQLServer/NucelarMonitor.server.04.fill.server.sql index 59cfb23..6dcb883 100644 --- a/SQLServer/NucelarMonitor.server.04.fill.server.sql +++ b/SQLServer/NucelarMonitor.server.04.fill.server.sql @@ -4,9 +4,7 @@ use NucelarMonitor if object_id(N'dbo.tables_fill', N'P') is not null drop procedure dbo.tables_fill go -create procedure dbo.tables_fill as begin - - set nocount on +create procedure dbo.tables_fill as begin set nocount on end go diff --git a/SQLServer/NucelarMonitor.server.05.common.server.sql b/SQLServer/NucelarMonitor.server.05.common.server.sql index 012dfd9..e93b88a 100644 --- a/SQLServer/NucelarMonitor.server.05.common.server.sql +++ b/SQLServer/NucelarMonitor.server.05.common.server.sql @@ -4,7 +4,9 @@ use NucelarMonitor if object_id(N'dbo.hash_big_data', N'FN') is not null drop function dbo.hash_big_data go -create function dbo.hash_big_data (@value varchar(max)) returns binary(64) begin +create function dbo.hash_big_data ( + @value varchar(max) +) returns binary(64) begin return (select top 1 convert(binary(64), hashbytes('SHA2_256', convert(nvarchar(max), @value)))) end go \ No newline at end of file diff --git a/SQLServer/NucelarMonitor.server.07.getters.server.sql b/SQLServer/NucelarMonitor.server.07.getters.server.sql index 0e62ed3..20870ff 100644 --- a/SQLServer/NucelarMonitor.server.07.getters.server.sql +++ b/SQLServer/NucelarMonitor.server.07.getters.server.sql @@ -8,7 +8,7 @@ create procedure dbo.get_procedure @database varchar(64), @procedure varchar(64), @id integer output -as begin +as begin set nocount on declare @database_id integer = ( select top 1 id from dbo.Databases where @@ -16,8 +16,6 @@ as begin [name] = @database ) - set nocount on - if @database_id is null begin insert into dbo.Databases([name]) values (@database) set @database_id = scope_identity() @@ -43,12 +41,10 @@ go create procedure dbo.get_big_data @value varchar(max), @id integer output -as begin +as begin set nocount on declare @hash binary(64) = dbo.hash_big_data(@value) - set nocount on - set @id = ( select top 1 id from dbo.BigData where date_out is null and @@ -68,9 +64,7 @@ go create procedure dbo.get_message @message varchar(128), @id integer output -as begin - - set nocount on +as begin set nocount on set @id = ( select top 1 id from dbo.Messages where @@ -126,6 +120,7 @@ as begin set nocount on end end +go if object_id(N'dbo.get_basic_data_ids', N'P') is not null drop procedure dbo.get_basic_data_ids go @@ -201,4 +196,26 @@ as begin set nocount on execute dbo.get_interface @name, @interface_id output end +go + +if object_id(N'dbo.get_candle_time', N'P') is not null drop procedure dbo.get_candle_time +go +create procedure dbo.get_candle_time + @candle_start datetime, + @candle_end datetime, + @candle_interations integer, + @id integer output +as begin set nocount on + set @id = ( + select top 1 id from dbo.CandlesTimes where + date_out is null and + [from] = @candle_start and + [to] = @candle_end and + iterations = @candle_interations + ) + if @id is null begin + insert into dbo.CandlesTimes([from], [to], iterations) values (@candle_start, @candle_end, @candle_interations) + set @id = scope_identity() + end +end go \ No newline at end of file diff --git a/SQLServer/NucelarMonitor.server.08.setter.server.sql b/SQLServer/NucelarMonitor.server.08.setter.server.sql index 61213a6..8083237 100644 --- a/SQLServer/NucelarMonitor.server.08.setter.server.sql +++ b/SQLServer/NucelarMonitor.server.08.setter.server.sql @@ -9,7 +9,7 @@ create procedure dbo.set_exception @procedure varchar(64), @message varchar(128), @parameters varchar(max) = null -as begin +as begin set nocount on declare @procedure_id integer declare @message_id integer @@ -17,8 +17,6 @@ as begin declare @exception_message_id integer declare @parameters_id integer - set nocount on - execute dbo.get_procedure @database, @procedure, @procedure_id output execute dbo.get_big_data @exception_message, @exception_message_id output execute dbo.get_big_data @parameters, @parameters_id output @@ -73,13 +71,13 @@ as begin set nocount on if (select top 1 id from dbo.MachinePlain where date_out is null and - machine = @machine_id and - candle_time = @candle_time_id + machine = @machine_id ) is null insert into dbo.MachinePlain([key], machine, candle_time, machine_ram, machine_cpu, ram_total, ram_in, ram_out, ram_minimum, ram_maximum, ram_average, cpu_in, cpu_out, cpu_minimum, cpu_maximum, cpu_average) values (@key, @machine_id, @candle_time_id, @machine_ram_id, @machine_cpu_id, @ram_total, @ram_in, @ram_out, @ram_minimum, @ram_maximum, @ram_average, @cpu_in, @cpu_out, @cpu_minimum, @cpu_maximum, @cpu_average) else update dbo.MachinePlain set + candle_time = @candle_time_id, machine_ram = @machine_ram_id, machine_cpu = @machine_cpu_id, ram_total = @ram_total, @@ -95,8 +93,7 @@ as begin set nocount on cpu_average = @cpu_average where date_out is null and - [key] = @key and - candle_time = @candle_time_id + [key] = @key end go @@ -106,67 +103,55 @@ go create procedure dbo.set_machine_interface_data @key varchar(32), @name varchar(32), - @bytes bigint, - @packages integer, - @errors integer, @is_ipv6 bit, @address varchar(45), - @mask tinyint + @mask tinyint, + @mounted bit, + @id integer output as begin set nocount on declare @machine_id integer declare @interface_id integer - declare @machine_interface_id integer - declare @machine_interface_data_id integer + declare @plain_id integer execute dbo.get_interface @name, @interface_id output execute dbo.get_machine @key, @machine_id output + set @plain_id = (select top 1 id from dbo.MachinePlain where date_out is null and machine = @machine_id) - set @machine_interface_id = ( + set @id = ( select top 1 id from dbo.MachineInterfaces where date_out is null and machine = @machine_id and [interface] = @interface_id ) - if @machine_interface_id is null begin - insert into dbo.MachineInterfaces(machine, [interface]) values (@machine_id, @interface_id) - set @machine_interface_id = scope_identity() - end - - set @machine_interface_data_id = ( - select top 1 id from dbo.MachineInterfacesData where - date_out is null and - machine_interface = @machine_interface_id and + if @id is null begin + insert into dbo.MachineInterfaces(machine, [interface], mounted, is_ipv6, [address], mask) values + (@machine_id, @interface_id, @mounted, @is_ipv6, @address, @mask) + set @id = scope_identity() + insert into dbo.MachineInterfacesPlain(plain, machine, [interface], machine_interface, mounted, is_ipv6, [address], mask) values + (@plain_id, @machine_id, @interface_id, @id, @mounted, @is_ipv6, @address, @mask) + end else if ( + select top 1 0 from dbo.MachineInterfaces where + id = @id and + mounted = @mounted and is_ipv6 = @is_ipv6 and [address] = @address and mask = @mask - ) - - if @machine_interface_data_id is null begin - - insert into dbo.MachineInterfacesData(machine_interface, is_ipv6, [address], mask) values - (@machine_interface_id, @is_ipv6, @address, @mask) - set @machine_interface_data_id = scope_identity() - - if (select top 1 0 from dbo.MachineInterfacesPlain where + ) is null begin + insert into dbo.MachineInterfaces(machine, [interface], mounted, is_ipv6, [address], mask) values + (@machine_id, @interface_id, @mounted, @is_ipv6, @address, @mask) + set @id = scope_identity() + update dbo.MachineInterfacesPlain set + machine_interface = @id, + mounted = @mounted, + is_ipv6 = @is_ipv6, + [address] = @address, + mask = @mask + where date_out is null and - machine = @machine_id and - [interface] = @interface_id - ) is null - insert into dbo.MachineInterfacesPlain(plain, machine, [interface], machine_interface, [name], is_ipv6, [address], mask) values - (null, @machine_id, @interface_id, @machine_interface_id, @name, @is_ipv6, @address, @mask) - else - update dbo.MachineInterfacesPlain set - machine_interface = @machine_interface_id, - is_ipv6 = @is_ipv6, - [address] = @address, - mask = @mask - where - date_out is null and - machine = @machine_id and - [interface] = @interface_id - + plain = @plain_id and + [interface] = @interface_id end end @@ -175,65 +160,45 @@ go if object_id(N'dbo.set_machine_interface_traffic', N'P') is not null drop procedure dbo.set_machine_interface_traffic go create procedure dbo.set_machine_interface_traffic - @key varchar(32), - @name varchar(32), + @id integer, @type varchar(32), @candle_start datetime, @candle_end datetime, @candle_interations integer, @bytes bigint, @packages integer, - @errors integer, + @errors integer as begin set nocount on - declare @machine_id integer declare @type_id integer declare @candle_time_id integer - declare @interface_id integer - declare @machine_interface_id integer declare @plain_id integer - - execute dbo.get_basic_data_ids - @key, - @candle_start, - @candle_end, - @candle_interations, - @machine_id output, - @candle_time_id output + execute dbo.get_type @type, @type_id output - execute dbo.get_interface @name, @interface_id output - - set @machine_interface_id = ( - select top 1 id from dbo.MachineInterfaces where - date_out is null and - machine = @machine_id and - [interface] = @interface_id - ) + execute dbo.get_candle_time @candle_start, @candle_end, @candle_interations, @candle_time_id output + set @plain_id = (select top 1 id from dbo.MachineInterfacesPlain where date_out is null and machine_interface = @id) insert into dbo.MachineInterfacesTraffic(machine_interface, [type], candle_time, bytes, packages, errors) values - (@machine_interface_id, @type_id, @candle_time_id, @bytes, @packages, @errors) - - set @plain_id = (select top 1 id from dbo.MachineInterfacesPlain where - date_out is null and - machine = @machine_id and - [interface] = @interface_id - ) + (@id, @type_id, @candle_time_id, @bytes, @packages, @errors) if (select top 1 0 from dbo.MachineInterfacesTrafficPlain where date_out is null and - machine_interface = @machine_interface_id and - [type] = @type_id + plain = @plain_id and + [type] = @type_id and + candle_time = @candle_time_id ) is null - insert into dbo.MachineInterfacesTrafficPlain(plain, machine_interface, candle_time, [type], bytes, packages, errors) values - (@plain_id, @machine_interface_id, @candle_time_id, @type_id, @bytes, @packages, @errors) + insert into dbo.MachineInterfacesTrafficPlain(plain, candle_time, [type], bytes, packages, errors) values + (@plain_id, @candle_time_id, @type_id, @bytes, @packages, @errors) else update dbo.MachineInterfacesTrafficPlain set bytes = @bytes, - candle_time = @candle_time_id + packages = @packages, + errors = @errors where date_out is null and - machine_interface = @machine_interface_id and - [type] = @type_id + plain = @plain_id and + [type] = @type_id and + candle_time = @candle_time_id end go \ No newline at end of file diff --git a/Tools/NucelarMonitor.sh b/Tools/NucelarMonitor.sh index b51fe32..d449427 100755 --- a/Tools/NucelarMonitor.sh +++ b/Tools/NucelarMonitor.sh @@ -1,3 +1,11 @@ #!/bin/bash cd `dirname $(readlink -f "$0")`/../Python +if [ ! -d .venv ];then + python3 -m venv .venv + source .venv/bin/activate + pip install --upgrade pip + [ -f requirements.txt ] && pip install -r requirements.txt +else + source .venv/bin/activate +fi python3 "run.py" \ No newline at end of file