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) {
47 char buffer[WL_BUF_SIZE] = {0};
50 if (0 != strcmp(ds->type, vl->type)) {
51 ERROR("write_log plugin: DS type does not match value list type");
55 status = format_graphite(buffer, sizeof(buffer), ds, vl, NULL, NULL, '_', 0);
56 if (status != 0) /* error message has been printed already. */
59 INFO("write_log values:\n%s", buffer);
62 } /* int wl_write_graphite */
64 static int wl_write_json(const data_set_t *ds, const value_list_t *vl) {
65 char buffer[WL_BUF_SIZE] = {0};
66 size_t bfree = sizeof(buffer);
69 if (0 != strcmp(ds->type, vl->type)) {
70 ERROR("write_log plugin: DS type does not match value list type");
74 format_json_initialize(buffer, &bfill, &bfree);
75 format_json_value_list(buffer, &bfill, &bfree, ds, vl,
76 /* store rates = */ 0);
77 format_json_finalize(buffer, &bfill, &bfree);
79 INFO("write_log values:\n%s", buffer);
82 } /* int wl_write_json */
84 static int wl_write(const data_set_t *ds, const value_list_t *vl,
85 __attribute__((unused)) user_data_t *user_data) {
88 if (wl_format == WL_FORMAT_GRAPHITE) {
89 status = wl_write_graphite(ds, vl);
90 } else if (wl_format == WL_FORMAT_JSON) {
91 status = wl_write_json(ds, vl);
97 static int wl_config(oconfig_item_t *ci) /* {{{ */
99 _Bool format_seen = 0;
101 for (int i = 0; i < ci->children_num; i++) {
102 oconfig_item_t *child = ci->children + i;
104 if (strcasecmp("Format", child->key) == 0) {
107 if (cf_util_get_string_buffer(child, str, sizeof(str)) != 0)
111 WARNING("write_log plugin: Redefining option `%s'.", child->key);
115 if (strcasecmp("Graphite", str) == 0)
116 wl_format = WL_FORMAT_GRAPHITE;
117 else if (strcasecmp("JSON", str) == 0)
118 wl_format = WL_FORMAT_JSON;
120 ERROR("write_log plugin: Unknown format `%s' for option `%s'.", str,
125 ERROR("write_log plugin: Invalid configuration option: `%s'.",
132 } /* }}} int wl_config */
134 void module_register(void) {
135 plugin_register_complex_config("write_log", wl_config);
136 /* If config is supplied, the global wl_format will be set. */
137 plugin_register_write("write_log", wl_write, NULL);