8a9355c3bc4dc6af2ac12f46e61bb0d9e44faaa4
[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 static int wl_write_graphite (const data_set_t *ds, const value_list_t *vl)
44 {
45     char buffer[WL_BUF_SIZE] = { 0 };
46     int status;
47
48     if (0 != strcmp (ds->type, vl->type))
49     {
50         ERROR ("write_log plugin: DS type does not match value list type");
51         return -1;
52     }
53
54     status = format_graphite (buffer, sizeof (buffer), ds, vl,
55                               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 {
66     char buffer[WL_BUF_SIZE] = { 0 };
67     size_t bfree = sizeof(buffer);
68     size_t bfill = 0;
69
70     if (0 != strcmp (ds->type, vl->type))
71     {
72         ERROR ("write_log plugin: DS type does not match value list type");
73         return -1;
74     }
75
76     format_json_initialize(buffer, &bfill, &bfree);
77     format_json_value_list(buffer, &bfill, &bfree, ds, vl,
78                            /* store rates = */ 0);
79     format_json_finalize(buffer, &bfill, &bfree);
80
81     INFO ("write_log values:\n%s", buffer);
82
83     return (0);
84 } /* int wl_write_json */
85
86 static int wl_write (const data_set_t *ds, const value_list_t *vl,
87         user_data_t *user_data)
88 {
89     int status = 0;
90     int mode = (int) (size_t) user_data->data;
91
92     if (mode == WL_FORMAT_GRAPHITE)
93     {
94         status = wl_write_graphite (ds, vl);
95     }
96     else if (mode == WL_FORMAT_JSON)
97     {
98         status = wl_write_json (ds, vl);
99     }
100
101     return (status);
102 }
103
104 static int wl_config (oconfig_item_t *ci) /* {{{ */
105 {
106     int mode = 0;
107     for (int i = 0; i < ci->children_num; i++)
108     {
109         oconfig_item_t *child = ci->children + i;
110
111         if (strcasecmp ("Format", child->key) == 0)
112         {
113             char *mode_str = NULL;
114             if ((child->values_num != 1)
115                 || (child->values[0].type != OCONFIG_TYPE_STRING))
116             {
117                 ERROR ("write_log plugin: Option `%s' requires "
118                     "exactly one string argument.", child->key);
119                 return (-EINVAL);
120             }
121             if (mode != 0)
122             {
123                 WARNING ("write_log plugin: Redefining option `%s'.",
124                     child->key);
125             }
126             mode_str = child->values[0].value.string;
127             if (strcasecmp ("Graphite", mode_str) == 0)
128                 mode = WL_FORMAT_GRAPHITE;
129             else if (strcasecmp ("JSON", mode_str) == 0)
130                 mode = WL_FORMAT_JSON;
131             else
132             {
133                 ERROR ("write_log plugin: Unknown mode `%s' for option `%s'.",
134                     mode_str, child->key);
135                 return (-EINVAL);
136             }
137         }
138         else
139         {
140             ERROR ("write_log plugin: Invalid configuration option: `%s'.",
141                 child->key);
142         }
143     }
144     if (mode == 0)
145         mode = WL_FORMAT_GRAPHITE;
146
147     user_data_t ud = {
148         .data = (void *) (size_t) mode,
149         .free_func = NULL
150     };
151
152     plugin_register_write ("write_log", wl_write, &ud);
153
154     return (0);
155 } /* }}} int wl_config */
156
157 void module_register (void)
158 {
159     plugin_register_complex_config ("write_log", wl_config);
160
161     user_data_t ud = {
162         .data = (void *) (size_t) WL_FORMAT_GRAPHITE,
163         .free_func = NULL
164     };
165
166     plugin_register_write ("write_log", wl_write, &ud);
167 }
168
169 /* vim: set sw=4 ts=4 sts=4 tw=78 et : */