diff options
Diffstat (limited to 'lib/output.c')
-rw-r--r-- | lib/output.c | 541 |
1 files changed, 541 insertions, 0 deletions
diff --git a/lib/output.c b/lib/output.c new file mode 100644 index 00000000..61fbf832 --- /dev/null +++ b/lib/output.c | |||
@@ -0,0 +1,541 @@ | |||
1 | #include "./output.h" | ||
2 | #include "./utils_base.h" | ||
3 | #include "../plugins/utils.h" | ||
4 | |||
5 | #include <assert.h> | ||
6 | #include <stdlib.h> | ||
7 | #include <string.h> | ||
8 | #include <strings.h> | ||
9 | // #include <cjson/cJSON.h> | ||
10 | #include "./vendor/cJSON/cJSON.h" | ||
11 | #include "perfdata.h" | ||
12 | #include "states.h" | ||
13 | |||
14 | // == Global variables | ||
15 | static mp_output_format output_format = MP_FORMAT_DEFAULT; | ||
16 | |||
17 | // == Prototypes == | ||
18 | static char *fmt_subcheck_output(mp_output_format output_format, mp_subcheck check, unsigned int indentation); | ||
19 | static inline cJSON *json_serialize_subcheck(mp_subcheck subcheck); | ||
20 | |||
21 | // == Implementation == | ||
22 | |||
23 | /* | ||
24 | * Generate output string for a mp_subcheck object | ||
25 | */ | ||
26 | static inline char *fmt_subcheck_perfdata(mp_subcheck check) { | ||
27 | char *result = strdup(""); | ||
28 | int added = 0; | ||
29 | |||
30 | if (check.perfdata != NULL) { | ||
31 | added = asprintf(&result, "%s", pd_list_to_string(*check.perfdata)); | ||
32 | } | ||
33 | |||
34 | if (check.subchecks == NULL) { | ||
35 | // No subchecks, return here | ||
36 | return result; | ||
37 | } | ||
38 | |||
39 | mp_subcheck_list *subchecks = check.subchecks; | ||
40 | |||
41 | while (subchecks != NULL) { | ||
42 | if (added > 0) { | ||
43 | added = asprintf(&result, "%s%s", result, fmt_subcheck_perfdata(subchecks->subcheck)); | ||
44 | } else { | ||
45 | // TODO free previous result here? | ||
46 | added = asprintf(&result, "%s", fmt_subcheck_perfdata(subchecks->subcheck)); | ||
47 | } | ||
48 | |||
49 | subchecks = subchecks->next; | ||
50 | } | ||
51 | |||
52 | return result; | ||
53 | } | ||
54 | |||
55 | /* | ||
56 | * Initialiser for a mp_check object. Always use this to get a new one! | ||
57 | * It sets useful defaults | ||
58 | */ | ||
59 | mp_check mp_check_init(void) { | ||
60 | mp_check check = {0}; | ||
61 | return check; | ||
62 | } | ||
63 | |||
64 | /* | ||
65 | * Initialiser for a mp_subcheck object. Always use this to get a new one! | ||
66 | * It sets useful defaults | ||
67 | */ | ||
68 | mp_subcheck mp_subcheck_init(void) { | ||
69 | mp_subcheck tmp = {0}; | ||
70 | tmp.default_state = STATE_UNKNOWN; // Default state is unknown | ||
71 | tmp.state_set_explicitly = false; | ||
72 | return tmp; | ||
73 | } | ||
74 | |||
75 | /* | ||
76 | * Add a subcheck to a (the one and only) check object | ||
77 | */ | ||
78 | int mp_add_subcheck_to_check(mp_check check[static 1], mp_subcheck subcheck) { | ||
79 | assert(subcheck.output != NULL); // There must be output in a subcheck | ||
80 | |||
81 | mp_subcheck_list *tmp = NULL; | ||
82 | |||
83 | if (check->subchecks == NULL) { | ||
84 | check->subchecks = (mp_subcheck_list *)calloc(1, sizeof(mp_subcheck_list)); | ||
85 | if (check->subchecks == NULL) { | ||
86 | die(STATE_UNKNOWN, "%s - %s #%d: %s", __FILE__, __func__, __LINE__, "malloc failed"); | ||
87 | } | ||
88 | |||
89 | check->subchecks->subcheck = subcheck; | ||
90 | check->subchecks->next = NULL; | ||
91 | } else { | ||
92 | // Search for the end | ||
93 | tmp = (mp_subcheck_list *)calloc(1, sizeof(mp_subcheck_list)); | ||
94 | if (tmp == NULL) { | ||
95 | die(STATE_UNKNOWN, "%s - %s #%d: %s", __FILE__, __func__, __LINE__, "malloc failed"); | ||
96 | } | ||
97 | |||
98 | tmp->subcheck = subcheck; | ||
99 | tmp->next = check->subchecks; | ||
100 | |||
101 | check->subchecks = tmp; | ||
102 | } | ||
103 | |||
104 | return 0; | ||
105 | } | ||
106 | |||
107 | /* | ||
108 | * Add a mp_perfdata data point to a mp_subcheck object | ||
109 | */ | ||
110 | void mp_add_perfdata_to_subcheck(mp_subcheck check[static 1], const mp_perfdata perfData) { | ||
111 | if (check->perfdata == NULL) { | ||
112 | check->perfdata = pd_list_init(); | ||
113 | } | ||
114 | pd_list_append(check->perfdata, perfData); | ||
115 | } | ||
116 | |||
117 | /* | ||
118 | * Add a mp_subcheck object to another one. The seconde mp_subcheck (argument) is the lower in the | ||
119 | * hierarchy | ||
120 | */ | ||
121 | int mp_add_subcheck_to_subcheck(mp_subcheck check[static 1], mp_subcheck subcheck) { | ||
122 | if (subcheck.output == NULL) { | ||
123 | die(STATE_UNKNOWN, "%s - %s #%d: %s", __FILE__, __func__, __LINE__, "Sub check output is NULL"); | ||
124 | } | ||
125 | |||
126 | mp_subcheck_list *tmp = NULL; | ||
127 | |||
128 | if (check->subchecks == NULL) { | ||
129 | check->subchecks = (mp_subcheck_list *)calloc(1, sizeof(mp_subcheck_list)); | ||
130 | if (check->subchecks == NULL) { | ||
131 | die(STATE_UNKNOWN, "%s - %s #%d: %s", __FILE__, __func__, __LINE__, "malloc failed"); | ||
132 | } | ||
133 | |||
134 | tmp = check->subchecks; | ||
135 | } else { | ||
136 | // Search for the end | ||
137 | tmp = check->subchecks; | ||
138 | |||
139 | while (tmp->next != NULL) { | ||
140 | tmp = tmp->next; | ||
141 | } | ||
142 | |||
143 | tmp->next = (mp_subcheck_list *)calloc(1, sizeof(mp_subcheck_list)); | ||
144 | if (tmp->next == NULL) { | ||
145 | die(STATE_UNKNOWN, "%s - %s #%d: %s", __FILE__, __func__, __LINE__, "malloc failed"); | ||
146 | } | ||
147 | |||
148 | tmp = tmp->next; | ||
149 | } | ||
150 | |||
151 | tmp->subcheck = subcheck; | ||
152 | |||
153 | return 0; | ||
154 | } | ||
155 | |||
156 | /* | ||
157 | * Add a manual summary to a mp_check object, effectively replacing | ||
158 | * the autogenerated one | ||
159 | */ | ||
160 | void mp_add_summary(mp_check check[static 1], char *summary) { check->summary = summary; } | ||
161 | |||
162 | /* | ||
163 | * Generate the summary string of a mp_check object based on it's subchecks | ||
164 | */ | ||
165 | char *get_subcheck_summary(mp_check check) { | ||
166 | mp_subcheck_list *subchecks = check.subchecks; | ||
167 | |||
168 | unsigned int ok = 0; | ||
169 | unsigned int warning = 0; | ||
170 | unsigned int critical = 0; | ||
171 | unsigned int unknown = 0; | ||
172 | while (subchecks != NULL) { | ||
173 | switch (subchecks->subcheck.state) { | ||
174 | case STATE_OK: | ||
175 | ok++; | ||
176 | break; | ||
177 | case STATE_WARNING: | ||
178 | warning++; | ||
179 | break; | ||
180 | case STATE_CRITICAL: | ||
181 | critical++; | ||
182 | break; | ||
183 | case STATE_UNKNOWN: | ||
184 | unknown++; | ||
185 | break; | ||
186 | default: | ||
187 | die(STATE_UNKNOWN, "Unknown state in get_subcheck_summary"); | ||
188 | } | ||
189 | subchecks = subchecks->next; | ||
190 | } | ||
191 | char *result = NULL; | ||
192 | asprintf(&result, "ok=%d, warning=%d, critical=%d, unknown=%d", ok, warning, critical, unknown); | ||
193 | return result; | ||
194 | } | ||
195 | |||
196 | /* | ||
197 | * Generate the result state of a mp_subcheck object based on it's own state and it's subchecks states | ||
198 | */ | ||
199 | mp_state_enum mp_compute_subcheck_state(const mp_subcheck check) { | ||
200 | if (check.state_set_explicitly) { | ||
201 | return check.state; | ||
202 | } | ||
203 | |||
204 | mp_subcheck_list *scl = check.subchecks; | ||
205 | mp_state_enum result = check.default_state; | ||
206 | |||
207 | while (scl != NULL) { | ||
208 | result = max_state_alt(result, mp_compute_subcheck_state(scl->subcheck)); | ||
209 | scl = scl->next; | ||
210 | } | ||
211 | |||
212 | return result; | ||
213 | } | ||
214 | |||
215 | /* | ||
216 | * Generate the result state of a mp_check object based on it's own state and it's subchecks states | ||
217 | */ | ||
218 | mp_state_enum mp_compute_check_state(const mp_check check) { | ||
219 | assert(check.subchecks != NULL); // a mp_check without subchecks is invalid, die here | ||
220 | |||
221 | mp_subcheck_list *scl = check.subchecks; | ||
222 | mp_state_enum result = STATE_OK; | ||
223 | |||
224 | while (scl != NULL) { | ||
225 | result = max_state_alt(result, mp_compute_subcheck_state(scl->subcheck)); | ||
226 | scl = scl->next; | ||
227 | } | ||
228 | |||
229 | return result; | ||
230 | } | ||
231 | |||
232 | /* | ||
233 | * Generate output string for a mp_check object | ||
234 | * Non static to be available for testing functions | ||
235 | */ | ||
236 | char *mp_fmt_output(mp_check check) { | ||
237 | char *result = NULL; | ||
238 | |||
239 | switch (output_format) { | ||
240 | case MP_FORMAT_MULTI_LINE: { | ||
241 | if (check.summary == NULL) { | ||
242 | check.summary = get_subcheck_summary(check); | ||
243 | } | ||
244 | |||
245 | asprintf(&result, "[%s] - %s", state_text(mp_compute_check_state(check)), check.summary); | ||
246 | |||
247 | mp_subcheck_list *subchecks = check.subchecks; | ||
248 | |||
249 | while (subchecks != NULL) { | ||
250 | asprintf(&result, "%s\n%s", result, fmt_subcheck_output(MP_FORMAT_MULTI_LINE, subchecks->subcheck, 1)); | ||
251 | subchecks = subchecks->next; | ||
252 | } | ||
253 | |||
254 | char *pd_string = NULL; | ||
255 | subchecks = check.subchecks; | ||
256 | |||
257 | while (subchecks != NULL) { | ||
258 | if (pd_string == NULL) { | ||
259 | asprintf(&pd_string, "%s", fmt_subcheck_perfdata(subchecks->subcheck)); | ||
260 | } else { | ||
261 | asprintf(&pd_string, "%s %s", pd_string, fmt_subcheck_perfdata(subchecks->subcheck)); | ||
262 | } | ||
263 | |||
264 | subchecks = subchecks->next; | ||
265 | } | ||
266 | |||
267 | if (pd_string != NULL && strlen(pd_string) > 0) { | ||
268 | asprintf(&result, "%s|%s", result, pd_string); | ||
269 | } | ||
270 | |||
271 | break; | ||
272 | } | ||
273 | case MP_FORMAT_TEST_JSON: { | ||
274 | cJSON *resultObject = cJSON_CreateObject(); | ||
275 | if (resultObject == NULL) { | ||
276 | die(STATE_UNKNOWN, "cJSON_CreateObject failed"); | ||
277 | } | ||
278 | |||
279 | cJSON *resultState = cJSON_CreateString(state_text(mp_compute_check_state(check))); | ||
280 | cJSON_AddItemToObject(resultObject, "state", resultState); | ||
281 | |||
282 | if (check.summary == NULL) { | ||
283 | check.summary = get_subcheck_summary(check); | ||
284 | } | ||
285 | |||
286 | cJSON *summary = cJSON_CreateString(check.summary); | ||
287 | cJSON_AddItemToObject(resultObject, "summary", summary); | ||
288 | |||
289 | if (check.subchecks != NULL) { | ||
290 | cJSON *subchecks = cJSON_CreateArray(); | ||
291 | |||
292 | mp_subcheck_list *sc = check.subchecks; | ||
293 | |||
294 | while (sc != NULL) { | ||
295 | cJSON *sc_json = json_serialize_subcheck(sc->subcheck); | ||
296 | cJSON_AddItemToArray(subchecks, sc_json); | ||
297 | sc = sc->next; | ||
298 | } | ||
299 | |||
300 | cJSON_AddItemToObject(resultObject, "checks", subchecks); | ||
301 | } | ||
302 | |||
303 | result = cJSON_PrintUnformatted(resultObject); | ||
304 | break; | ||
305 | } | ||
306 | default: | ||
307 | die(STATE_UNKNOWN, "Invalid format"); | ||
308 | } | ||
309 | |||
310 | return result; | ||
311 | } | ||
312 | |||
313 | /* | ||
314 | * Helper function to properly indent the output lines when using multiline | ||
315 | * formats | ||
316 | */ | ||
317 | static char *generate_indentation_string(unsigned int indentation) { | ||
318 | char *result = calloc(indentation + 1, sizeof(char)); | ||
319 | |||
320 | for (unsigned int i = 0; i < indentation; i++) { | ||
321 | result[i] = '\t'; | ||
322 | } | ||
323 | |||
324 | return result; | ||
325 | } | ||
326 | |||
327 | /* | ||
328 | * Helper function to generate the output string of mp_subcheck | ||
329 | */ | ||
330 | static inline char *fmt_subcheck_output(mp_output_format output_format, mp_subcheck check, unsigned int indentation) { | ||
331 | char *result = NULL; | ||
332 | mp_subcheck_list *subchecks = NULL; | ||
333 | |||
334 | switch (output_format) { | ||
335 | case MP_FORMAT_MULTI_LINE: | ||
336 | asprintf(&result, "%s\\_[%s] - %s", generate_indentation_string(indentation), state_text(mp_compute_subcheck_state(check)), | ||
337 | check.output); | ||
338 | |||
339 | subchecks = check.subchecks; | ||
340 | |||
341 | while (subchecks != NULL) { | ||
342 | asprintf(&result, "%s\n%s", result, fmt_subcheck_output(output_format, subchecks->subcheck, indentation + 1)); | ||
343 | subchecks = subchecks->next; | ||
344 | } | ||
345 | return result; | ||
346 | default: | ||
347 | die(STATE_UNKNOWN, "Invalid format"); | ||
348 | } | ||
349 | } | ||
350 | |||
351 | static inline cJSON *json_serialise_pd_value(mp_perfdata_value value) { | ||
352 | cJSON *result = cJSON_CreateObject(); | ||
353 | |||
354 | switch (value.type) { | ||
355 | case PD_TYPE_DOUBLE: | ||
356 | cJSON_AddStringToObject(result, "type", "double"); | ||
357 | break; | ||
358 | case PD_TYPE_INT: | ||
359 | cJSON_AddStringToObject(result, "type", "int"); | ||
360 | break; | ||
361 | case PD_TYPE_UINT: | ||
362 | cJSON_AddStringToObject(result, "type", "uint"); | ||
363 | break; | ||
364 | case PD_TYPE_NONE: | ||
365 | die(STATE_UNKNOWN, "Perfdata type was None in json_serialise_pd_value"); | ||
366 | } | ||
367 | cJSON_AddStringToObject(result, "value", pd_value_to_string(value)); | ||
368 | |||
369 | return result; | ||
370 | } | ||
371 | |||
372 | static inline cJSON *json_serialise_range(mp_range range) { | ||
373 | cJSON *result = cJSON_CreateObject(); | ||
374 | |||
375 | if (range.alert_on_inside_range) { | ||
376 | cJSON_AddBoolToObject(result, "alert_on_inside", true); | ||
377 | } else { | ||
378 | cJSON_AddBoolToObject(result, "alert_on_inside", false); | ||
379 | } | ||
380 | |||
381 | if (range.end_infinity) { | ||
382 | cJSON_AddStringToObject(result, "end", "inf"); | ||
383 | } else { | ||
384 | cJSON_AddItemToObject(result, "end", json_serialise_pd_value(range.end)); | ||
385 | } | ||
386 | |||
387 | if (range.start_infinity) { | ||
388 | cJSON_AddStringToObject(result, "start", "inf"); | ||
389 | } else { | ||
390 | cJSON_AddItemToObject(result, "start", json_serialise_pd_value(range.end)); | ||
391 | } | ||
392 | |||
393 | return result; | ||
394 | } | ||
395 | |||
396 | static inline cJSON *json_serialise_pd(mp_perfdata pd_val) { | ||
397 | cJSON *result = cJSON_CreateObject(); | ||
398 | |||
399 | // Label | ||
400 | cJSON_AddStringToObject(result, "label", pd_val.label); | ||
401 | |||
402 | // Value | ||
403 | cJSON_AddItemToObject(result, "value", json_serialise_pd_value(pd_val.value)); | ||
404 | |||
405 | // Uom | ||
406 | cJSON_AddStringToObject(result, "uom", pd_val.uom); | ||
407 | |||
408 | // Warn/Crit | ||
409 | if (pd_val.warn_present) { | ||
410 | cJSON *warn = json_serialise_range(pd_val.warn); | ||
411 | cJSON_AddItemToObject(result, "warn", warn); | ||
412 | } | ||
413 | if (pd_val.crit_present) { | ||
414 | cJSON *crit = json_serialise_range(pd_val.crit); | ||
415 | cJSON_AddItemToObject(result, "crit", crit); | ||
416 | } | ||
417 | |||
418 | if (pd_val.min_present) { | ||
419 | cJSON_AddItemToObject(result, "min", json_serialise_pd_value(pd_val.min)); | ||
420 | } | ||
421 | if (pd_val.max_present) { | ||
422 | cJSON_AddItemToObject(result, "max", json_serialise_pd_value(pd_val.max)); | ||
423 | } | ||
424 | |||
425 | return result; | ||
426 | } | ||
427 | |||
428 | static inline cJSON *json_serialise_pd_list(pd_list *list) { | ||
429 | cJSON *result = cJSON_CreateArray(); | ||
430 | |||
431 | do { | ||
432 | cJSON *pd_value = json_serialise_pd(list->data); | ||
433 | cJSON_AddItemToArray(result, pd_value); | ||
434 | list = list->next; | ||
435 | } while (list != NULL); | ||
436 | |||
437 | return result; | ||
438 | } | ||
439 | |||
440 | static inline cJSON *json_serialize_subcheck(mp_subcheck subcheck) { | ||
441 | cJSON *result = cJSON_CreateObject(); | ||
442 | |||
443 | // Human readable output | ||
444 | cJSON *output = cJSON_CreateString(subcheck.output); | ||
445 | cJSON_AddItemToObject(result, "output", output); | ||
446 | |||
447 | // Test state (aka Exit Code) | ||
448 | cJSON *state = cJSON_CreateString(state_text(mp_compute_subcheck_state(subcheck))); | ||
449 | cJSON_AddItemToObject(result, "state", state); | ||
450 | |||
451 | // Perfdata | ||
452 | if (subcheck.perfdata != NULL) { | ||
453 | cJSON *perfdata = json_serialise_pd_list(subcheck.perfdata); | ||
454 | cJSON_AddItemToObject(result, "perfdata", perfdata); | ||
455 | } | ||
456 | |||
457 | if (subcheck.subchecks != NULL) { | ||
458 | cJSON *subchecks = cJSON_CreateArray(); | ||
459 | |||
460 | mp_subcheck_list *sc = subcheck.subchecks; | ||
461 | |||
462 | while (sc != NULL) { | ||
463 | cJSON *sc_json = json_serialize_subcheck(sc->subcheck); | ||
464 | cJSON_AddItemToArray(subchecks, sc_json); | ||
465 | sc = sc->next; | ||
466 | } | ||
467 | |||
468 | cJSON_AddItemToObject(result, "checks", subchecks); | ||
469 | } | ||
470 | |||
471 | return result; | ||
472 | } | ||
473 | |||
474 | /* | ||
475 | * Wrapper function to print the output string of a mp_check object | ||
476 | * Use this in concrete plugins. | ||
477 | */ | ||
478 | void mp_print_output(mp_check check) { puts(mp_fmt_output(check)); } | ||
479 | |||
480 | /* | ||
481 | * Convenience function to print the output string of a mp_check object and exit | ||
482 | * the program with the resulting state. | ||
483 | * Intended to be used to exit a monitoring plugin. | ||
484 | */ | ||
485 | void mp_exit(mp_check check) { | ||
486 | mp_print_output(check); | ||
487 | if (output_format == MP_FORMAT_TEST_JSON) { | ||
488 | exit(0); | ||
489 | } | ||
490 | |||
491 | exit(mp_compute_check_state(check)); | ||
492 | } | ||
493 | |||
494 | /* | ||
495 | * Function to set the result state of a mp_subcheck object explicitly. | ||
496 | * This will overwrite the default state AND states derived from it's subchecks | ||
497 | */ | ||
498 | mp_subcheck mp_set_subcheck_state(mp_subcheck check, mp_state_enum state) { | ||
499 | check.state = state; | ||
500 | check.state_set_explicitly = true; | ||
501 | return check; | ||
502 | } | ||
503 | |||
504 | /* | ||
505 | * Function to set the default result state of a mp_subcheck object. This state | ||
506 | * will be used if neither an explicit state is set (see *mp_set_subcheck_state*) | ||
507 | * nor does it include other subchecks | ||
508 | */ | ||
509 | mp_subcheck mp_set_subcheck_default_state(mp_subcheck check, mp_state_enum state) { | ||
510 | check.default_state = state; | ||
511 | return check; | ||
512 | } | ||
513 | |||
514 | char *mp_output_format_map[] = { | ||
515 | [MP_FORMAT_MULTI_LINE] = "multi-line", | ||
516 | [MP_FORMAT_TEST_JSON] = "mp-test-json", | ||
517 | }; | ||
518 | |||
519 | /* | ||
520 | * Function to parse the output from a string | ||
521 | */ | ||
522 | parsed_output_format mp_parse_output_format(char *format_string) { | ||
523 | parsed_output_format result = { | ||
524 | .parsing_success = false, | ||
525 | .output_format = MP_FORMAT_DEFAULT, | ||
526 | }; | ||
527 | |||
528 | for (mp_output_format i = 0; i < (sizeof(mp_output_format_map) / sizeof(char *)); i++) { | ||
529 | if (strcasecmp(mp_output_format_map[i], format_string) == 0) { | ||
530 | result.parsing_success = true; | ||
531 | result.output_format = i; | ||
532 | break; | ||
533 | } | ||
534 | } | ||
535 | |||
536 | return result; | ||
537 | } | ||
538 | |||
539 | void mp_set_format(mp_output_format format) { output_format = format; } | ||
540 | |||
541 | mp_output_format mp_get_format(void) { return output_format; } | ||