-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbase.py
More file actions
872 lines (668 loc) · 25.8 KB
/
base.py
File metadata and controls
872 lines (668 loc) · 25.8 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
#! /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.rst
"""Provides very common every-day functions.
"""
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2026030301'
import numbers
import operator
import os
import sys
from traceback import format_exc
from .globals import STATE_CRIT, STATE_OK, STATE_UNKNOWN, STATE_WARN
from . import txt
WINDOWS = os.name == "nt"
LINUX = sys.platform.startswith("linux")
X86_64 = sys.maxsize > 2**32
_OPS = {
'ge': operator.ge,
'gt': operator.gt,
'le': operator.le,
'lt': operator.lt,
'eq': operator.eq,
'ne': operator.ne,
}
_STATE_NAMES = {
STATE_OK: '[OK]',
STATE_WARN: '[WARNING]',
STATE_CRIT: '[CRITICAL]',
STATE_UNKNOWN: '[UNKNOWN]',
}
def coe(result, state=STATE_UNKNOWN):
"""
Continue or Exit (CoE)
This function simplifies error handling for function calls that return a `(success, result)`
tuple. If the operation fails, it sanitizes and prints the error message and exits with a given
state. Otherwise, it returns the successful result and allows the script to continue.
### Parameters
- **result** (`tuple`): A two-element tuple returned from a function.
- `result[0]` (`bool`): Success indicator (`True` if successful, `False` otherwise).
- `result[1]` (`any`): The actual result or an error message.
- **state** (`int`, optional): Exit code to use if the function fails.
Defaults to `STATE_UNKNOWN` (3).
### Returns
- **any type**: The second element of the result tuple (`result[1]`) if successful.
### Notes
- Sensitive information in error messages is automatically redacted before printing.
- This function is intended to be used **only** inside the `main()` function of a plugin,
not inside library functions.
- If the function fails (`result[0]` is `False`), the script immediately exits after printing
the sanitized message.
### Example
Without `coe`:
>>> success, html = lib.url.fetch(URL)
>>> if not success:
>>> print(html)
>>> sys.exit(STATE_UNKNOWN)
With `coe`:
>>> html = lib.base.coe(lib.url.fetch(URL))
"""
if result[0]:
# success
return result[1]
# getting something like `(False, 'Error message')`; hide passwords in error message
print(txt.sanitize_sensitive_data(result[1]))
sys.exit(state)
def cu(msg=None):
"""
See you (cu)
Print an optional error message and stack trace, then exit with STATE_UNKNOWN.
This function prints an optional sanitized message, attaches a stack trace if an error occurred,
and exits the script with `STATE_UNKNOWN`. It ensures output is safe for display in web GUIs
by replacing `<` and `>` characters.
### Parameters
- **msg** (`str`, optional): An optional message to print before exiting.
If provided, it will be stripped, sanitized, and printed.
### Returns
- **None**: This function does not return; it always exits the script with `STATE_UNKNOWN`.
### Notes
- If a traceback exists, it is included for debugging, with `<` and `>` replaced by `'`.
- Sensitive information in the message is automatically redacted before printing.
- If no traceback is present, only the optional message (if any) is printed.
### Example
>>> cu("Unable to connect to server")
>>> cu()
"""
tb = format_exc()
has_traceback = tb and 'NoneType: None' not in tb
if msg is not None:
msg = txt.sanitize_sensitive_data(msg).strip()
print(msg, end='')
print(' (Traceback for debugging purposes attached)\n' if has_traceback else '\n')
if has_traceback:
print(tb.replace('<', "'").replace('>', "'"))
sys.exit(STATE_UNKNOWN)
def get_perfdata(label, value, uom=None, warn=None, crit=None, _min=None, _max=None):
"""
Returns a Nagios performance data string in the format:
`'label'=value[UOM];[warn];[crit];[min];[max]`
### Parameters
- **label** (`str`): The name of the performance data label.
- **value** (`int` or `float`): The measured value.
- **uom** (`str`, optional): The unit of measurement (e.g., 's', 'B', '%'). Defaults to None.
- **warn** (`int` or `float`, optional): Warning threshold. Defaults to None.
- **crit** (`int` or `float`, optional): Critical threshold. Defaults to None.
- **_min** (`int` or `float`, optional): Minimum value. Defaults to None.
- **_max** (`int` or `float`, optional): Maximum value. Defaults to None.
### Returns
- **str**: A properly formatted Nagios performance data string.
### Example
>>> get_perfdata('load1', 0.42, '', 1.0, 5.0, 0, 10)
"'load1'=0.42;1.0;5.0;0;10 "
"""
msg = f"'{label}'={value}{uom or ''};"
msg += f'{warn};' if warn is not None else ';'
msg += f'{crit};' if crit is not None else ';'
msg += f'{_min};' if _min is not None else ';'
msg += f'{_max}' if _max is not None else ''
return msg.rstrip(';') + ' '
def get_state(value, warn, crit, _operator='ge'):
"""
Returns the STATE by comparing `value` to the given thresholds using
a comparison `_operator`. `warn` and `crit` thresholds may also be `None`.
### Parameters
- **value** (`float`): Numeric value to evaluate.
- **warn** (`float`): Numeric warning threshold.
- **crit** (`float`): Numeric critical threshold.
- **_operator** (`str`): Comparison operator to use:
- `eq`: equal to
- `ge`: greater or equal
- `gt`: greater than
- `le`: less or equal
- `lt`: less than
- `ne`: not equal to
- `range`: match Nagios range definition
### Returns
- **int**: `STATE_OK`, `STATE_WARN`, or `STATE_CRIT`.
### Example
>>> get_state(15, 10, 20, 'ge')
1 # STATE_WARN
>>> get_state(10, 10, 20, 'gt')
0 # STATE_OK
"""
# make sure to use float comparison
value = float(value)
if _operator == 'range':
if crit is not None and not coe(match_range(value, crit)):
return STATE_CRIT
if warn is not None and not coe(match_range(value, warn)):
return STATE_WARN
return STATE_OK
op = _OPS.get(_operator)
if op is None:
return STATE_UNKNOWN
if crit is not None and op(value, float(crit)):
return STATE_CRIT
if warn is not None and op(value, float(warn)):
return STATE_WARN
return STATE_OK
def get_table(data, cols, header=None, strip=True, sort_by_key=None, sort_order_reverse=False):
"""
Format a list of dictionaries into a simple ASCII table (generator version).
Each dictionary must contain the specified columns (`cols`). Optionally supports a custom
header, sorting by a given key, and stripping whitespace from values.
### Parameters
- **data** (`list`): List of dictionaries representing the table rows.
- **cols** (`list`): List of keys to display as table columns.
- **header** (`list`, optional): List of custom column headers. Defaults to None.
- **strip** (`bool`, optional): Whether to strip whitespace from values. Defaults to True.
- **sort_by_key** (`str`, optional): Column key to sort the table by. Defaults to None.
- **sort_order_reverse** (`bool`, optional): Sort descending if True. Defaults to False.
### Returns
- **str**: A string containing the formatted table.
### Example
>>> data = [{'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}]
>>> cols = ['name', 'age']
>>> print(get_table(data, cols))
name ! age
------+----
Alice ! 30
Bob ! 25
"""
if not data:
return ''
data = data.copy() # data has been passed by-reference - kick the reference
if sort_by_key:
data = sorted(data, key=operator.itemgetter(sort_by_key), reverse=sort_order_reverse)
if header:
data.insert(0, dict(zip(cols, header)))
# Process values and calculate column widths in a single pass
processed_rows = []
column_widths = {}
for row in data:
processed_row = {}
for col in cols:
if col not in row:
return f'Unknown column "{col}"'
value = str(row[col])
if strip:
value = value.strip()
processed_row[col] = value
column_widths[col] = max(column_widths.get(col, 0), len(value))
processed_rows.append(processed_row)
if header:
divider = {col: '-' * width for col, width in column_widths.items()}
processed_rows.insert(1, divider)
# Generate output lines
lines = []
for idx, row in enumerate(processed_rows):
parts = [f'{row[col]:<{column_widths[col]}}' for col in column_widths]
lines.append(('-+-' if header and idx == 1 else ' ! ').join(parts))
return '\n'.join(lines) + '\n'
def get_worst(state1, state2):
"""
Compares `state1` to `state2` and returns the worse state based on the following priority:
STATE_OK < STATE_UNKNOWN < STATE_WARNING < STATE_CRITICAL
It will prioritize any non-OK state.
Note that numerically the priority order does not match their integer values.
### Parameters
- **state1** (`int`): The first state to compare.
- **state2** (`int`): The second state to compare.
### Returns
- **int**: The worse state according to the priority order.
### Example
>>> get_worst(STATE_OK, STATE_WARNING)
STATE_WARNING
>>> get_worst(STATE_UNKNOWN, STATE_CRITICAL)
STATE_CRITICAL
"""
state1 = int(state1)
state2 = int(state2)
if STATE_CRIT in (state1, state2):
return STATE_CRIT
if STATE_WARN in (state1, state2):
return STATE_WARN
if STATE_UNKNOWN in (state1, state2):
return STATE_UNKNOWN
return STATE_OK
def guess_type(v, consumer='python'):
"""
Guess the type of a value (None, int, float, or string) for different types of consumers
(e.g., Python, SQLite).
For Python, it returns the actual value converted to its type (`int`, `float`, or `str`).
For SQLite, it returns a string describing the type (`'integer'`, `'real'`, `'text'`).
### Parameters
- **v** (`any`): The value to guess the type for.
- **consumer** (`str`, optional): The consumer type ('python' or 'sqlite'). Defaults to
'python'.
### Returns
- **any**:
- If `consumer='python'`, returns `None`, `int`, `float`, or `str`.
- If `consumer='sqlite'`, returns `'integer'`, `'real'`, or `'text'`.
### Example
>>> guess_type('1')
1
>>> guess_type('1', 'sqlite')
'integer'
>>> guess_type('1.0')
1.0
>>> guess_type('1.0', 'sqlite')
'real'
>>> guess_type('abc')
'abc'
>>> guess_type('abc', 'sqlite')
'text'
>>> value_type = lib.base.guess_type(value)
>>> if isinstance(value_type, int) or isinstance(value_type, float):
>>> ...
"""
if v is None:
return None if consumer == 'python' else 'text'
try:
result = int(v)
return result if consumer == 'python' else 'integer'
except (ValueError, TypeError):
try:
result = float(v)
return result if consumer == 'python' else 'real'
except (ValueError, TypeError):
return str(v) if consumer == 'python' else 'text'
def is_empty_list(lst):
"""
Check if a list only contains either empty elements or whitespace.
### Parameters
- **l** (`list`): The list to check.
### Returns
- **bool**: True if all elements are empty strings or whitespace, otherwise False.
### Example
>>> is_empty_list(['', ' ', ''])
True
>>> is_empty_list(['text', ''])
False
"""
return all(not s.strip() for s in lst)
def is_numeric(value):
"""
Return True if the value is truly numeric (int, float, etc.).
### Parameters
- **value** (`any`): The value to check.
### Returns
- **bool**: True if the value is numeric, otherwise False.
### Example
>>> is_numeric(+53.4)
True
>>> is_numeric('53.4')
False
"""
return isinstance(value, numbers.Number)
def lookup_lod(haystack, key, needle):
"""
Search in a list of dictionaries ("lod") for a key containing a specific value
and return the first dictionary item found.
Returns `(index, item)` if the needle was found, otherwise `(-1, None)`.
### Parameters
- **haystack** (`list`): A list of dictionaries to search through.
- **key** (`str`): The key to look for in each dictionary.
- **needle** (`any`): The value to match against the specified key.
### Returns
- **tuple**:
- If found: (index, dictionary item).
- If not found: (-1, None).
### Example
>>> haystack = [
... {"name": "Tom", "age": 10},
... {"name": "Mark", "age": 5},
... {"name": "Pam", "age": 7},
... {"name": "Dick", "age": 12}
... ]
>>> lookup_lod(haystack, 'name', 'Pam')
(2, {'name': 'Pam', 'age': 7})
>>> lookup_lod(haystack, 'name', 'Pamela')
(-1, None)
"""
for idx, item in enumerate(haystack):
if isinstance(item, dict) and key in item and item[key] == needle:
return idx, item
return -1, None
def _parse_range_atom(atom, default):
if not atom:
return default
return float(atom) if '.' in atom else int(atom)
def _parse_range(spec_):
"""
Inspired by
https://github.com/mpounsett/nagiosplugin/blob/master/nagiosplugin/range.py
+--------+-------------------+-------------------+----------------+
| -w, -c | OK if result is | WARN/CRIT if | returns |
+--------+-------------------+-------------------+----------------+
| 10 | in (0..10) | not in (0..10) | (0, 10, False) |
+--------+-------------------+-------------------+----------------+
| -10 | in (-10..0) | not in (-10..0) | (0, -10, False)|
+--------+-------------------+-------------------+----------------+
| 10: | in (10..inf) | not in (10..inf) | (10, inf, F) |
+--------+-------------------+-------------------+----------------+
| : | in (0..inf) | not in (0..inf) | (0, inf, False)|
+--------+-------------------+-------------------+----------------+
| ~:10 | in (-inf..10) | not in (-inf..10) | (-inf, 10, F) |
+--------+-------------------+-------------------+----------------+
| 10:20 | in (10..20) | not in (10..20) | (10, 20, False)|
+--------+-------------------+-------------------+----------------+
| @10:20 | not in (10..20) | in 10..20 | (10, 20, True) |
+--------+-------------------+-------------------+----------------+
| @~:20 | not in (-inf..20) | in (-inf..20) | (-inf, 20, T) |
+--------+-------------------+-------------------+----------------+
| @ | not in (0..inf) | in (0..inf) | (0, inf, True) |
+--------+-------------------+-------------------+----------------+
"""
if spec_ is None or str(spec_).lower() == 'none':
return True, None
if not isinstance(spec_, str):
spec_ = str(spec_)
invert = spec_.startswith('@')
if invert:
spec_ = spec_[1:]
if ':' in spec_:
try:
start, end = spec_.split(':')
except ValueError:
return False, 'Not using range definition correctly'
else:
start, end = '', spec_
start = float('-inf') if start == '~' \
else _parse_range_atom(start, 0)
end = _parse_range_atom(end, float('inf'))
if start > end:
return (
False,
f'Start {start} must not be greater than end {end}',
)
return True, (start, end, invert)
def match_range(value, spec):
"""
Decides if `value` is inside or outside the Nagios threshold
specification.
### Parameters
- **value** (`int` or `float`): The numeric value to check.
- **spec** (`str`): The Nagios range specification string.
### Returns
- **bool**:
- True if `value` is inside the bounds for a non-inverted
`spec`, or outside the bounds for an inverted `spec`.
- Otherwise, False.
### Example
>>> match_range(15, '10')
0 10 False
>>> match_range(15, '-10')
(False, 'Start 0 must not be greater than end -10')
>>> match_range(15, '10:')
10 inf False
>>> match_range(15, ':')
0 inf False
>>> match_range(15, '~:10')
-inf 10 False
>>> match_range(15, '10:20')
10 20 False
>>> match_range(15, '@10')
0 10 True
>>> match_range(15, '@~:20')
-inf 20 True
>>> match_range(15, '@')
0 inf True
"""
if isinstance(spec, str):
spec = spec.lstrip('\\')
if spec is None or str(spec).lower() == 'none':
return True, True
success, result = _parse_range(spec)
if not success:
return success, result
start, end, invert = result
if isinstance(value, (str, bytes)):
value = float(value.replace('%', ''))
if value < start or value > end:
return True, bool(invert)
return True, not invert
def oao(msg, state=STATE_OK, perfdata='', always_ok=False):
"""
Over and Out (OaO)
Print a sanitized plugin message with optional performance data and exit the script.
This function formats and prints a plugin message, appends performance data if provided,
sanitizes sensitive information, replaces reserved `|` characters, and exits with the
specified state code. Optionally, it can always exit with `STATE_OK` regardless of the given
state.
### Parameters
- **msg** (`str`): The plugin message to print. Will be stripped, sanitized, and processed.
- **state** (`int`, optional): The exit code to use. Defaults to `STATE_OK`.
- **perfdata** (`str`, optional): Performance data to append after a `|` separator.
Defaults to an empty string (no performance data).
- **always_ok** (`bool`, optional): If `True`, forces the exit code to `STATE_OK` regardless
of the specified `state`. Defaults to `False`.
### Returns
- **None**: This function does not return; it terminates the script via `sys.exit()`.
### Notes
- Any `|` characters inside the message are replaced with `!` to avoid breaking Nagios plugin
output format.
- Sensitive information like passwords, tokens, and keys is automatically redacted.
- `perfdata`, if provided, must follow monitoring plugin standards for performance metrics.
### Example
>>> oao("Service is healthy", STATE_OK, "load=0.12;1.00;5.00", always_ok=False)
Service is healthy|load=0.12;1.00;5.00
(and exits with code 0)
>>> oao("password=secret123 found!", STATE_CRITICAL)
password=****** found!
(and exits with code 2)
"""
msg = txt.sanitize_sensitive_data(msg.strip()).replace('|', '!')
if always_ok and msg:
# Instead of splitlines(), we just split('\n', 1), so only first line is touched.
parts = msg.split('\n', 1)
parts[0] += ' (always ok)'
msg = '\n'.join(parts)
print(f'{msg}|{perfdata.strip()}' if perfdata else msg)
sys.exit(STATE_OK if always_ok else state)
def smartcast(value):
"""
Returns the value converted to `float` if possible, else to `str`, else returns
the uncasted value.
### Parameters
- **value** (`any`): The value to attempt to cast.
### Returns
- **float**, **str**, or **any**:
- If convertible to `float`, returns a `float`.
- If not, tries to convert to `str`.
- If neither succeeds, returns the original value unchanged.
### Example
>>> smartcast('3.14')
3.14
>>> smartcast(42)
42.0
>>> smartcast('hello')
'hello'
"""
try:
return float(value)
except (ValueError, TypeError):
try:
return str(value)
except (ValueError, TypeError):
return value
def sort(array, reverse=True, sort_by_key=False):
"""
Sort a dict, list, or tuple.
- If dict: sorts by values (default) or keys (if sort_by_key=True).
- If list or tuple: sorts the elements.
- Other types: returned unchanged.
When a dictionary is provided, this function returns a list of (key, value)
tuples sorted based on the specified criteria:
- If `sort_by_key` is False (default), the dictionary items are sorted by their values.
- If `sort_by_key` is True, the items are sorted by their keys (compared case-insensitively).
The sort order is descending by default (`reverse=True`).
If the input is not a dictionary, the original input is returned unmodified.
### Parameters
- **array** (`dict` or `any`): The dictionary to be sorted. If not a dictionary, the input is
returned as is.
- **reverse** (`bool`, optional): If True, sort in descending order; if False, ascending.
Defaults to True.
- **sort_by_key** (`bool`, optional): If True, sort by dictionary keys; if False, by values.
Defaults to False.
### Returns
- **list** or **any**: A list of sorted (key, value) tuples if a dictionary is provided,
otherwise the original input.
### Example
>>> sort({'a': 2, 'b': 1})
[('a', 2), ('b', 1)]
>>> sort({'a': 2, 'b': 1}, reverse=False)
[('b', 1), ('a', 2)]
>>> sort({'a': 2, 'B': 1}, sort_by_key=True)
[('a', 2), ('B', 1)]
"""
if isinstance(array, dict):
keyfunc = (lambda x: str(x[0]).lower()) if sort_by_key else (lambda x: x[1])
return sorted(array.items(), key=keyfunc, reverse=reverse)
if isinstance(array, (list, tuple)):
return sorted(array, reverse=reverse)
return array
def state2str(state, empty_ok=True, prefix='', suffix=''):
"""
Return the state's string representation.
The square brackets around the state cause Icinga Web 2 to color the state.
### Parameters
- **state** (`int`): The state code (e.g., 0, 1, 2, 3).
- **empty_ok** (`bool`, optional): If True and the state is OK (0), return an empty string.
Defaults to True.
- **prefix** (`str`, optional): A prefix string to prepend to the result. Defaults to ''.
- **suffix** (`str`, optional): A suffix string to append to the result. Defaults to ''.
### Returns
- **str**: A formatted string representation of the state.
### Example
>>> lib.base.state2str(2)
'[CRIT]'
>>> state2str(0)
''
>>> state2str(0, empty_ok=False)
'[OK]'
>>> state2str(0, empty_ok=False, suffix=' ')
'[OK] '
>>> state2str(0, empty_ok=False, prefix=' (', suffix=')')
' ([OK])'
"""
state = int(state)
text = _STATE_NAMES.get(state, str(state))
if state == STATE_OK and empty_ok:
return ''
return f'{prefix}{text}{suffix}'
def str2bool(s):
"""
Return True or False depending on the given string.
### Parameters
- **s** (`str`): The input string to evaluate.
### Returns
- **bool**: True if the string is not empty and not equal to "false" (case-insensitive),
otherwise False.
### Example
>>> str2bool("")
False
>>> str2bool("false")
False
>>> str2bool("FalSE")
False
>>> str2bool("true")
True
>>> str2bool("Linuxfabrik")
True
>>> str2bool("0")
True
>>> str2bool("1")
True
"""
return bool(s) and s.lower() != 'false'
def str2state(string, ignore_error=True):
"""
Return the numeric state based on a (case-insensitive) string.
Matches up to the first four characters of the input string.
### Parameters
- **string** (`str`): The input string to match against known states.
- **ignore_error** (`bool`, optional): If True, unrecognized strings return `STATE_UNKNOWN`.
If False, unrecognized strings return None. Defaults to True.
### Returns
- **int** or **None**:
- The numeric state code (`STATE_OK`, `STATE_WARN`, `STATE_CRIT`, `STATE_UNKNOWN`) if
recognized.
- Otherwise, `STATE_UNKNOWN` or None, depending on `ignore_error`.
### Example
>>> str2state('ok')
0
>>> str2state('okidoki')
3
>>> str2state('okidoki', ignore_error=False)
None
>>> str2state('war')
3
>>> str2state('warn')
1
>>> str2state('Warnung')
1
>>> str2state('CrITical')
2
>>> str2state('UNKNOWN')
3
>>> str2state('gobbledygook')
3
>>> str2state('gobbledygook', ignore_error=False)
None
"""
lookup = {
'ok': STATE_OK,
'warn': STATE_WARN,
'crit': STATE_CRIT,
'unkn': STATE_UNKNOWN,
}
return lookup.get(str(string).lower()[:4], STATE_UNKNOWN if ignore_error else None)
def sum_dict(dict1, dict2):
"""
Sum up two dictionaries, possibly with different keys.
Only numeric values are considered for summation; non-numeric values are ignored.
### Parameters
- **dict1** (`dict`): The first dictionary to sum.
- **dict2** (`dict`): The second dictionary to sum.
### Returns
- **dict**: A new dictionary with summed numeric values by key.
### Example
>>> sum_dict({'in': 100, 'out': 10}, {'in': 50, 'error': 5, 'uuid': '1234-xyz'})
{'in': 150, 'out': 10, 'error': 5}
"""
return sum_lod([dict1, dict2])
def sum_lod(mylist):
"""
Sum up a list of (simple 1-dimensional) dictionary items.
Only numeric values are considered for summation; non-numeric values are ignored.
### Parameters
- **mylist** (`list`): A list of dictionaries to sum.
### Returns
- **dict**: A dictionary with summed numeric values by key.
### Example
>>> sum_lod([{'in': 100, 'out': 10}, {'in': 50, 'out': 20}, {'error': 5, 'uuid': '1234-xyz'}])
{'in': 150, 'out': 30, 'error': 5}
"""
total = {}
for d in mylist:
for key, value in d.items():
if is_numeric(value):
total[key] = total.get(key, 0) + value
return total