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