Added Agent class functions

This commit is contained in:
Enrique Martin 2023-08-02 13:13:43 +02:00
parent bde41cac3d
commit f5df8eb24c
6 changed files with 274 additions and 48 deletions

View File

@ -3,8 +3,8 @@ from subprocess import *
import hashlib import hashlib
import sys import sys
import os import os
from .general import now,set_dict_key_value from .general import debug_dict,now,set_dict_key_value,generate_md5
from .modules import print_module,print_log_module from .modules import init_module,init_log_module,print_module,print_log_module
#### ####
# Define global variables dict, used in functions as default values. # Define global variables dict, used in functions as default values.
@ -60,7 +60,8 @@ class Agent:
def __init__( def __init__(
self, self,
config: dict = None, config: dict = None,
modules_def: list = [] modules_def: list = [],
log_modules_def: list = []
): ):
if config is None: if config is None:
@ -68,6 +69,137 @@ class Agent:
self.config = config self.config = config
self.modules_def = modules_def 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 # Init agent template
@ -93,7 +225,7 @@ def init_agent(
"address" : "", "address" : "",
"group" : GLOBAL_VARIABLES['agents_group_name'], "group" : GLOBAL_VARIABLES['agents_group_name'],
"interval" : GLOBAL_VARIABLES['interval'], "interval" : GLOBAL_VARIABLES['interval'],
"agent_mode" : "1", "agent_mode" : "1"
} }
for key, value in default_values.items(): for key, value in default_values.items():

View File

@ -1,5 +1,6 @@
import sys import sys
import json import json
from .general import debug_dict
#### ####
# Define some global variables # Define some global variables

View File

@ -2,6 +2,7 @@ from requests_ntlm import HttpNtlmAuth
from requests.auth import HTTPBasicAuth from requests.auth import HTTPBasicAuth
from requests.auth import HTTPDigestAuth from requests.auth import HTTPDigestAuth
from requests.sessions import Session from requests.sessions import Session
from .general import debug_dict
#### ####
# Auth URL session # Auth URL session

View File

@ -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} # Returns module in XML format. Accepts only {dict}
######################################################################################### #########################################################################################
@ -36,91 +104,92 @@ def print_module(
else: else:
module_xml += "\t<data><![CDATA[" + str(data["value"]) + "]]></data>\n" module_xml += "\t<data><![CDATA[" + str(data["value"]) + "]]></data>\n"
if "desc" in data: if "desc" in data and len(str(data["desc"]).strip()) > 0:
module_xml += "\t<description><![CDATA[" + str(data["desc"]) + "]]></description>\n" module_xml += "\t<description><![CDATA[" + str(data["desc"]) + "]]></description>\n"
if "unit" in data: if "unit" in data and len(str(data["unit"]).strip()) > 0:
module_xml += "\t<unit><![CDATA[" + str(data["unit"]) + "]]></unit>\n" module_xml += "\t<unit><![CDATA[" + str(data["unit"]) + "]]></unit>\n"
if "interval" in data: if "interval" in data and len(str(data["interval"]).strip()) > 0:
module_xml += "\t<module_interval><![CDATA[" + str(data["interval"]) + "]]></module_interval>\n" module_xml += "\t<module_interval><![CDATA[" + str(data["interval"]) + "]]></module_interval>\n"
if "tags" in data: if "tags" in data and len(str(data["tags"]).strip()) > 0:
module_xml += "\t<tags>" + str(data["tags"]) + "</tags>\n" module_xml += "\t<tags>" + str(data["tags"]) + "</tags>\n"
if "module_group" in data: if "module_group" in data and len(str(data["module_group"]).strip()) > 0:
module_xml += "\t<module_group>" + str(data["module_group"]) + "</module_group>\n" module_xml += "\t<module_group>" + str(data["module_group"]) + "</module_group>\n"
if "module_parent" in data: if "module_parent" in data and len(str(data["module_parent"]).strip()) > 0:
module_xml += "\t<module_parent>" + str(data["module_parent"]) + "</module_parent>\n" module_xml += "\t<module_parent>" + str(data["module_parent"]) + "</module_parent>\n"
if "min_warning" in data: if "min_warning" in data and len(str(data["min_warning"]).strip()) > 0:
module_xml += "\t<min_warning><![CDATA[" + str(data["min_warning"]) + "]]></min_warning>\n" module_xml += "\t<min_warning><![CDATA[" + str(data["min_warning"]) + "]]></min_warning>\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<min_warning_forced><![CDATA[" + str(data["min_warning_forced"]) + "]]></min_warning_forced>\n" module_xml += "\t<min_warning_forced><![CDATA[" + str(data["min_warning_forced"]) + "]]></min_warning_forced>\n"
if "max_warning" in data: if "max_warning" in data and len(str(data["max_warning"]).strip()) > 0:
module_xml += "\t<max_warning><![CDATA[" + str(data["max_warning"]) + "]]></max_warning>\n" module_xml += "\t<max_warning><![CDATA[" + str(data["max_warning"]) + "]]></max_warning>\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<max_warning_forced><![CDATA[" + str(data["max_warning_forced"]) + "]]></max_warning_forced>\n" module_xml += "\t<max_warning_forced><![CDATA[" + str(data["max_warning_forced"]) + "]]></max_warning_forced>\n"
if "min_critical" in data: if "min_critical" in data and len(str(data["min_critical"]).strip()) > 0:
module_xml += "\t<min_critical><![CDATA[" + str(data["min_critical"]) + "]]></min_critical>\n" module_xml += "\t<min_critical><![CDATA[" + str(data["min_critical"]) + "]]></min_critical>\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<min_critical_forced><![CDATA[" + str(data["min_critical_forced"]) + "]]></min_critical_forced>\n" module_xml += "\t<min_critical_forced><![CDATA[" + str(data["min_critical_forced"]) + "]]></min_critical_forced>\n"
if "max_critical" in data: if "max_critical" in data and len(str(data["max_critical"]).strip()) > 0:
module_xml += "\t<max_critical><![CDATA[" + str(data["max_critical"]) + "]]></max_critical>\n" module_xml += "\t<max_critical><![CDATA[" + str(data["max_critical"]) + "]]></max_critical>\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<max_critical_forced><![CDATA[" + str(data["max_critical_forced"]) + "]]></max_critical_forced>\n" module_xml += "\t<max_critical_forced><![CDATA[" + str(data["max_critical_forced"]) + "]]></max_critical_forced>\n"
if "str_warning" in data: if "str_warning" in data and len(str(data["str_warning"]).strip()) > 0:
module_xml += "\t<str_warning><![CDATA[" + str(data["str_warning"]) + "]]></str_warning>\n" module_xml += "\t<str_warning><![CDATA[" + str(data["str_warning"]) + "]]></str_warning>\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<str_warning_forced><![CDATA[" + str(data["str_warning_forced"]) + "]]></str_warning_forced>\n" module_xml += "\t<str_warning_forced><![CDATA[" + str(data["str_warning_forced"]) + "]]></str_warning_forced>\n"
if "str_critical" in data: if "str_critical" in data and len(str(data["str_critical"]).strip()) > 0:
module_xml += "\t<str_critical><![CDATA[" + str(data["str_critical"]) + "]]></str_critical>\n" module_xml += "\t<str_critical><![CDATA[" + str(data["str_critical"]) + "]]></str_critical>\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<str_critical_forced><![CDATA[" + str(data["str_critical_forced"]) + "]]></str_critical_forced>\n" module_xml += "\t<str_critical_forced><![CDATA[" + str(data["str_critical_forced"]) + "]]></str_critical_forced>\n"
if "critical_inverse" in data: if "critical_inverse" in data and len(str(data["critical_inverse"]).strip()) > 0:
module_xml += "\t<critical_inverse><![CDATA[" + str(data["critical_inverse"]) + "]]></critical_inverse>\n" module_xml += "\t<critical_inverse><![CDATA[" + str(data["critical_inverse"]) + "]]></critical_inverse>\n"
if "warning_inverse" in data: if "warning_inverse" in data and len(str(data["warning_inverse"]).strip()) > 0:
module_xml += "\t<warning_inverse><![CDATA[" + str(data["warning_inverse"]) + "]]></warning_inverse>\n" module_xml += "\t<warning_inverse><![CDATA[" + str(data["warning_inverse"]) + "]]></warning_inverse>\n"
if "max" in data: if "max" in data and len(str(data["max"]).strip()) > 0:
module_xml += "\t<max><![CDATA[" + str(data["max"]) + "]]></max>\n" module_xml += "\t<max><![CDATA[" + str(data["max"]) + "]]></max>\n"
if "min" in data: if "min" in data and len(str(data["min"]).strip()) > 0:
module_xml += "\t<min><![CDATA[" + str(data["min"]) + "]]></min>\n" module_xml += "\t<min><![CDATA[" + str(data["min"]) + "]]></min>\n"
if "post_process" in data: if "post_process" in data and len(str(data["post_process"]).strip()) > 0:
module_xml += "\t<post_process><![CDATA[" + str(data["post_process"]) + "]]></post_process>\n" module_xml += "\t<post_process><![CDATA[" + str(data["post_process"]) + "]]></post_process>\n"
if "disabled" in data: if "disabled" in data and len(str(data["disabled"]).strip()) > 0:
module_xml += "\t<disabled><![CDATA[" + str(data["disabled"]) + "]]></disabled>\n" module_xml += "\t<disabled><![CDATA[" + str(data["disabled"]) + "]]></disabled>\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<min_ff_event><![CDATA[" + str(data["min_ff_event"]) + "]]></min_ff_event>\n" module_xml += "\t<min_ff_event><![CDATA[" + str(data["min_ff_event"]) + "]]></min_ff_event>\n"
if "status" in data: if "status" in data and len(str(data["status"]).strip()) > 0:
module_xml += "\t<status><![CDATA[" + str(data["status"]) + "]]></status>\n" module_xml += "\t<status><![CDATA[" + str(data["status"]) + "]]></status>\n"
if "timestamp" in data: if "timestamp" in data and len(str(data["timestamp"]).strip()) > 0:
module_xml += "\t<timestamp><![CDATA[" + str(data["timestamp"]) + "]]></timestamp>\n" module_xml += "\t<timestamp><![CDATA[" + str(data["timestamp"]) + "]]></timestamp>\n"
if "custom_id" in data: if "custom_id" in data and len(str(data["custom_id"]).strip()) > 0:
module_xml += "\t<custom_id><![CDATA[" + str(data["custom_id"]) + "]]></custom_id>\n" module_xml += "\t<custom_id><![CDATA[" + str(data["custom_id"]) + "]]></custom_id>\n"
if "critical_instructions" in data: if "critical_instructions" in data and len(str(data["critical_instructions"]).strip()) > 0:
module_xml += "\t<critical_instructions><![CDATA[" + str(data["critical_instructions"]) + "]]></critical_instructions>\n" module_xml += "\t<critical_instructions><![CDATA[" + str(data["critical_instructions"]) + "]]></critical_instructions>\n"
if "warning_instructions" in data: if "warning_instructions" in data and len(str(data["warning_instructions"]).strip()) > 0:
module_xml += "\t<warning_instructions><![CDATA[" + str(data["warning_instructions"]) + "]]></warning_instructions>\n" module_xml += "\t<warning_instructions><![CDATA[" + str(data["warning_instructions"]) + "]]></warning_instructions>\n"
if "unknown_instructions" in data: if "unknown_instructions" in data and len(str(data["unknown_instructions"]).strip()) > 0:
module_xml += "\t<unknown_instructions><![CDATA[" + str(data["unknown_instructions"]) + "]]></unknown_instructions>\n" module_xml += "\t<unknown_instructions><![CDATA[" + str(data["unknown_instructions"]) + "]]></unknown_instructions>\n"
if "quiet" in data: if "quiet" in data and len(str(data["quiet"]).strip()) > 0:
module_xml += "\t<quiet><![CDATA[" + str(data["quiet"]) + "]]></quiet>\n" module_xml += "\t<quiet><![CDATA[" + str(data["quiet"]) + "]]></quiet>\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<module_ff_interval><![CDATA[" + str(data["module_ff_interval"]) + "]]></module_ff_interval>\n" module_xml += "\t<module_ff_interval><![CDATA[" + str(data["module_ff_interval"]) + "]]></module_ff_interval>\n"
if "crontab" in data: if "crontab" in data and len(str(data["crontab"]).strip()) > 0:
module_xml += "\t<crontab><![CDATA[" + str(data["crontab"]) + "]]></crontab>\n" module_xml += "\t<crontab><![CDATA[" + str(data["crontab"]) + "]]></crontab>\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<min_ff_event_normal><![CDATA[" + str(data["min_ff_event_normal"]) + "]]></min_ff_event_normal>\n" module_xml += "\t<min_ff_event_normal><![CDATA[" + str(data["min_ff_event_normal"]) + "]]></min_ff_event_normal>\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<min_ff_event_warning><![CDATA[" + str(data["min_ff_event_warning"]) + "]]></min_ff_event_warning>\n" module_xml += "\t<min_ff_event_warning><![CDATA[" + str(data["min_ff_event_warning"]) + "]]></min_ff_event_warning>\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<min_ff_event_critical><![CDATA[" + str(data["min_ff_event_critical"]) + "]]></min_ff_event_critical>\n" module_xml += "\t<min_ff_event_critical><![CDATA[" + str(data["min_ff_event_critical"]) + "]]></min_ff_event_critical>\n"
if "ff_type" in data: if "ff_type" in data and len(str(data["ff_type"]).strip()) > 0:
module_xml += "\t<ff_type><![CDATA[" + str(data["ff_type"]) + "]]></ff_type>\n" module_xml += "\t<ff_type><![CDATA[" + str(data["ff_type"]) + "]]></ff_type>\n"
if "ff_timeout" in data: if "ff_timeout" in data and len(str(data["ff_timeout"]).strip()) > 0:
module_xml += "\t<ff_timeout><![CDATA[" + str(data["ff_timeout"]) + "]]></ff_timeout>\n" module_xml += "\t<ff_timeout><![CDATA[" + str(data["ff_timeout"]) + "]]></ff_timeout>\n"
if "each_ff" in data: if "each_ff" in data and len(str(data["each_ff"]).strip()) > 0:
module_xml += "\t<each_ff><![CDATA[" + str(data["each_ff"]) + "]]></each_ff>\n" module_xml += "\t<each_ff><![CDATA[" + str(data["each_ff"]) + "]]></each_ff>\n"
if "module_parent_unlink" in data: if "module_parent_unlink" in data and len(str(data["module_parent_unlink"]).strip()) > 0:
module_xml += "\t<module_parent_unlink><![CDATA[" + str(data["parent_unlink"]) + "]]></module_parent_unlink>\n" module_xml += "\t<module_parent_unlink><![CDATA[" + str(data["module_parent_unlink"]) + "]]></module_parent_unlink>\n"
if "alert" in data: if "alert" in data:
for alert in data["alert"]: for alert in data["alert"]:
module_xml += "\t<alert_template><![CDATA[" + alert + "]]></alert_template>\n" if len(str(alert).strip()) > 0:
module_xml += "\t<alert_template><![CDATA[" + str(alert) + "]]></alert_template>\n"
module_xml += "</module>\n" module_xml += "</module>\n"
if print_flag: if print_flag:
@ -128,6 +197,28 @@ def print_module(
return module_xml 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} # Returns log module in XML format. Accepts only {dict}

View File

@ -2,6 +2,7 @@ import sys
from queue import Queue from queue import Queue
from threading import Thread from threading import Thread
from multiprocessing import Pool, Manager from multiprocessing import Pool, Manager
from .general import debug_dict
#### ####
# Define multi-processing internal global variables. # Define multi-processing internal global variables.

View File

@ -4,7 +4,7 @@ import shutil
import subprocess import subprocess
import os import os
import sys 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. # Define global variables dict, used in functions as default values.