2023-07-27 15:53:10 +02:00
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
|
|
|
|
####
|
|
|
|
# Define some global variables
|
|
|
|
#########################################################################################
|
|
|
|
|
2023-08-02 11:32:34 +02:00
|
|
|
ERROR_LEVEL = 0
|
|
|
|
SUMMARY = {}
|
|
|
|
INFO = ""
|
|
|
|
MONITORING_DATA = []
|
|
|
|
|
2023-08-10 14:07:57 +02:00
|
|
|
####
|
|
|
|
# Internal: Alias for output.print_debug function
|
|
|
|
#########################################################################################
|
|
|
|
|
|
|
|
def _print_debug(
|
|
|
|
var = "",
|
|
|
|
print_errors: bool = False
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Prints any list, dict, string, float or integer as a json
|
|
|
|
"""
|
|
|
|
from .output import print_debug
|
|
|
|
print_debug(var, print_errors)
|
|
|
|
|
2023-08-02 11:32:34 +02:00
|
|
|
####
|
|
|
|
# Set error level to value
|
|
|
|
#########################################################################################
|
2023-08-10 14:07:57 +02:00
|
|
|
def set_disco_error_level(
|
2023-08-02 11:32:34 +02:00
|
|
|
value: int = 0
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Sets the error level to the specified value.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
value (int, optional): The error level value. Default is 0.
|
|
|
|
"""
|
|
|
|
global ERROR_LEVEL
|
|
|
|
|
|
|
|
ERROR_LEVEL = value
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
####
|
|
|
|
# Set fixed value to summary key
|
|
|
|
#########################################################################################
|
2023-08-10 14:07:57 +02:00
|
|
|
def set_disco_summary_value(
|
2023-07-27 15:53:10 +02:00
|
|
|
key: str = "",
|
2023-08-02 11:42:20 +02:00
|
|
|
value = None
|
2023-07-27 15:53:10 +02:00
|
|
|
):
|
|
|
|
"""
|
2023-08-02 11:32:34 +02:00
|
|
|
Sets a fixed value for a key in the 'SUMMARY' dictionary.
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
key (str): Key to set the value for.
|
|
|
|
value (any): Value to assign to the key.
|
|
|
|
"""
|
2023-08-02 11:32:34 +02:00
|
|
|
global SUMMARY
|
2023-07-27 15:53:10 +02:00
|
|
|
|
2023-08-02 11:32:34 +02:00
|
|
|
SUMMARY[key] = value
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
####
|
|
|
|
# Add value to summary key
|
|
|
|
#########################################################################################
|
2023-08-10 14:07:57 +02:00
|
|
|
def add_disco_summary_value(
|
2023-07-27 15:53:10 +02:00
|
|
|
key: str = "",
|
2023-08-02 11:42:20 +02:00
|
|
|
value = None
|
2023-07-27 15:53:10 +02:00
|
|
|
):
|
|
|
|
"""
|
2023-08-02 11:32:34 +02:00
|
|
|
Adds a value to a key in the 'SUMMARY' dictionary.
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
If the key already exists, the value will be incremented. Otherwise, a new key will be created.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
key (str): Key to add the value to.
|
|
|
|
value (any): Value to add to the key.
|
|
|
|
"""
|
2023-08-02 11:32:34 +02:00
|
|
|
global SUMMARY
|
2023-07-27 15:53:10 +02:00
|
|
|
|
2023-08-02 11:32:34 +02:00
|
|
|
if key in SUMMARY:
|
|
|
|
SUMMARY[key] += value
|
2023-07-27 15:53:10 +02:00
|
|
|
else:
|
|
|
|
set_summary_value(key, value)
|
|
|
|
|
|
|
|
####
|
2023-08-02 11:32:34 +02:00
|
|
|
# Set fixed value to info
|
2023-07-27 15:53:10 +02:00
|
|
|
#########################################################################################
|
2023-08-10 14:07:57 +02:00
|
|
|
def set_disco_info_value(
|
2023-08-02 11:32:34 +02:00
|
|
|
value: str = ""
|
2023-07-27 15:53:10 +02:00
|
|
|
):
|
|
|
|
"""
|
2023-08-02 11:32:34 +02:00
|
|
|
Sets a fixed value to the 'INFO' variable.
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
Args:
|
2023-08-02 11:32:34 +02:00
|
|
|
data (str, optional): The value to set in the 'INFO' variable. Default is an empty string.
|
2023-07-27 15:53:10 +02:00
|
|
|
"""
|
2023-08-02 11:32:34 +02:00
|
|
|
global INFO
|
2023-07-27 15:53:10 +02:00
|
|
|
|
2023-08-02 11:32:34 +02:00
|
|
|
INFO = value
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
####
|
|
|
|
# Add data to info
|
|
|
|
#########################################################################################
|
2023-08-10 14:07:57 +02:00
|
|
|
def add_disco_info_value(
|
2023-08-02 11:32:34 +02:00
|
|
|
value: str = ""
|
2023-07-27 15:53:10 +02:00
|
|
|
):
|
|
|
|
"""
|
2023-08-02 11:32:34 +02:00
|
|
|
Adds data to the 'INFO' variable.
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
Args:
|
2023-08-02 11:32:34 +02:00
|
|
|
data (str, optional): The data to add to the 'INFO' variable. Default is an empty string.
|
2023-07-27 15:53:10 +02:00
|
|
|
"""
|
2023-08-02 11:32:34 +02:00
|
|
|
global INFO
|
2023-07-27 15:53:10 +02:00
|
|
|
|
2023-08-02 11:32:34 +02:00
|
|
|
INFO += value
|
2023-07-27 15:53:10 +02:00
|
|
|
|
|
|
|
####
|
2023-08-02 11:32:34 +02:00
|
|
|
# Set fixed value to monitoring data
|
2023-07-27 15:53:10 +02:00
|
|
|
#########################################################################################
|
2023-08-10 14:07:57 +02:00
|
|
|
def set_disco_monitoring_data(
|
2023-08-02 11:32:34 +02:00
|
|
|
data: list = []
|
2023-07-27 15:53:10 +02:00
|
|
|
):
|
|
|
|
"""
|
2023-08-02 11:32:34 +02:00
|
|
|
TODO: Add comments
|
2023-07-27 15:53:10 +02:00
|
|
|
"""
|
2023-08-02 11:32:34 +02:00
|
|
|
global MONITORING_DATA
|
2023-07-27 15:53:10 +02:00
|
|
|
|
2023-08-02 11:32:34 +02:00
|
|
|
MONITORING_DATA = data
|
2023-07-27 15:53:10 +02:00
|
|
|
|
2023-07-28 12:02:34 +02:00
|
|
|
####
|
2023-08-02 11:32:34 +02:00
|
|
|
# Add value to monitoring data
|
2023-07-28 12:02:34 +02:00
|
|
|
#########################################################################################
|
2023-08-10 14:07:57 +02:00
|
|
|
def add_disco_monitoring_data(
|
2023-07-28 12:02:34 +02:00
|
|
|
data: dict = {}
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
TODO: Add comments
|
|
|
|
"""
|
2023-08-02 11:32:34 +02:00
|
|
|
global MONITORING_DATA
|
2023-07-28 12:02:34 +02:00
|
|
|
|
2023-08-02 11:32:34 +02:00
|
|
|
MONITORING_DATA.append(data)
|
2023-07-28 12:02:34 +02:00
|
|
|
|
2023-07-27 15:53:10 +02:00
|
|
|
####
|
|
|
|
# Print JSON output and exit script
|
|
|
|
#########################################################################################
|
2023-08-10 14:07:57 +02:00
|
|
|
def disco_output():
|
2023-07-27 15:53:10 +02:00
|
|
|
"""
|
|
|
|
Prints the JSON output and exits the script.
|
|
|
|
|
2023-08-02 11:32:34 +02:00
|
|
|
The function uses the global variables 'ERROR_LEVEL', 'SUMMARY', and 'info'
|
2023-07-27 15:53:10 +02:00
|
|
|
to create the JSON output. It then prints the JSON string and exits the script with
|
2023-08-02 11:32:34 +02:00
|
|
|
the 'ERROR_LEVEL' as the exit code.
|
2023-07-27 15:53:10 +02:00
|
|
|
"""
|
2023-08-09 13:46:09 +02:00
|
|
|
from .output import print_stdout
|
2023-07-27 15:53:10 +02:00
|
|
|
|
2023-08-02 11:32:34 +02:00
|
|
|
global ERROR_LEVEL
|
|
|
|
global SUMMARY
|
|
|
|
global INFO
|
|
|
|
global MONITORING_DATA
|
2023-07-27 15:53:10 +02:00
|
|
|
|
2023-08-02 11:32:34 +02:00
|
|
|
OUTPUT={}
|
|
|
|
if SUMMARY:
|
|
|
|
OUTPUT["summary"] = SUMMARY
|
2023-07-27 15:53:10 +02:00
|
|
|
|
2023-08-02 11:32:34 +02:00
|
|
|
if INFO:
|
|
|
|
OUTPUT["info"] = INFO
|
2023-07-28 12:02:34 +02:00
|
|
|
|
2023-08-02 11:32:34 +02:00
|
|
|
if MONITORING_DATA:
|
|
|
|
OUTPUT["monitoring_data"] = MONITORING_DATA
|
2023-07-27 15:53:10 +02:00
|
|
|
|
2023-08-02 11:32:34 +02:00
|
|
|
json_string = json.dumps(OUTPUT)
|
2023-07-27 15:53:10 +02:00
|
|
|
|
2023-08-09 13:46:09 +02:00
|
|
|
print_stdout(json_string)
|
2023-08-02 11:32:34 +02:00
|
|
|
sys.exit(ERROR_LEVEL)
|