####
# Internal: Alias for output.print_debug function
#########################################################################################
def _print_debug(
var = "",
print_errors: bool = False
):
"""
Print the provided variable as JSON format, supporting various data types.
Args:
var (any, optional): The variable to be printed as JSON. Defaults to an empty string.
print_errors (bool, optional): Set to True to print errors encountered during printing. Defaults to False.
"""
from .output import print_debug
print_debug(var, print_errors)
####
# Init module template
#########################################################################################
def init_module(
default_values: dict = {}
) -> dict:
"""
Initializes a module template with default values.
Args:
default_values (dict, optional): Dictionary containing default values to override template values. Defaults to an empty dictionary.
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}
#########################################################################################
def print_module(
module: dict = None,
print_flag: bool = False
) -> str:
"""
Returns module in XML format. Accepts only {dict}.
Args:
module (dict, optional): Dictionary containing module data. Defaults to None.
print_flag (bool, optional): Flag to print the module XML to STDOUT. Defaults to False.
Returns:
str: Module data in XML format.
"""
from .output import print_stdout
module_xml = ""
if module is not None:
data = dict(module)
module_xml = ("\n"
"\t\n"
"\t" + str(data["type"]) + "\n"
)
if type(data["type"]) is not str and "string" not in data["type"]: #### Strip spaces if module not generic_data_string
data["value"] = data["value"].strip()
if isinstance(data["value"], list): # Checks if value is a list
module_xml += "\t\n"
else:
module_xml += "\t\n"
if "desc" in data and len(str(data["desc"]).strip()) > 0:
module_xml += "\t\n"
if "unit" in data and len(str(data["unit"]).strip()) > 0:
module_xml += "\t\n"
if "interval" in data and len(str(data["interval"]).strip()) > 0:
module_xml += "\t\n"
if "tags" in data and len(str(data["tags"]).strip()) > 0:
module_xml += "\t" + str(data["tags"]) + "\n"
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 and len(str(data["module_parent"]).strip()) > 0:
module_xml += "\t" + str(data["module_parent"]) + "\n"
if "min_warning" in data and len(str(data["min_warning"]).strip()) > 0:
module_xml += "\t\n"
if "min_warning_forced" in data and len(str(data["min_warning_forced"]).strip()) > 0:
module_xml += "\t\n"
if "max_warning" in data and len(str(data["max_warning"]).strip()) > 0:
module_xml += "\t\n"
if "max_warning_forced" in data and len(str(data["max_warning_forced"]).strip()) > 0:
module_xml += "\t\n"
if "min_critical" in data and len(str(data["min_critical"]).strip()) > 0:
module_xml += "\t\n"
if "min_critical_forced" in data and len(str(data["min_critical_forced"]).strip()) > 0:
module_xml += "\t\n"
if "max_critical" in data and len(str(data["max_critical"]).strip()) > 0:
module_xml += "\t\n"
if "max_critical_forced" in data and len(str(data["max_critical_forced"]).strip()) > 0:
module_xml += "\t\n"
if "str_warning" in data and len(str(data["str_warning"]).strip()) > 0:
module_xml += "\t\n"
if "str_warning_forced" in data and len(str(data["str_warning_forced"]).strip()) > 0:
module_xml += "\t\n"
if "str_critical" in data and len(str(data["str_critical"]).strip()) > 0:
module_xml += "\t\n"
if "str_critical_forced" in data and len(str(data["str_critical_forced"]).strip()) > 0:
module_xml += "\t\n"
if "critical_inverse" in data and len(str(data["critical_inverse"]).strip()) > 0:
module_xml += "\t\n"
if "warning_inverse" in data and len(str(data["warning_inverse"]).strip()) > 0:
module_xml += "\t\n"
if "max" in data and len(str(data["max"]).strip()) > 0:
module_xml += "\t\n"
if "min" in data and len(str(data["min"]).strip()) > 0:
module_xml += "\t\n"
if "post_process" in data and len(str(data["post_process"]).strip()) > 0:
module_xml += "\t\n"
if "disabled" in data and len(str(data["disabled"]).strip()) > 0:
module_xml += "\t\n"
if "min_ff_event" in data and len(str(data["min_ff_event"]).strip()) > 0:
module_xml += "\t\n"
if "status" in data and len(str(data["status"]).strip()) > 0:
module_xml += "\t\n"
if "timestamp" in data and len(str(data["timestamp"]).strip()) > 0:
module_xml += "\t\n"
if "custom_id" in data and len(str(data["custom_id"]).strip()) > 0:
module_xml += "\t\n"
if "critical_instructions" in data and len(str(data["critical_instructions"]).strip()) > 0:
module_xml += "\t\n"
if "warning_instructions" in data and len(str(data["warning_instructions"]).strip()) > 0:
module_xml += "\t\n"
if "unknown_instructions" in data and len(str(data["unknown_instructions"]).strip()) > 0:
module_xml += "\t\n"
if "quiet" in data and len(str(data["quiet"]).strip()) > 0:
module_xml += "\t\n"
if "module_ff_interval" in data and len(str(data["module_ff_interval"]).strip()) > 0:
module_xml += "\t\n"
if "crontab" in data and len(str(data["crontab"]).strip()) > 0:
module_xml += "\t\n"
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 and len(str(data["min_ff_event_warning"]).strip()) > 0:
module_xml += "\t\n"
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 and len(str(data["ff_type"]).strip()) > 0:
module_xml += "\t\n"
if "ff_timeout" in data and len(str(data["ff_timeout"]).strip()) > 0:
module_xml += "\t\n"
if "each_ff" in data and len(str(data["each_ff"]).strip()) > 0:
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"]:
if len(str(alert).strip()) > 0:
module_xml += "\t\n"
module_xml += "\n"
if print_flag:
print_stdout(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.
Args:
default_values (dict, optional): Default values to initialize the log module with. Defaults to an empty dictionary.
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}
#########################################################################################
def print_log_module(
module: dict = None,
print_flag: bool = False
) -> str:
"""
Returns log module in XML format. Accepts only {dict}.
- Only works with one module at a time: otherwise iteration is needed.
- Module "value" field accepts str type.
- Use not_print_flag to avoid printing the XML (only populates variables).
Args:
module (dict, optional): Dictionary representing the log module. Defaults to None.
print_flag (bool, optional): Flag to indicate whether to print the XML. Defaults to False.
Returns:
str: XML representation of the log module.
"""
from .output import print_stdout
module_xml = ""
if module is not None:
data = dict(module)
module_xml = ("\n"
"\t\n"
"\t\"" + str(data["value"]) + "\"\n"
)
module_xml += "\n"
if print_flag:
print_stdout(module_xml)
return module_xml