pandorafms/pandora_server/extras/pandoraPlugintools/transfer.py

188 lines
6.6 KiB
Python
Raw Normal View History

from datetime import datetime
from subprocess import *
import shutil
import subprocess
import os
import sys
####
# Define global variables dict, used in functions as default values.
# Its values can be changed.
#########################################################################################
GLOBAL_VARIABLES = {
'transfer_mode' : 'tentacle',
'temporal' : '/tmp',
'data_dir' : '/var/spool/pandora/data_in/',
'tentacle_client' : 'tentacle_client',
'tentacle_ip' : '127.0.0.1',
'tentacle_port' : 41121,
'tentacle_extra_opts' : ''
}
####
# 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)
####
# Set a global variable with the specified name and assigns a value to it.
#########################################################################################
def set_global_variable(
variable_name: str = "",
2023-08-02 11:42:20 +02:00
value = None
):
2023-07-26 11:21:15 +02:00
"""
Sets the value of a global variable in the 'GLOBAL_VARIABLES' dictionary.
2023-07-26 11:21:15 +02:00
Args:
variable_name (str): Name of the variable to set.
value (any): Value to assign to the variable.
"""
2023-08-09 13:46:09 +02:00
from .general import set_dict_key_value
set_dict_key_value(GLOBAL_VARIABLES, variable_name, value)
####
# Sends file using tentacle protocol
#########################################################################################
def tentacle_xml(
data_file: str = "",
tentacle_ops: dict = {},
tentacle_path: str = GLOBAL_VARIABLES['tentacle_client'],
debug: int = 0,
print_errors: bool = True
) -> bool:
"""
Sends file using tentacle protocol
- Only works with one file at time.
- file variable needs full file path.
- tentacle_opts should be a dict with tentacle options (address [password] [port]).
- tentacle_path allows to define a custom path for tentacle client in case is not in sys path).
- if debug is enabled, the data file will not be removed after being sent.
- if print_errors is enabled, function will print all error messages
Returns True for OK and False for errors.
"""
2023-08-09 13:46:09 +02:00
from .output import print_stderr
if data_file is not None :
if not 'address' in tentacle_ops:
tentacle_ops['address'] = GLOBAL_VARIABLES['tentacle_ip']
if not 'port' in tentacle_ops:
tentacle_ops['port'] = GLOBAL_VARIABLES['tentacle_port']
if not 'extra_opts' in tentacle_ops:
tentacle_ops['extra_opts'] = GLOBAL_VARIABLES['tentacle_extra_opts']
if tentacle_ops['address'] is None :
if print_errors:
2023-08-09 13:46:09 +02:00
print_stderr("Tentacle error: No address defined")
return False
try :
with open(data_file.strip(), 'r') as data:
data.read()
data.close()
except Exception as e :
if print_errors:
2023-08-09 13:46:09 +02:00
print_stderr(f"Tentacle error: {type(e).__name__} {e}")
return False
tentacle_cmd = f"{tentacle_path} -v -a {tentacle_ops['address']} -p {tentacle_ops['port']} {tentacle_ops['extra_opts']} {data_file.strip()}"
2023-08-09 13:46:09 +02:00
tentacle_exe=subprocess.Popen(tentacle_cmd, stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
rc=tentacle_exe.wait()
if debug == 0 :
os.remove(data_file.strip())
if rc != 0 :
if print_errors:
stderr = tentacle_exe.stderr.read().decode()
msg="Tentacle error:" + str(stderr)
2023-08-09 13:46:09 +02:00
print_stderr(str(datetime.today().strftime('%Y-%m-%d %H:%M')) + msg)
return False
else:
if print_errors:
2023-08-09 13:46:09 +02:00
print_stderr("Tentacle error: file path is required.")
return False
####
# Detect transfer mode and send XML.
#########################################################################################
def transfer_xml(
file: str = "",
transfer_mode: str = GLOBAL_VARIABLES['transfer_mode'],
tentacle_ip: str = GLOBAL_VARIABLES['tentacle_ip'],
tentacle_port: int = GLOBAL_VARIABLES['tentacle_port'],
tentacle_extra_opts: str = GLOBAL_VARIABLES['tentacle_extra_opts'],
data_dir: str = GLOBAL_VARIABLES['data_dir']
):
2023-07-26 11:21:15 +02:00
"""
Detects the transfer mode and calls the agentplugin() function to perform the transfer.
Args:
file (str): Path to file to send.
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_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'].
"""
if file is not None:
if transfer_mode != "local":
tentacle_conf = {
'address' : tentacle_ip,
'port' : tentacle_port,
'extra_opts' : tentacle_extra_opts
}
tentacle_xml(file, tentacle_conf)
else:
shutil.move(file, data_dir)
####
# Creates a agent .data file in the specified data_dir folder
#########################################################################################
def write_xml(
xml: str = "",
agent_name: str = "",
2023-08-09 13:46:09 +02:00
data_dir: str = GLOBAL_VARIABLES['temporal'],
print_errors: bool = False
) -> str:
"""
Creates a agent .data file in the specified data_dir folder
Args:
- xml (str): XML string to be written in the file.
- agent_name (str): agent name for the xml and file name.
- data_dir (str): folder in which the file will be created.
2023-07-26 11:21:15 +02:00
"""
2023-08-09 13:46:09 +02:00
from .general import generate_md5
from .output import print_stderr
Utime = datetime.now().strftime('%s')
agent_name_md5 = generate_md5(agent_name)
data_file = "%s/%s.%s.data" %(str(data_dir),agent_name_md5,str(Utime))
try:
with open(data_file, 'x') as data:
data.write(xml)
except OSError as o:
2023-08-09 13:46:09 +02:00
if print_errors:
print_stderr(f"ERROR - Could not write file: {o}, please check directory permissions")
except Exception as e:
2023-08-09 13:46:09 +02:00
if print_errors:
print_stderr(f"{type(e).__name__}: {e}")
return data_file