diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/output.c | 103 |
1 files changed, 102 insertions, 1 deletions
diff --git a/lib/output.c b/lib/output.c index 4c5041c8..2c537a01 100644 --- a/lib/output.c +++ b/lib/output.c | |||
| @@ -8,6 +8,8 @@ | |||
| 8 | #include <strings.h> | 8 | #include <strings.h> |
| 9 | // #include <cjson/cJSON.h> | 9 | // #include <cjson/cJSON.h> |
| 10 | #include "./vendor/cJSON/cJSON.h" | 10 | #include "./vendor/cJSON/cJSON.h" |
| 11 | #include "perfdata.h" | ||
| 12 | #include "states.h" | ||
| 11 | 13 | ||
| 12 | // == Prototypes == | 14 | // == Prototypes == |
| 13 | static char *fmt_subcheck_output(mp_output_format output_format, mp_subcheck check, unsigned int indentation); | 15 | static char *fmt_subcheck_output(mp_output_format output_format, mp_subcheck check, unsigned int indentation); |
| @@ -359,7 +361,7 @@ static inline char *fmt_subcheck_output(mp_output_format output_format, mp_subch | |||
| 359 | switch (output_format) { | 361 | switch (output_format) { |
| 360 | case MP_FORMAT_ICINGA_WEB_2: | 362 | case MP_FORMAT_ICINGA_WEB_2: |
| 361 | asprintf(&result, "%s\\_[%s] - %s", generate_indentation_string(indentation), state_text(mp_compute_subcheck_state(check)), | 363 | asprintf(&result, "%s\\_[%s] - %s", generate_indentation_string(indentation), state_text(mp_compute_subcheck_state(check)), |
| 362 | check.output); | 364 | check.output); |
| 363 | 365 | ||
| 364 | subchecks = check.subchecks; | 366 | subchecks = check.subchecks; |
| 365 | 367 | ||
| @@ -385,13 +387,112 @@ static inline char *fmt_subcheck_output(mp_output_format output_format, mp_subch | |||
| 385 | } | 387 | } |
| 386 | } | 388 | } |
| 387 | 389 | ||
| 390 | static inline cJSON *json_serialise_pd_value(mp_perfdata_value value) { | ||
| 391 | cJSON *result = cJSON_CreateObject(); | ||
| 392 | |||
| 393 | switch (value.type) { | ||
| 394 | case PD_TYPE_DOUBLE: | ||
| 395 | cJSON_AddStringToObject(result, "type", "double"); | ||
| 396 | break; | ||
| 397 | case PD_TYPE_INT: | ||
| 398 | cJSON_AddStringToObject(result, "type", "int"); | ||
| 399 | break; | ||
| 400 | case PD_TYPE_UINT: | ||
| 401 | cJSON_AddStringToObject(result, "type", "uint"); | ||
| 402 | break; | ||
| 403 | case PD_TYPE_NONE: | ||
| 404 | die(STATE_UNKNOWN, "Perfdata type was None in json_serialise_pd_value"); | ||
| 405 | } | ||
| 406 | cJSON_AddStringToObject(result, "value", pd_value_to_string(value)); | ||
| 407 | |||
| 408 | return result; | ||
| 409 | } | ||
| 410 | |||
| 411 | static inline cJSON *json_serialise_range(mp_range range) { | ||
| 412 | cJSON *result = cJSON_CreateObject(); | ||
| 413 | |||
| 414 | if (range.alert_on_inside_range) { | ||
| 415 | cJSON_AddBoolToObject(result, "alert_on_inside", true); | ||
| 416 | } else { | ||
| 417 | cJSON_AddBoolToObject(result, "alert_on_inside", false); | ||
| 418 | } | ||
| 419 | |||
| 420 | if (range.end_infinity) { | ||
| 421 | cJSON_AddStringToObject(result, "end", "inf"); | ||
| 422 | } else { | ||
| 423 | cJSON_AddItemToObject(result, "end", json_serialise_pd_value(range.end)); | ||
| 424 | } | ||
| 425 | |||
| 426 | if (range.start_infinity) { | ||
| 427 | cJSON_AddStringToObject(result, "start", "inf"); | ||
| 428 | } else { | ||
| 429 | cJSON_AddItemToObject(result, "start", json_serialise_pd_value(range.end)); | ||
| 430 | } | ||
| 431 | |||
| 432 | return result; | ||
| 433 | } | ||
| 434 | |||
| 435 | static inline cJSON *json_serialise_pd(mp_perfdata pd_val) { | ||
| 436 | cJSON *result = cJSON_CreateObject(); | ||
| 437 | |||
| 438 | // Label | ||
| 439 | cJSON_AddStringToObject(result, "label", pd_val.label); | ||
| 440 | |||
| 441 | // Value | ||
| 442 | cJSON_AddItemToObject(result, "value", json_serialise_pd_value(pd_val.value)); | ||
| 443 | |||
| 444 | // Uom | ||
| 445 | cJSON_AddStringToObject(result, "uom", pd_val.uom); | ||
| 446 | |||
| 447 | // Warn/Crit | ||
| 448 | if (pd_val.warn_present) { | ||
| 449 | cJSON *warn = json_serialise_range(pd_val.warn); | ||
| 450 | cJSON_AddItemToObject(result, "warn", warn); | ||
| 451 | } | ||
| 452 | if (pd_val.crit_present) { | ||
| 453 | cJSON *crit = json_serialise_range(pd_val.crit); | ||
| 454 | cJSON_AddItemToObject(result, "crit", crit); | ||
| 455 | } | ||
| 456 | |||
| 457 | if (pd_val.min_present) { | ||
| 458 | cJSON_AddItemToObject(result, "min", json_serialise_pd_value(pd_val.min)); | ||
| 459 | } | ||
| 460 | if (pd_val.max_present) { | ||
| 461 | cJSON_AddItemToObject(result, "max", json_serialise_pd_value(pd_val.max)); | ||
| 462 | } | ||
| 463 | |||
| 464 | return result; | ||
| 465 | } | ||
| 466 | |||
| 467 | static inline cJSON *json_serialise_pd_list(pd_list *list) { | ||
| 468 | cJSON *result = cJSON_CreateArray(); | ||
| 469 | |||
| 470 | do { | ||
| 471 | cJSON *pd_value = json_serialise_pd(list->data); | ||
| 472 | cJSON_AddItemToArray(result, pd_value); | ||
| 473 | list = list->next; | ||
| 474 | } while (list != NULL); | ||
| 475 | |||
| 476 | return result; | ||
| 477 | } | ||
| 478 | |||
| 388 | static inline cJSON *json_serialize_subcheck(mp_subcheck subcheck) { | 479 | static inline cJSON *json_serialize_subcheck(mp_subcheck subcheck) { |
| 389 | cJSON *result = cJSON_CreateObject(); | 480 | cJSON *result = cJSON_CreateObject(); |
| 481 | |||
| 482 | // Human readable output | ||
| 390 | cJSON *output = cJSON_CreateString(subcheck.output); | 483 | cJSON *output = cJSON_CreateString(subcheck.output); |
| 391 | cJSON_AddItemToObject(result, "output", output); | 484 | cJSON_AddItemToObject(result, "output", output); |
| 485 | |||
| 486 | // Test state (aka Exit Code) | ||
| 392 | cJSON *state = cJSON_CreateString(state_text(mp_compute_subcheck_state(subcheck))); | 487 | cJSON *state = cJSON_CreateString(state_text(mp_compute_subcheck_state(subcheck))); |
| 393 | cJSON_AddItemToObject(result, "state", state); | 488 | cJSON_AddItemToObject(result, "state", state); |
| 394 | 489 | ||
| 490 | // Perfdata | ||
| 491 | if (subcheck.perfdata != NULL) { | ||
| 492 | cJSON *perfdata = json_serialise_pd_list(subcheck.perfdata); | ||
| 493 | cJSON_AddItemToObject(result, "perfdata", perfdata); | ||
| 494 | } | ||
| 495 | |||
| 395 | if (subcheck.subchecks != NULL) { | 496 | if (subcheck.subchecks != NULL) { |
| 396 | cJSON *subchecks = cJSON_CreateArray(); | 497 | cJSON *subchecks = cJSON_CreateArray(); |
| 397 | 498 | ||
