Changed global variables names and minor changes

This commit is contained in:
Enrique Martin 2023-08-02 11:32:34 +02:00
parent 3c5ee4e03b
commit 3a5949ac5e
4 changed files with 126 additions and 115 deletions

View File

@ -12,7 +12,7 @@ from .transfer import write_xml
# Its values can be changed. # Its values can be changed.
######################################################################################### #########################################################################################
global_variables = { GLOBAL_VARIABLES = {
'agents_group_name' : '', 'agents_group_name' : '',
'interval' : 300 'interval' : 300
} }
@ -41,13 +41,13 @@ def set_global_variable(
value value
): ):
""" """
Sets the value of a global variable in the 'global_variables' dictionary. Sets the value of a global variable in the 'GLOBAL_VARIABLES' dictionary.
Args: Args:
variable_name (str): Name of the variable to set. variable_name (str): Name of the variable to set.
value (any): Value to assign to the variable. value (any): Value to assign to the variable.
""" """
set_dict_key_value(global_variables, variable_name, value) set_dict_key_value(GLOBAL_VARIABLES, variable_name, value)
#### ####
# Agent class # Agent class
@ -92,8 +92,8 @@ def init_agent(
"os_version" : "", "os_version" : "",
"timestamp" : now(), "timestamp" : now(),
"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",
} }

View File

@ -5,52 +5,10 @@ import json
# Define some global variables # Define some global variables
######################################################################################### #########################################################################################
output = {} ERROR_LEVEL = 0
error_level = 0 SUMMARY = {}
summary = {} INFO = ""
info = "" MONITORING_DATA = []
monitoring_data = []
####
# Set fixed value to summary key
#########################################################################################
def set_summary_value(
key: str = "",
value = ""
):
"""
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.
"""
global summary
summary[key] = value
####
# Add value to summary key
#########################################################################################
def add_summary_value(
key: str = "",
value = ""
):
"""
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.
"""
global summary
if key in summary:
summary[key] += value
else:
set_summary_value(key, value)
#### ####
# Set error level to value # Set error level to value
@ -64,44 +22,98 @@ def set_error_level(
Args: Args:
value (int, optional): The error level value. Default is 0. value (int, optional): The error level value. Default is 0.
""" """
global error_level global ERROR_LEVEL
error_level = value ERROR_LEVEL = value
#### ####
# Add data to info # Set fixed value to summary key
######################################################################################### #########################################################################################
def add_info_value( def set_summary_value(
data: str = "" key: str = "",
value = ""
): ):
""" """
Adds data to the 'info' variable. Sets a fixed value for a key in the 'SUMMARY' dictionary.
Args: Args:
data (str, optional): The data to add to the 'info' variable. Default is an empty string. key (str): Key to set the value for.
value (any): Value to assign to the key.
""" """
global info global SUMMARY
info += data SUMMARY[key] = value
####
# Add value to summary key
#########################################################################################
def add_summary_value(
key: str = "",
value = ""
):
"""
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.
"""
global SUMMARY
if key in SUMMARY:
SUMMARY[key] += value
else:
set_summary_value(key, value)
#### ####
# Set fixed value to info # Set fixed value to info
######################################################################################### #########################################################################################
def set_info_value( def set_info_value(
data: str = "" value: str = ""
): ):
""" """
Sets a fixed value to the 'info' variable. Sets a fixed value to the 'INFO' variable.
Args: Args:
data (str, optional): The value to set in the 'info' variable. Default is an empty string. data (str, optional): The value to set in the 'INFO' variable. Default is an empty string.
""" """
global info global INFO
info = data INFO = value
#### ####
# Set fixed value to info # Add data to info
#########################################################################################
def add_info_value(
value: str = ""
):
"""
Adds data to the 'INFO' variable.
Args:
data (str, optional): The data to add to the 'INFO' variable. Default is an empty string.
"""
global INFO
INFO += value
####
# Set fixed value to monitoring data
#########################################################################################
def set_monitoring_data(
data: list = []
):
"""
TODO: Add comments
"""
global MONITORING_DATA
MONITORING_DATA = data
####
# Add value to monitoring data
######################################################################################### #########################################################################################
def add_monitoring_data( def add_monitoring_data(
data: dict = {} data: dict = {}
@ -109,9 +121,9 @@ def add_monitoring_data(
""" """
TODO: Add comments TODO: Add comments
""" """
global monitoring_data global MONITORING_DATA
monitoring_data.append(data) MONITORING_DATA.append(data)
#### ####
# Print JSON output and exit script # Print JSON output and exit script
@ -120,28 +132,27 @@ def print_output():
""" """
Prints the JSON output and exits the script. Prints the JSON output and exits the script.
The function uses the global variables 'output', 'error_level', 'summary', and 'info' The function uses the global variables 'ERROR_LEVEL', 'SUMMARY', and 'info'
to create the JSON output. It then prints the JSON string and exits the script with to create the JSON output. It then prints the JSON string and exits the script with
the 'error_level' as the exit code. the 'ERROR_LEVEL' as the exit code.
""" """
global output global ERROR_LEVEL
global error_level global SUMMARY
global summary global INFO
global info global MONITORING_DATA
global monitoring_data
output={} OUTPUT={}
if summary: if SUMMARY:
output["summary"] = summary OUTPUT["summary"] = SUMMARY
if info: if INFO:
output["info"] = info OUTPUT["info"] = INFO
if monitoring_data: if MONITORING_DATA:
output["monitoring_data"] = monitoring_data OUTPUT["monitoring_data"] = MONITORING_DATA
json_string = json.dumps(output) json_string = json.dumps(OUTPUT)
print(json_string) print(json_string)
sys.exit(error_level) sys.exit(ERROR_LEVEL)

View File

@ -7,9 +7,9 @@ from multiprocessing import Pool, Manager
# Define multi-processing internal global variables. # Define multi-processing internal global variables.
######################################################################################### #########################################################################################
_manager = Manager() _MANAGER = Manager()
_shared_dict = _manager.dict() _SHARED_DICT = _MANAGER.dict()
_shared_dict_lock = _manager.Lock() _SHARED_DICT_LOCK = _MANAGER.Lock()
#### ####
# Internal use only: Run a given function in a thread # Internal use only: Run a given function in a thread
@ -110,11 +110,11 @@ def set_shared_dict_value(
Set a given value to a key in the internal shared dict. Set a given value to a key in the internal shared dict.
Used by all parallel processes. Used by all parallel processes.
""" """
global _shared_dict global _SHARED_DICT
if key is not None: if key is not None:
with _shared_dict_lock: with _SHARED_DICT_LOCK:
_shared_dict[key] = value _SHARED_DICT[key] = value
#### ####
# Add a given value to a key in the internal shared dict. # Add a given value to a key in the internal shared dict.
@ -128,12 +128,12 @@ def add_shared_dict_value(
Add a given value to a key in the internal shared dict. Add a given value to a key in the internal shared dict.
Used by all parallel processes. Used by all parallel processes.
""" """
global _shared_dict global _SHARED_DICT
if key is not None: if key is not None:
with _shared_dict_lock: with _SHARED_DICT_LOCK:
if key in _shared_dict: if key in _SHARED_DICT:
_shared_dict[key] += value _SHARED_DICT[key] += value
else: else:
set_shared_dict_value(key, value) set_shared_dict_value(key, value)
@ -148,11 +148,11 @@ def get_shared_dict_value(
Get the value of a key in the internal shared dict. Get the value of a key in the internal shared dict.
Used by all parallel processes. Used by all parallel processes.
""" """
global _shared_dict global _SHARED_DICT
with _shared_dict_lock: with _SHARED_DICT_LOCK:
if key in _shared_dict and key is not None: if key in _SHARED_DICT and key is not None:
return _shared_dict[key] return _SHARED_DICT[key]
else: else:
return None return None

View File

@ -12,7 +12,7 @@ from .agents import print_agent
# Its values can be changed. # Its values can be changed.
######################################################################################### #########################################################################################
global_variables = { GLOBAL_VARIABLES = {
'transfer_mode' : 'tentacle', 'transfer_mode' : 'tentacle',
'temporal' : '/tmp', 'temporal' : '/tmp',
'data_dir' : '/var/spool/pandora/data_in/', 'data_dir' : '/var/spool/pandora/data_in/',
@ -30,13 +30,13 @@ def set_global_variable(
value value
): ):
""" """
Sets the value of a global variable in the 'global_variables' dictionary. Sets the value of a global variable in the 'GLOBAL_VARIABLES' dictionary.
Args: Args:
variable_name (str): Name of the variable to set. variable_name (str): Name of the variable to set.
value (any): Value to assign to the variable. value (any): Value to assign to the variable.
""" """
set_dict_key_value(global_variables, variable_name, value) set_dict_key_value(GLOBAL_VARIABLES, variable_name, value)
#### ####
# Sends file using tentacle protocol # Sends file using tentacle protocol
@ -44,7 +44,7 @@ def set_global_variable(
def tentacle_xml( def tentacle_xml(
data_file: str = "", data_file: str = "",
tentacle_ops: dict = {}, tentacle_ops: dict = {},
tentacle_path: str = global_variables['tentacle_client'], tentacle_path: str = GLOBAL_VARIABLES['tentacle_client'],
debug: int = 0, debug: int = 0,
print_errors: bool = True print_errors: bool = True
) -> bool: ) -> bool:
@ -63,11 +63,11 @@ def tentacle_xml(
if data_file is not None : if data_file is not None :
if not 'address' in tentacle_ops: if not 'address' in tentacle_ops:
tentacle_ops['address'] = global_variables['tentacle_ip'] tentacle_ops['address'] = GLOBAL_VARIABLES['tentacle_ip']
if not 'port' in tentacle_ops: if not 'port' in tentacle_ops:
tentacle_ops['port'] = global_variables['tentacle_port'] tentacle_ops['port'] = GLOBAL_VARIABLES['tentacle_port']
if not 'extra_opts' in tentacle_ops: if not 'extra_opts' in tentacle_ops:
tentacle_ops['extra_opts'] = global_variables['tentacle_extra_opts'] tentacle_ops['extra_opts'] = GLOBAL_VARIABLES['tentacle_extra_opts']
if tentacle_ops['address'] is None : if tentacle_ops['address'] is None :
if print_errors: if print_errors:
@ -108,11 +108,11 @@ def tentacle_xml(
######################################################################################### #########################################################################################
def transfer_xml( def transfer_xml(
file: str = "", file: str = "",
transfer_mode: str = global_variables['transfer_mode'], transfer_mode: str = GLOBAL_VARIABLES['transfer_mode'],
tentacle_ip: str = global_variables['tentacle_ip'], tentacle_ip: str = GLOBAL_VARIABLES['tentacle_ip'],
tentacle_port: int = global_variables['tentacle_port'], tentacle_port: int = GLOBAL_VARIABLES['tentacle_port'],
tentacle_extra_opts: str = global_variables['tentacle_extra_opts'], tentacle_extra_opts: str = GLOBAL_VARIABLES['tentacle_extra_opts'],
data_dir: str = global_variables['data_dir'] data_dir: str = GLOBAL_VARIABLES['data_dir']
): ):
""" """
@ -120,10 +120,10 @@ def transfer_xml(
Args: Args:
file (str): Path to file to send. file (str): Path to file to send.
transfer_mode (str, optional): Transfer mode. Default is global_variables['transfer_mode']. transfer_mode (str, optional): Transfer mode. Default is GLOBAL_VARIABLES['transfer_mode'].
tentacle_ip (str, optional): IP address for Tentacle. Default is global_variables['tentacle_ip']. tentacle_ip (str, optional): IP address for Tentacle. Default is GLOBAL_VARIABLES['tentacle_ip'].
tentacle_port (str, optional): Port for Tentacle. Default is global_variables['tentacle_port']. tentacle_port (str, optional): Port for Tentacle. Default is GLOBAL_VARIABLES['tentacle_port'].
data_dir (str, optional): Path to data dir with local transfer mode. Default is global_variables['data_dir']. data_dir (str, optional): Path to data dir with local transfer mode. Default is GLOBAL_VARIABLES['data_dir'].
""" """
if file is not None: if file is not None:
if transfer_mode != "local": if transfer_mode != "local":
@ -142,7 +142,7 @@ def transfer_xml(
def write_xml( def write_xml(
xml: str = "", xml: str = "",
agent_name: str = "", agent_name: str = "",
data_dir: str = global_variables['temporal'] data_dir: str = GLOBAL_VARIABLES['temporal']
) -> str: ) -> str:
""" """
Creates a agent .data file in the specified data_dir folder Creates a agent .data file in the specified data_dir folder