Fix compile time issues
[collectd.git] / src / write_log.c
1 /**
2  * collectd - src/write_log.c
3  * Copyright (C) 2015       Pierre-Yves Ritschard
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors:
24  *   Pierre-Yves Ritschard <pyr at spootnik.org>
25  *
26  **/
27
28 #include "collectd.h"
29
30 #include "plugin.h"
31 #include "utils/common/common.h"
32
33 #include "utils/format_graphite/format_graphite.h"
34 #include "utils/format_json/format_json.h"
35
36 #include <netdb.h>
37
38 #define WL_BUF_SIZE 16384
39
40 #define WL_FORMAT_GRAPHITE 1
41 #define WL_FORMAT_JSON 2
42
43 /* Plugin:WriteLog has to also operate without a config, so use a global. */
44 int wl_format = WL_FORMAT_GRAPHITE;
45
46 static int wl_write_graphite(const data_set_t *ds, const value_list_t *vl) {
47   char buffer[WL_BUF_SIZE] = {0};
48   int status;
49
50   if (0 != strcmp(ds->type, vl->type)) {
51     ERROR("write_log plugin: DS type does not match value list type");
52     return -1;
53   }
54
55   status = format_graphite(buffer, sizeof(buffer), ds, vl, NULL, NULL, '_', 0);
56   if (status != 0) /* error message has been printed already. */
57     return status;
58
59   INFO("write_log values:\n%s", buffer);
60
61   return 0;
62 } /* int wl_write_graphite */
63
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);
67   size_t bfill = 0;
68
69   if (0 != strcmp(ds->type, vl->type)) {
70     ERROR("write_log plugin: DS type does not match value list type");
71     return -1;
72   }
73
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);
78
79   INFO("write_log values:\n%s", buffer);
80
81   return 0;
82 } /* int wl_write_json */
83
84 static int wl_write(const data_set_t *ds, const value_list_t *vl,
85                     __attribute__((unused)) user_data_t *user_data) {
86   int status = 0;
87
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);
92   }
93
94   return status;
95 }
96
97 static int wl_config(oconfig_item_t *ci) /* {{{ */
98 {
99   bool format_seen = false;
100
101   for (int i = 0; i < ci->children_num; i++) {
102     oconfig_item_t *child = ci->children + i;
103
104     if (strcasecmp("Format", child->key) == 0) {
105       char str[16];
106
107       if (cf_util_get_string_buffer(child, str, sizeof(str)) != 0)
108         continue;
109
110       if (format_seen) {
111         WARNING("write_log plugin: Redefining option `%s'.", child->key);
112       }
113       format_seen = true;
114
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;
119       else {
120         ERROR("write_log plugin: Unknown format `%s' for option `%s'.", str,
121               child->key);
122         return -EINVAL;
123       }
124     } else {
125       ERROR("write_log plugin: Invalid configuration option: `%s'.",
126             child->key);
127       return -EINVAL;
128     }
129   }
130
131   return 0;
132 } /* }}} int wl_config */
133
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);
138 }