-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathargs.py
More file actions
282 lines (230 loc) · 8.38 KB
/
args.py
File metadata and controls
282 lines (230 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#! /usr/bin/env python3
# -*- coding: utf-8; py-indent-offset: 4 -*-
#
# Author: Linuxfabrik GmbH, Zurich, Switzerland
# Contact: info (at) linuxfabrik (dot) ch
# https://www.linuxfabrik.ch/
# License: The Unlicense, see LICENSE file.
# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.md
"""Extends argparse by new input argument data types on demand."""
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2026041901'
# Help text descriptions only - no "Default:" here.
# Plugins append their own default info, e.g.:
# help=lib.args.help('--timeout') + ' Default: %(default)s (seconds)',
# Switches (store_true/store_false) don't need a default.
HELP_TEXTS = {
'--always-ok': 'Always returns OK.',
'--cache-expire': (
'The amount of time after which the credential/data cache expires, in minutes.'
),
'--check-major': (
'Alert when a new major release is available, even if the current version is '
'not yet EOL. '
'Example: running v26 (not yet EOL) and v27 is available.'
),
'--check-minor': (
'Alert when a new major.minor release is available, even if the current version '
'is not yet EOL. '
'Example: running v26.2 (not yet EOL) and v26.3 is available.'
),
'--check-patch': (
'Alert when a new major.minor.patch release is available, even if the current '
'version is not yet EOL. '
'Example: running v26.2.7 (not yet EOL) and v26.2.8 is available.'
),
'--check-security': (
'Alert when the vendor version-check service reports a security-relevant update '
'for the currently installed version (security severity, critical vulnerability '
'or similar). '
'Requires online access to the vendor service. '
'Has no effect on plugins that do not implement an upstream security check.'
),
'--count': (
'Number of consecutive checks the threshold must be exceeded before alerting.'
),
'--critical': 'CRIT threshold in percent.',
'--critical-count': 'CRIT threshold for the number of matching items.',
'--critical-seconds': 'CRIT threshold in seconds.',
'--defaults-file': (
'Specifies a cnf file to read parameters like user, host and password from '
'(for MySQL/MariaDB cnf-style files).'
),
'--defaults-group': 'Group/section to read from in the cnf file.',
'--hostname': 'Hostname or IP address.',
'--ipv6': 'Use IPv6.',
'--ignore': (
'Any item matching this string will be ignored. '
'Case-sensitive. '
'Can be specified multiple times.'
),
'--ignore-pattern': (
'Any item containing this pattern will be ignored. '
'Case-insensitive. '
'Can be specified multiple times. '
'Example: `boot` matches both `/boot` and `/boot/efi`.'
),
'--ignore-regex': (
'Any item matching this Python regex will be ignored. '
'Can be specified multiple times. '
'Example: `(?i)linuxfabrik` for a case-insensitive match.'
),
'--insecure': 'This option explicitly allows insecure SSL connections.',
'--lengthy': 'Extended reporting.',
'--match': (
'Filter by this Python regular expression. '
'Case-sensitive by default; use `(?i)` for case-insensitive matching. '
'Can be specified multiple times. '
'Examples: '
'`(?i)example` to match "example" regardless of case. '
'`^(?!.*example).*$` to match any string except "example" (negative lookahead).'
),
'--no-proxy': 'Do not use a proxy.',
'--offset-eol': (
'Alert n days before ("-30") or after an EOL date ("30" or "+30").'
),
'--password': 'Password.',
'--path': 'Local path to the installation.',
'--port': 'Port number.',
'--severity': 'Severity for alerting.',
'--stratum': (
'Warns if the determined stratum of the time server is greater than or equal '
'to this value. '
'Stratum 1 indicates a computer with a locally attached reference clock. '
'A computer that is synchronised to a stratum 1 computer is at stratum 2. '
'A computer that is synchronised to a stratum 2 computer is at stratum 3, '
'and so on.'
),
'--test': (
'For unit tests. Needs "path-to-stdout-file,path-to-stderr-file,expected-retc".'
),
'--timeout': 'Network timeout in seconds.',
'--url': 'URL to the endpoint.',
'--username': 'Username.',
'--verbose': (
'Makes this plugin verbose during the operation. '
'Useful for debugging and seeing what is going on under the hood.'
),
'--warning': 'WARN threshold in percent.',
'--warning-count': 'WARN threshold for the number of matching items.',
'--warning-seconds': 'WARN threshold in seconds.',
}
# Predefined sets for checking units and methods
_UNITS = {'%', 'K', 'M', 'G', 'T', 'P'}
_METHODS = {'USED', 'FREE'}
def csv(arg):
"""Converts a CSV string into a list of values.
### Parameters
- **arg** (`str`): A string containing values separated by commas.
### Returns
- **list**: A list of stripped strings.
### Example
>>> csv('apple, orange, banana, grape')
['apple', 'orange', 'banana', 'grape']
"""
return [x.strip() for x in arg.split(',')]
def float_or_none(arg):
"""Converts an input to a float, or returns None if the input is 'none' or None.
### Parameters
- **arg** (`str`, `None`, or `float`): The input value.
### Returns
- **float** or **None**
### Example
>>> float_or_none('123.45')
123.45
>>> float_or_none('none')
None
"""
if arg is None:
return None
if isinstance(arg, str) and arg.strip().lower() == 'none':
return None
return float(arg)
def help(param):
"""Retrieves the global help text for a given parameter.
Returns only the description, without "Default:" suffix.
The plugin appends the default info as needed, e.g.:
help=lib.args.help('--timeout') + ' Default: %(default)s (seconds)',
### Parameters
- **param** (`str`): The parameter name (e.g. '--timeout').
### Returns
- **str**: The help text, or an empty string if not found.
### Example
>>> help('--timeout')
'Network timeout in seconds.'
"""
return HELP_TEXTS.get(param, '')
def int_or_none(arg):
"""Converts a given argument to an integer or returns None.
### Parameters
- **arg** (`str` or `None`): The input value.
### Returns
- **int** or **None**
### Example
>>> int_or_none('42')
42
>>> int_or_none('none')
None
"""
if arg is None:
return None
if isinstance(arg, str) and arg.strip().lower() == 'none':
return None
return int(arg)
def number_unit_method(arg, unit='%', method='USED'):
"""Parses a string in the format `<number>[unit][method]` for threshold arguments.
### Parameters
- **arg** (`str`): The input string.
- **unit** (`str`, optional): Default unit. Defaults to `%`.
- **method** (`str`, optional): Default method. Defaults to `USED`.
### Returns
- **tuple**: (number, unit, method)
### Example
>>> number_unit_method('95')
('95.0', '%', 'USED')
>>> number_unit_method('9.5GFREE')
('9.5', 'G', 'FREE')
"""
arg = arg.strip()
number_part = []
unit_part = ''
method_part = ''
i = 0
while i < len(arg) and (arg[i].isdigit() or arg[i] == '.'):
number_part.append(arg[i])
i += 1
if i < len(arg) and arg[i].upper() in _UNITS:
unit_part = arg[i]
i += 1
if i < len(arg):
method_part = arg[i:].upper()
number = ''.join(number_part)
if not number:
return '0.0', unit.upper(), method.upper()
if unit_part:
unit = unit_part
if method_part in _METHODS:
method = method_part
return number, unit.upper(), method.upper()
def range_or_none(arg):
"""See str_or_none()."""
return str_or_none(arg)
def str_or_none(arg):
"""Converts an input argument into a string or returns None.
### Parameters
- **arg** (`any`): The input argument.
### Returns
- **str** or **None**
### Example
>>> str_or_none(123)
'123'
>>> str_or_none('none')
None
"""
if arg is None:
return None
if isinstance(arg, str):
if arg.strip().lower() == 'none':
return None
return arg
return str(arg)