Skip to content

Commit

Permalink
network.c: allow notification forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
carlospeon committed Dec 21, 2023
1 parent 3a282cc commit fcc531b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 43 deletions.
12 changes: 12 additions & 0 deletions src/daemon/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2637,6 +2637,18 @@ int plugin_notification_meta_free(notification_meta_t *n) {
return 0;
} /* int plugin_notification_meta_free */

int plugin_notification_meta_get_boolean(notification_meta_t *n,
const char *name, bool *value) {
for (notification_meta_t *meta = n; meta != NULL; meta = meta->next) {
if (meta->type == NM_TYPE_BOOLEAN &&
strncmp(name, meta->name, strlen(name)) == 0) {
*value = meta->nm_value.nm_boolean;
return 0;
}
}
return -ENOENT;
} /* int plugin_notification_meta_get_boolean */

static void plugin_ctx_destructor(void *ctx) {
sfree(ctx);
} /* void plugin_ctx_destructor */
Expand Down
15 changes: 8 additions & 7 deletions src/daemon/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@
#define DS_TYPE_ABSOLUTE 3

#define DS_TYPE_TO_STRING(t) \
(t == DS_TYPE_COUNTER) \
? "counter" \
: (t == DS_TYPE_GAUGE) \
? "gauge" \
: (t == DS_TYPE_DERIVE) \
? "derive" \
: (t == DS_TYPE_ABSOLUTE) ? "absolute" : "unknown"
(t == DS_TYPE_COUNTER) ? "counter" \
: (t == DS_TYPE_GAUGE) ? "gauge" \
: (t == DS_TYPE_DERIVE) ? "derive" \
: (t == DS_TYPE_ABSOLUTE) ? "absolute" \
: "unknown"

#ifndef LOG_ERR
#define LOG_ERR 3
Expand Down Expand Up @@ -449,6 +447,9 @@ int plugin_notification_meta_copy(notification_t *dst,

int plugin_notification_meta_free(notification_meta_t *n);

int plugin_notification_meta_get_boolean(notification_meta_t *n,
const char *name, bool *value);

/*
* Plugin context management.
*/
Expand Down
69 changes: 33 additions & 36 deletions src/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
* Aman Gupta <aman at tmm1.net>
**/

#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
#define _BSD_SOURCE /* For struct ip_mreq */

#include "collectd.h"
Expand Down Expand Up @@ -358,41 +360,6 @@ static bool check_send_okay(const value_list_t *vl) /* {{{ */
return !received;
} /* }}} bool check_send_okay */

static bool check_notify_received(const notification_t *n) /* {{{ */
{
for (notification_meta_t *ptr = n->meta; ptr != NULL; ptr = ptr->next)
if ((strcmp("network:received", ptr->name) == 0) &&
(ptr->type == NM_TYPE_BOOLEAN))
return (bool)ptr->nm_value.nm_boolean;

return 0;
} /* }}} bool check_notify_received */

static bool check_send_notify_okay(const notification_t *n) /* {{{ */
{
static c_complain_t complain_forwarding = C_COMPLAIN_INIT_STATIC;
bool received = 0;

if (n->meta == NULL)
return 1;

received = check_notify_received(n);

if (network_config_forward && received) {
c_complain_once(
LOG_ERR, &complain_forwarding,
"network plugin: A notification has been received via the network "
"and forwarding is enabled. Forwarding of notifications is currently "
"not supported, because there is not loop-detection available. "
"Please contact the collectd mailing list if you need this "
"feature.");
}

/* By default, only *send* value lists that were not *received* by the
* network plugin. */
return !received;
} /* }}} bool check_send_notify_okay */

static int network_dispatch_values(value_list_t *vl, /* {{{ */
const char *username,
struct sockaddr_storage *address) {
Expand Down Expand Up @@ -3059,6 +3026,32 @@ static int network_config(oconfig_item_t *ci) /* {{{ */
return 0;
} /* }}} int network_config */

static bool check_send_notification(const notification_t *n) {
bool received = 0;
int status;

if (network_config_forward)
return true;

if (n->meta == NULL)
return true;

status = plugin_notification_meta_get_boolean(n->meta, "network:received",
&received);
if (status == -ENOENT)
return true;
else if (status != 0) {
ERROR("network plugin: check_send_notification: "
"plugin_notification_meta_get_boolean failed with status %i.",
status);
return true;
}

/* By default, only *send* notifications that were not *received* by the
* network plugin. */
return !received;
}

static int network_notification(const notification_t *n,
user_data_t __attribute__((unused)) *
user_data) {
Expand All @@ -3067,8 +3060,12 @@ static int network_notification(const notification_t *n,
size_t buffer_free = sizeof(buffer);
int status;

if (!check_send_notify_okay(n))
if (!check_send_notification(n)) {
DEBUG("network plugin: network_notification: "
"NOT sending %s.",
n->message);
return 0;
}

memset(buffer, 0, sizeof(buffer));

Expand Down
2 changes: 2 additions & 0 deletions src/network_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#define TEST_PLUGIN_NETWORK 1

#include "daemon/plugin.h"
#include "daemon/plugin.c"
#include "network.c" /* (sic) */

#include "testing.h"
Expand Down

0 comments on commit fcc531b

Please sign in to comment.