summaryrefslogtreecommitdiffstats
path: root/plugins/check_snmp.d/check_snmp_helpers.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_snmp.d/check_snmp_helpers.c')
-rw-r--r--plugins/check_snmp.d/check_snmp_helpers.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/plugins/check_snmp.d/check_snmp_helpers.c b/plugins/check_snmp.d/check_snmp_helpers.c
new file mode 100644
index 00000000..9db1d9f4
--- /dev/null
+++ b/plugins/check_snmp.d/check_snmp_helpers.c
@@ -0,0 +1,103 @@
1#include "./check_snmp_helpers.h"
2#include <string.h>
3#include "../../lib/utils_base.h"
4
5check_snmp_test_unit check_snmp_test_unit_init() {
6 check_snmp_test_unit tmp = {
7 .threshold = mp_thresholds_init(),
8 };
9 return tmp;
10}
11
12
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) {
16 // Got a comma in the string, should be multiple values
17 size_t tmp_counter = 0;
18 mp_range range_buffer;
19 bool first_value = true;
20 for (char *ptr = strtok(threshold_string, ", "); ptr != NULL;
21 ptr = strtok(NULL, ", "), tmp_counter++) {
22
23 // edge case: maybe we got `,,` to skip a value
24 if (strlen(ptr) == 0) {
25 // use the previous value in this case
26 // or do not overwrite the loop value to be specific
27 if (first_value) {
28 die(STATE_UNKNOWN, "Empty threshold value");
29 }
30 } else {
31 mp_range_parsed tmp = mp_parse_range_string(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 }
37
38 if (is_critical) {
39 tu[tmp_counter].threshold.critical = range_buffer;
40 tu[tmp_counter].threshold.critical_is_set = true;
41 } else {
42 tu[tmp_counter].threshold.warning = range_buffer;
43 tu[tmp_counter].threshold.warning_is_set = true;
44 }
45 first_value = false;
46 }
47 } else {
48 // Single value
49 mp_range_parsed tmp = mp_parse_range_string(threshold_string);
50 if (tmp.error != MP_PARSING_SUCCES) {
51 die(STATE_UNKNOWN, "Unable to parse critical threshold range: %s", threshold_string);
52 }
53
54 for (size_t i = 0; i < max_test_units; i++) {
55 if (is_critical) {
56 tu[i].threshold.critical = tmp.range;
57 tu[i].threshold.critical_is_set = true;
58 } else {
59 tu[i].threshold.warning = tmp.range;
60 tu[i].threshold.warning_is_set = true;
61 }
62 }
63 }
64
65 return 0;
66}
67
68const int DEFAULT_PROTOCOL = SNMP_VERSION_1;
69const char DEFAULT_OUTPUT_DELIMITER[] = " ";
70
71const int RANDOM_STATE_DATA_LENGTH_PREDICTION = 1024;
72
73check_snmp_config check_snmp_config_init() {
74 check_snmp_config tmp = {
75 .use_getnext = false,
76
77 .ignore_mib_parsing_errors = false,
78 .need_mibs = false,
79
80 .test_units = NULL,
81 .num_of_test_units = 0,
82
83 .nulloid_result = STATE_UNKNOWN, // state to return if no result for query
84
85 .invert_search = true,
86 .regex_cmp_value = {},
87 .string_cmp_value = "",
88
89 .multiplier = 1.0,
90 .offset = 0,
91
92 .use_perf_data_labels_from_input = false,
93 };
94
95 snmp_sess_init(&tmp.snmp_session);
96
97 tmp.snmp_session.retries = DEFAULT_RETRIES;
98 tmp.snmp_session.version = DEFAULT_SNMP_VERSION;
99 tmp.snmp_session.securityLevel = SNMP_SEC_LEVEL_NOAUTH;
100 tmp.snmp_session.community = (unsigned char *)"public";
101 tmp.snmp_session.community_len = strlen("public");
102 return tmp;
103}