From f5df8eb24c48985aac8563f6d89362ac66d3897b Mon Sep 17 00:00:00 2001 From: Enrique Martin Date: Wed, 2 Aug 2023 13:13:43 +0200 Subject: [PATCH] Added Agent class functions --- .../extras/pandoraPlugintools/agents.py | 140 +++++++++++++- .../extras/pandoraPlugintools/discovery.py | 1 + .../extras/pandoraPlugintools/http.py | 1 + .../extras/pandoraPlugintools/modules.py | 177 +++++++++++++----- .../extras/pandoraPlugintools/threads.py | 1 + .../extras/pandoraPlugintools/transfer.py | 2 +- 6 files changed, 274 insertions(+), 48 deletions(-) diff --git a/pandora_server/extras/pandoraPlugintools/agents.py b/pandora_server/extras/pandoraPlugintools/agents.py index 0f95c615cc..610b3f6587 100644 --- a/pandora_server/extras/pandoraPlugintools/agents.py +++ b/pandora_server/extras/pandoraPlugintools/agents.py @@ -3,8 +3,8 @@ from subprocess import * import hashlib import sys import os -from .general import now,set_dict_key_value -from .modules import print_module,print_log_module +from .general import debug_dict,now,set_dict_key_value,generate_md5 +from .modules import init_module,init_log_module,print_module,print_log_module #### # Define global variables dict, used in functions as default values. @@ -60,7 +60,8 @@ class Agent: def __init__( self, config: dict = None, - modules_def: list = [] + modules_def: list = [], + log_modules_def: list = [] ): if config is None: @@ -68,6 +69,137 @@ class Agent: self.config = config self.modules_def = modules_def + self.log_modules_def = log_modules_def + self.added_modules = [] + + ''' + TODO: Add commnets + ''' + def update_config( + self, + config: dict = {} + ): + + for key, value in config.items(): + if key in self.config: + self.config[key] = value + + ''' + TODO: Add commnets + ''' + def get_config( + self + ) -> dict: + + return self.config + + ''' + TODO: Add commnets + ''' + def add_module( + self, + module: dict = {} + ): + + if "name" in module and type(module["name"]) == str and len(module["name"].strip()) > 0: + self.modules_def.append(init_module(module)) + self.added_modules.append(generate_md5(module["name"])) + + ''' + TODO: Add commnets + ''' + def del_module( + self, + module_name: str = "" + ): + + if len(module_name.strip()) > 0: + try: + module_id = self.added_modules.index(generate_md5(module_name)) + except: + module_id = None + + if module_id is not None: + self.added_modules.pop(module_id) + self.modules_def.pop(module_id) + + ''' + TODO: Add commnets + ''' + def update_module( + self, + module_name: str = "", + module: dict = {} + ): + + module_def = self.get_module(module_name) + + if module_def: + if "name" not in module: + module["name"] = module_name + + module_def.update(module) + + self.del_module(module_name) + self.add_module(module_def) + + ''' + TODO: Add commnets + ''' + def get_module( + self, + module_name: str = "" + ) -> dict: + + if len(module_name.strip()) > 0: + try: + module_id = self.added_modules.index(generate_md5(module_name)) + except: + module_id = None + + if module_id is not None: + return self.modules_def[module_id] + else: + return {} + + ''' + TODO: Add commnets + ''' + def get_modules_def( + self + ) -> dict: + + return self.modules_def + + ''' + TODO: Add commnets + ''' + def add_log_module( + self, + log_module: dict = {} + ): + + if "source" in module and type(module["source"]) == str and len(module["source"].strip()) > 0: + self.log_modules_def.append(init_log_module(log_module)) + + ''' + TODO: Add commnets + ''' + def get_log_modules_def( + self + ) -> dict: + + return self.log_modules_def + + ''' + TODO: Add commnets + ''' + def print_xml( + self, + print_flag: bool = False + ) -> str: + + return print_agent(self.get_config(), self.get_modules_def(), self.get_log_modules_def(), print_flag) #### # Init agent template @@ -93,7 +225,7 @@ def init_agent( "address" : "", "group" : GLOBAL_VARIABLES['agents_group_name'], "interval" : GLOBAL_VARIABLES['interval'], - "agent_mode" : "1", + "agent_mode" : "1" } for key, value in default_values.items(): diff --git a/pandora_server/extras/pandoraPlugintools/discovery.py b/pandora_server/extras/pandoraPlugintools/discovery.py index d2a4bad632..4954100353 100644 --- a/pandora_server/extras/pandoraPlugintools/discovery.py +++ b/pandora_server/extras/pandoraPlugintools/discovery.py @@ -1,5 +1,6 @@ import sys import json +from .general import debug_dict #### # Define some global variables diff --git a/pandora_server/extras/pandoraPlugintools/http.py b/pandora_server/extras/pandoraPlugintools/http.py index 913655ecb6..c7f8cb253f 100644 --- a/pandora_server/extras/pandoraPlugintools/http.py +++ b/pandora_server/extras/pandoraPlugintools/http.py @@ -2,6 +2,7 @@ from requests_ntlm import HttpNtlmAuth from requests.auth import HTTPBasicAuth from requests.auth import HTTPDigestAuth from requests.sessions import Session +from .general import debug_dict #### # Auth URL session diff --git a/pandora_server/extras/pandoraPlugintools/modules.py b/pandora_server/extras/pandoraPlugintools/modules.py index 2e9c7449a6..830795afc9 100644 --- a/pandora_server/extras/pandoraPlugintools/modules.py +++ b/pandora_server/extras/pandoraPlugintools/modules.py @@ -1,3 +1,71 @@ +from .general import debug_dict + +#### +# Init module template +######################################################################################### +def init_module( + default_values: dict = {} + ) -> dict: + """ + Initializes a module template with default values. + + Returns: + dict: Dictionary representing the module template with default values. + """ + module = { + "name" : None, + "type" : "generic_data_string", + "value" : "0", + "desc" : "", + "unit" : "", + "interval" : "", + "tags" : "", + "module_group" : "", + "module_parent" : "", + "min_warning" : "", + "min_warning_forced" : "", + "max_warning" : "", + "max_warning_forced" : "", + "min_critical" : "", + "min_critical_forced" : "", + "max_critical" : "", + "max_critical_forced" : "", + "str_warning" : "", + "str_warning_forced" : "", + "str_critical" : "", + "str_critical_forced" : "", + "critical_inverse" : "", + "warning_inverse" : "", + "max" : "", + "min" : "", + "post_process" : "", + "disabled" : "", + "min_ff_event" : "", + "status" : "", + "timestamp" : "", + "custom_id" : "", + "critical_instructions" : "", + "warning_instructions" : "", + "unknown_instructions" : "", + "quiet" : "", + "module_ff_interval" : "", + "crontab" : "", + "min_ff_event_normal" : "", + "min_ff_event_warning" : "", + "min_ff_event_critical" : "", + "ff_type" : "", + "ff_timeout" : "", + "each_ff" : "", + "module_parent_unlink" : "", + "alert" : [] + } + + for key, value in default_values.items(): + if key in module: + module[key] = value + + return module + #### # Returns module in XML format. Accepts only {dict} ######################################################################################### @@ -36,91 +104,92 @@ def print_module( else: module_xml += "\t\n" - if "desc" in data: + if "desc" in data and len(str(data["desc"]).strip()) > 0: module_xml += "\t\n" - if "unit" in data: + if "unit" in data and len(str(data["unit"]).strip()) > 0: module_xml += "\t\n" - if "interval" in data: + if "interval" in data and len(str(data["interval"]).strip()) > 0: module_xml += "\t\n" - if "tags" in data: + if "tags" in data and len(str(data["tags"]).strip()) > 0: module_xml += "\t" + str(data["tags"]) + "\n" - if "module_group" in data: + if "module_group" in data and len(str(data["module_group"]).strip()) > 0: module_xml += "\t" + str(data["module_group"]) + "\n" - if "module_parent" in data: + if "module_parent" in data and len(str(data["module_parent"]).strip()) > 0: module_xml += "\t" + str(data["module_parent"]) + "\n" - if "min_warning" in data: + if "min_warning" in data and len(str(data["min_warning"]).strip()) > 0: module_xml += "\t\n" - if "min_warning_forced" in data: + if "min_warning_forced" in data and len(str(data["min_warning_forced"]).strip()) > 0: module_xml += "\t\n" - if "max_warning" in data: + if "max_warning" in data and len(str(data["max_warning"]).strip()) > 0: module_xml += "\t\n" - if "max_warning_forced" in data: + if "max_warning_forced" in data and len(str(data["max_warning_forced"]).strip()) > 0: module_xml += "\t\n" - if "min_critical" in data: + if "min_critical" in data and len(str(data["min_critical"]).strip()) > 0: module_xml += "\t\n" - if "min_critical_forced" in data: + if "min_critical_forced" in data and len(str(data["min_critical_forced"]).strip()) > 0: module_xml += "\t\n" - if "max_critical" in data: + if "max_critical" in data and len(str(data["max_critical"]).strip()) > 0: module_xml += "\t\n" - if "max_critical_forced" in data: + if "max_critical_forced" in data and len(str(data["max_critical_forced"]).strip()) > 0: module_xml += "\t\n" - if "str_warning" in data: + if "str_warning" in data and len(str(data["str_warning"]).strip()) > 0: module_xml += "\t\n" - if "str_warning_forced" in data: + if "str_warning_forced" in data and len(str(data["str_warning_forced"]).strip()) > 0: module_xml += "\t\n" - if "str_critical" in data: + if "str_critical" in data and len(str(data["str_critical"]).strip()) > 0: module_xml += "\t\n" - if "str_critical_forced" in data: + if "str_critical_forced" in data and len(str(data["str_critical_forced"]).strip()) > 0: module_xml += "\t\n" - if "critical_inverse" in data: + if "critical_inverse" in data and len(str(data["critical_inverse"]).strip()) > 0: module_xml += "\t\n" - if "warning_inverse" in data: + if "warning_inverse" in data and len(str(data["warning_inverse"]).strip()) > 0: module_xml += "\t\n" - if "max" in data: + if "max" in data and len(str(data["max"]).strip()) > 0: module_xml += "\t\n" - if "min" in data: + if "min" in data and len(str(data["min"]).strip()) > 0: module_xml += "\t\n" - if "post_process" in data: + if "post_process" in data and len(str(data["post_process"]).strip()) > 0: module_xml += "\t\n" - if "disabled" in data: + if "disabled" in data and len(str(data["disabled"]).strip()) > 0: module_xml += "\t\n" - if "min_ff_event" in data: + if "min_ff_event" in data and len(str(data["min_ff_event"]).strip()) > 0: module_xml += "\t\n" - if "status" in data: + if "status" in data and len(str(data["status"]).strip()) > 0: module_xml += "\t\n" - if "timestamp" in data: + if "timestamp" in data and len(str(data["timestamp"]).strip()) > 0: module_xml += "\t\n" - if "custom_id" in data: + if "custom_id" in data and len(str(data["custom_id"]).strip()) > 0: module_xml += "\t\n" - if "critical_instructions" in data: + if "critical_instructions" in data and len(str(data["critical_instructions"]).strip()) > 0: module_xml += "\t\n" - if "warning_instructions" in data: + if "warning_instructions" in data and len(str(data["warning_instructions"]).strip()) > 0: module_xml += "\t\n" - if "unknown_instructions" in data: + if "unknown_instructions" in data and len(str(data["unknown_instructions"]).strip()) > 0: module_xml += "\t\n" - if "quiet" in data: + if "quiet" in data and len(str(data["quiet"]).strip()) > 0: module_xml += "\t\n" - if "module_ff_interval" in data: + if "module_ff_interval" in data and len(str(data["module_ff_interval"]).strip()) > 0: module_xml += "\t\n" - if "crontab" in data: + if "crontab" in data and len(str(data["crontab"]).strip()) > 0: module_xml += "\t\n" - if "min_ff_event_normal" in data: + if "min_ff_event_normal" in data and len(str(data["min_ff_event_normal"]).strip()) > 0: module_xml += "\t\n" - if "min_ff_event_warning" in data: + if "min_ff_event_warning" in data and len(str(data["min_ff_event_warning"]).strip()) > 0: module_xml += "\t\n" - if "min_ff_event_critical" in data: + if "min_ff_event_critical" in data and len(str(data["min_ff_event_critical"]).strip()) > 0: module_xml += "\t\n" - if "ff_type" in data: + if "ff_type" in data and len(str(data["ff_type"]).strip()) > 0: module_xml += "\t\n" - if "ff_timeout" in data: + if "ff_timeout" in data and len(str(data["ff_timeout"]).strip()) > 0: module_xml += "\t\n" - if "each_ff" in data: + if "each_ff" in data and len(str(data["each_ff"]).strip()) > 0: module_xml += "\t\n" - if "module_parent_unlink" in data: - module_xml += "\t\n" + if "module_parent_unlink" in data and len(str(data["module_parent_unlink"]).strip()) > 0: + module_xml += "\t\n" if "alert" in data: for alert in data["alert"]: - module_xml += "\t\n" + if len(str(alert).strip()) > 0: + module_xml += "\t\n" module_xml += "\n" if print_flag: @@ -128,6 +197,28 @@ def print_module( return module_xml +#### +# Init log module template +######################################################################################### +def init_log_module( + default_values: dict = {} + ) -> dict: + """ + Initializes a log module template with default values. + + Returns: + dict: Dictionary representing the log module template with default values. + """ + module = { + "source" : None, + "value" : "" + } + + for key, value in default_values.items(): + if key in module: + module[key] = value + + return module #### # Returns log module in XML format. Accepts only {dict} diff --git a/pandora_server/extras/pandoraPlugintools/threads.py b/pandora_server/extras/pandoraPlugintools/threads.py index 8e3a5dfa09..0a77a3bd63 100644 --- a/pandora_server/extras/pandoraPlugintools/threads.py +++ b/pandora_server/extras/pandoraPlugintools/threads.py @@ -2,6 +2,7 @@ import sys from queue import Queue from threading import Thread from multiprocessing import Pool, Manager +from .general import debug_dict #### # Define multi-processing internal global variables. diff --git a/pandora_server/extras/pandoraPlugintools/transfer.py b/pandora_server/extras/pandoraPlugintools/transfer.py index bce60935c8..a32d95aad8 100644 --- a/pandora_server/extras/pandoraPlugintools/transfer.py +++ b/pandora_server/extras/pandoraPlugintools/transfer.py @@ -4,7 +4,7 @@ import shutil import subprocess import os import sys -from .general import generate_md5,set_dict_key_value +from .general import debug_dict,generate_md5,set_dict_key_value #### # Define global variables dict, used in functions as default values.