Merge branch 'collectd-5.6'
[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 "common.h"
31 #include "plugin.h"
32
33 #include "utils_format_graphite.h"
34 #include "utils_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 {
48     char buffer[WL_BUF_SIZE] = { 0 };
49     int status;
50
51     if (0 != strcmp (ds->type, vl->type))
52     {
53         ERROR ("write_log plugin: DS type does not match value list type");
54         return -1;
55     }
56
57     status = format_graphite (buffer, sizeof (buffer), ds, vl,
58                               NULL, NULL, '_', 0);
59     if (status != 0) /* error message has been printed already. */
60         return (status);
61
62     INFO ("write_log values:\n%s", buffer);
63
64     return (0);
65 } /* int wl_write_graphite */
66
67 static int wl_write_json (const data_set_t *ds, const value_list_t *vl)
68 {
69     char buffer[WL_BUF_SIZE] = { 0 };
70     size_t bfree = sizeof(buffer);
71     size_t bfill = 0;
72
73     if (0 != strcmp (ds->type, vl->type))
74     {
75         ERROR ("write_log plugin: DS type does not match value list type");
76         return -1;
77     }
78
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);
83
84     INFO ("write_log values:\n%s", buffer);
85
86     return (0);
87 } /* int wl_write_json */
88
89 static int wl_write (const data_set_t *ds, const value_list_t *vl,
90         __attribute__ ((unused)) user_data_t *user_data)
91 {
92     int status = 0;
93
94     if (wl_format == WL_FORMAT_GRAPHITE)
95     {
96         status = wl_write_graphite (ds, vl);
97     }
98     else if (wl_format == WL_FORMAT_JSON)
99     {
100         status = wl_write_json (ds, vl);
101     }
102
103     return (status);
104 }
105
106 static int wl_config (oconfig_item_t *ci) /* {{{ */
107 {
108     _Bool format_seen = 0;
109
110     for (int i = 0; i < ci->children_num; i++)
111     {
112         oconfig_item_t *child = ci->children + i;
113
114         if (strcasecmp ("Format", child->key) == 0)
115         {
116             char str[16];
117
118             if (cf_util_get_string_buffer (child, str, sizeof (str)) != 0)
119                 continue;
120
121             if (format_seen)
122             {
123                 WARNING ("write_log plugin: Redefining option `%s'.",
124                     child->key);
125             }
126             format_seen = 1;
127
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;
132             else
133             {
134                 ERROR ("write_log plugin: Unknown format `%s' for option `%s'.",
135                     str, child->key);
136                 return (-EINVAL);
137             }
138         }
139         else
140         {
141             ERROR ("write_log plugin: Invalid configuration option: `%s'.",
142                 child->key);
143             return (-EINVAL);
144         }
145     }
146
147     return (0);
148 } /* }}} int wl_config */
149
150 void module_register (void)
151 {
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);
155 }
156
157 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */