pandoraPlugintools-basic 1.1.1__tar.gz → 1.2.0__tar.gz
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.
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/PKG-INFO +72 -1
- pandoraplugintools_basic-1.2.0/README.md +169 -0
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools/__init__.py +2 -1
- pandoraplugintools_basic-1.2.0/pandoraPlugintools/post_transforms.py +400 -0
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools_basic.egg-info/PKG-INFO +72 -1
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools_basic.egg-info/SOURCES.txt +3 -1
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/setup.cfg +1 -1
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/setup.py +1 -1
- pandoraplugintools_basic-1.2.0/tests/test_post_transforms.py +308 -0
- pandoraplugintools_basic-1.1.1/README.md +0 -98
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/LICENSE +0 -0
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools/agents.py +0 -0
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools/discovery.py +0 -0
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools/general.py +0 -0
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools/modules.py +0 -0
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools/monitoring.py +0 -0
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools/output.py +0 -0
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools/threads.py +0 -0
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools/transfer.py +0 -0
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools_basic.egg-info/dependency_links.txt +0 -0
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools_basic.egg-info/top_level.txt +0 -0
- {pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/tests/test_basic.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pandoraPlugintools-basic
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.2.0
|
|
4
4
|
Summary: A plugin tool set of basic functions for pandorafms
|
|
5
5
|
Home-page: https://github.com/pandorafms/pandoraPlugintoolsBasic
|
|
6
6
|
Author: PandoraFMS projects department
|
|
@@ -50,7 +50,9 @@ The package exports the following modules:
|
|
|
50
50
|
- `modules`: Functions to build Pandora FMS module payloads.
|
|
51
51
|
- `transfer`: File transfer and data sending helpers.
|
|
52
52
|
- `discovery`: Helpers for Pandora FMS discovery plugins.
|
|
53
|
+
- `monitoring`: Helpers to accumulate and emit `/monitoring` API payloads.
|
|
53
54
|
- `output`: Output formatting and return helpers.
|
|
55
|
+
- `post_transforms`: Post-transform block engine for Discovery tasks (regex rules that merge whitelisted module fields before serialization).
|
|
54
56
|
|
|
55
57
|
Reference documentation:
|
|
56
58
|
[PluginTools Reference Documentation](https://pandorafms.com/guides/public/books/plugintools)
|
|
@@ -69,6 +71,8 @@ The README stays focused on the quick path. For a complete function list and ful
|
|
|
69
71
|
| `transfer` | `write_xml`, `transfer_xml`, `tentacle_xml` |
|
|
70
72
|
| `threads` | `run_threads`, `run_processes`, `set_shared_dict_value`, `get_shared_dict_value` |
|
|
71
73
|
| `discovery` | `set_disco_summary`, `add_disco_summary_value`, `add_disco_info_value`, `add_disco_monitoring_data`, `disco_output` |
|
|
74
|
+
| `monitoring` | `set_monitoring_items`, `add_monitoring_item`, `get_monitoring_items`, `print_monitoring_payload` |
|
|
75
|
+
| `post_transforms` | `parse_transform_blocks`, `apply_post_transforms`, `TransformParseError` |
|
|
72
76
|
| `output` | `print_stdout`, `print_stderr`, `print_debug`, `logger` |
|
|
73
77
|
|
|
74
78
|
## Quick example
|
|
@@ -113,3 +117,70 @@ ppt.transfer_xml(
|
|
|
113
117
|
)
|
|
114
118
|
```
|
|
115
119
|
|
|
120
|
+
## Post-transform blocks for Discovery tasks
|
|
121
|
+
|
|
122
|
+
Discovery plugins can let task operators enrich modules without touching plugin
|
|
123
|
+
code. Operators write ordered rule blocks in plain text; the plugin applies
|
|
124
|
+
them over the collected agent/module structure right before serializing it, so
|
|
125
|
+
the same rules work for the native JSON path and the tentacle XML path.
|
|
126
|
+
|
|
127
|
+
```text
|
|
128
|
+
transform_begin
|
|
129
|
+
transform_name cpu thresholds
|
|
130
|
+
match_module .*cpu_percent
|
|
131
|
+
match_extra_data .*:metric:cpu
|
|
132
|
+
set_min_warning 80
|
|
133
|
+
set_max_critical 0
|
|
134
|
+
set_module_group Rendimiento
|
|
135
|
+
set_tags infra:cpu
|
|
136
|
+
transform_end
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Semantics:
|
|
140
|
+
|
|
141
|
+
- `match_module` and `match_extra_data` are regexes (substring match; use
|
|
142
|
+
`^...$` for full match) AND-ed inside a block; a rule needs at least one.
|
|
143
|
+
- Rules apply in order; for each field the last matching rule wins.
|
|
144
|
+
- `set_<field>` accepts exactly the module fields the Discovery ingestion
|
|
145
|
+
already supports (thresholds, `str_*`, `tags`, `module_group`,
|
|
146
|
+
`alert_template`, FF settings, `custom_id`, ...). Unknown fields are errors.
|
|
147
|
+
- `set_tags` holds a comma-separated list (`set_tags infra,ops`), the same
|
|
148
|
+
convention as the agent's `module_tags`: the whole string travels in one
|
|
149
|
+
field and the server splits it.
|
|
150
|
+
- `name`, `type`, `data`, `value`, `extra_data`, `timestamp` and `status` are
|
|
151
|
+
protected: a rule cannot change module identity or the collected value.
|
|
152
|
+
- Threshold ranges are half-open `[min, max)`; `max_warning=0` and
|
|
153
|
+
`max_critical=0` mean "no upper limit", not the literal zero.
|
|
154
|
+
- Invalid regexes, unknown keys and mistyped values are reported with the
|
|
155
|
+
block and key that caused them: the tolerant API
|
|
156
|
+
(`parse_transform_blocks_tolerant`) skips the offending rule and keeps the
|
|
157
|
+
rest, so one bad rule never throws away the monitoring run; the strict API
|
|
158
|
+
(`parse_transform_blocks`) raises `TransformParseError` instead.
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
import pandoraPlugintools as ppt
|
|
162
|
+
|
|
163
|
+
rules_text = open("post_transforms.conf").read()
|
|
164
|
+
|
|
165
|
+
# Tolerant parse: bad rules are skipped and reported; good rules apply.
|
|
166
|
+
rules, errors = ppt.parse_transform_blocks_tolerant(rules_text)
|
|
167
|
+
if errors:
|
|
168
|
+
ppt.set_disco_error_level(1)
|
|
169
|
+
for error in errors:
|
|
170
|
+
ppt.add_disco_info_value("post-transforms: %s\n" % error)
|
|
171
|
+
ppt.print_stderr("post-transforms: %s\n" % error)
|
|
172
|
+
|
|
173
|
+
# JSON path: over the accumulated monitoring items.
|
|
174
|
+
items = ppt.get_monitoring_items()
|
|
175
|
+
ppt.apply_post_transforms(items, rules)
|
|
176
|
+
payload = ppt.print_monitoring_payload()
|
|
177
|
+
|
|
178
|
+
# XML path: over the module list, before print_agent().
|
|
179
|
+
ppt.apply_post_transforms(modules, rules)
|
|
180
|
+
xml_content = ppt.print_agent(agent, modules)
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
The errors are reported in the discovery JSON (`info` plus a non-zero error
|
|
184
|
+
level) and on stderr, so manual runs show them immediately; the monitoring
|
|
185
|
+
itself is never stopped by a bad rule.
|
|
186
|
+
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# pandoraPlugintoolsBasic
|
|
2
|
+
|
|
3
|
+
`pandoraPlugintoolsBasic` is a lightweight, dependency-free version of `pandoraPlugintools` for Pandora FMS plugin development.
|
|
4
|
+
|
|
5
|
+
It is designed as a drop-in replacement for the most commonly used functionality, with a smaller footprint and simpler deployment.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install pandoraPlugintools-basic
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Migration from pandoraPlugintools
|
|
14
|
+
|
|
15
|
+
If your plugin only uses the basic features included in this package, migrate with:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip uninstall pandoraPlugintools
|
|
19
|
+
pip install pandoraPlugintools-basic
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Important:
|
|
23
|
+
- This package does not include all features from the full `pandoraPlugintools` package.
|
|
24
|
+
- If your current code depends on advanced modules (for example SNMP or encryption helpers), keep using the standard package.
|
|
25
|
+
|
|
26
|
+
## What is included
|
|
27
|
+
|
|
28
|
+
The package exports the following modules:
|
|
29
|
+
|
|
30
|
+
- `general`: General-purpose helper functions for plugin development.
|
|
31
|
+
- `threads`: Utilities for running functions in parallel.
|
|
32
|
+
- `agents`: Functions to build Pandora FMS agent payloads.
|
|
33
|
+
- `modules`: Functions to build Pandora FMS module payloads.
|
|
34
|
+
- `transfer`: File transfer and data sending helpers.
|
|
35
|
+
- `discovery`: Helpers for Pandora FMS discovery plugins.
|
|
36
|
+
- `monitoring`: Helpers to accumulate and emit `/monitoring` API payloads.
|
|
37
|
+
- `output`: Output formatting and return helpers.
|
|
38
|
+
- `post_transforms`: Post-transform block engine for Discovery tasks (regex rules that merge whitelisted module fields before serialization).
|
|
39
|
+
|
|
40
|
+
Reference documentation:
|
|
41
|
+
[PluginTools Reference Documentation](https://pandorafms.com/guides/public/books/plugintools)
|
|
42
|
+
|
|
43
|
+
For a full, runnable example per function, see [docs/examples.md](docs/examples.md).
|
|
44
|
+
|
|
45
|
+
## Most used functions
|
|
46
|
+
|
|
47
|
+
The README stays focused on the quick path. For a complete function list and full details, use the reference documentation link above.
|
|
48
|
+
|
|
49
|
+
| Module | Common functions |
|
|
50
|
+
|-------|------------------|
|
|
51
|
+
| `general` | `generate_md5`, `safe_input`, `safe_output`, `parse_configuration`, `parse_csv_file`, `translate_macros`, `now` |
|
|
52
|
+
| `agents` | `init_agent`, `print_agent`, `get_os` |
|
|
53
|
+
| `modules` | `init_module`, `print_module`, `init_log_module`, `print_log_module` |
|
|
54
|
+
| `transfer` | `write_xml`, `transfer_xml`, `tentacle_xml` |
|
|
55
|
+
| `threads` | `run_threads`, `run_processes`, `set_shared_dict_value`, `get_shared_dict_value` |
|
|
56
|
+
| `discovery` | `set_disco_summary`, `add_disco_summary_value`, `add_disco_info_value`, `add_disco_monitoring_data`, `disco_output` |
|
|
57
|
+
| `monitoring` | `set_monitoring_items`, `add_monitoring_item`, `get_monitoring_items`, `print_monitoring_payload` |
|
|
58
|
+
| `post_transforms` | `parse_transform_blocks`, `apply_post_transforms`, `TransformParseError` |
|
|
59
|
+
| `output` | `print_stdout`, `print_stderr`, `print_debug`, `logger` |
|
|
60
|
+
|
|
61
|
+
## Quick example
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
import pandoraPlugintools as ppt
|
|
65
|
+
|
|
66
|
+
# Define agent
|
|
67
|
+
server_name = "WIN-SERV"
|
|
68
|
+
|
|
69
|
+
agent = ppt.init_agent(
|
|
70
|
+
{
|
|
71
|
+
"agent_name": ppt.generate_md5(server_name),
|
|
72
|
+
"agent_alias": server_name,
|
|
73
|
+
"description": "Default Windows server",
|
|
74
|
+
}
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
# Define modules
|
|
78
|
+
modules = []
|
|
79
|
+
data = 10
|
|
80
|
+
|
|
81
|
+
modules.append(
|
|
82
|
+
{
|
|
83
|
+
"name": "CPU usage",
|
|
84
|
+
"type": "generic_data",
|
|
85
|
+
"value": data,
|
|
86
|
+
"desc": "Percentage of CPU utilization",
|
|
87
|
+
"unit": "%",
|
|
88
|
+
}
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
# Generate and transfer XML
|
|
92
|
+
xml_content = ppt.print_agent(agent, modules)
|
|
93
|
+
xml_file = ppt.write_xml(xml_content, agent["agent_name"])
|
|
94
|
+
|
|
95
|
+
ppt.transfer_xml(
|
|
96
|
+
xml_file,
|
|
97
|
+
transfer_mode="tentacle",
|
|
98
|
+
tentacle_ip="192.168.1.20",
|
|
99
|
+
tentacle_port="41121",
|
|
100
|
+
)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Post-transform blocks for Discovery tasks
|
|
104
|
+
|
|
105
|
+
Discovery plugins can let task operators enrich modules without touching plugin
|
|
106
|
+
code. Operators write ordered rule blocks in plain text; the plugin applies
|
|
107
|
+
them over the collected agent/module structure right before serializing it, so
|
|
108
|
+
the same rules work for the native JSON path and the tentacle XML path.
|
|
109
|
+
|
|
110
|
+
```text
|
|
111
|
+
transform_begin
|
|
112
|
+
transform_name cpu thresholds
|
|
113
|
+
match_module .*cpu_percent
|
|
114
|
+
match_extra_data .*:metric:cpu
|
|
115
|
+
set_min_warning 80
|
|
116
|
+
set_max_critical 0
|
|
117
|
+
set_module_group Rendimiento
|
|
118
|
+
set_tags infra:cpu
|
|
119
|
+
transform_end
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Semantics:
|
|
123
|
+
|
|
124
|
+
- `match_module` and `match_extra_data` are regexes (substring match; use
|
|
125
|
+
`^...$` for full match) AND-ed inside a block; a rule needs at least one.
|
|
126
|
+
- Rules apply in order; for each field the last matching rule wins.
|
|
127
|
+
- `set_<field>` accepts exactly the module fields the Discovery ingestion
|
|
128
|
+
already supports (thresholds, `str_*`, `tags`, `module_group`,
|
|
129
|
+
`alert_template`, FF settings, `custom_id`, ...). Unknown fields are errors.
|
|
130
|
+
- `set_tags` holds a comma-separated list (`set_tags infra,ops`), the same
|
|
131
|
+
convention as the agent's `module_tags`: the whole string travels in one
|
|
132
|
+
field and the server splits it.
|
|
133
|
+
- `name`, `type`, `data`, `value`, `extra_data`, `timestamp` and `status` are
|
|
134
|
+
protected: a rule cannot change module identity or the collected value.
|
|
135
|
+
- Threshold ranges are half-open `[min, max)`; `max_warning=0` and
|
|
136
|
+
`max_critical=0` mean "no upper limit", not the literal zero.
|
|
137
|
+
- Invalid regexes, unknown keys and mistyped values are reported with the
|
|
138
|
+
block and key that caused them: the tolerant API
|
|
139
|
+
(`parse_transform_blocks_tolerant`) skips the offending rule and keeps the
|
|
140
|
+
rest, so one bad rule never throws away the monitoring run; the strict API
|
|
141
|
+
(`parse_transform_blocks`) raises `TransformParseError` instead.
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
import pandoraPlugintools as ppt
|
|
145
|
+
|
|
146
|
+
rules_text = open("post_transforms.conf").read()
|
|
147
|
+
|
|
148
|
+
# Tolerant parse: bad rules are skipped and reported; good rules apply.
|
|
149
|
+
rules, errors = ppt.parse_transform_blocks_tolerant(rules_text)
|
|
150
|
+
if errors:
|
|
151
|
+
ppt.set_disco_error_level(1)
|
|
152
|
+
for error in errors:
|
|
153
|
+
ppt.add_disco_info_value("post-transforms: %s\n" % error)
|
|
154
|
+
ppt.print_stderr("post-transforms: %s\n" % error)
|
|
155
|
+
|
|
156
|
+
# JSON path: over the accumulated monitoring items.
|
|
157
|
+
items = ppt.get_monitoring_items()
|
|
158
|
+
ppt.apply_post_transforms(items, rules)
|
|
159
|
+
payload = ppt.print_monitoring_payload()
|
|
160
|
+
|
|
161
|
+
# XML path: over the module list, before print_agent().
|
|
162
|
+
ppt.apply_post_transforms(modules, rules)
|
|
163
|
+
xml_content = ppt.print_agent(agent, modules)
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
The errors are reported in the discovery JSON (`info` plus a non-zero error
|
|
167
|
+
level) and on stderr, so manual runs show them immediately; the monitoring
|
|
168
|
+
itself is never stopped by a bad rule.
|
|
169
|
+
|
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
####
|
|
2
|
+
# Post-transform block engine for Discovery tasks (ticket #22040).
|
|
3
|
+
#
|
|
4
|
+
# Task operators describe ordered transform rules as plain-text blocks and the
|
|
5
|
+
# engine merges whitelisted module configuration fields into the collected
|
|
6
|
+
# agent/module structure BEFORE it is serialized to its final format, so the
|
|
7
|
+
# same rules work for the native JSON path and the tentacle XML path.
|
|
8
|
+
#
|
|
9
|
+
# Block grammar (one rule per block):
|
|
10
|
+
#
|
|
11
|
+
# transform_begin
|
|
12
|
+
# transform_name <optional label>
|
|
13
|
+
# match_module <regex against the module name>
|
|
14
|
+
# match_extra_data <regex against the module extra_data>
|
|
15
|
+
# set_<field> <typed value from the whitelist>
|
|
16
|
+
# transform_end
|
|
17
|
+
#
|
|
18
|
+
# Semantics:
|
|
19
|
+
# - Comments (#) and blank lines are ignored everywhere.
|
|
20
|
+
# - Match keys inside a block are AND-ed; a rule must have at least one.
|
|
21
|
+
# - Rules apply in order; for each field the last matching rule wins.
|
|
22
|
+
# - 'name', 'type', 'data', 'value', 'extra_data', 'timestamp' and 'status'
|
|
23
|
+
# are protected: they define module identity and the collected value.
|
|
24
|
+
# - Threshold ranges are half-open [min, max); max_warning=0 / max_critical=0
|
|
25
|
+
# mean "no upper limit", not the literal zero.
|
|
26
|
+
# - Unknown keys, invalid regexes and mistyped values are reported as rule
|
|
27
|
+
# errors naming the block and the key. The tolerant API skips the
|
|
28
|
+
# offending rule and keeps the rest, so one bad rule never throws away
|
|
29
|
+
# the whole monitoring run; the strict API raises instead.
|
|
30
|
+
#########################################################################################
|
|
31
|
+
|
|
32
|
+
import re
|
|
33
|
+
|
|
34
|
+
####
|
|
35
|
+
# Whitelist: set_<field> key -> value type.
|
|
36
|
+
# Mirrors the module fields that init_module() templates and print_module()
|
|
37
|
+
# serializes, i.e. the vocabulary the Discovery ingestion already accepts.
|
|
38
|
+
#########################################################################################
|
|
39
|
+
|
|
40
|
+
WHITELIST = {
|
|
41
|
+
# Numeric thresholds.
|
|
42
|
+
"min_warning": "number",
|
|
43
|
+
"max_warning": "number",
|
|
44
|
+
"min_critical": "number",
|
|
45
|
+
"max_critical": "number",
|
|
46
|
+
"min_warning_forced": "number",
|
|
47
|
+
"max_warning_forced": "number",
|
|
48
|
+
"min_critical_forced": "number",
|
|
49
|
+
"max_critical_forced": "number",
|
|
50
|
+
"min": "number",
|
|
51
|
+
"max": "number",
|
|
52
|
+
"post_process": "number",
|
|
53
|
+
# String thresholds.
|
|
54
|
+
"str_warning": "str",
|
|
55
|
+
"str_critical": "str",
|
|
56
|
+
"str_warning_forced": "str",
|
|
57
|
+
"str_critical_forced": "str",
|
|
58
|
+
# Inverse / on-off flags (0 or 1).
|
|
59
|
+
"critical_inverse": "int01",
|
|
60
|
+
"warning_inverse": "int01",
|
|
61
|
+
"quiet": "int01",
|
|
62
|
+
"disabled": "int01",
|
|
63
|
+
"each_ff": "int01",
|
|
64
|
+
# Flip-flop settings, interval and cron.
|
|
65
|
+
"min_ff_event": "int",
|
|
66
|
+
"min_ff_event_normal": "int",
|
|
67
|
+
"min_ff_event_warning": "int",
|
|
68
|
+
"min_ff_event_critical": "int",
|
|
69
|
+
"ff_type": "int",
|
|
70
|
+
"ff_timeout": "int",
|
|
71
|
+
"module_ff_interval": "int",
|
|
72
|
+
"interval": "int",
|
|
73
|
+
"crontab": "str",
|
|
74
|
+
# Grouping, identification and instructions.
|
|
75
|
+
"tags": "str",
|
|
76
|
+
"module_group": "str",
|
|
77
|
+
"module_parent": "str",
|
|
78
|
+
"module_parent_unlink": "str",
|
|
79
|
+
"custom_id": "str",
|
|
80
|
+
"description": "str",
|
|
81
|
+
"critical_instructions": "str",
|
|
82
|
+
"warning_instructions": "str",
|
|
83
|
+
"unknown_instructions": "str",
|
|
84
|
+
# Alert templates: appended to the module "alert" list.
|
|
85
|
+
"alert_template": "alert",
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
####
|
|
89
|
+
# Fields a rule can never write: module identity and the collected value.
|
|
90
|
+
#########################################################################################
|
|
91
|
+
|
|
92
|
+
PROTECTED_FIELDS = (
|
|
93
|
+
"name",
|
|
94
|
+
"type",
|
|
95
|
+
"data",
|
|
96
|
+
"value",
|
|
97
|
+
"extra_data",
|
|
98
|
+
"timestamp",
|
|
99
|
+
"status",
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
####
|
|
103
|
+
# Keys allowed inside a transform block, besides set_<whitelist field>.
|
|
104
|
+
#########################################################################################
|
|
105
|
+
|
|
106
|
+
MATCH_KEYS = {
|
|
107
|
+
"match_module": "module",
|
|
108
|
+
"match_extra_data": "extra_data",
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
####
|
|
112
|
+
# Raised when transform blocks cannot be parsed or validated.
|
|
113
|
+
# The message always names the block (and line when known) so the task owner
|
|
114
|
+
# can fix the rule file without reading engine code.
|
|
115
|
+
#########################################################################################
|
|
116
|
+
|
|
117
|
+
class TransformParseError(ValueError):
|
|
118
|
+
"""Transform blocks are malformed or contain invalid values."""
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
####
|
|
122
|
+
# Internal: strip the invisible characters the IoT filter files also strip.
|
|
123
|
+
#########################################################################################
|
|
124
|
+
|
|
125
|
+
def _clean_line(line: str) -> str:
|
|
126
|
+
return re.sub(r'[\u200b\ufeff]', '', line).strip()
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
####
|
|
130
|
+
# Internal: key/value grammar of a line inside a block.
|
|
131
|
+
#########################################################################################
|
|
132
|
+
|
|
133
|
+
_LINE_RE = re.compile(r'^([a-z_][a-z0-9_]*)\s+(.+)$')
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
####
|
|
137
|
+
# Internal: coerce a raw block value to the type the whitelist declares.
|
|
138
|
+
#########################################################################################
|
|
139
|
+
|
|
140
|
+
def _coerce_value(field: str, raw: str, block_no: int, lineno: int):
|
|
141
|
+
kind = WHITELIST[field]
|
|
142
|
+
where = "transform block %d, line %d" % (block_no, lineno)
|
|
143
|
+
|
|
144
|
+
if kind == "number":
|
|
145
|
+
try:
|
|
146
|
+
number = float(raw)
|
|
147
|
+
except ValueError:
|
|
148
|
+
raise TransformParseError(
|
|
149
|
+
"%s: set_%s expects a number, got %r" % (where, field, raw)
|
|
150
|
+
)
|
|
151
|
+
return int(number) if number.is_integer() else number
|
|
152
|
+
|
|
153
|
+
if kind in ("int", "int01"):
|
|
154
|
+
try:
|
|
155
|
+
number = int(raw)
|
|
156
|
+
except ValueError:
|
|
157
|
+
raise TransformParseError(
|
|
158
|
+
"%s: set_%s expects an integer, got %r" % (where, field, raw)
|
|
159
|
+
)
|
|
160
|
+
if kind == "int01" and number not in (0, 1):
|
|
161
|
+
raise TransformParseError(
|
|
162
|
+
"%s: set_%s expects 0 or 1, got %r" % (where, field, raw)
|
|
163
|
+
)
|
|
164
|
+
return number
|
|
165
|
+
|
|
166
|
+
return raw
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
####
|
|
170
|
+
# Tolerant parse core: parse every block, skipping the invalid RULES while
|
|
171
|
+
# keeping the valid ones, and collecting one error string per problem, each
|
|
172
|
+
# naming its block and line. Rule granularity: one bad key invalidates its
|
|
173
|
+
# whole rule (block), because applying half of an intended configuration is
|
|
174
|
+
# worse than skipping it.
|
|
175
|
+
#########################################################################################
|
|
176
|
+
|
|
177
|
+
def _parse_blocks_tolerant(rules_text: str):
|
|
178
|
+
rules = []
|
|
179
|
+
errors = []
|
|
180
|
+
current = None
|
|
181
|
+
current_errors = None
|
|
182
|
+
block_no = 0
|
|
183
|
+
lineno = 0
|
|
184
|
+
|
|
185
|
+
for raw_line in rules_text.splitlines():
|
|
186
|
+
lineno += 1
|
|
187
|
+
line = _clean_line(raw_line)
|
|
188
|
+
|
|
189
|
+
if not line or line.startswith("#"):
|
|
190
|
+
continue
|
|
191
|
+
|
|
192
|
+
if line == "transform_begin":
|
|
193
|
+
if current is not None:
|
|
194
|
+
errors.append(
|
|
195
|
+
"transform block %d, line %d: missing transform_end "
|
|
196
|
+
"before a new transform_begin; rule skipped"
|
|
197
|
+
% (block_no, lineno)
|
|
198
|
+
)
|
|
199
|
+
block_no += 1
|
|
200
|
+
current = {"name": "", "match": {}, "set": {}}
|
|
201
|
+
current_errors = []
|
|
202
|
+
continue
|
|
203
|
+
|
|
204
|
+
if line == "transform_end":
|
|
205
|
+
if current is None:
|
|
206
|
+
errors.append(
|
|
207
|
+
"line %d: transform_end without transform_begin" % lineno
|
|
208
|
+
)
|
|
209
|
+
continue
|
|
210
|
+
|
|
211
|
+
if not current["match"]:
|
|
212
|
+
current_errors.append(
|
|
213
|
+
"transform block %d: rule has no match_module or "
|
|
214
|
+
"match_extra_data key; rule skipped" % block_no
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
for key, pattern in current["match"].items():
|
|
218
|
+
try:
|
|
219
|
+
re.compile(pattern)
|
|
220
|
+
except re.error as error:
|
|
221
|
+
current_errors.append(
|
|
222
|
+
"transform block %d: invalid regex in match key %r: "
|
|
223
|
+
"%s; rule skipped" % (block_no, key, error)
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
if current_errors:
|
|
227
|
+
errors.extend(current_errors)
|
|
228
|
+
else:
|
|
229
|
+
rules.append(current)
|
|
230
|
+
|
|
231
|
+
current = None
|
|
232
|
+
current_errors = None
|
|
233
|
+
continue
|
|
234
|
+
|
|
235
|
+
if current is None:
|
|
236
|
+
errors.append(
|
|
237
|
+
"line %d: content outside a transform block: %r"
|
|
238
|
+
% (lineno, line)
|
|
239
|
+
)
|
|
240
|
+
continue
|
|
241
|
+
|
|
242
|
+
parsed = _LINE_RE.match(line)
|
|
243
|
+
if parsed is None:
|
|
244
|
+
current_errors.append(
|
|
245
|
+
"transform block %d, line %d: cannot parse line: %r; rule "
|
|
246
|
+
"skipped" % (block_no, lineno, line)
|
|
247
|
+
)
|
|
248
|
+
continue
|
|
249
|
+
|
|
250
|
+
key = parsed.group(1)
|
|
251
|
+
value = parsed.group(2).strip()
|
|
252
|
+
|
|
253
|
+
if key == "transform_name":
|
|
254
|
+
current["name"] = value
|
|
255
|
+
elif key in MATCH_KEYS:
|
|
256
|
+
current["match"][MATCH_KEYS[key]] = value
|
|
257
|
+
elif key.startswith("set_"):
|
|
258
|
+
field = key[4:]
|
|
259
|
+
if field in PROTECTED_FIELDS:
|
|
260
|
+
current_errors.append(
|
|
261
|
+
"transform block %d, line %d: field %r is protected and "
|
|
262
|
+
"cannot be set; rule skipped" % (block_no, lineno, field)
|
|
263
|
+
)
|
|
264
|
+
elif field not in WHITELIST:
|
|
265
|
+
current_errors.append(
|
|
266
|
+
"transform block %d, line %d: unknown set field %r (not "
|
|
267
|
+
"in the whitelist); rule skipped" % (block_no, lineno, field)
|
|
268
|
+
)
|
|
269
|
+
else:
|
|
270
|
+
try:
|
|
271
|
+
current["set"][field] = _coerce_value(
|
|
272
|
+
field, value, block_no, lineno
|
|
273
|
+
)
|
|
274
|
+
except TransformParseError as error:
|
|
275
|
+
current_errors.append("%s; rule skipped" % error)
|
|
276
|
+
else:
|
|
277
|
+
current_errors.append(
|
|
278
|
+
"transform block %d, line %d: unknown key %r; rule skipped"
|
|
279
|
+
% (block_no, lineno, key)
|
|
280
|
+
)
|
|
281
|
+
|
|
282
|
+
if current is not None:
|
|
283
|
+
errors.append(
|
|
284
|
+
"transform block %d: missing transform_end at end of input; rule "
|
|
285
|
+
"skipped" % block_no
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
return rules, errors
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
####
|
|
292
|
+
# Parse transform blocks into an ordered list of rule dicts:
|
|
293
|
+
# {"name": <label>, "match": {"module": <regex>, "extra_data": <regex>},
|
|
294
|
+
# "set": {<field>: <typed value>}}
|
|
295
|
+
#
|
|
296
|
+
# STRICT variant: raises TransformParseError on the first invalid rule.
|
|
297
|
+
#########################################################################################
|
|
298
|
+
|
|
299
|
+
def parse_transform_blocks(rules_text: str) -> list:
|
|
300
|
+
rules, errors = _parse_blocks_tolerant(rules_text)
|
|
301
|
+
|
|
302
|
+
if errors:
|
|
303
|
+
raise TransformParseError("\n".join(errors))
|
|
304
|
+
|
|
305
|
+
return rules
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
####
|
|
309
|
+
# Tolerant variant for plugins: one bad rule never throws away the whole
|
|
310
|
+
# monitoring run. Returns (rules, errors); valid rules apply normally and
|
|
311
|
+
# each error names its block and line so the task can report it (for example
|
|
312
|
+
# through add_disco_info_value plus an error level) while continuing.
|
|
313
|
+
#########################################################################################
|
|
314
|
+
|
|
315
|
+
def parse_transform_blocks_tolerant(rules_text: str) -> tuple:
|
|
316
|
+
return _parse_blocks_tolerant(rules_text)
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
####
|
|
320
|
+
# Internal: does one rule match one module?
|
|
321
|
+
# Match uses re.search semantics (substring); anchor patterns with ^...$ to
|
|
322
|
+
# require a full match.
|
|
323
|
+
#########################################################################################
|
|
324
|
+
|
|
325
|
+
def _rule_matches(module: dict, rule: dict) -> bool:
|
|
326
|
+
match = rule["match"]
|
|
327
|
+
|
|
328
|
+
if "module" in match:
|
|
329
|
+
if not re.search(match["module"], str(module.get("name") or "")):
|
|
330
|
+
return False
|
|
331
|
+
|
|
332
|
+
if "extra_data" in match:
|
|
333
|
+
if not re.search(match["extra_data"], str(module.get("extra_data") or "")):
|
|
334
|
+
return False
|
|
335
|
+
|
|
336
|
+
return True
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
####
|
|
340
|
+
# Internal: merge one rule's fields into one module. Never touches the
|
|
341
|
+
# protected identity fields: 'set_' keys for them do not parse.
|
|
342
|
+
#########################################################################################
|
|
343
|
+
|
|
344
|
+
def _apply_rule(module: dict, rule: dict) -> bool:
|
|
345
|
+
touched = False
|
|
346
|
+
|
|
347
|
+
for field, value in rule["set"].items():
|
|
348
|
+
if WHITELIST.get(field) == "alert":
|
|
349
|
+
alerts = module.get("alert")
|
|
350
|
+
if not isinstance(alerts, list):
|
|
351
|
+
alerts = []
|
|
352
|
+
module["alert"] = alerts
|
|
353
|
+
alerts.append(value)
|
|
354
|
+
else:
|
|
355
|
+
module[field] = value
|
|
356
|
+
touched = True
|
|
357
|
+
|
|
358
|
+
return touched
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
####
|
|
362
|
+
# Apply post-transform rules over collected monitoring data, in place,
|
|
363
|
+
# BEFORE serializing it to its final format.
|
|
364
|
+
#
|
|
365
|
+
# Args:
|
|
366
|
+
# data (list): either the monitoring items list
|
|
367
|
+
# ([{"agent_data": {...}, "module_data": [module dicts]}]) or a plain
|
|
368
|
+
# list of module dicts (the XML path, before print_agent()).
|
|
369
|
+
# rules (list | str): parsed rules from parse_transform_blocks() or the
|
|
370
|
+
# raw block text (parsed here, so errors fail the task before output).
|
|
371
|
+
#
|
|
372
|
+
# Returns:
|
|
373
|
+
# int: number of modules that received at least one field.
|
|
374
|
+
#########################################################################################
|
|
375
|
+
|
|
376
|
+
def apply_post_transforms(data: list, rules) -> int:
|
|
377
|
+
if isinstance(rules, str):
|
|
378
|
+
rules = parse_transform_blocks(rules)
|
|
379
|
+
|
|
380
|
+
changed = 0
|
|
381
|
+
|
|
382
|
+
for entry in data:
|
|
383
|
+
if isinstance(entry, dict) and isinstance(entry.get("module_data"), list):
|
|
384
|
+
modules = entry["module_data"]
|
|
385
|
+
else:
|
|
386
|
+
modules = [entry]
|
|
387
|
+
|
|
388
|
+
for module in modules:
|
|
389
|
+
if not isinstance(module, dict):
|
|
390
|
+
continue
|
|
391
|
+
|
|
392
|
+
touched = False
|
|
393
|
+
for rule in rules:
|
|
394
|
+
if _rule_matches(module, rule) and _apply_rule(module, rule):
|
|
395
|
+
touched = True
|
|
396
|
+
|
|
397
|
+
if touched:
|
|
398
|
+
changed += 1
|
|
399
|
+
|
|
400
|
+
return changed
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pandoraPlugintools-basic
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.2.0
|
|
4
4
|
Summary: A plugin tool set of basic functions for pandorafms
|
|
5
5
|
Home-page: https://github.com/pandorafms/pandoraPlugintoolsBasic
|
|
6
6
|
Author: PandoraFMS projects department
|
|
@@ -50,7 +50,9 @@ The package exports the following modules:
|
|
|
50
50
|
- `modules`: Functions to build Pandora FMS module payloads.
|
|
51
51
|
- `transfer`: File transfer and data sending helpers.
|
|
52
52
|
- `discovery`: Helpers for Pandora FMS discovery plugins.
|
|
53
|
+
- `monitoring`: Helpers to accumulate and emit `/monitoring` API payloads.
|
|
53
54
|
- `output`: Output formatting and return helpers.
|
|
55
|
+
- `post_transforms`: Post-transform block engine for Discovery tasks (regex rules that merge whitelisted module fields before serialization).
|
|
54
56
|
|
|
55
57
|
Reference documentation:
|
|
56
58
|
[PluginTools Reference Documentation](https://pandorafms.com/guides/public/books/plugintools)
|
|
@@ -69,6 +71,8 @@ The README stays focused on the quick path. For a complete function list and ful
|
|
|
69
71
|
| `transfer` | `write_xml`, `transfer_xml`, `tentacle_xml` |
|
|
70
72
|
| `threads` | `run_threads`, `run_processes`, `set_shared_dict_value`, `get_shared_dict_value` |
|
|
71
73
|
| `discovery` | `set_disco_summary`, `add_disco_summary_value`, `add_disco_info_value`, `add_disco_monitoring_data`, `disco_output` |
|
|
74
|
+
| `monitoring` | `set_monitoring_items`, `add_monitoring_item`, `get_monitoring_items`, `print_monitoring_payload` |
|
|
75
|
+
| `post_transforms` | `parse_transform_blocks`, `apply_post_transforms`, `TransformParseError` |
|
|
72
76
|
| `output` | `print_stdout`, `print_stderr`, `print_debug`, `logger` |
|
|
73
77
|
|
|
74
78
|
## Quick example
|
|
@@ -113,3 +117,70 @@ ppt.transfer_xml(
|
|
|
113
117
|
)
|
|
114
118
|
```
|
|
115
119
|
|
|
120
|
+
## Post-transform blocks for Discovery tasks
|
|
121
|
+
|
|
122
|
+
Discovery plugins can let task operators enrich modules without touching plugin
|
|
123
|
+
code. Operators write ordered rule blocks in plain text; the plugin applies
|
|
124
|
+
them over the collected agent/module structure right before serializing it, so
|
|
125
|
+
the same rules work for the native JSON path and the tentacle XML path.
|
|
126
|
+
|
|
127
|
+
```text
|
|
128
|
+
transform_begin
|
|
129
|
+
transform_name cpu thresholds
|
|
130
|
+
match_module .*cpu_percent
|
|
131
|
+
match_extra_data .*:metric:cpu
|
|
132
|
+
set_min_warning 80
|
|
133
|
+
set_max_critical 0
|
|
134
|
+
set_module_group Rendimiento
|
|
135
|
+
set_tags infra:cpu
|
|
136
|
+
transform_end
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Semantics:
|
|
140
|
+
|
|
141
|
+
- `match_module` and `match_extra_data` are regexes (substring match; use
|
|
142
|
+
`^...$` for full match) AND-ed inside a block; a rule needs at least one.
|
|
143
|
+
- Rules apply in order; for each field the last matching rule wins.
|
|
144
|
+
- `set_<field>` accepts exactly the module fields the Discovery ingestion
|
|
145
|
+
already supports (thresholds, `str_*`, `tags`, `module_group`,
|
|
146
|
+
`alert_template`, FF settings, `custom_id`, ...). Unknown fields are errors.
|
|
147
|
+
- `set_tags` holds a comma-separated list (`set_tags infra,ops`), the same
|
|
148
|
+
convention as the agent's `module_tags`: the whole string travels in one
|
|
149
|
+
field and the server splits it.
|
|
150
|
+
- `name`, `type`, `data`, `value`, `extra_data`, `timestamp` and `status` are
|
|
151
|
+
protected: a rule cannot change module identity or the collected value.
|
|
152
|
+
- Threshold ranges are half-open `[min, max)`; `max_warning=0` and
|
|
153
|
+
`max_critical=0` mean "no upper limit", not the literal zero.
|
|
154
|
+
- Invalid regexes, unknown keys and mistyped values are reported with the
|
|
155
|
+
block and key that caused them: the tolerant API
|
|
156
|
+
(`parse_transform_blocks_tolerant`) skips the offending rule and keeps the
|
|
157
|
+
rest, so one bad rule never throws away the monitoring run; the strict API
|
|
158
|
+
(`parse_transform_blocks`) raises `TransformParseError` instead.
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
import pandoraPlugintools as ppt
|
|
162
|
+
|
|
163
|
+
rules_text = open("post_transforms.conf").read()
|
|
164
|
+
|
|
165
|
+
# Tolerant parse: bad rules are skipped and reported; good rules apply.
|
|
166
|
+
rules, errors = ppt.parse_transform_blocks_tolerant(rules_text)
|
|
167
|
+
if errors:
|
|
168
|
+
ppt.set_disco_error_level(1)
|
|
169
|
+
for error in errors:
|
|
170
|
+
ppt.add_disco_info_value("post-transforms: %s\n" % error)
|
|
171
|
+
ppt.print_stderr("post-transforms: %s\n" % error)
|
|
172
|
+
|
|
173
|
+
# JSON path: over the accumulated monitoring items.
|
|
174
|
+
items = ppt.get_monitoring_items()
|
|
175
|
+
ppt.apply_post_transforms(items, rules)
|
|
176
|
+
payload = ppt.print_monitoring_payload()
|
|
177
|
+
|
|
178
|
+
# XML path: over the module list, before print_agent().
|
|
179
|
+
ppt.apply_post_transforms(modules, rules)
|
|
180
|
+
xml_content = ppt.print_agent(agent, modules)
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
The errors are reported in the discovery JSON (`info` plus a non-zero error
|
|
184
|
+
level) and on stderr, so manual runs show them immediately; the monitoring
|
|
185
|
+
itself is never stopped by a bad rule.
|
|
186
|
+
|
|
@@ -9,10 +9,12 @@ pandoraPlugintools/general.py
|
|
|
9
9
|
pandoraPlugintools/modules.py
|
|
10
10
|
pandoraPlugintools/monitoring.py
|
|
11
11
|
pandoraPlugintools/output.py
|
|
12
|
+
pandoraPlugintools/post_transforms.py
|
|
12
13
|
pandoraPlugintools/threads.py
|
|
13
14
|
pandoraPlugintools/transfer.py
|
|
14
15
|
pandoraPlugintools_basic.egg-info/PKG-INFO
|
|
15
16
|
pandoraPlugintools_basic.egg-info/SOURCES.txt
|
|
16
17
|
pandoraPlugintools_basic.egg-info/dependency_links.txt
|
|
17
18
|
pandoraPlugintools_basic.egg-info/top_level.txt
|
|
18
|
-
tests/test_basic.py
|
|
19
|
+
tests/test_basic.py
|
|
20
|
+
tests/test_post_transforms.py
|
|
@@ -8,7 +8,7 @@ with open("README.md", mode="r", encoding="utf-8") as f:
|
|
|
8
8
|
|
|
9
9
|
setup(
|
|
10
10
|
name="pandoraPlugintools-basic",
|
|
11
|
-
version="1.
|
|
11
|
+
version="1.2.0",
|
|
12
12
|
author="PandoraFMS projects department",
|
|
13
13
|
author_email="<info@pandorafms.com>",
|
|
14
14
|
description="A plugin tool set of basic functions for pandorafms",
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
# Post-transform block engine tests (ticket #22040).
|
|
2
|
+
# Run with: python3 tests/test_post_transforms.py
|
|
3
|
+
# Follows the plain assert-script convention of tests/test_basic.py.
|
|
4
|
+
|
|
5
|
+
# Python 3.14 defaults to forkserver; threads.py creates a Manager() at import
|
|
6
|
+
# time, which under forkserver re-imports this script and dies. Use the
|
|
7
|
+
# classic fork start method before importing the package.
|
|
8
|
+
import multiprocessing
|
|
9
|
+
multiprocessing.set_start_method("fork", force=True)
|
|
10
|
+
|
|
11
|
+
import pandoraPlugintools as ppt
|
|
12
|
+
|
|
13
|
+
VALID_RULES = """
|
|
14
|
+
# Thresholds for CPU-like metrics.
|
|
15
|
+
transform_begin
|
|
16
|
+
transform_name cpu thresholds
|
|
17
|
+
match_module .*cpu_percent
|
|
18
|
+
match_extra_data .*:metric:cpu
|
|
19
|
+
set_min_warning 80
|
|
20
|
+
set_max_critical 0
|
|
21
|
+
set_module_group Rendimiento
|
|
22
|
+
set_tags infra:cpu
|
|
23
|
+
transform_end
|
|
24
|
+
|
|
25
|
+
transform_begin
|
|
26
|
+
transform_name disk usage
|
|
27
|
+
match_module .*disk_usage.*percent
|
|
28
|
+
set_min_warning 0.5
|
|
29
|
+
set_str_warning high usage
|
|
30
|
+
transform_end
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
# --- Parsing ---------------------------------------------------------------
|
|
34
|
+
|
|
35
|
+
rules = ppt.parse_transform_blocks(VALID_RULES)
|
|
36
|
+
assert len(rules) == 2, "expected two rules, got %d" % len(rules)
|
|
37
|
+
|
|
38
|
+
assert rules[0]["name"] == "cpu thresholds"
|
|
39
|
+
assert rules[0]["match"]["module"] == ".*cpu_percent"
|
|
40
|
+
assert rules[0]["match"]["extra_data"] == ".*:metric:cpu"
|
|
41
|
+
assert rules[0]["set"]["min_warning"] == 80, "int coercion failed"
|
|
42
|
+
assert rules[0]["set"]["max_critical"] == 0, "int coercion of 0 failed"
|
|
43
|
+
assert rules[0]["set"]["module_group"] == "Rendimiento"
|
|
44
|
+
assert rules[0]["set"]["tags"] == "infra:cpu"
|
|
45
|
+
|
|
46
|
+
assert rules[1]["set"]["min_warning"] == 0.5, "float coercion failed"
|
|
47
|
+
assert rules[1]["set"]["str_warning"] == "high usage"
|
|
48
|
+
|
|
49
|
+
# Strict grammar: unknown set key is an error naming block and key.
|
|
50
|
+
try:
|
|
51
|
+
ppt.parse_transform_blocks(
|
|
52
|
+
"transform_begin\nmatch_module a\nset_min_warnig 80\ntransform_end\n"
|
|
53
|
+
)
|
|
54
|
+
raise AssertionError("unknown key accepted")
|
|
55
|
+
except ppt.TransformParseError as e:
|
|
56
|
+
assert "block 1" in str(e) and "min_warnig" in str(e), str(e)
|
|
57
|
+
|
|
58
|
+
# Strict grammar: unknown match key is an error.
|
|
59
|
+
try:
|
|
60
|
+
ppt.parse_transform_blocks(
|
|
61
|
+
"transform_begin\nmatch_modul a\nset_min_warning 1\ntransform_end\n"
|
|
62
|
+
)
|
|
63
|
+
raise AssertionError("unknown match key accepted")
|
|
64
|
+
except ppt.TransformParseError:
|
|
65
|
+
pass
|
|
66
|
+
|
|
67
|
+
# Strict grammar: garbage line inside a block is an error.
|
|
68
|
+
try:
|
|
69
|
+
ppt.parse_transform_blocks("transform_begin\nmatch_module a\nrandom junk\ntransform_end\n")
|
|
70
|
+
raise AssertionError("garbage line accepted")
|
|
71
|
+
except ppt.TransformParseError:
|
|
72
|
+
pass
|
|
73
|
+
|
|
74
|
+
# Strict grammar: non-empty non-comment line outside a block is an error.
|
|
75
|
+
try:
|
|
76
|
+
ppt.parse_transform_blocks("random junk outside\ntransform_begin\nmatch_module a\ntransform_end\n")
|
|
77
|
+
raise AssertionError("junk outside blocks accepted")
|
|
78
|
+
except ppt.TransformParseError:
|
|
79
|
+
pass
|
|
80
|
+
|
|
81
|
+
# Unterminated block is an error.
|
|
82
|
+
try:
|
|
83
|
+
ppt.parse_transform_blocks("transform_begin\nmatch_module a\nset_min_warning 1\n")
|
|
84
|
+
raise AssertionError("unterminated block accepted")
|
|
85
|
+
except ppt.TransformParseError as e:
|
|
86
|
+
assert "block 1" in str(e), str(e)
|
|
87
|
+
|
|
88
|
+
# Strict API: nested begin reports the skipped rule.
|
|
89
|
+
try:
|
|
90
|
+
ppt.parse_transform_blocks("transform_begin\nmatch_module a\ntransform_begin\ntransform_end")
|
|
91
|
+
raise AssertionError("nested begin accepted")
|
|
92
|
+
except ppt.TransformParseError as e:
|
|
93
|
+
assert "missing transform_end before a new transform_begin" in str(e), str(e)
|
|
94
|
+
|
|
95
|
+
# Block without any match key is an error.
|
|
96
|
+
try:
|
|
97
|
+
ppt.parse_transform_blocks("transform_begin\nset_min_warning 1\ntransform_end\n")
|
|
98
|
+
raise AssertionError("match-less rule accepted")
|
|
99
|
+
except ppt.TransformParseError:
|
|
100
|
+
pass
|
|
101
|
+
|
|
102
|
+
# Invalid regex is an error naming the block.
|
|
103
|
+
try:
|
|
104
|
+
ppt.parse_transform_blocks("transform_begin\nmatch_module ([unclosed\nset_min_warning 1\ntransform_end\n")
|
|
105
|
+
raise AssertionError("invalid regex accepted")
|
|
106
|
+
except ppt.TransformParseError as e:
|
|
107
|
+
assert "block 1" in str(e), str(e)
|
|
108
|
+
|
|
109
|
+
# Non-numeric value for a numeric field is an error.
|
|
110
|
+
try:
|
|
111
|
+
ppt.parse_transform_blocks("transform_begin\nmatch_module a\nset_min_warning abc\ntransform_end\n")
|
|
112
|
+
raise AssertionError("non-numeric threshold accepted")
|
|
113
|
+
except ppt.TransformParseError:
|
|
114
|
+
pass
|
|
115
|
+
|
|
116
|
+
# Protected fields are not writable.
|
|
117
|
+
for protected in ("name", "type", "data", "value", "extra_data", "timestamp", "status"):
|
|
118
|
+
try:
|
|
119
|
+
ppt.parse_transform_blocks(
|
|
120
|
+
"transform_begin\nmatch_module a\nset_%s x\ntransform_end\n" % protected
|
|
121
|
+
)
|
|
122
|
+
raise AssertionError("protected field %s accepted" % protected)
|
|
123
|
+
except ppt.TransformParseError:
|
|
124
|
+
pass
|
|
125
|
+
|
|
126
|
+
# --- Apply over monitoring items (native JSON path) -----------------------
|
|
127
|
+
|
|
128
|
+
items = [
|
|
129
|
+
{
|
|
130
|
+
"agent_data": {"agent_name": "host_1"},
|
|
131
|
+
"module_data": [
|
|
132
|
+
{
|
|
133
|
+
"name": "host_1 cpu_percent",
|
|
134
|
+
"type": "generic_data",
|
|
135
|
+
"data": 55,
|
|
136
|
+
"extra_data": "nginx:metric:cpu",
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
"name": "host_1 disk_usage_raw",
|
|
140
|
+
"type": "generic_data",
|
|
141
|
+
"data": 3,
|
|
142
|
+
},
|
|
143
|
+
],
|
|
144
|
+
}
|
|
145
|
+
]
|
|
146
|
+
|
|
147
|
+
# Convenience: raw block text is accepted and parsed by apply itself.
|
|
148
|
+
count = ppt.apply_post_transforms(items, VALID_RULES)
|
|
149
|
+
assert count == 1, "expected exactly one module matched, got %d" % count
|
|
150
|
+
|
|
151
|
+
matched = items[0]["module_data"][0]
|
|
152
|
+
assert matched["min_warning"] == 80
|
|
153
|
+
assert matched["max_critical"] == 0
|
|
154
|
+
assert matched["module_group"] == "Rendimiento"
|
|
155
|
+
assert matched["tags"] == "infra:cpu"
|
|
156
|
+
|
|
157
|
+
unmatched = items[0]["module_data"][1]
|
|
158
|
+
assert "min_warning" not in unmatched, "unmatched module was modified"
|
|
159
|
+
assert "max_critical" not in unmatched
|
|
160
|
+
|
|
161
|
+
# Field-level last-match-wins: a later block overrides only its own fields.
|
|
162
|
+
layered = """
|
|
163
|
+
transform_begin
|
|
164
|
+
match_module .*cpu
|
|
165
|
+
set_min_warning 10
|
|
166
|
+
set_module_group Base
|
|
167
|
+
transform_end
|
|
168
|
+
|
|
169
|
+
transform_begin
|
|
170
|
+
match_module .*cpu
|
|
171
|
+
set_min_warning 20
|
|
172
|
+
transform_end
|
|
173
|
+
"""
|
|
174
|
+
items2 = [
|
|
175
|
+
{
|
|
176
|
+
"agent_data": {"agent_name": "h2"},
|
|
177
|
+
"module_data": [{"name": "h2 cpu", "type": "generic_data", "data": 1}],
|
|
178
|
+
}
|
|
179
|
+
]
|
|
180
|
+
ppt.apply_post_transforms(items2, layered)
|
|
181
|
+
mod = items2[0]["module_data"][0]
|
|
182
|
+
assert mod["min_warning"] == 20, "last-match-wins failed"
|
|
183
|
+
assert mod["module_group"] == "Base", "field from earlier block lost"
|
|
184
|
+
|
|
185
|
+
# AND semantics: both match keys must match.
|
|
186
|
+
and_rules = """
|
|
187
|
+
transform_begin
|
|
188
|
+
match_module .*cpu_percent
|
|
189
|
+
match_extra_data .*:metric:cpu
|
|
190
|
+
set_min_warning 99
|
|
191
|
+
transform_end
|
|
192
|
+
"""
|
|
193
|
+
items3 = [
|
|
194
|
+
{
|
|
195
|
+
"agent_data": {"agent_name": "h3"},
|
|
196
|
+
"module_data": [
|
|
197
|
+
{"name": "h3 cpu_percent", "type": "generic_data", "data": 1,
|
|
198
|
+
"extra_data": "nginx:metric:memory"},
|
|
199
|
+
],
|
|
200
|
+
}
|
|
201
|
+
]
|
|
202
|
+
count3 = ppt.apply_post_transforms(items3, and_rules)
|
|
203
|
+
assert count3 == 0, "AND semantics failed"
|
|
204
|
+
assert "min_warning" not in items3[0]["module_data"][0]
|
|
205
|
+
|
|
206
|
+
# --- Apply over a plain module list (XML/tentacle path) ---------------------
|
|
207
|
+
|
|
208
|
+
modules = [
|
|
209
|
+
ppt.init_module(
|
|
210
|
+
{
|
|
211
|
+
"name": "x cpu_percent",
|
|
212
|
+
"type": "generic_data",
|
|
213
|
+
"value": "12",
|
|
214
|
+
"extra_data": "nginx:metric:cpu",
|
|
215
|
+
}
|
|
216
|
+
),
|
|
217
|
+
]
|
|
218
|
+
countx = ppt.apply_post_transforms(modules, VALID_RULES)
|
|
219
|
+
assert countx == 1
|
|
220
|
+
xml = ppt.print_module(modules[0])
|
|
221
|
+
assert "<min_warning><![CDATA[80]]></min_warning>" in xml, xml
|
|
222
|
+
assert "<max_critical><![CDATA[0]]></max_critical>" in xml
|
|
223
|
+
assert "<module_group>Rendimiento</module_group>" in xml
|
|
224
|
+
assert "<tags>infra:cpu</tags>" in xml
|
|
225
|
+
|
|
226
|
+
# Multiple tags: comma-separated list passes through verbatim, matching the
|
|
227
|
+
# agent's module_tags convention (one <tags> element, server splits it).
|
|
228
|
+
multi_tags = """
|
|
229
|
+
transform_begin
|
|
230
|
+
match_module .*cpu_percent
|
|
231
|
+
set_tags infra:cpu,team:ops
|
|
232
|
+
transform_end
|
|
233
|
+
"""
|
|
234
|
+
modules_multi = [
|
|
235
|
+
ppt.init_module(
|
|
236
|
+
{
|
|
237
|
+
"name": "x cpu_percent",
|
|
238
|
+
"type": "generic_data",
|
|
239
|
+
"value": "12",
|
|
240
|
+
"extra_data": "nginx:metric:cpu",
|
|
241
|
+
}
|
|
242
|
+
),
|
|
243
|
+
]
|
|
244
|
+
ppt.apply_post_transforms(modules_multi, multi_tags)
|
|
245
|
+
assert modules_multi[0]["tags"] == "infra:cpu,team:ops"
|
|
246
|
+
xml_multi = ppt.print_module(modules_multi[0])
|
|
247
|
+
assert "<tags>infra:cpu,team:ops</tags>" in xml_multi, xml_multi
|
|
248
|
+
|
|
249
|
+
# set_alert_template appends to the alert list and reaches <alert_template>.
|
|
250
|
+
alert_rules = """
|
|
251
|
+
transform_begin
|
|
252
|
+
match_module .*cpu_percent
|
|
253
|
+
set_alert_template Critical mail
|
|
254
|
+
transform_end
|
|
255
|
+
"""
|
|
256
|
+
modules2 = [ppt.init_module({"name": "x cpu_percent", "type": "generic_data", "value": "12"})]
|
|
257
|
+
ppt.apply_post_transforms(modules2, alert_rules)
|
|
258
|
+
assert modules2[0]["alert"] == ["Critical mail"]
|
|
259
|
+
xml2 = ppt.print_module(modules2[0])
|
|
260
|
+
assert "<alert_template><![CDATA[Critical mail]]></alert_template>" in xml2, xml2
|
|
261
|
+
|
|
262
|
+
# --- Tolerant parsing: one bad rule never kills the whole run ---------------
|
|
263
|
+
|
|
264
|
+
# A typo in one rule skips that rule; the valid rules still apply.
|
|
265
|
+
mixed_rules = """
|
|
266
|
+
transform_begin
|
|
267
|
+
match_module .*good_one.*
|
|
268
|
+
set_min_warning 42
|
|
269
|
+
transform_end
|
|
270
|
+
|
|
271
|
+
transform_begin
|
|
272
|
+
match_module .*bad.*
|
|
273
|
+
set_min_warnig 99
|
|
274
|
+
transform_end
|
|
275
|
+
|
|
276
|
+
transform_begin
|
|
277
|
+
match_module .*good_two.*
|
|
278
|
+
set_max_critical 7
|
|
279
|
+
transform_end
|
|
280
|
+
"""
|
|
281
|
+
rules_t, errors_t = ppt.parse_transform_blocks_tolerant(mixed_rules)
|
|
282
|
+
assert len(rules_t) == 2, "expected the two good rules, got %d" % len(rules_t)
|
|
283
|
+
assert rules_t[0]["set"]["min_warning"] == 42
|
|
284
|
+
assert rules_t[1]["set"]["max_critical"] == 7
|
|
285
|
+
assert len(errors_t) == 1, errors_t
|
|
286
|
+
assert "block 2" in errors_t[0] and "min_warnig" in errors_t[0], errors_t[0]
|
|
287
|
+
|
|
288
|
+
# A bad regex only kills its own rule.
|
|
289
|
+
bad_regex_rules = """
|
|
290
|
+
transform_begin
|
|
291
|
+
match_module .*good.*
|
|
292
|
+
set_min_warning 42
|
|
293
|
+
transform_end
|
|
294
|
+
|
|
295
|
+
transform_begin
|
|
296
|
+
match_module ([unclosed
|
|
297
|
+
set_min_warning 1
|
|
298
|
+
transform_end
|
|
299
|
+
"""
|
|
300
|
+
rules_r, errors_r = ppt.parse_transform_blocks_tolerant(bad_regex_rules)
|
|
301
|
+
assert len(rules_r) == 1
|
|
302
|
+
assert len(errors_r) == 1 and "invalid regex" in errors_r[0], errors_r
|
|
303
|
+
|
|
304
|
+
# Tolerant API on empty text: no rules, no errors.
|
|
305
|
+
rules_empty, errors_empty = ppt.parse_transform_blocks_tolerant("")
|
|
306
|
+
assert rules_empty == [] and errors_empty == []
|
|
307
|
+
|
|
308
|
+
print("ok")
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
# pandoraPlugintoolsBasic
|
|
2
|
-
|
|
3
|
-
`pandoraPlugintoolsBasic` is a lightweight, dependency-free version of `pandoraPlugintools` for Pandora FMS plugin development.
|
|
4
|
-
|
|
5
|
-
It is designed as a drop-in replacement for the most commonly used functionality, with a smaller footprint and simpler deployment.
|
|
6
|
-
|
|
7
|
-
## Install
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
pip install pandoraPlugintools-basic
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Migration from pandoraPlugintools
|
|
14
|
-
|
|
15
|
-
If your plugin only uses the basic features included in this package, migrate with:
|
|
16
|
-
|
|
17
|
-
```bash
|
|
18
|
-
pip uninstall pandoraPlugintools
|
|
19
|
-
pip install pandoraPlugintools-basic
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
Important:
|
|
23
|
-
- This package does not include all features from the full `pandoraPlugintools` package.
|
|
24
|
-
- If your current code depends on advanced modules (for example SNMP or encryption helpers), keep using the standard package.
|
|
25
|
-
|
|
26
|
-
## What is included
|
|
27
|
-
|
|
28
|
-
The package exports the following modules:
|
|
29
|
-
|
|
30
|
-
- `general`: General-purpose helper functions for plugin development.
|
|
31
|
-
- `threads`: Utilities for running functions in parallel.
|
|
32
|
-
- `agents`: Functions to build Pandora FMS agent payloads.
|
|
33
|
-
- `modules`: Functions to build Pandora FMS module payloads.
|
|
34
|
-
- `transfer`: File transfer and data sending helpers.
|
|
35
|
-
- `discovery`: Helpers for Pandora FMS discovery plugins.
|
|
36
|
-
- `output`: Output formatting and return helpers.
|
|
37
|
-
|
|
38
|
-
Reference documentation:
|
|
39
|
-
[PluginTools Reference Documentation](https://pandorafms.com/guides/public/books/plugintools)
|
|
40
|
-
|
|
41
|
-
For a full, runnable example per function, see [docs/examples.md](docs/examples.md).
|
|
42
|
-
|
|
43
|
-
## Most used functions
|
|
44
|
-
|
|
45
|
-
The README stays focused on the quick path. For a complete function list and full details, use the reference documentation link above.
|
|
46
|
-
|
|
47
|
-
| Module | Common functions |
|
|
48
|
-
|-------|------------------|
|
|
49
|
-
| `general` | `generate_md5`, `safe_input`, `safe_output`, `parse_configuration`, `parse_csv_file`, `translate_macros`, `now` |
|
|
50
|
-
| `agents` | `init_agent`, `print_agent`, `get_os` |
|
|
51
|
-
| `modules` | `init_module`, `print_module`, `init_log_module`, `print_log_module` |
|
|
52
|
-
| `transfer` | `write_xml`, `transfer_xml`, `tentacle_xml` |
|
|
53
|
-
| `threads` | `run_threads`, `run_processes`, `set_shared_dict_value`, `get_shared_dict_value` |
|
|
54
|
-
| `discovery` | `set_disco_summary`, `add_disco_summary_value`, `add_disco_info_value`, `add_disco_monitoring_data`, `disco_output` |
|
|
55
|
-
| `output` | `print_stdout`, `print_stderr`, `print_debug`, `logger` |
|
|
56
|
-
|
|
57
|
-
## Quick example
|
|
58
|
-
|
|
59
|
-
```python
|
|
60
|
-
import pandoraPlugintools as ppt
|
|
61
|
-
|
|
62
|
-
# Define agent
|
|
63
|
-
server_name = "WIN-SERV"
|
|
64
|
-
|
|
65
|
-
agent = ppt.init_agent(
|
|
66
|
-
{
|
|
67
|
-
"agent_name": ppt.generate_md5(server_name),
|
|
68
|
-
"agent_alias": server_name,
|
|
69
|
-
"description": "Default Windows server",
|
|
70
|
-
}
|
|
71
|
-
)
|
|
72
|
-
|
|
73
|
-
# Define modules
|
|
74
|
-
modules = []
|
|
75
|
-
data = 10
|
|
76
|
-
|
|
77
|
-
modules.append(
|
|
78
|
-
{
|
|
79
|
-
"name": "CPU usage",
|
|
80
|
-
"type": "generic_data",
|
|
81
|
-
"value": data,
|
|
82
|
-
"desc": "Percentage of CPU utilization",
|
|
83
|
-
"unit": "%",
|
|
84
|
-
}
|
|
85
|
-
)
|
|
86
|
-
|
|
87
|
-
# Generate and transfer XML
|
|
88
|
-
xml_content = ppt.print_agent(agent, modules)
|
|
89
|
-
xml_file = ppt.write_xml(xml_content, agent["agent_name"])
|
|
90
|
-
|
|
91
|
-
ppt.transfer_xml(
|
|
92
|
-
xml_file,
|
|
93
|
-
transfer_mode="tentacle",
|
|
94
|
-
tentacle_ip="192.168.1.20",
|
|
95
|
-
tentacle_port="41121",
|
|
96
|
-
)
|
|
97
|
-
```
|
|
98
|
-
|
|
File without changes
|
{pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools/agents.py
RENAMED
|
File without changes
|
{pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools/discovery.py
RENAMED
|
File without changes
|
{pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools/general.py
RENAMED
|
File without changes
|
{pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools/modules.py
RENAMED
|
File without changes
|
{pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools/monitoring.py
RENAMED
|
File without changes
|
{pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools/output.py
RENAMED
|
File without changes
|
{pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools/threads.py
RENAMED
|
File without changes
|
{pandoraplugintools_basic-1.1.1 → pandoraplugintools_basic-1.2.0}/pandoraPlugintools/transfer.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|