summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 806091e)
raw | patch | inline | side by side (parent: 806091e)
author | Igor Peshansky <igorpeshansky@github.com> | |
Thu, 8 Sep 2016 15:39:50 +0000 (11:39 -0400) | ||
committer | Igor Peshansky <igorpeshansky@github.com> | |
Fri, 9 Sep 2016 19:13:48 +0000 (15:13 -0400) |
src/Makefile.am | patch | blob | history | |
src/write_log.c | patch | blob | history |
diff --git a/src/Makefile.am b/src/Makefile.am
index b76e43cd8fb8b4cbd470966c1e8354cfcb573b28..3477dc24f968e9bed45c743e6f2017d2fbb6699d 100644 (file)
--- a/src/Makefile.am
+++ b/src/Makefile.am
write_log_la_SOURCES = write_log.c \
utils_format_graphite.c utils_format_graphite.h
write_log_la_LDFLAGS = $(PLUGIN_LDFLAGS)
+write_log_la_LIBADD = libformat_json.la
endif
if BUILD_PLUGIN_WRITE_MONGODB
diff --git a/src/write_log.c b/src/write_log.c
index b0dc6f11cca43072ed7a094ba6ef3c2f2e6ac794..8a9355c3bc4dc6af2ac12f46e61bb0d9e44faaa4 100644 (file)
--- a/src/write_log.c
+++ b/src/write_log.c
#include "plugin.h"
#include "utils_format_graphite.h"
+#include "utils_format_json.h"
#include <netdb.h>
-#define WL_BUF_SIZE 8192
+#define WL_BUF_SIZE 16384
-static int wl_write_messages (const data_set_t *ds, const value_list_t *vl)
+#define WL_FORMAT_GRAPHITE 1
+#define WL_FORMAT_JSON 2
+
+static int wl_write_graphite (const data_set_t *ds, const value_list_t *vl)
{
char buffer[WL_BUF_SIZE] = { 0 };
int status;
if (0 != strcmp (ds->type, vl->type))
{
- ERROR ("write_log plugin: DS type does not match "
- "value list type");
+ ERROR ("write_log plugin: DS type does not match value list type");
return -1;
}
INFO ("write_log values:\n%s", buffer);
return (0);
-} /* int wl_write_messages */
+} /* int wl_write_graphite */
+
+static int wl_write_json (const data_set_t *ds, const value_list_t *vl)
+{
+ char buffer[WL_BUF_SIZE] = { 0 };
+ size_t bfree = sizeof(buffer);
+ size_t bfill = 0;
+
+ if (0 != strcmp (ds->type, vl->type))
+ {
+ ERROR ("write_log plugin: DS type does not match value list type");
+ return -1;
+ }
+
+ format_json_initialize(buffer, &bfill, &bfree);
+ format_json_value_list(buffer, &bfill, &bfree, ds, vl,
+ /* store rates = */ 0);
+ format_json_finalize(buffer, &bfill, &bfree);
+
+ INFO ("write_log values:\n%s", buffer);
+
+ return (0);
+} /* int wl_write_json */
static int wl_write (const data_set_t *ds, const value_list_t *vl,
- __attribute__ ((unused)) user_data_t *user_data)
+ user_data_t *user_data)
{
- int status;
+ int status = 0;
+ int mode = (int) (size_t) user_data->data;
- status = wl_write_messages (ds, vl);
+ if (mode == WL_FORMAT_GRAPHITE)
+ {
+ status = wl_write_graphite (ds, vl);
+ }
+ else if (mode == WL_FORMAT_JSON)
+ {
+ status = wl_write_json (ds, vl);
+ }
return (status);
}
+static int wl_config (oconfig_item_t *ci) /* {{{ */
+{
+ int mode = 0;
+ for (int i = 0; i < ci->children_num; i++)
+ {
+ oconfig_item_t *child = ci->children + i;
+
+ if (strcasecmp ("Format", child->key) == 0)
+ {
+ char *mode_str = NULL;
+ if ((child->values_num != 1)
+ || (child->values[0].type != OCONFIG_TYPE_STRING))
+ {
+ ERROR ("write_log plugin: Option `%s' requires "
+ "exactly one string argument.", child->key);
+ return (-EINVAL);
+ }
+ if (mode != 0)
+ {
+ WARNING ("write_log plugin: Redefining option `%s'.",
+ child->key);
+ }
+ mode_str = child->values[0].value.string;
+ if (strcasecmp ("Graphite", mode_str) == 0)
+ mode = WL_FORMAT_GRAPHITE;
+ else if (strcasecmp ("JSON", mode_str) == 0)
+ mode = WL_FORMAT_JSON;
+ else
+ {
+ ERROR ("write_log plugin: Unknown mode `%s' for option `%s'.",
+ mode_str, child->key);
+ return (-EINVAL);
+ }
+ }
+ else
+ {
+ ERROR ("write_log plugin: Invalid configuration option: `%s'.",
+ child->key);
+ }
+ }
+ if (mode == 0)
+ mode = WL_FORMAT_GRAPHITE;
+
+ user_data_t ud = {
+ .data = (void *) (size_t) mode,
+ .free_func = NULL
+ };
+
+ plugin_register_write ("write_log", wl_write, &ud);
+
+ return (0);
+} /* }}} int wl_config */
+
void module_register (void)
{
- plugin_register_write ("write_log", wl_write, NULL);
+ plugin_register_complex_config ("write_log", wl_config);
+
+ user_data_t ud = {
+ .data = (void *) (size_t) WL_FORMAT_GRAPHITE,
+ .free_func = NULL
+ };
+
+ plugin_register_write ("write_log", wl_write, &ud);
}
/* vim: set sw=4 ts=4 sts=4 tw=78 et : */