summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-08-27 16:41:46 +0200
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-08-27 16:41:46 +0200
commite490c5f969b3844c1c48fcff129091fdca849d58 (patch)
tree26ce870094a6d22444098b49f51e9484acf49f12
parentf976155863357e15961b280413244cc0badd89cb (diff)
downloadmonitoring-plugins-e490c5f969b3844c1c48fcff129091fdca849d58.tar.gz
check_snmp: hopefully fix helpers
-rw-r--r--plugins/check_snmp.d/check_snmp_helpers.c70
-rw-r--r--plugins/check_snmp.d/check_snmp_helpers.h3
2 files changed, 41 insertions, 32 deletions
diff --git a/plugins/check_snmp.d/check_snmp_helpers.c b/plugins/check_snmp.d/check_snmp_helpers.c
index 9db1d9f4..7725cc18 100644
--- a/plugins/check_snmp.d/check_snmp_helpers.c
+++ b/plugins/check_snmp.d/check_snmp_helpers.c
@@ -9,56 +9,66 @@ check_snmp_test_unit check_snmp_test_unit_init() {
9 return tmp; 9 return tmp;
10} 10}
11 11
12int check_snmp_set_thresholds(const char *threshold_string, check_snmp_test_unit test_units[],
13 size_t max_test_units, bool is_critical) {
14
15 if (threshold_string == NULL || strlen(threshold_string) == 0) {
16 // No input, do nothing
17 return 0;
18 }
12 19
13int check_snmp_set_thresholds(char *threshold_string, check_snmp_test_unit tu[],
14 size_t max_test_units, bool is_critical) {
15 if (strchr(threshold_string, ',') != NULL) { 20 if (strchr(threshold_string, ',') != NULL) {
16 // Got a comma in the string, should be multiple values 21 // Got a comma in the string, should be multiple values
17 size_t tmp_counter = 0; 22 size_t tu_index = 0;
18 mp_range range_buffer; 23
19 bool first_value = true; 24 while (threshold_string[0] == ',') {
25 // got commas at the beginning, so skip some values
26 tu_index++;
27 threshold_string++;
28 }
29
20 for (char *ptr = strtok(threshold_string, ", "); ptr != NULL; 30 for (char *ptr = strtok(threshold_string, ", "); ptr != NULL;
21 ptr = strtok(NULL, ", "), tmp_counter++) { 31 ptr = strtok(NULL, ", "), tu_index++) {
32
33 if (tu_index > max_test_units) {
34 // More thresholds then values, just ignore them
35 return 0;
36 }
22 37
23 // edge case: maybe we got `,,` to skip a value 38 // edge case: maybe we got `,,` to skip a value
24 if (strlen(ptr) == 0) { 39 if (strlen(ptr) == 0) {
25 // use the previous value in this case 40 // no threshold given, do not set it then
26 // or do not overwrite the loop value to be specific 41 continue;
27 if (first_value) { 42 }
28 die(STATE_UNKNOWN, "Empty threshold value"); 43
29 } 44 mp_range_parsed tmp = mp_parse_range_string(ptr);
30 } else { 45 if (tmp.error != MP_PARSING_SUCCES) {
31 mp_range_parsed tmp = mp_parse_range_string(ptr); 46 die(STATE_UNKNOWN, "Unable to parse critical threshold range: %s", ptr);
32 if (tmp.error != MP_PARSING_SUCCES) {
33 die(STATE_UNKNOWN, "Unable to parse critical threshold range: %s", ptr);
34 }
35 range_buffer = tmp.range;
36 } 47 }
37 48
38 if (is_critical) { 49 if (is_critical) {
39 tu[tmp_counter].threshold.critical = range_buffer; 50 test_units[tu_index].threshold.critical = tmp.range;
40 tu[tmp_counter].threshold.critical_is_set = true; 51 test_units[tu_index].threshold.critical_is_set = true;
41 } else { 52 } else {
42 tu[tmp_counter].threshold.warning = range_buffer; 53 test_units[tu_index].threshold.warning = tmp.range;
43 tu[tmp_counter].threshold.warning_is_set = true; 54 test_units[tu_index].threshold.warning_is_set = true;
44 } 55 }
45 first_value = false;
46 } 56 }
57
47 } else { 58 } else {
48 // Single value 59 // Single value
60 // only valid for the first test unit
49 mp_range_parsed tmp = mp_parse_range_string(threshold_string); 61 mp_range_parsed tmp = mp_parse_range_string(threshold_string);
50 if (tmp.error != MP_PARSING_SUCCES) { 62 if (tmp.error != MP_PARSING_SUCCES) {
51 die(STATE_UNKNOWN, "Unable to parse critical threshold range: %s", threshold_string); 63 die(STATE_UNKNOWN, "Unable to parse critical threshold range: %s", threshold_string);
52 } 64 }
53 65
54 for (size_t i = 0; i < max_test_units; i++) { 66 if (is_critical) {
55 if (is_critical) { 67 test_units[0].threshold.critical = tmp.range;
56 tu[i].threshold.critical = tmp.range; 68 test_units[0].threshold.critical_is_set = true;
57 tu[i].threshold.critical_is_set = true; 69 } else {
58 } else { 70 test_units[0].threshold.warning = tmp.range;
59 tu[i].threshold.warning = tmp.range; 71 test_units[0].threshold.warning_is_set = true;
60 tu[i].threshold.warning_is_set = true;
61 }
62 } 72 }
63 } 73 }
64 74
diff --git a/plugins/check_snmp.d/check_snmp_helpers.h b/plugins/check_snmp.d/check_snmp_helpers.h
index 74e7d0cd..28e3c4e3 100644
--- a/plugins/check_snmp.d/check_snmp_helpers.h
+++ b/plugins/check_snmp.d/check_snmp_helpers.h
@@ -3,6 +3,5 @@
3#include "./config.h" 3#include "./config.h"
4 4
5check_snmp_test_unit check_snmp_test_unit_init(); 5check_snmp_test_unit check_snmp_test_unit_init();
6int check_snmp_set_thresholds(char *threshold_string, check_snmp_test_unit tu[], 6int check_snmp_set_thresholds(const char *, check_snmp_test_unit[], size_t, bool);
7 size_t max_test_units, bool is_critical);
8check_snmp_config check_snmp_config_init(); 7check_snmp_config check_snmp_config_init();