1 /*
2 * SysDB - src/backend/collectd/unixsock.c
3 * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "sysdb.h"
29 #include "core/plugin.h"
30 #include "core/store.h"
31 #include "utils/error.h"
32 #include "utils/unixsock.h"
34 #include "liboconfig/utils.h"
36 #include <assert.h>
38 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
44 SDB_PLUGIN_MAGIC;
46 /*
47 * private data types
48 */
50 typedef struct {
51 char *current_host;
52 sdb_time_t current_timestamp;
53 int svc_updated;
54 int svc_failed;
55 } sdb_collectd_state_t;
56 #define SDB_COLLECTD_STATE_INIT { NULL, 0, 0, 0 }
58 /*
59 * private helper functions
60 */
62 static int
63 sdb_collectd_add_host(const char *hostname, sdb_time_t last_update)
64 {
65 int status;
67 status = sdb_store_host(hostname, last_update);
69 if (status < 0) {
70 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
71 "store/update host '%s'.", hostname);
72 return -1;
73 }
74 else if (status > 0) /* value too old */
75 return 0;
77 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
78 "host '%s' (last update timestamp = %"PRIscTIME").",
79 hostname, last_update);
80 return 0;
81 } /* sdb_collectd_add_host */
83 static int
84 sdb_collectd_add_svc(const char *hostname, const char *plugin,
85 const char *type, sdb_time_t last_update)
86 {
87 char name[strlen(plugin) + strlen(type) + 2];
88 int status;
90 snprintf(name, sizeof(name), "%s/%s", plugin, type);
92 status = sdb_store_service(hostname, name, last_update);
93 if (status < 0) {
94 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
95 "store/update service '%s/%s'.", hostname, name);
96 return -1;
97 }
98 return 0;
99 } /* sdb_collectd_add_svc */
101 static int
102 sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
103 size_t n, sdb_data_t *data, sdb_object_t *user_data)
104 {
105 sdb_collectd_state_t *state;
107 const char *hostname;
108 const char *plugin;
109 const char *type;
110 sdb_time_t last_update;
112 assert(user_data);
114 assert(n == 4);
115 assert((data[0].type == SDB_TYPE_DATETIME)
116 && (data[1].type == SDB_TYPE_STRING)
117 && (data[2].type == SDB_TYPE_STRING)
118 && (data[3].type == SDB_TYPE_STRING));
120 last_update = data[0].data.datetime;
121 hostname = data[1].data.string;
122 plugin = data[2].data.string;
123 type = data[3].data.string;
125 state = SDB_OBJ_WRAPPER(user_data)->data;
127 if (! state->current_host) {
128 state->current_host = strdup(hostname);
129 state->current_timestamp = last_update;
130 }
132 if (! state->current_host) {
133 char errbuf[1024];
134 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
135 "string buffer: %s",
136 sdb_strerror(errno, errbuf, sizeof(errbuf)));
137 return -1;
138 }
140 if (! sdb_store_has_host(hostname))
141 sdb_collectd_add_host(hostname, last_update);
143 if (sdb_collectd_add_svc(hostname, plugin, type, last_update))
144 ++state->svc_failed;
145 else
146 ++state->svc_updated;
148 if (! strcasecmp(state->current_host, hostname)) {
149 if (last_update > state->current_timestamp)
150 state->current_timestamp = last_update;
151 return 0;
152 }
154 /* new host */
155 sdb_collectd_add_host(hostname, last_update);
157 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
158 "%i service%s (%i failed) for host '%s'.",
159 state->svc_updated, state->svc_updated == 1 ? "" : "s",
160 state->svc_failed, state->current_host);
161 state->svc_updated = state->svc_failed = 0;
163 free(state->current_host);
164 state->current_host = strdup(hostname);
165 state->current_timestamp = last_update;
166 return 0;
167 } /* sdb_collectd_get_data */
169 /*
170 * plugin API
171 */
173 static int
174 sdb_collectd_init(sdb_object_t *user_data)
175 {
176 sdb_unixsock_client_t *client;
178 if (! user_data)
179 return -1;
181 client = SDB_OBJ_WRAPPER(user_data)->data;
182 if (sdb_unixsock_client_connect(client)) {
183 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: "
184 "Failed to connect to collectd.");
185 return -1;
186 }
188 sdb_log(SDB_LOG_INFO, "collectd::unixsock backend: Successfully "
189 "connected to collectd @ %s.",
190 sdb_unixsock_client_path(client));
191 return 0;
192 } /* sdb_collectd_init */
194 static int
195 sdb_collectd_shutdown(__attribute__((unused)) sdb_object_t *user_data)
196 {
197 return 0;
198 } /* sdb_collectd_shutdown */
200 static int
201 sdb_collectd_collect(sdb_object_t *user_data)
202 {
203 sdb_unixsock_client_t *client;
205 char buffer[1024];
206 char *line;
207 char *msg;
209 char *endptr = NULL;
210 long int count;
212 sdb_collectd_state_t state = SDB_COLLECTD_STATE_INIT;
213 sdb_object_wrapper_t state_obj = SDB_OBJECT_WRAPPER_STATIC(&state,
214 /* destructor = */ NULL);
216 if (! user_data)
217 return -1;
219 client = SDB_OBJ_WRAPPER(user_data)->data;
221 if (sdb_unixsock_client_send(client, "LISTVAL") <= 0) {
222 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to send "
223 "LISTVAL command to collectd @ %s.",
224 sdb_unixsock_client_path(client));
225 return -1;
226 }
228 line = sdb_unixsock_client_recv(client, buffer, sizeof(buffer));
229 if (! line) {
230 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to read "
231 "status of LISTVAL command from collectd @ %s.",
232 sdb_unixsock_client_path(client));
233 return -1;
234 }
236 msg = strchr(line, ' ');
237 if (msg) {
238 *msg = '\0';
239 ++msg;
240 }
242 errno = 0;
243 count = strtol(line, &endptr, /* base */ 0);
244 if (errno || (line == endptr)) {
245 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
246 "status of LISTVAL command from collectd @ %s.",
247 sdb_unixsock_client_path(client));
248 return -1;
249 }
251 if (count < 0) {
252 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to get "
253 "value list from collectd @ %s: %s",
254 sdb_unixsock_client_path(client),
255 msg ? msg : line);
256 return -1;
257 }
259 if (sdb_unixsock_client_process_lines(client, sdb_collectd_get_data,
260 SDB_OBJ(&state_obj), count, /* delim */ " /",
261 /* column count = */ 4,
262 SDB_TYPE_DATETIME, SDB_TYPE_STRING,
263 SDB_TYPE_STRING, SDB_TYPE_STRING)) {
264 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed "
265 "to read response from collectd @ %s.",
266 sdb_unixsock_client_path(client));
267 return -1;
268 }
270 if (state.current_host) {
271 sdb_collectd_add_host(state.current_host, state.current_timestamp);
272 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
273 "%i service%s (%i failed) for host '%s'.",
274 state.svc_updated, state.svc_updated == 1 ? "" : "s",
275 state.svc_failed, state.current_host);
276 }
277 return 0;
278 } /* sdb_collectd_collect */
280 static int
281 sdb_collectd_config_instance(oconfig_item_t *ci)
282 {
283 char *name = NULL;
284 char *socket_path = NULL;
286 char cb_name[1024];
288 sdb_object_t *user_data;
289 sdb_unixsock_client_t *client;
291 int i;
293 if (oconfig_get_string(ci, &name)) {
294 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance requires "
295 "a single string argument\n\tUsage: <Instance NAME>");
296 return -1;
297 }
299 for (i = 0; i < ci->children_num; ++i) {
300 oconfig_item_t *child = ci->children + i;
302 if (! strcasecmp(child->key, "Socket"))
303 oconfig_get_string(child, &socket_path);
304 else
305 sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
306 "unknown config option '%s' inside <Instance %s>.",
307 child->key, name);
308 }
310 if (! socket_path) {
311 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance '%s' "
312 "missing the 'Socket' option.", name);
313 return -1;
314 }
316 snprintf(cb_name, sizeof(cb_name), "collectd::unixsock::%s", name);
317 cb_name[sizeof(cb_name) - 1] = '\0';
319 client = sdb_unixsock_client_create(socket_path);
320 if (! client) {
321 char errbuf[1024];
322 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to create "
323 "unixsock client: %s",
324 sdb_strerror(errno, errbuf, sizeof(errbuf)));
325 return -1;
326 }
328 user_data = sdb_object_create_wrapper("unixsock-client", client,
329 (void (*)(void *))sdb_unixsock_client_destroy);
330 if (! user_data) {
331 sdb_unixsock_client_destroy(client);
332 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
333 "sdb_object_t");
334 return -1;
335 }
337 sdb_plugin_register_init(cb_name, sdb_collectd_init, user_data);
338 sdb_plugin_register_shutdown(cb_name, sdb_collectd_shutdown, user_data);
340 sdb_plugin_register_collector(cb_name, sdb_collectd_collect,
341 /* interval */ NULL, user_data);
343 /* pass control to the list */
344 sdb_object_deref(user_data);
345 return 0;
346 } /* sdb_collectd_config_instance */
348 static int
349 sdb_collectd_config(oconfig_item_t *ci)
350 {
351 int i;
353 for (i = 0; i < ci->children_num; ++i) {
354 oconfig_item_t *child = ci->children + i;
356 if (! strcasecmp(child->key, "Instance"))
357 sdb_collectd_config_instance(child);
358 else
359 sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
360 "unknown config option '%s'.", child->key);
361 }
362 return 0;
363 } /* sdb_collectd_config */
365 int
366 sdb_module_init(sdb_plugin_info_t *info)
367 {
368 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "collectd::unixsock");
369 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
370 "backend accessing the system statistics collection daemon "
371 "throught the UNIXSOCK interface");
372 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
373 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
374 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
375 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
376 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
378 sdb_plugin_register_config("collectd::unixsock", sdb_collectd_config);
379 return 0;
380 } /* sdb_version_extra */
382 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */