From 4cdd30c7c00c75e4a7a3f81a83afcec8826c2fc6 Mon Sep 17 00:00:00 2001 From: nook24 Date: Mon, 19 Feb 2024 17:57:55 +0100 Subject: [PATCH] Add file nebstructs_json.c which will contain all functions to encode nebstructs into json objects Signed-off-by: nook24 --- Makefile.am | 1 + src/naemon/nebstructs_json.c | 80 +++++++++++++++++++++++++++++++ src/naemon/nebstructs_json.h | 18 +++++++ src/naemon/nerd_json.c | 93 ++++++------------------------------ src/naemon/nerd_json.h | 9 ---- 5 files changed, 113 insertions(+), 88 deletions(-) create mode 100644 src/naemon/nebstructs_json.c create mode 100644 src/naemon/nebstructs_json.h diff --git a/Makefile.am b/Makefile.am index 7f912f63..804d0a7f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -97,6 +97,7 @@ common_sources = \ src/naemon/nebcallbacks.h src/naemon/neberrors.h \ src/naemon/nebmods.c src/naemon/nebmods.h \ src/naemon/nebmodules.h src/naemon/nebstructs.h \ + src/naemon/nebstructs_json.c src/naemon/nebstructs_json.h \ src/naemon/nerd.c src/naemon/nerd.h \ src/naemon/nerd_json.c src/naemon/nerd_json.h \ src/naemon/nm_alloc.c src/naemon/nm_alloc.h \ diff --git a/src/naemon/nebstructs_json.c b/src/naemon/nebstructs_json.c new file mode 100644 index 00000000..de773262 --- /dev/null +++ b/src/naemon/nebstructs_json.c @@ -0,0 +1,80 @@ +#include +#include +#include "config.h" +#include "lib/libnaemon.h" +#include "broker.h" +#include "nebmods.h" +#include "nebmodules.h" +#include "nebstructs.h" +#include "nebstructs_json.h" + +#ifdef HAVE_JSONC +#include +#endif + +/* + * Convert nebstruct data into json_objects + * + * This file provides functions that can convert nebstruct data into + * json oecjts. To goal of this is to help developers to faster create + * Event Broker Modules. + * + * Most of this code is stolen (with permission) from the + * Statusengine Event Broker, which is also a good referenfe if you want to + * add missing functions + * https://github.com/statusengine/module/blob/master/src/statusengine.c + */ + + +// This function converts host_check_data into a json_object +// Remember to call json_object_put() an pass the json_object to free up the memory +json_object *nebstruct_encode_host_check_as_json(nebstruct_host_check_data *ds) { + json_object *my_object; + json_object *hostcheck_object; + nebstruct_host_check_data *current_hostcheck; + + char *raw_command; + host *current_host; + + my_object = json_object_new_object(); + hostcheck_object = json_object_new_object(); + + json_object_object_add(my_object, "type", json_object_new_int(ds->type)); + json_object_object_add(my_object, "flags", json_object_new_int(ds->flags)); + json_object_object_add(my_object, "attr", json_object_new_int(ds->attr)); + json_object_object_add(my_object, "timestamp", json_object_new_int(ds->timestamp.tv_sec)); + + current_host = (host *)ds->object_ptr; + current_hostcheck = ds; + + HOSTCHECKFIELD_STRING(host_name); + + raw_command = NULL; + get_raw_command_line_r(get_global_macros(), current_host->check_command_ptr, current_host->check_command, &raw_command, 0); + + json_object_object_add(hostcheck_object, "command_line", (raw_command != NULL ? json_object_new_string(raw_command) : NULL)); + json_object_object_add(hostcheck_object, "command_name", (current_host->check_command != NULL ? json_object_new_string(current_host->check_command) : NULL)); + + HOSTCHECKFIELD_STRING(output); + HOSTCHECKFIELD_STRING(long_output); + HOSTCHECKFIELD_STRING(perf_data); + HOSTCHECKFIELD_INT(check_type); + HOSTCHECKFIELD_INT(current_attempt); + HOSTCHECKFIELD_INT(max_attempts); + HOSTCHECKFIELD_INT(state_type); + HOSTCHECKFIELD_INT(state); + HOSTCHECKFIELD_INT(timeout); + json_object_object_add(hostcheck_object, "start_time", json_object_new_int64(ds->start_time.tv_sec)); + json_object_object_add(hostcheck_object, "end_time", json_object_new_int64(ds->end_time.tv_sec)); + HOSTCHECKFIELD_INT(early_timeout); + HOSTCHECKFIELD_DOUBLE(execution_time); + HOSTCHECKFIELD_DOUBLE(latency); + HOSTCHECKFIELD_INT(return_code); + + json_object_object_add(my_object, "hostcheck", hostcheck_object); + + free(raw_command); + + return my_object; +} + diff --git a/src/naemon/nebstructs_json.h b/src/naemon/nebstructs_json.h new file mode 100644 index 00000000..1f16ded5 --- /dev/null +++ b/src/naemon/nebstructs_json.h @@ -0,0 +1,18 @@ +#include "nebstructs.h" +#ifdef HAVE_JSONC +#include +#else +#error "Requires that the json-c library is loaded" +#endif + +#define HOSTCHECKFIELD_STRING(FIELD) \ + json_object_object_add(hostcheck_object, #FIELD, (current_hostcheck->FIELD != NULL ? json_object_new_string(current_hostcheck->FIELD) : NULL)) + +#define HOSTCHECKFIELD_INT(FIELD) \ + json_object_object_add(hostcheck_object, #FIELD, json_object_new_int64(current_hostcheck->FIELD)) + +#define HOSTCHECKFIELD_DOUBLE(FIELD) \ + json_object_object_add(hostcheck_object, #FIELD, json_object_new_double(current_hostcheck->FIELD)) + + +json_object *nebstruct_encode_host_check_as_json(nebstruct_host_check_data *ds); diff --git a/src/naemon/nerd_json.c b/src/naemon/nerd_json.c index a5ab4a54..12ea2950 100644 --- a/src/naemon/nerd_json.c +++ b/src/naemon/nerd_json.c @@ -9,85 +9,20 @@ #include "logging.h" #include "nerd.h" #include "nerd_json.h" +#include "worker.h" #ifdef HAVE_JSONC #include +#include "nebstructs_json.h" #endif static unsigned int chan_host_checks_json_id; - -json_object *encode_host_check_as_json(nebstruct_host_check_data *ds) { - json_object *my_object; - json_object *hostcheck_object; - nebstruct_host_check_data *current_hostcheck; - - char *raw_command; - host *current_host; - - my_object = json_object_new_object(); - hostcheck_object = json_object_new_object(); - - json_object_object_add(my_object, "type", json_object_new_int(ds->type)); - json_object_object_add(my_object, "flags", json_object_new_int(ds->flags)); - json_object_object_add(my_object, "attr", json_object_new_int(ds->attr)); - json_object_object_add(my_object, "timestamp", json_object_new_int(ds->timestamp.tv_sec)); - - current_host = (host *)ds->object_ptr; - current_hostcheck = ds; - - HOSTCHECKFIELD_STRING(host_name); - - raw_command = NULL; - get_raw_command_line_r(get_global_macros(), current_host->check_command_ptr, current_host->check_command, &raw_command, 0); - - json_object_object_add(hostcheck_object, "command_line", (raw_command != NULL ? json_object_new_string(raw_command) : NULL)); - json_object_object_add(hostcheck_object, "command_name", (current_host->check_command != NULL ? json_object_new_string(current_host->check_command) : NULL)); - - HOSTCHECKFIELD_STRING(output); - HOSTCHECKFIELD_STRING(long_output); - HOSTCHECKFIELD_STRING(perf_data); - HOSTCHECKFIELD_INT(check_type); - HOSTCHECKFIELD_INT(current_attempt); - HOSTCHECKFIELD_INT(max_attempts); - HOSTCHECKFIELD_INT(state_type); - HOSTCHECKFIELD_INT(state); - HOSTCHECKFIELD_INT(timeout); - json_object_object_add(hostcheck_object, "start_time", json_object_new_int64(ds->start_time.tv_sec)); - json_object_object_add(hostcheck_object, "end_time", json_object_new_int64(ds->end_time.tv_sec)); - HOSTCHECKFIELD_INT(early_timeout); - HOSTCHECKFIELD_DOUBLE(execution_time); - HOSTCHECKFIELD_DOUBLE(latency); - HOSTCHECKFIELD_INT(return_code); - - json_object_object_add(my_object, "hostcheck", hostcheck_object); - - free(raw_command); - - return my_object; -} - -char *append_null_terminator(const char *input_string) { - // Calculate the length of the input string - size_t input_length = strlen(input_string); - - // Allocate memory for a new string that includes space for the null terminator - char *new_string = (char *)malloc(input_length + 1); - - if (new_string != NULL) { - // Copy the input string to the new block of memory - strcpy(new_string, input_string); - - // Append the null terminator - new_string[input_length] = '\0'; - } - - return new_string; -} +//static unsigned int chan_service_checks_json_id; static int chan_host_checks_json(int cb, void *data) { - json_object *my_object; + json_object *hostcheck_object; const char *json_string; - char *json_string_null; + char *json_string_dub; nebstruct_host_check_data *ds = (nebstruct_host_check_data *)data; @@ -98,21 +33,21 @@ static int chan_host_checks_json(int cb, void *data) return 0; // Encode the current host check event as JSON object - my_object = encode_host_check_as_json(ds); + hostcheck_object = nebstruct_encode_host_check_as_json(ds); // Convert the JSON object to a string - json_string = json_object_to_json_string(my_object); + json_string = json_object_to_json_string(hostcheck_object); - // Append null null terminator to the json_string - json_string_null = append_null_terminator(json_string); - - // Send JSON through the query handler socket - nerd_broadcast(chan_host_checks_json_id, (void *)json_string_null, strlen(json_string_null)); + // Duplicate so we can free everything + json_string_dub = nm_strdup(json_string); // Release resources - json_object_put(my_object); + json_object_put(hostcheck_object); + + // Send JSON through the query handler socket strlen()+1 to include the null terminator + nerd_broadcast(chan_host_checks_json_id, (void *)json_string_dub, strlen(json_string_dub)+1); - free(json_string_null); + free(json_string_dub); return 0; } diff --git a/src/naemon/nerd_json.h b/src/naemon/nerd_json.h index 1ee7c458..794a2fae 100644 --- a/src/naemon/nerd_json.h +++ b/src/naemon/nerd_json.h @@ -1,13 +1,4 @@ -#define HOSTCHECKFIELD_STRING(FIELD) \ - json_object_object_add(hostcheck_object, #FIELD, (current_hostcheck->FIELD != NULL ? json_object_new_string(current_hostcheck->FIELD) : NULL)) - -#define HOSTCHECKFIELD_INT(FIELD) \ - json_object_object_add(hostcheck_object, #FIELD, json_object_new_int64(current_hostcheck->FIELD)) - -#define HOSTCHECKFIELD_DOUBLE(FIELD) \ - json_object_object_add(hostcheck_object, #FIELD, json_object_new_double(current_hostcheck->FIELD)) - /*** Naemon Event Radio Dispatcher JSON functions ***/ int nerd_init_json(void);