pandoraPlugintools-basic 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ from .general import *
2
+ from .output import *
3
+ from .threads import *
4
+ from .agents import *
5
+ from .modules import *
6
+ from .transfer import *
7
+ from .discovery import *
@@ -0,0 +1,391 @@
1
+ import os
2
+ import sys
3
+
4
+ ####
5
+ # Define global variables dict, used in functions as default values.
6
+ # Its values can be changed.
7
+ #########################################################################################
8
+
9
+ _GLOBAL_VARIABLES = {"agents_group_name": "", "interval": 300}
10
+
11
+ ####
12
+ # Define some global variables
13
+ #########################################################################################
14
+
15
+ _WINDOWS = os.name == "nt" or os.name == "ce"
16
+ _LINUX = sys.platform.startswith("linux")
17
+ _MACOS = sys.platform.startswith("darwin")
18
+ _OSX = _MACOS # deprecated alias
19
+ _FREEBSD = sys.platform.startswith("freebsd")
20
+ _OPENBSD = sys.platform.startswith("openbsd")
21
+ _NETBSD = sys.platform.startswith("netbsd")
22
+ _BSD = _FREEBSD or _OPENBSD or _NETBSD
23
+ _SUNOS = sys.platform.startswith(("sunos", "solaris"))
24
+ _AIX = sys.platform.startswith("aix")
25
+
26
+ ####
27
+ # Internal: Alias for output.print_debug function
28
+ #########################################################################################
29
+
30
+
31
+ def _print_debug(var="", print_errors: bool = False):
32
+ """
33
+ Prints any list, dict, string, float or integer as a json
34
+ """
35
+ from .output import print_debug
36
+
37
+ print_debug(var, print_errors)
38
+
39
+
40
+ ####
41
+ # Set a global variable with the specified name and assigns a value to it.
42
+ #########################################################################################
43
+ def set_global_variable(variable_name: str = "", value=None) -> None:
44
+ """
45
+ Sets the value of a global variable in the '_GLOBAL_VARIABLES' dictionary.
46
+
47
+ Args:
48
+ variable_name (str): Name of the variable to set.
49
+ value (any): Value to assign to the variable.
50
+
51
+ Returns:
52
+ None
53
+ """
54
+ from .general import set_dict_key_value
55
+
56
+ set_dict_key_value(_GLOBAL_VARIABLES, variable_name, value)
57
+
58
+
59
+ ####
60
+ # Get a global variable with the specified name.
61
+ #########################################################################################
62
+ def get_global_variable(variable_name: str = "") -> None:
63
+ """
64
+ Gets the value of a global variable in the '_GLOBAL_VARIABLES' dictionary.
65
+
66
+ Args:
67
+ variable_name (str): Name of the variable to set.
68
+
69
+ Returns:
70
+ None
71
+ """
72
+ from .general import get_dict_key_value
73
+
74
+ get_dict_key_value(_GLOBAL_VARIABLES, variable_name)
75
+
76
+
77
+ ####
78
+ # Agent class
79
+ #########################################################################################
80
+
81
+
82
+ class Agent:
83
+ """
84
+ Basic agent class. Requires agent parameters (config {dictionary})
85
+ and module definition (modules_def [list of dictionaries])
86
+ """
87
+
88
+ def __init__(
89
+ self, config: dict = {}, modules_def: list = [], log_modules_def: list = []
90
+ ):
91
+
92
+ self.modules_def = []
93
+ self.added_modules = []
94
+ self.log_modules_def = []
95
+
96
+ self.config = init_agent(config)
97
+
98
+ for module in modules_def:
99
+ self.add_module(module)
100
+
101
+ for log_module in log_modules_def:
102
+ self.add_log_module(log_module)
103
+
104
+ def update_config(self, config: dict = {}) -> None:
105
+ """
106
+ Update the configuration settings with new values.
107
+
108
+ Args:
109
+ config (dict): A dictionary containing configuration keys and their new values.
110
+
111
+ Returns:
112
+ None
113
+ """
114
+ for key, value in config.items():
115
+ if key in self.config:
116
+ self.config[key] = value
117
+
118
+ def get_config(self) -> dict:
119
+ """
120
+ Retrieve the current configuration settings.
121
+
122
+ Returns:
123
+ dict: A dictionary containing the current configuration settings.
124
+ """
125
+ return self.config
126
+
127
+ def add_module(self, module: dict = {}) -> None:
128
+ """
129
+ Add a new module to the list of modules.
130
+
131
+ Args:
132
+ module (dict): A dictionary containing module information.
133
+
134
+ Returns:
135
+ None
136
+ """
137
+ from .general import generate_md5
138
+ from .modules import init_module
139
+
140
+ if (
141
+ "name" in module
142
+ and type(module["name"]) == str
143
+ and len(module["name"].strip()) > 0
144
+ ):
145
+ self.modules_def.append(init_module(module))
146
+ self.added_modules.append(generate_md5(module["name"]))
147
+
148
+ def del_module(self, module_name: str = "") -> None:
149
+ """
150
+ Delete a module based on its name.
151
+
152
+ Args:
153
+ module_name (str): The name of the module to be deleted.
154
+
155
+ Returns:
156
+ None
157
+ """
158
+ from .general import generate_md5
159
+
160
+ if len(module_name.strip()) > 0:
161
+ try:
162
+ module_id = self.added_modules.index(generate_md5(module_name))
163
+ except:
164
+ module_id = None
165
+
166
+ if module_id is not None:
167
+ self.added_modules.pop(module_id)
168
+ self.modules_def.pop(module_id)
169
+
170
+ def update_module(self, module_name: str = "", module: dict = {}) -> None:
171
+ """
172
+ Update a module based on its name.
173
+
174
+ Args:
175
+ module_name (str): The name of the module to be updated.
176
+ module (dict): A dictionary containing updated module information.
177
+
178
+ Returns:
179
+ None
180
+ """
181
+ module_def = self.get_module(module_name)
182
+
183
+ if module_def:
184
+ if "name" not in module:
185
+ module["name"] = module_name
186
+
187
+ module_def.update(module)
188
+
189
+ self.del_module(module_name)
190
+ self.add_module(module_def)
191
+
192
+ def get_module(self, module_name: str = "") -> dict:
193
+ """
194
+ Retrieve module information based on its name.
195
+
196
+ Args:
197
+ module_name (str): The name of the module to retrieve.
198
+
199
+ Returns:
200
+ dict: A dictionary containing module information if found, otherwise an empty dictionary.
201
+ """
202
+ from .general import generate_md5
203
+
204
+ if len(module_name.strip()) > 0:
205
+ try:
206
+ module_id = self.added_modules.index(generate_md5(module_name))
207
+ except:
208
+ module_id = None
209
+
210
+ if module_id is not None:
211
+ return self.modules_def[module_id]
212
+ else:
213
+ return {}
214
+
215
+ def get_modules_def(self) -> dict:
216
+ """
217
+ Retrieve the definitions of all added modules.
218
+
219
+ Returns:
220
+ dict: A dictionary containing the definitions of all added modules.
221
+ """
222
+ return self.modules_def
223
+
224
+ def add_log_module(self, log_module: dict = {}) -> None:
225
+ """
226
+ Add a new log module to the list of log modules.
227
+
228
+ Args:
229
+ log_module (dict): A dictionary containing log module information.
230
+
231
+ Returns:
232
+ None
233
+ """
234
+ from .modules import init_log_module
235
+
236
+ if (
237
+ "source" in log_module
238
+ and type(log_module["source"]) == str
239
+ and len(log_module["source"].strip()) > 0
240
+ ):
241
+ self.log_modules_def.append(init_log_module(log_module))
242
+
243
+ def get_log_modules_def(self) -> dict:
244
+ """
245
+ Retrieve the definitions of all added log modules.
246
+
247
+ Returns:
248
+ dict: A dictionary containing the definitions of all added log modules.
249
+ """
250
+ return self.log_modules_def
251
+
252
+ def print_xml(self, print_flag: bool = False) -> str:
253
+ """
254
+ Generate and optionally print the XML representation of the agent.
255
+
256
+ Args:
257
+ print_flag (bool): A flag indicating whether to print the XML representation.
258
+
259
+ Returns:
260
+ str: The XML representation of the agent.
261
+ """
262
+ return print_agent(
263
+ self.get_config(),
264
+ self.get_modules_def(),
265
+ self.get_log_modules_def(),
266
+ print_flag,
267
+ )
268
+
269
+
270
+ ####
271
+ # Gets system OS name
272
+ #########################################################################################
273
+ def get_os() -> str:
274
+ """
275
+ Gets system OS name
276
+
277
+ Returns:
278
+ str: OS name.
279
+ """
280
+ os = "Other"
281
+
282
+ if _WINDOWS:
283
+ os = "Windows"
284
+
285
+ if _LINUX:
286
+ os = "Linux"
287
+
288
+ if _MACOS or _OSX:
289
+ os = "MacOS"
290
+
291
+ if _FREEBSD or _OPENBSD or _NETBSD or _BSD:
292
+ os = "BSD"
293
+
294
+ if _SUNOS:
295
+ os = "Solaris"
296
+
297
+ if _AIX:
298
+ os = "AIX"
299
+
300
+ return os
301
+
302
+
303
+ ####
304
+ # Init agent template
305
+ #########################################################################################
306
+ def init_agent(default_values: dict = {}) -> dict:
307
+ """
308
+ Initialize an agent template with default values.
309
+
310
+ Args:
311
+ default_values (dict): A dictionary containing custom default values for the agent template.
312
+
313
+ Returns:
314
+ dict: A dictionary representing the agent template with default and custom values.
315
+ """
316
+ from .general import now
317
+
318
+ agent = {
319
+ "agent_name": "",
320
+ "agent_alias": "",
321
+ "parent_agent_name": "",
322
+ "description": "",
323
+ "version": None,
324
+ "os_name": "",
325
+ "os_version": "",
326
+ "timestamp": now(),
327
+ "address": "",
328
+ "group": _GLOBAL_VARIABLES["agents_group_name"],
329
+ "interval": _GLOBAL_VARIABLES["interval"],
330
+ "agent_mode": "1",
331
+ }
332
+
333
+ for key, value in default_values.items():
334
+ if key in agent:
335
+ agent[key] = value
336
+
337
+ return agent
338
+
339
+
340
+ ####
341
+ # Prints agent XML. Requires agent conf (dict) and modules (list) as arguments.
342
+ #########################################################################################
343
+ def print_agent(
344
+ agent: dict = None,
345
+ modules: list = [],
346
+ log_modules: list = [],
347
+ log_encoding: str = False,
348
+ print_flag: bool = False,
349
+ ) -> str:
350
+ """
351
+ Print the XML representation of an agent.
352
+
353
+ Args:
354
+ agent (dict): A dictionary containing agent configuration.
355
+ modules (list): A list of dictionaries representing modules.
356
+ log_modules (list): A list of dictionaries representing log modules.
357
+ log_encoding (str): The encoding format used when printing log modules
358
+ print_flag (bool): A flag indicating whether to print the XML representation.
359
+
360
+ Returns:
361
+ str: The XML representation of the agent.
362
+ """
363
+ from .modules import print_log_module, print_module
364
+ from .output import print_stdout
365
+
366
+ xml = ""
367
+ data_file = None
368
+
369
+ if agent is not None:
370
+ header = "<?xml version='1.0' encoding='UTF-8'?>\n"
371
+ header += "<agent_data"
372
+ for dato in agent:
373
+ if agent[dato] is not None:
374
+ header += " " + str(dato) + "='" + str(agent[dato]) + "'"
375
+ header += ">\n"
376
+ xml = header
377
+
378
+ for module in modules:
379
+ modules_xml = print_module(module)
380
+ xml += str(modules_xml)
381
+
382
+ for log_module in log_modules:
383
+ modules_xml = print_log_module(log_module, encoding=log_encoding)
384
+ xml += str(modules_xml)
385
+
386
+ xml += "</agent_data>"
387
+
388
+ if print_flag:
389
+ print_stdout(xml)
390
+
391
+ return xml
@@ -0,0 +1,215 @@
1
+ import json
2
+ import sys
3
+
4
+ ####
5
+ # Define some global variables
6
+ #########################################################################################
7
+
8
+ _ERROR_LEVEL = 0
9
+ _SUMMARY = {}
10
+ _INFO = ""
11
+ _MONITORING_DATA = []
12
+
13
+ ####
14
+ # Internal: Alias for output.print_debug function
15
+ #########################################################################################
16
+
17
+
18
+ def _print_debug(var="", print_errors: bool = False):
19
+ """
20
+ Print the variable as a JSON-like representation for debugging purposes.
21
+
22
+ Args:
23
+ var (any): The variable to be printed.
24
+ print_errors (bool): A flag indicating whether to print errors during debugging.
25
+ """
26
+ from .output import print_debug
27
+
28
+ print_debug(var, print_errors)
29
+
30
+
31
+ ####
32
+ # Set error level to value
33
+ #########################################################################################
34
+ def set_disco_error_level(value: int = 0) -> None:
35
+ """
36
+ Sets the error level to the specified value.
37
+
38
+ Args:
39
+ value (int, optional): The error level value. Default is 0.
40
+ """
41
+ global _ERROR_LEVEL
42
+
43
+ _ERROR_LEVEL = value
44
+
45
+
46
+ ####
47
+ # Set fixed value to summary dict
48
+ #########################################################################################
49
+ def set_disco_summary(data: dict = {}) -> None:
50
+ """
51
+ Sets the disk summary data in the internal summary dictionary.
52
+
53
+ This function updates the summary dictionary with the provided disk summary data.
54
+
55
+ Args:
56
+ data (dict): A dictionary containing disk summary data.
57
+
58
+ Returns:
59
+ None
60
+ """
61
+ global _SUMMARY
62
+
63
+ _SUMMARY = {}
64
+
65
+
66
+ ####
67
+ # Set fixed value to summary key
68
+ #########################################################################################
69
+ def set_disco_summary_value(key: str = "", value=None) -> None:
70
+ """
71
+ Sets a fixed value for a key in the '_SUMMARY' dictionary.
72
+
73
+ Args:
74
+ key (str): Key to set the value for.
75
+ value (any): Value to assign to the key.
76
+
77
+ Returns:
78
+ None
79
+ """
80
+ global _SUMMARY
81
+
82
+ _SUMMARY[key] = value
83
+
84
+
85
+ ####
86
+ # Add value to summary key
87
+ #########################################################################################
88
+ def add_disco_summary_value(key: str = "", value=None) -> None:
89
+ """
90
+ Adds a value to a key in the 'SUMMARY' dictionary.
91
+
92
+ If the key already exists, the value will be incremented. Otherwise, a new key will be created.
93
+
94
+ Args:
95
+ key (str): Key to add the value to.
96
+ value (any): Value to add to the key.
97
+
98
+ Returns:
99
+ None
100
+ """
101
+ global _SUMMARY
102
+
103
+ if key in _SUMMARY:
104
+ _SUMMARY[key] += value
105
+ else:
106
+ set_disco_summary_value(key, value)
107
+
108
+
109
+ ####
110
+ # Set fixed value to info
111
+ #########################################################################################
112
+ def set_disco_info_value(value: str = "") -> None:
113
+ """
114
+ Sets a fixed value to the '_INFO' variable.
115
+
116
+ Args:
117
+ data (str, optional): The value to set in the '_INFO' variable. Default is an empty string.
118
+
119
+ Returns:
120
+ None
121
+ """
122
+ global _INFO
123
+
124
+ _INFO = value
125
+
126
+
127
+ ####
128
+ # Add data to info
129
+ #########################################################################################
130
+ def add_disco_info_value(value: str = "") -> None:
131
+ """
132
+ Adds data to the '_INFO' variable.
133
+
134
+ Args:
135
+ data (str, optional): The data to add to the '_INFO' variable. Default is an empty string.
136
+
137
+ Returns:
138
+ None
139
+ """
140
+ global _INFO
141
+
142
+ _INFO += value
143
+
144
+
145
+ ####
146
+ # Set fixed value to monitoring data
147
+ #########################################################################################
148
+ def set_disco_monitoring_data(data: list = []) -> None:
149
+ """
150
+ Set the monitoring data for disk usage.
151
+
152
+ Args:
153
+ data (list): A list containing disk monitoring data.
154
+
155
+ Returns:
156
+ None
157
+ """
158
+ global _MONITORING_DATA
159
+
160
+ _MONITORING_DATA = data
161
+
162
+
163
+ ####
164
+ # Add value to monitoring data
165
+ #########################################################################################
166
+ def add_disco_monitoring_data(data: dict = {}) -> None:
167
+ """
168
+ Add disk monitoring data to the global monitoring dataset.
169
+
170
+ Args:
171
+ data (dict): A dictionary containing disk monitoring data.
172
+
173
+ Returns:
174
+ None
175
+ """
176
+ global _MONITORING_DATA
177
+
178
+ _MONITORING_DATA.append(data)
179
+
180
+
181
+ ####
182
+ # Print JSON output and exit script
183
+ #########################################################################################
184
+ def disco_output() -> None:
185
+ """
186
+ Prints the JSON output and exits the script.
187
+
188
+ The function uses the global variables '_ERROR_LEVEL', '_SUMMARY', '_INFO' and '_MONITORING_DATA'
189
+ to create the JSON output. It then prints the JSON string and exits the script with
190
+ the '_ERROR_LEVEL' as the exit code.
191
+
192
+ Returns:
193
+ None
194
+ """
195
+ from .output import print_stdout
196
+
197
+ global _ERROR_LEVEL
198
+ global _SUMMARY
199
+ global _INFO
200
+ global _MONITORING_DATA
201
+
202
+ output = {}
203
+ if _SUMMARY:
204
+ output["summary"] = _SUMMARY
205
+
206
+ if _INFO:
207
+ output["info"] = _INFO
208
+
209
+ if _MONITORING_DATA:
210
+ output["monitoring_data"] = _MONITORING_DATA
211
+
212
+ json_string = json.dumps(output)
213
+
214
+ print_stdout(json_string)
215
+ sys.exit(_ERROR_LEVEL)