diff options
Diffstat (limited to 'plugins/check_snmp.d/check_snmp_helpers.c')
-rw-r--r-- | plugins/check_snmp.d/check_snmp_helpers.c | 934 |
1 files changed, 934 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..ecbfc5dd --- /dev/null +++ b/plugins/check_snmp.d/check_snmp_helpers.c | |||
@@ -0,0 +1,934 @@ | |||
1 | #include "./check_snmp_helpers.h" | ||
2 | #include <string.h> | ||
3 | #include "../../lib/utils_base.h" | ||
4 | #include "config.h" | ||
5 | #include <assert.h> | ||
6 | #include "../utils.h" | ||
7 | #include "output.h" | ||
8 | #include "states.h" | ||
9 | #include <sys/stat.h> | ||
10 | #include <ctype.h> | ||
11 | |||
12 | extern int verbose; | ||
13 | |||
14 | check_snmp_test_unit check_snmp_test_unit_init() { | ||
15 | check_snmp_test_unit tmp = { | ||
16 | .threshold = mp_thresholds_init(), | ||
17 | }; | ||
18 | return tmp; | ||
19 | } | ||
20 | |||
21 | int check_snmp_set_thresholds(const char *threshold_string, check_snmp_test_unit test_units[], | ||
22 | size_t max_test_units, bool is_critical) { | ||
23 | |||
24 | if (threshold_string == NULL || strlen(threshold_string) == 0) { | ||
25 | // No input, do nothing | ||
26 | return 0; | ||
27 | } | ||
28 | |||
29 | if (strchr(threshold_string, ',') != NULL) { | ||
30 | // Got a comma in the string, should be multiple values | ||
31 | size_t tu_index = 0; | ||
32 | |||
33 | while (threshold_string[0] == ',') { | ||
34 | // got commas at the beginning, so skip some values | ||
35 | tu_index++; | ||
36 | threshold_string++; | ||
37 | } | ||
38 | |||
39 | for (char *ptr = strtok(threshold_string, ", "); ptr != NULL; | ||
40 | ptr = strtok(NULL, ", "), tu_index++) { | ||
41 | |||
42 | if (tu_index > max_test_units) { | ||
43 | // More thresholds then values, just ignore them | ||
44 | return 0; | ||
45 | } | ||
46 | |||
47 | // edge case: maybe we got `,,` to skip a value | ||
48 | if (strlen(ptr) == 0) { | ||
49 | // no threshold given, do not set it then | ||
50 | continue; | ||
51 | } | ||
52 | |||
53 | mp_range_parsed tmp = mp_parse_range_string(ptr); | ||
54 | if (tmp.error != MP_PARSING_SUCCES) { | ||
55 | die(STATE_UNKNOWN, "Unable to parse critical threshold range: %s", ptr); | ||
56 | } | ||
57 | |||
58 | if (is_critical) { | ||
59 | test_units[tu_index].threshold.critical = tmp.range; | ||
60 | test_units[tu_index].threshold.critical_is_set = true; | ||
61 | } else { | ||
62 | test_units[tu_index].threshold.warning = tmp.range; | ||
63 | test_units[tu_index].threshold.warning_is_set = true; | ||
64 | } | ||
65 | } | ||
66 | |||
67 | } else { | ||
68 | // Single value | ||
69 | // only valid for the first test unit | ||
70 | mp_range_parsed tmp = mp_parse_range_string(threshold_string); | ||
71 | if (tmp.error != MP_PARSING_SUCCES) { | ||
72 | die(STATE_UNKNOWN, "Unable to parse critical threshold range: %s", threshold_string); | ||
73 | } | ||
74 | |||
75 | if (is_critical) { | ||
76 | test_units[0].threshold.critical = tmp.range; | ||
77 | test_units[0].threshold.critical_is_set = true; | ||
78 | } else { | ||
79 | test_units[0].threshold.warning = tmp.range; | ||
80 | test_units[0].threshold.warning_is_set = true; | ||
81 | } | ||
82 | } | ||
83 | |||
84 | return 0; | ||
85 | } | ||
86 | |||
87 | const int DEFAULT_PROTOCOL = SNMP_VERSION_1; | ||
88 | const char DEFAULT_OUTPUT_DELIMITER[] = " "; | ||
89 | |||
90 | const int RANDOM_STATE_DATA_LENGTH_PREDICTION = 8192; | ||
91 | |||
92 | check_snmp_config check_snmp_config_init() { | ||
93 | check_snmp_config tmp = { | ||
94 | .snmp_params = | ||
95 | { | ||
96 | .use_getnext = false, | ||
97 | |||
98 | .ignore_mib_parsing_errors = false, | ||
99 | .need_mibs = false, | ||
100 | |||
101 | .test_units = NULL, | ||
102 | .num_of_test_units = 0, | ||
103 | }, | ||
104 | |||
105 | .evaluation_params = | ||
106 | { | ||
107 | .nulloid_result = STATE_UNKNOWN, // state to return if no result for query | ||
108 | |||
109 | .invert_search = true, | ||
110 | .regex_cmp_value = {}, | ||
111 | .string_cmp_value = "", | ||
112 | |||
113 | .multiplier = 1.0, | ||
114 | .multiplier_set = false, | ||
115 | .offset = 0, | ||
116 | .offset_set = false, | ||
117 | |||
118 | .use_oid_as_perf_data_label = false, | ||
119 | |||
120 | .calculate_rate = false, | ||
121 | .rate_multiplier = 1, | ||
122 | }, | ||
123 | }; | ||
124 | |||
125 | snmp_sess_init(&tmp.snmp_params.snmp_session); | ||
126 | |||
127 | tmp.snmp_params.snmp_session.retries = DEFAULT_RETRIES; | ||
128 | tmp.snmp_params.snmp_session.version = DEFAULT_SNMP_VERSION; | ||
129 | tmp.snmp_params.snmp_session.securityLevel = SNMP_SEC_LEVEL_NOAUTH; | ||
130 | tmp.snmp_params.snmp_session.community = (unsigned char *)"public"; | ||
131 | tmp.snmp_params.snmp_session.community_len = strlen("public"); | ||
132 | return tmp; | ||
133 | } | ||
134 | |||
135 | snmp_responces do_snmp_query(check_snmp_config_snmp_parameters parameters) { | ||
136 | if (parameters.ignore_mib_parsing_errors) { | ||
137 | char *opt_toggle_res = snmp_mib_toggle_options("e"); | ||
138 | if (opt_toggle_res != NULL) { | ||
139 | die(STATE_UNKNOWN, "Unable to disable MIB parsing errors"); | ||
140 | } | ||
141 | } | ||
142 | |||
143 | struct snmp_pdu *pdu = NULL; | ||
144 | if (parameters.use_getnext) { | ||
145 | pdu = snmp_pdu_create(SNMP_MSG_GETNEXT); | ||
146 | } else { | ||
147 | pdu = snmp_pdu_create(SNMP_MSG_GET); | ||
148 | } | ||
149 | |||
150 | for (size_t i = 0; i < parameters.num_of_test_units; i++) { | ||
151 | assert(parameters.test_units[i].oid != NULL); | ||
152 | if (verbose > 0) { | ||
153 | printf("OID %zu to parse: %s\n", i, parameters.test_units[i].oid); | ||
154 | } | ||
155 | |||
156 | oid tmp_OID[MAX_OID_LEN]; | ||
157 | size_t tmp_OID_len = MAX_OID_LEN; | ||
158 | if (snmp_parse_oid(parameters.test_units[i].oid, tmp_OID, &tmp_OID_len) != NULL) { | ||
159 | // success | ||
160 | snmp_add_null_var(pdu, tmp_OID, tmp_OID_len); | ||
161 | } else { | ||
162 | // failed | ||
163 | snmp_perror("Parsing failure"); | ||
164 | die(STATE_UNKNOWN, "Failed to parse OID\n"); | ||
165 | } | ||
166 | } | ||
167 | |||
168 | const int timeout_safety_tolerance = 5; | ||
169 | alarm((timeout_interval * (unsigned int)parameters.snmp_session.retries) + | ||
170 | timeout_safety_tolerance); | ||
171 | |||
172 | struct snmp_session *active_session = snmp_open(¶meters.snmp_session); | ||
173 | if (active_session == NULL) { | ||
174 | int pcliberr = 0; | ||
175 | int psnmperr = 0; | ||
176 | char *pperrstring = NULL; | ||
177 | snmp_error(¶meters.snmp_session, &pcliberr, &psnmperr, &pperrstring); | ||
178 | die(STATE_UNKNOWN, "Failed to open SNMP session: %s\n", pperrstring); | ||
179 | } | ||
180 | |||
181 | struct snmp_pdu *response = NULL; | ||
182 | int snmp_query_status = snmp_synch_response(active_session, pdu, &response); | ||
183 | |||
184 | if (!(snmp_query_status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR)) { | ||
185 | int pcliberr = 0; | ||
186 | int psnmperr = 0; | ||
187 | char *pperrstring = NULL; | ||
188 | snmp_error(active_session, &pcliberr, &psnmperr, &pperrstring); | ||
189 | |||
190 | if (psnmperr == SNMPERR_TIMEOUT) { | ||
191 | // We exit with critical here for some historical reason | ||
192 | die(STATE_CRITICAL, "SNMP query ran into a timeout\n"); | ||
193 | } | ||
194 | die(STATE_UNKNOWN, "SNMP query failed: %s\n", pperrstring); | ||
195 | } | ||
196 | |||
197 | snmp_close(active_session); | ||
198 | |||
199 | /* disable alarm again */ | ||
200 | alarm(0); | ||
201 | |||
202 | snmp_responces result = { | ||
203 | .errorcode = OK, | ||
204 | .response_values = calloc(parameters.num_of_test_units, sizeof(response_value)), | ||
205 | }; | ||
206 | |||
207 | if (result.response_values == NULL) { | ||
208 | result.errorcode = ERROR; | ||
209 | return result; | ||
210 | } | ||
211 | |||
212 | // We got the the query results, now process them | ||
213 | size_t loop_index = 0; | ||
214 | for (netsnmp_variable_list *vars = response->variables; vars; | ||
215 | vars = vars->next_variable, loop_index++) { | ||
216 | |||
217 | for (size_t jdx = 0; jdx < vars->name_length; jdx++) { | ||
218 | result.response_values[loop_index].oid[jdx] = vars->name[jdx]; | ||
219 | } | ||
220 | result.response_values[loop_index].oid_length = vars->name_length; | ||
221 | |||
222 | switch (vars->type) { | ||
223 | case ASN_OCTET_STR: { | ||
224 | result.response_values[loop_index].string_response = strdup((char *)vars->val.string); | ||
225 | result.response_values[loop_index].type = vars->type; | ||
226 | if (verbose) { | ||
227 | printf("Debug: Got a string as response: %s\n", vars->val.string); | ||
228 | } | ||
229 | } | ||
230 | continue; | ||
231 | case ASN_OPAQUE: | ||
232 | if (verbose) { | ||
233 | printf("Debug: Got OPAQUE\n"); | ||
234 | } | ||
235 | break; | ||
236 | /* Numerical values */ | ||
237 | case ASN_COUNTER64: { | ||
238 | if (verbose) { | ||
239 | printf("Debug: Got counter64\n"); | ||
240 | } | ||
241 | struct counter64 tmp = *(vars->val.counter64); | ||
242 | uint64_t counter = (tmp.high << 32) + tmp.low; | ||
243 | result.response_values[loop_index].value.uIntVal = counter; | ||
244 | result.response_values[loop_index].type = vars->type; | ||
245 | } break; | ||
246 | case ASN_GAUGE: // same as ASN_UNSIGNED | ||
247 | case ASN_TIMETICKS: | ||
248 | case ASN_COUNTER: | ||
249 | case ASN_UINTEGER: { | ||
250 | if (verbose) { | ||
251 | printf("Debug: Got a Integer like\n"); | ||
252 | } | ||
253 | result.response_values[loop_index].value.uIntVal = (unsigned long)*(vars->val.integer); | ||
254 | result.response_values[loop_index].type = vars->type; | ||
255 | } break; | ||
256 | case ASN_INTEGER: { | ||
257 | if (verbose) { | ||
258 | printf("Debug: Got a Integer\n"); | ||
259 | } | ||
260 | result.response_values[loop_index].value.intVal = *(vars->val.integer); | ||
261 | result.response_values[loop_index].type = vars->type; | ||
262 | } break; | ||
263 | case ASN_FLOAT: { | ||
264 | if (verbose) { | ||
265 | printf("Debug: Got a float\n"); | ||
266 | } | ||
267 | result.response_values[loop_index].value.doubleVal = *(vars->val.floatVal); | ||
268 | result.response_values[loop_index].type = vars->type; | ||
269 | } break; | ||
270 | case ASN_DOUBLE: { | ||
271 | if (verbose) { | ||
272 | printf("Debug: Got a double\n"); | ||
273 | } | ||
274 | result.response_values[loop_index].value.doubleVal = *(vars->val.doubleVal); | ||
275 | result.response_values[loop_index].type = vars->type; | ||
276 | } break; | ||
277 | case ASN_IPADDRESS: | ||
278 | if (verbose) { | ||
279 | printf("Debug: Got an IP address\n"); | ||
280 | } | ||
281 | result.response_values[loop_index].type = vars->type; | ||
282 | |||
283 | // TODO: print address here, state always ok? or regex match? | ||
284 | break; | ||
285 | default: | ||
286 | if (verbose) { | ||
287 | printf("Debug: Got a unmatched result type: %hhu\n", vars->type); | ||
288 | } | ||
289 | // TODO: Error here? | ||
290 | break; | ||
291 | } | ||
292 | } | ||
293 | |||
294 | return result; | ||
295 | } | ||
296 | |||
297 | check_snmp_evaluation evaluate_single_unit(response_value response, | ||
298 | check_snmp_evaluation_parameters eval_params, | ||
299 | check_snmp_test_unit test_unit, time_t query_timestamp, | ||
300 | check_snmp_state_entry prev_state, | ||
301 | bool have_previous_state) { | ||
302 | mp_subcheck sc_oid_test = mp_subcheck_init(); | ||
303 | |||
304 | if ((test_unit.label != NULL) && (strcmp(test_unit.label, "") != 0)) { | ||
305 | xasprintf(&sc_oid_test.output, "%s - ", test_unit.label); | ||
306 | } else { | ||
307 | sc_oid_test.output = strdup(""); | ||
308 | } | ||
309 | |||
310 | char oid_string[(MAX_OID_LEN * 2) + 1] = {}; | ||
311 | |||
312 | int oid_string_result = | ||
313 | snprint_objid(oid_string, (MAX_OID_LEN * 2) + 1, response.oid, response.oid_length); | ||
314 | if (oid_string_result <= 0) { | ||
315 | // TODO error here | ||
316 | die(STATE_UNKNOWN, "snprint_objid failed\n"); | ||
317 | } | ||
318 | |||
319 | xasprintf(&sc_oid_test.output, "%sOID: %s", sc_oid_test.output, oid_string); | ||
320 | sc_oid_test = mp_set_subcheck_default_state(sc_oid_test, STATE_OK); | ||
321 | |||
322 | if (verbose > 2) { | ||
323 | printf("Processing oid %s\n", oid_string); | ||
324 | } | ||
325 | |||
326 | bool got_a_numerical_value = false; | ||
327 | mp_perfdata_value pd_result_val = {0}; | ||
328 | |||
329 | check_snmp_state_entry result_state = { | ||
330 | .timestamp = query_timestamp, | ||
331 | .oid_length = response.oid_length, | ||
332 | .type = response.type, | ||
333 | }; | ||
334 | |||
335 | for (size_t i = 0; i < response.oid_length; i++) { | ||
336 | result_state.oid[i] = response.oid[i]; | ||
337 | } | ||
338 | |||
339 | if (have_previous_state) { | ||
340 | if (query_timestamp == prev_state.timestamp) { | ||
341 | // somehow we have the same timestamp again, that can't be good | ||
342 | sc_oid_test = mp_set_subcheck_state(sc_oid_test, STATE_UNKNOWN); | ||
343 | xasprintf(&sc_oid_test.output, "Time duration between plugin calls is invalid"); | ||
344 | |||
345 | check_snmp_evaluation result = { | ||
346 | .sc = sc_oid_test, | ||
347 | .state = result_state, | ||
348 | }; | ||
349 | |||
350 | return result; | ||
351 | } | ||
352 | } | ||
353 | // compute rate time difference | ||
354 | double timeDiff = 0; | ||
355 | if (have_previous_state) { | ||
356 | if (verbose) { | ||
357 | printf("Previous timestamp: %s", ctime(&prev_state.timestamp)); | ||
358 | printf("Current timestamp: %s", ctime(&query_timestamp)); | ||
359 | } | ||
360 | timeDiff = difftime(query_timestamp, prev_state.timestamp) / eval_params.rate_multiplier; | ||
361 | } | ||
362 | |||
363 | mp_perfdata pd_num_val = {}; | ||
364 | |||
365 | switch (response.type) { | ||
366 | case ASN_OCTET_STR: { | ||
367 | char *tmp = response.string_response; | ||
368 | if (strchr(tmp, '"') != NULL) { | ||
369 | // got double quote in the string | ||
370 | if (strchr(tmp, '\'') != NULL) { | ||
371 | // got single quote in the string too | ||
372 | // dont quote that at all to avoid even more confusion | ||
373 | xasprintf(&sc_oid_test.output, "%s - Value: %s", sc_oid_test.output, tmp); | ||
374 | } else { | ||
375 | // quote with single quotes | ||
376 | xasprintf(&sc_oid_test.output, "%s - Value: '%s'", sc_oid_test.output, tmp); | ||
377 | } | ||
378 | } else { | ||
379 | // quote with double quotes | ||
380 | xasprintf(&sc_oid_test.output, "%s - Value: \"%s\"", sc_oid_test.output, tmp); | ||
381 | } | ||
382 | |||
383 | if (strlen(tmp) == 0) { | ||
384 | sc_oid_test = mp_set_subcheck_state(sc_oid_test, eval_params.nulloid_result); | ||
385 | } | ||
386 | |||
387 | // String matching test | ||
388 | if ((test_unit.eval_mthd.crit_string)) { | ||
389 | if (strcmp(tmp, eval_params.string_cmp_value)) { | ||
390 | sc_oid_test = mp_set_subcheck_state( | ||
391 | sc_oid_test, (eval_params.invert_search) ? STATE_CRITICAL : STATE_OK); | ||
392 | } else { | ||
393 | sc_oid_test = mp_set_subcheck_state( | ||
394 | sc_oid_test, (eval_params.invert_search) ? STATE_OK : STATE_CRITICAL); | ||
395 | } | ||
396 | } else if (test_unit.eval_mthd.crit_regex) { | ||
397 | const size_t nmatch = eval_params.regex_cmp_value.re_nsub + 1; | ||
398 | regmatch_t pmatch[nmatch]; | ||
399 | memset(pmatch, '\0', sizeof(regmatch_t) * nmatch); | ||
400 | |||
401 | int excode = regexec(&eval_params.regex_cmp_value, tmp, nmatch, pmatch, 0); | ||
402 | if (excode == 0) { | ||
403 | sc_oid_test = mp_set_subcheck_state( | ||
404 | sc_oid_test, (eval_params.invert_search) ? STATE_OK : STATE_CRITICAL); | ||
405 | } else if (excode != REG_NOMATCH) { | ||
406 | char errbuf[MAX_INPUT_BUFFER] = ""; | ||
407 | regerror(excode, &eval_params.regex_cmp_value, errbuf, MAX_INPUT_BUFFER); | ||
408 | printf(_("Execute Error: %s\n"), errbuf); | ||
409 | exit(STATE_CRITICAL); | ||
410 | } else { // REG_NOMATCH | ||
411 | sc_oid_test = mp_set_subcheck_state( | ||
412 | sc_oid_test, eval_params.invert_search ? STATE_CRITICAL : STATE_OK); | ||
413 | } | ||
414 | } | ||
415 | } break; | ||
416 | case ASN_COUNTER64: | ||
417 | got_a_numerical_value = true; | ||
418 | |||
419 | result_state.value.uIntVal = response.value.uIntVal; | ||
420 | result_state.type = response.type; | ||
421 | |||
422 | // TODO: perfdata unit counter | ||
423 | if (eval_params.calculate_rate && have_previous_state) { | ||
424 | if (prev_state.value.uIntVal > response.value.uIntVal) { | ||
425 | // overflow | ||
426 | unsigned long long tmp = | ||
427 | (UINT64_MAX - prev_state.value.uIntVal) + response.value.uIntVal; | ||
428 | |||
429 | tmp /= timeDiff; | ||
430 | pd_result_val = mp_create_pd_value(tmp); | ||
431 | } else { | ||
432 | pd_result_val = mp_create_pd_value( | ||
433 | (response.value.uIntVal - prev_state.value.uIntVal) / timeDiff); | ||
434 | } | ||
435 | } else { | ||
436 | // It's only a counter if we cont compute rate | ||
437 | pd_num_val.uom = "c"; | ||
438 | pd_result_val = mp_create_pd_value(response.value.uIntVal); | ||
439 | } | ||
440 | break; | ||
441 | case ASN_GAUGE: // same as ASN_UNSIGNED | ||
442 | case ASN_TIMETICKS: | ||
443 | case ASN_COUNTER: | ||
444 | case ASN_UINTEGER: { | ||
445 | got_a_numerical_value = true; | ||
446 | long long treated_value = (long long)response.value.uIntVal; | ||
447 | |||
448 | if (eval_params.multiplier_set || eval_params.offset_set) { | ||
449 | double processed = 0; | ||
450 | if (eval_params.offset_set) { | ||
451 | processed += eval_params.offset; | ||
452 | } | ||
453 | |||
454 | if (eval_params.multiplier_set) { | ||
455 | processed = processed * eval_params.multiplier; | ||
456 | } | ||
457 | |||
458 | treated_value = lround(processed); | ||
459 | } | ||
460 | |||
461 | result_state.value.intVal = treated_value; | ||
462 | |||
463 | if (eval_params.calculate_rate && have_previous_state) { | ||
464 | if (verbose > 2) { | ||
465 | printf("%s: Rate calculation (int/counter/gauge): prev: %lli\n", __FUNCTION__, | ||
466 | prev_state.value.intVal); | ||
467 | printf("%s: Rate calculation (int/counter/gauge): current: %lli\n", __FUNCTION__, | ||
468 | treated_value); | ||
469 | } | ||
470 | double rate = (treated_value - prev_state.value.intVal) / timeDiff; | ||
471 | pd_result_val = mp_create_pd_value(rate); | ||
472 | } else { | ||
473 | pd_result_val = mp_create_pd_value(treated_value); | ||
474 | |||
475 | if (response.type == ASN_COUNTER) { | ||
476 | pd_num_val.uom = "c"; | ||
477 | } | ||
478 | } | ||
479 | |||
480 | } break; | ||
481 | case ASN_INTEGER: { | ||
482 | if (eval_params.multiplier_set || eval_params.offset_set) { | ||
483 | double processed = 0; | ||
484 | if (eval_params.multiplier_set) { | ||
485 | processed = (double)response.value.intVal * eval_params.multiplier; | ||
486 | } | ||
487 | |||
488 | if (eval_params.offset_set) { | ||
489 | processed += eval_params.offset; | ||
490 | } | ||
491 | |||
492 | result_state.value.doubleVal = processed; | ||
493 | |||
494 | if (eval_params.calculate_rate && have_previous_state) { | ||
495 | pd_result_val = | ||
496 | mp_create_pd_value((processed - prev_state.value.doubleVal) / timeDiff); | ||
497 | } else { | ||
498 | pd_result_val = mp_create_pd_value(processed); | ||
499 | } | ||
500 | } else { | ||
501 | result_state.value.intVal = response.value.intVal; | ||
502 | |||
503 | if (eval_params.calculate_rate && have_previous_state) { | ||
504 | pd_result_val = mp_create_pd_value( | ||
505 | (response.value.intVal - prev_state.value.intVal) / timeDiff); | ||
506 | } else { | ||
507 | pd_result_val = mp_create_pd_value(response.value.intVal); | ||
508 | } | ||
509 | } | ||
510 | |||
511 | got_a_numerical_value = true; | ||
512 | } break; | ||
513 | case ASN_FLOAT: // fallthrough | ||
514 | case ASN_DOUBLE: { | ||
515 | got_a_numerical_value = true; | ||
516 | double tmp = response.value.doubleVal; | ||
517 | if (eval_params.offset_set) { | ||
518 | tmp += eval_params.offset; | ||
519 | } | ||
520 | |||
521 | if (eval_params.multiplier_set) { | ||
522 | tmp *= eval_params.multiplier; | ||
523 | } | ||
524 | |||
525 | if (eval_params.calculate_rate && have_previous_state) { | ||
526 | pd_result_val = mp_create_pd_value((tmp - prev_state.value.doubleVal) / timeDiff); | ||
527 | } else { | ||
528 | pd_result_val = mp_create_pd_value(tmp); | ||
529 | } | ||
530 | got_a_numerical_value = true; | ||
531 | |||
532 | result_state.value.doubleVal = tmp; | ||
533 | } break; | ||
534 | case ASN_IPADDRESS: | ||
535 | // TODO | ||
536 | break; | ||
537 | } | ||
538 | |||
539 | if (got_a_numerical_value) { | ||
540 | if (eval_params.use_oid_as_perf_data_label) { | ||
541 | // Use oid for perdata label | ||
542 | pd_num_val.label = strdup(oid_string); | ||
543 | // TODO strdup error checking | ||
544 | } else if (test_unit.label != NULL && strcmp(test_unit.label, "") != 0) { | ||
545 | pd_num_val.label = strdup(test_unit.label); | ||
546 | } else { | ||
547 | pd_num_val.label = strdup(test_unit.oid); | ||
548 | } | ||
549 | |||
550 | if (!(eval_params.calculate_rate && !have_previous_state)) { | ||
551 | // some kind of numerical value | ||
552 | if (test_unit.unit_value != NULL && strcmp(test_unit.unit_value, "") != 0) { | ||
553 | pd_num_val.uom = test_unit.unit_value; | ||
554 | } | ||
555 | |||
556 | pd_num_val.value = pd_result_val; | ||
557 | |||
558 | xasprintf(&sc_oid_test.output, "%s Value: %s", sc_oid_test.output, | ||
559 | pd_value_to_string(pd_result_val)); | ||
560 | |||
561 | if (test_unit.unit_value != NULL && strcmp(test_unit.unit_value, "") != 0) { | ||
562 | xasprintf(&sc_oid_test.output, "%s%s", sc_oid_test.output, test_unit.unit_value); | ||
563 | } | ||
564 | |||
565 | if (test_unit.threshold.warning_is_set || test_unit.threshold.critical_is_set) { | ||
566 | pd_num_val = mp_pd_set_thresholds(pd_num_val, test_unit.threshold); | ||
567 | mp_state_enum tmp_state = mp_get_pd_status(pd_num_val); | ||
568 | |||
569 | if (tmp_state == STATE_WARNING) { | ||
570 | sc_oid_test = mp_set_subcheck_state(sc_oid_test, STATE_WARNING); | ||
571 | xasprintf(&sc_oid_test.output, "%s - number violates warning threshold", | ||
572 | sc_oid_test.output); | ||
573 | } else if (tmp_state == STATE_CRITICAL) { | ||
574 | sc_oid_test = mp_set_subcheck_state(sc_oid_test, STATE_CRITICAL); | ||
575 | xasprintf(&sc_oid_test.output, "%s - number violates critical threshold", | ||
576 | sc_oid_test.output); | ||
577 | } | ||
578 | } | ||
579 | |||
580 | mp_add_perfdata_to_subcheck(&sc_oid_test, pd_num_val); | ||
581 | } else { | ||
582 | // should calculate rate, but there is no previous state, so first run | ||
583 | // exit with ok now | ||
584 | sc_oid_test = mp_set_subcheck_state(sc_oid_test, STATE_OK); | ||
585 | xasprintf(&sc_oid_test.output, "%s - No previous data to calculate rate - assume okay", | ||
586 | sc_oid_test.output); | ||
587 | } | ||
588 | } | ||
589 | |||
590 | check_snmp_evaluation result = { | ||
591 | .sc = sc_oid_test, | ||
592 | .state = result_state, | ||
593 | }; | ||
594 | |||
595 | return result; | ||
596 | } | ||
597 | |||
598 | char *_np_state_generate_key(int argc, char **argv); | ||
599 | |||
600 | /* | ||
601 | * If time=NULL, use current time. Create state file, with state format | ||
602 | * version, default text. Writes version, time, and data. Avoid locking | ||
603 | * problems - use mv to write and then swap. Possible loss of state data if | ||
604 | * two things writing to same key at same time. | ||
605 | * Will die with UNKNOWN if errors | ||
606 | */ | ||
607 | void np_state_write_string(state_key stateKey, time_t timestamp, char *stringToStore) { | ||
608 | time_t current_time; | ||
609 | if (timestamp == 0) { | ||
610 | time(¤t_time); | ||
611 | } else { | ||
612 | current_time = timestamp; | ||
613 | } | ||
614 | |||
615 | int result = 0; | ||
616 | |||
617 | /* If file doesn't currently exist, create directories */ | ||
618 | if (access(stateKey._filename, F_OK) != 0) { | ||
619 | char *directories = NULL; | ||
620 | result = asprintf(&directories, "%s", stateKey._filename); | ||
621 | if (result < 0) { | ||
622 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
623 | } | ||
624 | |||
625 | for (char *p = directories + 1; *p; p++) { | ||
626 | if (*p == '/') { | ||
627 | *p = '\0'; | ||
628 | if ((access(directories, F_OK) != 0) && (mkdir(directories, S_IRWXU) != 0)) { | ||
629 | /* Can't free this! Otherwise error message is wrong! */ | ||
630 | /* np_free(directories); */ | ||
631 | die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories); | ||
632 | } | ||
633 | *p = '/'; | ||
634 | } | ||
635 | } | ||
636 | |||
637 | if (directories) { | ||
638 | free(directories); | ||
639 | } | ||
640 | } | ||
641 | |||
642 | char *temp_file = NULL; | ||
643 | result = asprintf(&temp_file, "%s.XXXXXX", stateKey._filename); | ||
644 | if (result < 0) { | ||
645 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
646 | } | ||
647 | |||
648 | int temp_file_desc = 0; | ||
649 | if ((temp_file_desc = mkstemp(temp_file)) == -1) { | ||
650 | if (temp_file) { | ||
651 | free(temp_file); | ||
652 | } | ||
653 | die(STATE_UNKNOWN, _("Cannot create temporary filename")); | ||
654 | } | ||
655 | |||
656 | FILE *temp_file_pointer = fdopen(temp_file_desc, "w"); | ||
657 | if (temp_file_pointer == NULL) { | ||
658 | close(temp_file_desc); | ||
659 | unlink(temp_file); | ||
660 | if (temp_file) { | ||
661 | free(temp_file); | ||
662 | } | ||
663 | die(STATE_UNKNOWN, _("Unable to open temporary state file")); | ||
664 | } | ||
665 | |||
666 | fprintf(temp_file_pointer, "# NP State file\n"); | ||
667 | fprintf(temp_file_pointer, "%d\n", NP_STATE_FORMAT_VERSION); | ||
668 | fprintf(temp_file_pointer, "%d\n", stateKey.data_version); | ||
669 | fprintf(temp_file_pointer, "%lu\n", current_time); | ||
670 | fprintf(temp_file_pointer, "%s\n", stringToStore); | ||
671 | |||
672 | fchmod(temp_file_desc, S_IRUSR | S_IWUSR | S_IRGRP); | ||
673 | |||
674 | fflush(temp_file_pointer); | ||
675 | |||
676 | result = fclose(temp_file_pointer); | ||
677 | |||
678 | fsync(temp_file_desc); | ||
679 | |||
680 | if (result != 0) { | ||
681 | unlink(temp_file); | ||
682 | if (temp_file) { | ||
683 | free(temp_file); | ||
684 | } | ||
685 | die(STATE_UNKNOWN, _("Error writing temp file")); | ||
686 | } | ||
687 | |||
688 | if (rename(temp_file, stateKey._filename) != 0) { | ||
689 | unlink(temp_file); | ||
690 | if (temp_file) { | ||
691 | free(temp_file); | ||
692 | } | ||
693 | die(STATE_UNKNOWN, _("Cannot rename state temp file")); | ||
694 | } | ||
695 | |||
696 | if (temp_file) { | ||
697 | free(temp_file); | ||
698 | } | ||
699 | } | ||
700 | |||
701 | /* | ||
702 | * Read the state file | ||
703 | */ | ||
704 | bool _np_state_read_file(FILE *state_file, state_key stateKey) { | ||
705 | time_t current_time; | ||
706 | time(¤t_time); | ||
707 | |||
708 | /* Note: This introduces a limit of 8192 bytes in the string data */ | ||
709 | char *line = (char *)calloc(1, 8192); | ||
710 | if (line == NULL) { | ||
711 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
712 | } | ||
713 | |||
714 | bool status = false; | ||
715 | enum { | ||
716 | STATE_FILE_VERSION, | ||
717 | STATE_DATA_VERSION, | ||
718 | STATE_DATA_TIME, | ||
719 | STATE_DATA_TEXT, | ||
720 | STATE_DATA_END | ||
721 | } expected = STATE_FILE_VERSION; | ||
722 | |||
723 | int failure = 0; | ||
724 | while (!failure && (fgets(line, 8192, state_file)) != NULL) { | ||
725 | size_t pos = strlen(line); | ||
726 | if (line[pos - 1] == '\n') { | ||
727 | line[pos - 1] = '\0'; | ||
728 | } | ||
729 | |||
730 | if (line[0] == '#') { | ||
731 | continue; | ||
732 | } | ||
733 | |||
734 | switch (expected) { | ||
735 | case STATE_FILE_VERSION: { | ||
736 | int i = atoi(line); | ||
737 | if (i != NP_STATE_FORMAT_VERSION) { | ||
738 | failure++; | ||
739 | } else { | ||
740 | expected = STATE_DATA_VERSION; | ||
741 | } | ||
742 | } break; | ||
743 | case STATE_DATA_VERSION: { | ||
744 | int i = atoi(line); | ||
745 | if (i != stateKey.data_version) { | ||
746 | failure++; | ||
747 | } else { | ||
748 | expected = STATE_DATA_TIME; | ||
749 | } | ||
750 | } break; | ||
751 | case STATE_DATA_TIME: { | ||
752 | /* If time > now, error */ | ||
753 | time_t data_time = strtoul(line, NULL, 10); | ||
754 | if (data_time > current_time) { | ||
755 | failure++; | ||
756 | } else { | ||
757 | stateKey.state_data->time = data_time; | ||
758 | expected = STATE_DATA_TEXT; | ||
759 | } | ||
760 | } break; | ||
761 | case STATE_DATA_TEXT: | ||
762 | stateKey.state_data->data = strdup(line); | ||
763 | if (stateKey.state_data->data == NULL) { | ||
764 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | ||
765 | } | ||
766 | stateKey.state_data->length = strlen(line); | ||
767 | expected = STATE_DATA_END; | ||
768 | status = true; | ||
769 | break; | ||
770 | case STATE_DATA_END:; | ||
771 | } | ||
772 | } | ||
773 | |||
774 | if (line) { | ||
775 | free(line); | ||
776 | } | ||
777 | return status; | ||
778 | } | ||
779 | /* | ||
780 | * Will return NULL if no data is available (first run). If key currently | ||
781 | * exists, read data. If state file format version is not expected, return | ||
782 | * as if no data. Get state data version number and compares to expected. | ||
783 | * If numerically lower, then return as no previous state. die with UNKNOWN | ||
784 | * if exceptional error. | ||
785 | */ | ||
786 | state_data *np_state_read(state_key stateKey) { | ||
787 | /* Open file. If this fails, no previous state found */ | ||
788 | FILE *statefile = fopen(stateKey._filename, "r"); | ||
789 | state_data *this_state_data = (state_data *)calloc(1, sizeof(state_data)); | ||
790 | if (statefile != NULL) { | ||
791 | |||
792 | if (this_state_data == NULL) { | ||
793 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
794 | } | ||
795 | |||
796 | this_state_data->data = NULL; | ||
797 | stateKey.state_data = this_state_data; | ||
798 | |||
799 | if (_np_state_read_file(statefile, stateKey)) { | ||
800 | this_state_data->errorcode = OK; | ||
801 | } else { | ||
802 | this_state_data->errorcode = ERROR; | ||
803 | } | ||
804 | |||
805 | fclose(statefile); | ||
806 | } else { | ||
807 | // Failed to open state file | ||
808 | this_state_data->errorcode = ERROR; | ||
809 | } | ||
810 | |||
811 | return stateKey.state_data; | ||
812 | } | ||
813 | |||
814 | /* | ||
815 | * Internal function. Returns either: | ||
816 | * envvar NAGIOS_PLUGIN_STATE_DIRECTORY | ||
817 | * statically compiled shared state directory | ||
818 | */ | ||
819 | char *_np_state_calculate_location_prefix(void) { | ||
820 | char *env_dir; | ||
821 | |||
822 | /* Do not allow passing MP_STATE_PATH in setuid plugins | ||
823 | * for security reasons */ | ||
824 | if (!mp_suid()) { | ||
825 | env_dir = getenv("MP_STATE_PATH"); | ||
826 | if (env_dir && env_dir[0] != '\0') { | ||
827 | return env_dir; | ||
828 | } | ||
829 | /* This is the former ENV, for backward-compatibility */ | ||
830 | env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); | ||
831 | if (env_dir && env_dir[0] != '\0') { | ||
832 | return env_dir; | ||
833 | } | ||
834 | } | ||
835 | |||
836 | return NP_STATE_DIR_PREFIX; | ||
837 | } | ||
838 | |||
839 | /* | ||
840 | * Initiatializer for state routines. | ||
841 | * Sets variables. Generates filename. Returns np_state_key. die with | ||
842 | * UNKNOWN if exception | ||
843 | */ | ||
844 | state_key np_enable_state(char *keyname, int expected_data_version, char *plugin_name, int argc, | ||
845 | char **argv) { | ||
846 | state_key *this_state = (state_key *)calloc(1, sizeof(state_key)); | ||
847 | if (this_state == NULL) { | ||
848 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
849 | } | ||
850 | |||
851 | char *temp_keyname = NULL; | ||
852 | if (keyname == NULL) { | ||
853 | temp_keyname = _np_state_generate_key(argc, argv); | ||
854 | } else { | ||
855 | temp_keyname = strdup(keyname); | ||
856 | if (temp_keyname == NULL) { | ||
857 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | ||
858 | } | ||
859 | } | ||
860 | |||
861 | /* Die if invalid characters used for keyname */ | ||
862 | char *tmp_char = temp_keyname; | ||
863 | while (*tmp_char != '\0') { | ||
864 | if (!(isalnum(*tmp_char) || *tmp_char == '_')) { | ||
865 | die(STATE_UNKNOWN, _("Invalid character for keyname - only alphanumerics or '_'")); | ||
866 | } | ||
867 | tmp_char++; | ||
868 | } | ||
869 | this_state->name = temp_keyname; | ||
870 | this_state->plugin_name = plugin_name; | ||
871 | this_state->data_version = expected_data_version; | ||
872 | this_state->state_data = NULL; | ||
873 | |||
874 | /* Calculate filename */ | ||
875 | char *temp_filename = NULL; | ||
876 | int error = asprintf(&temp_filename, "%s/%lu/%s/%s", _np_state_calculate_location_prefix(), | ||
877 | (unsigned long)geteuid(), plugin_name, this_state->name); | ||
878 | if (error < 0) { | ||
879 | die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), strerror(errno)); | ||
880 | } | ||
881 | |||
882 | this_state->_filename = temp_filename; | ||
883 | |||
884 | return *this_state; | ||
885 | } | ||
886 | |||
887 | /* | ||
888 | * Returns a string to use as a keyname, based on an md5 hash of argv, thus | ||
889 | * hopefully a unique key per service/plugin invocation. Use the extra-opts | ||
890 | * parse of argv, so that uniqueness in parameters are reflected there. | ||
891 | */ | ||
892 | char *_np_state_generate_key(int argc, char **argv) { | ||
893 | unsigned char result[256]; | ||
894 | |||
895 | #ifdef USE_OPENSSL | ||
896 | /* | ||
897 | * This code path is chosen if openssl is available (which should be the most common | ||
898 | * scenario). Alternatively, the gnulib implementation/ | ||
899 | * | ||
900 | */ | ||
901 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); | ||
902 | |||
903 | EVP_DigestInit(ctx, EVP_sha256()); | ||
904 | |||
905 | for (int i = 0; i < argc; i++) { | ||
906 | EVP_DigestUpdate(ctx, argv[i], strlen(argv[i])); | ||
907 | } | ||
908 | |||
909 | EVP_DigestFinal(ctx, result, NULL); | ||
910 | #else | ||
911 | |||
912 | struct sha256_ctx ctx; | ||
913 | |||
914 | for (int i = 0; i < this_monitoring_plugin->argc; i++) { | ||
915 | sha256_process_bytes(argv[i], strlen(argv[i]), &ctx); | ||
916 | } | ||
917 | |||
918 | sha256_finish_ctx(&ctx, result); | ||
919 | #endif // FOUNDOPENSSL | ||
920 | |||
921 | char keyname[41]; | ||
922 | for (int i = 0; i < 20; ++i) { | ||
923 | sprintf(&keyname[2 * i], "%02x", result[i]); | ||
924 | } | ||
925 | |||
926 | keyname[40] = '\0'; | ||
927 | |||
928 | char *keyname_copy = strdup(keyname); | ||
929 | if (keyname_copy == NULL) { | ||
930 | die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); | ||
931 | } | ||
932 | |||
933 | return keyname_copy; | ||
934 | } | ||