pandorafms/pandora_server/extras/pandoraPlugintools/discovery.py

226 lines
5.2 KiB
Python
Raw Normal View History

import sys
import json
####
# Define some global variables
#########################################################################################
2023-08-16 08:12:14 +02:00
_ERROR_LEVEL = 0
_SUMMARY = {}
_INFO = ""
_MONITORING_DATA = []
####
# Internal: Alias for output.print_debug function
#########################################################################################
def _print_debug(
var = "",
print_errors: bool = False
):
"""
Print the variable as a JSON-like representation for debugging purposes.
Args:
var (any): The variable to be printed.
print_errors (bool): A flag indicating whether to print errors during debugging.
"""
from .output import print_debug
print_debug(var, print_errors)
####
# Set error level to value
#########################################################################################
def set_disco_error_level(
value: int = 0
2023-08-18 12:53:03 +02:00
)-> None:
"""
Sets the error level to the specified value.
Args:
value (int, optional): The error level value. Default is 0.
"""
2023-08-16 08:12:14 +02:00
global _ERROR_LEVEL
2023-08-16 08:12:14 +02:00
_ERROR_LEVEL = value
####
# Set fixed value to summary dict
#########################################################################################
def set_disco_summary(
data: dict = {}
2023-08-18 12:53:03 +02:00
)-> None:
2023-08-16 08:12:14 +02:00
"""
2023-08-18 12:53:03 +02:00
Sets the disk summary data in the internal summary dictionary.
This function updates the summary dictionary with the provided disk summary data.
Args:
data (dict): A dictionary containing disk summary data.
Returns:
None
2023-08-16 08:12:14 +02:00
"""
global _SUMMARY
_SUMMARY = {}
####
# Set fixed value to summary key
#########################################################################################
def set_disco_summary_value(
key: str = "",
2023-08-02 11:42:20 +02:00
value = None
2023-08-18 12:53:03 +02:00
)-> None:
"""
2023-08-16 08:12:14 +02:00
Sets a fixed value for a key in the '_SUMMARY' dictionary.
Args:
key (str): Key to set the value for.
value (any): Value to assign to the key.
2023-08-18 12:53:03 +02:00
Returns:
None
"""
2023-08-16 08:12:14 +02:00
global _SUMMARY
2023-08-16 08:12:14 +02:00
_SUMMARY[key] = value
####
# Add value to summary key
#########################################################################################
def add_disco_summary_value(
key: str = "",
2023-08-02 11:42:20 +02:00
value = None
2023-08-18 12:53:03 +02:00
)-> None:
"""
Adds a value to a key in the 'SUMMARY' dictionary.
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-18 12:53:03 +02:00
Returns:
None
"""
2023-08-16 08:12:14 +02:00
global _SUMMARY
2023-08-16 08:12:14 +02:00
if key in _SUMMARY:
_SUMMARY[key] += value
else:
set_disco_summary_value(key, value)
####
# Set fixed value to info
#########################################################################################
def set_disco_info_value(
value: str = ""
2023-08-18 12:53:03 +02:00
)-> None:
"""
2023-08-16 08:12:14 +02:00
Sets a fixed value to the '_INFO' variable.
Args:
2023-08-16 08:12:14 +02:00
data (str, optional): The value to set in the '_INFO' variable. Default is an empty string.
2023-08-18 12:53:03 +02:00
Returns:
None
"""
2023-08-16 08:12:14 +02:00
global _INFO
2023-08-16 08:12:14 +02:00
_INFO = value
####
# Add data to info
#########################################################################################
def add_disco_info_value(
value: str = ""
2023-08-18 12:53:03 +02:00
)-> None:
"""
2023-08-16 08:12:14 +02:00
Adds data to the '_INFO' variable.
Args:
2023-08-16 08:12:14 +02:00
data (str, optional): The data to add to the '_INFO' variable. Default is an empty string.
2023-08-18 12:53:03 +02:00
Returns:
None
"""
2023-08-16 08:12:14 +02:00
global _INFO
2023-08-16 08:12:14 +02:00
_INFO += value
####
# Set fixed value to monitoring data
#########################################################################################
def set_disco_monitoring_data(
data: list = []
2023-08-18 12:53:03 +02:00
)-> None:
"""
Set the monitoring data for disk usage.
Args:
data (list): A list containing disk monitoring data.
2023-08-18 12:53:03 +02:00
Returns:
None
"""
2023-08-16 08:12:14 +02:00
global _MONITORING_DATA
2023-08-16 08:12:14 +02:00
_MONITORING_DATA = data
2023-07-28 12:02:34 +02:00
####
# Add value to monitoring data
2023-07-28 12:02:34 +02:00
#########################################################################################
def add_disco_monitoring_data(
2023-07-28 12:02:34 +02:00
data: dict = {}
2023-08-18 12:53:03 +02:00
)-> None:
2023-07-28 12:02:34 +02:00
"""
Add disk monitoring data to the global monitoring dataset.
Args:
data (dict): A dictionary containing disk monitoring data.
2023-08-18 12:53:03 +02:00
Returns:
None
2023-07-28 12:02:34 +02:00
"""
2023-08-16 08:12:14 +02:00
global _MONITORING_DATA
2023-07-28 12:02:34 +02:00
2023-08-16 08:12:14 +02:00
_MONITORING_DATA.append(data)
2023-07-28 12:02:34 +02:00
####
# Print JSON output and exit script
#########################################################################################
2023-08-18 12:53:03 +02:00
def disco_output()-> None:
"""
Prints the JSON output and exits the script.
2023-08-16 08:12:14 +02:00
The function uses the global variables '_ERROR_LEVEL', '_SUMMARY', '_INFO' and '_MONITORING_DATA'
to create the JSON output. It then prints the JSON string and exits the script with
2023-08-16 08:12:14 +02:00
the '_ERROR_LEVEL' as the exit code.
2023-08-18 12:53:03 +02:00
Returns:
None
"""
2023-08-09 13:46:09 +02:00
from .output import print_stdout
2023-08-16 08:12:14 +02:00
global _ERROR_LEVEL
global _SUMMARY
global _INFO
global _MONITORING_DATA
2023-08-16 08:12:14 +02:00
output={}
if _SUMMARY:
output["summary"] = _SUMMARY
2023-08-16 08:12:14 +02:00
if _INFO:
output["info"] = _INFO
2023-07-28 12:02:34 +02:00
2023-08-16 08:12:14 +02:00
if _MONITORING_DATA:
output["monitoring_data"] = _MONITORING_DATA
2023-08-16 08:12:14 +02:00
json_string = json.dumps(output)
2023-08-09 13:46:09 +02:00
print_stdout(json_string)
2023-08-16 08:12:14 +02:00
sys.exit(_ERROR_LEVEL)