Skip to content

Commit

Permalink
Add file nebstructs_json.c which will contain all functions to encode…
Browse files Browse the repository at this point in the history
… nebstructs into json objects

Signed-off-by: nook24 <[email protected]>
  • Loading branch information
nook24 committed Feb 19, 2024
1 parent b0794ba commit 4cdd30c
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 88 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
80 changes: 80 additions & 0 deletions src/naemon/nebstructs_json.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <stdio.h>
#include <string.h>
#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 <json-c/json.h>
#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;
}

18 changes: 18 additions & 0 deletions src/naemon/nebstructs_json.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "nebstructs.h"
#ifdef HAVE_JSONC
#include <json-c/json.h>
#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);
93 changes: 14 additions & 79 deletions src/naemon/nerd_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,85 +9,20 @@
#include "logging.h"
#include "nerd.h"
#include "nerd_json.h"
#include "worker.h"
#ifdef HAVE_JSONC
#include <json-c/json.h>
#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;

Expand All @@ -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;
}
Expand Down
9 changes: 0 additions & 9 deletions src/naemon/nerd_json.h
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 4cdd30c

Please sign in to comment.