2 * collectd - src/write_log.c
3 * Copyright (C) 2015 Pierre-Yves Ritschard
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Pierre-Yves Ritschard <pyr at spootnik.org>
33 #include "utils_format_graphite.h"
34 #include "utils_format_json.h"
38 #define WL_BUF_SIZE 16384
40 #define WL_FORMAT_GRAPHITE 1
41 #define WL_FORMAT_JSON 2
43 /* Plugin:WriteLog has to also operate without a config, so use a global. */
44 int wl_format = WL_FORMAT_GRAPHITE;
46 static int wl_write_graphite (const data_set_t *ds, const value_list_t *vl)
48 char buffer[WL_BUF_SIZE] = { 0 };
51 if (0 != strcmp (ds->type, vl->type))
53 ERROR ("write_log plugin: DS type does not match value list type");
57 status = format_graphite (buffer, sizeof (buffer), ds, vl,
59 if (status != 0) /* error message has been printed already. */
62 INFO ("write_log values:\n%s", buffer);
65 } /* int wl_write_graphite */
67 static int wl_write_json (const data_set_t *ds, const value_list_t *vl)
69 char buffer[WL_BUF_SIZE] = { 0 };
70 size_t bfree = sizeof(buffer);
73 if (0 != strcmp (ds->type, vl->type))
75 ERROR ("write_log plugin: DS type does not match value list type");
79 format_json_initialize(buffer, &bfill, &bfree);
80 format_json_value_list(buffer, &bfill, &bfree, ds, vl,
81 /* store rates = */ 0);
82 format_json_finalize(buffer, &bfill, &bfree);
84 INFO ("write_log values:\n%s", buffer);
87 } /* int wl_write_json */
89 static int wl_write (const data_set_t *ds, const value_list_t *vl,
90 __attribute__ ((unused)) user_data_t *user_data)
94 if (wl_format == WL_FORMAT_GRAPHITE)
96 status = wl_write_graphite (ds, vl);
98 else if (wl_format == WL_FORMAT_JSON)
100 status = wl_write_json (ds, vl);
106 static int wl_config (oconfig_item_t *ci) /* {{{ */
108 _Bool format_seen = 0;
110 for (int i = 0; i < ci->children_num; i++)
112 oconfig_item_t *child = ci->children + i;
114 if (strcasecmp ("Format", child->key) == 0)
118 if (cf_util_get_string_buffer (child, str, sizeof (str)) != 0)
123 WARNING ("write_log plugin: Redefining option `%s'.",
128 if (strcasecmp ("Graphite", str) == 0)
129 wl_format = WL_FORMAT_GRAPHITE;
130 else if (strcasecmp ("JSON", str) == 0)
131 wl_format = WL_FORMAT_JSON;
134 ERROR ("write_log plugin: Unknown format `%s' for option `%s'.",
141 ERROR ("write_log plugin: Invalid configuration option: `%s'.",
148 } /* }}} int wl_config */
150 void module_register (void)
152 plugin_register_complex_config ("write_log", wl_config);
153 /* If config is supplied, the global wl_format will be set. */
154 plugin_register_write ("write_log", wl_write, NULL);
157 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */