1 /*
2 * SysDB - src/plugins/backend/collectd/unixsock.c
3 * Copyright (C) 2012-2014 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 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
33 #include "core/plugin.h"
34 #include "core/store.h"
35 #include "utils/error.h"
36 #include "utils/unixsock.h"
38 #include "liboconfig/utils.h"
40 #include <assert.h>
42 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
48 SDB_PLUGIN_MAGIC;
50 /*
51 * private data types
52 */
54 typedef struct {
55 sdb_unixsock_client_t *client;
57 char *ts_type;
58 char *ts_base;
59 } user_data_t;
60 #define UD(obj) ((user_data_t *)(obj))
62 typedef struct {
63 char *current_host;
64 sdb_time_t current_timestamp;
65 int metrics_updated;
66 int metrics_failed;
68 user_data_t *ud;
69 } state_t;
70 #define STATE_INIT { NULL, 0, 0, 0, NULL }
72 /*
73 * private helper functions
74 */
76 static void
77 user_data_destroy(void *obj)
78 {
79 user_data_t *ud = UD(obj);
81 if (! obj)
82 return;
84 if (ud->client)
85 sdb_unixsock_client_destroy(ud->client);
86 ud->client = NULL;
88 if (ud->ts_type)
89 free(ud->ts_type);
90 if (ud->ts_base)
91 free(ud->ts_base);
92 ud->ts_type = ud->ts_base = NULL;
93 } /* user_data_destroy */
95 /* store the specified host-name (once per iteration) */
96 static int
97 store_host(state_t *state, const char *hostname, sdb_time_t last_update)
98 {
99 int status;
101 if (last_update > state->current_timestamp)
102 state->current_timestamp = last_update;
104 if (state->current_host && (! strcasecmp(state->current_host, hostname)))
105 return 0;
106 /* else: first/new host */
108 if (state->current_host) {
109 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
110 "%i metric%s (%i failed) for host '%s'.",
111 state->metrics_updated, state->metrics_updated == 1 ? "" : "s",
112 state->metrics_failed, state->current_host);
113 state->metrics_updated = state->metrics_failed = 0;
114 free(state->current_host);
115 }
117 state->current_host = strdup(hostname);
118 if (! state->current_host) {
119 char errbuf[1024];
120 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
121 "string buffer: %s",
122 sdb_strerror(errno, errbuf, sizeof(errbuf)));
123 return -1;
124 }
126 status = sdb_store_host(hostname, last_update);
128 if (status < 0) {
129 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
130 "store/update host '%s'.", hostname);
131 return -1;
132 }
133 else if (status > 0) /* value too old */
134 return 0;
136 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
137 "host '%s' (last update timestamp = %"PRIsdbTIME").",
138 hostname, last_update);
139 return 0;
140 } /* store_host */
142 static int
143 add_metrics(const char *hostname, char *plugin, char *type,
144 sdb_time_t last_update, user_data_t *ud)
145 {
146 char name[strlen(plugin) + strlen(type) + 2];
147 char *plugin_instance, *type_instance;
149 char metric_id[(ud->ts_base ? strlen(ud->ts_base) : 0)
150 + strlen(hostname) + sizeof(name) + 7];
151 sdb_metric_store_t store = { ud->ts_type, metric_id };
153 sdb_data_t data = { SDB_TYPE_STRING, { .string = NULL } };
155 int status;
157 snprintf(name, sizeof(name), "%s/%s", plugin, type);
159 if (ud->ts_base) {
160 snprintf(metric_id, sizeof(metric_id), "%s/%s/%s.rrd",
161 ud->ts_base, hostname, name);
162 status = sdb_store_metric(hostname, name, &store, last_update);
163 }
164 else
165 status = sdb_store_metric(hostname, name, NULL, last_update);
166 if (status < 0) {
167 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
168 "store/update metric '%s/%s'.", hostname, name);
169 return -1;
170 }
172 plugin_instance = strchr(plugin, '-');
173 if (plugin_instance) {
174 *plugin_instance = '\0';
175 ++plugin_instance;
177 data.data.string = plugin_instance;
178 sdb_store_metric_attr(hostname, name,
179 "plugin_instance", &data, last_update);
180 }
182 type_instance = strchr(type, '-');
183 if (type_instance) {
184 *type_instance = '\0';
185 ++type_instance;
187 data.data.string = type_instance;
188 sdb_store_metric_attr(hostname, name,
189 "type_instance", &data, last_update);
190 }
192 data.data.string = plugin;
193 sdb_store_metric_attr(hostname, name, "plugin", &data, last_update);
194 data.data.string = type;
195 sdb_store_metric_attr(hostname, name, "type", &data, last_update);
196 return 0;
197 } /* add_metrics */
199 static int
200 get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
201 size_t n, sdb_data_t *data, sdb_object_t *user_data)
202 {
203 state_t *state;
204 sdb_data_t last_update;
206 char *hostname;
207 char *plugin;
208 char *type;
210 assert(user_data);
212 /* 0: <last_update> <hostname>
213 * 1: <plugin>
214 * 2: <type> */
215 assert(n == 3);
216 assert((data[0].type == SDB_TYPE_STRING)
217 && (data[1].type == SDB_TYPE_STRING)
218 && (data[2].type == SDB_TYPE_STRING));
220 hostname = data[0].data.string;
221 plugin = data[1].data.string;
222 type = data[2].data.string;
224 hostname = strchr(hostname, ' ');
225 if (! hostname) {
226 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Expected to find "
227 "a space character in the LISTVAL response");
228 return -1;
229 }
230 *hostname = '\0';
231 ++hostname;
233 if (sdb_data_parse(data[0].data.string, SDB_TYPE_DATETIME, &last_update)) {
234 char errbuf[1024];
235 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
236 "timestamp '%s' returned by LISTVAL: %s", data[0].data.string,
237 sdb_strerror(errno, errbuf, sizeof(errbuf)));
238 return -1;
239 }
241 state = SDB_OBJ_WRAPPER(user_data)->data;
242 if (store_host(state, hostname, last_update.data.datetime))
243 return -1;
245 if (add_metrics(hostname, plugin, type,
246 last_update.data.datetime, state->ud))
247 ++state->metrics_failed;
248 else
249 ++state->metrics_updated;
250 return 0;
251 } /* get_data */
253 /*
254 * plugin API
255 */
257 static int
258 sdb_collectd_init(sdb_object_t *user_data)
259 {
260 user_data_t *ud;
262 if (! user_data)
263 return -1;
265 ud = SDB_OBJ_WRAPPER(user_data)->data;
266 if (sdb_unixsock_client_connect(ud->client)) {
267 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: "
268 "Failed to connect to collectd.");
269 return -1;
270 }
272 sdb_log(SDB_LOG_INFO, "collectd::unixsock backend: Successfully "
273 "connected to collectd @ %s.",
274 sdb_unixsock_client_path(ud->client));
275 return 0;
276 } /* sdb_collectd_init */
278 static int
279 sdb_collectd_collect(sdb_object_t *user_data)
280 {
281 user_data_t *ud;
283 char buffer[1024];
284 char *line;
285 char *msg;
287 char *endptr = NULL;
288 long int count;
290 state_t state = STATE_INIT;
291 sdb_object_wrapper_t state_obj = SDB_OBJECT_WRAPPER_STATIC(&state);
293 if (! user_data)
294 return -1;
296 ud = SDB_OBJ_WRAPPER(user_data)->data;
297 state.ud = ud;
299 if (sdb_unixsock_client_send(ud->client, "LISTVAL") <= 0) {
300 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to send "
301 "LISTVAL command to collectd @ %s.",
302 sdb_unixsock_client_path(ud->client));
303 return -1;
304 }
306 line = sdb_unixsock_client_recv(ud->client, buffer, sizeof(buffer));
307 if (! line) {
308 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to read "
309 "status of LISTVAL command from collectd @ %s.",
310 sdb_unixsock_client_path(ud->client));
311 return -1;
312 }
314 msg = strchr(line, ' ');
315 if (msg) {
316 *msg = '\0';
317 ++msg;
318 }
320 errno = 0;
321 count = strtol(line, &endptr, /* base */ 0);
322 if (errno || (line == endptr)) {
323 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
324 "status of LISTVAL command from collectd @ %s.",
325 sdb_unixsock_client_path(ud->client));
326 return -1;
327 }
329 if (count < 0) {
330 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to get "
331 "value list from collectd @ %s: %s",
332 sdb_unixsock_client_path(ud->client),
333 msg ? msg : line);
334 return -1;
335 }
337 if (sdb_unixsock_client_process_lines(ud->client, get_data,
338 SDB_OBJ(&state_obj), count, /* delim */ "/",
339 /* column count = */ 3,
340 SDB_TYPE_STRING, SDB_TYPE_STRING, SDB_TYPE_STRING)) {
341 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed "
342 "to read response from collectd @ %s.",
343 sdb_unixsock_client_path(ud->client));
344 return -1;
345 }
347 if (state.current_host) {
348 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
349 "%i metric%s (%i failed) for host '%s'.",
350 state.metrics_updated, state.metrics_updated == 1 ? "" : "s",
351 state.metrics_failed, state.current_host);
352 free(state.current_host);
353 }
354 return 0;
355 } /* collect */
357 static int
358 sdb_collectd_config_instance(oconfig_item_t *ci)
359 {
360 char *name = NULL;
361 char *socket_path = NULL;
363 sdb_object_t *user_data;
364 user_data_t *ud;
366 int i;
368 if (oconfig_get_string(ci, &name)) {
369 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance requires "
370 "a single string argument\n\tUsage: <Instance NAME>");
371 return -1;
372 }
374 ud = calloc(1, sizeof(*ud));
375 if (! ud) {
376 char errbuf[1024];
377 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
378 "allocate user-data object: %s",
379 sdb_strerror(errno, errbuf, sizeof(errbuf)));
380 return -1;
381 }
383 for (i = 0; i < ci->children_num; ++i) {
384 oconfig_item_t *child = ci->children + i;
386 if (! strcasecmp(child->key, "Socket"))
387 oconfig_get_string(child, &socket_path);
388 else if (! strcasecmp(child->key, "TimeseriesBackend"))
389 oconfig_get_string(child, &ud->ts_type);
390 else if (! strcasecmp(child->key, "TimeseriesBaseURL"))
391 oconfig_get_string(child, &ud->ts_base);
392 else
393 sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
394 "unknown config option '%s' inside <Instance %s>.",
395 child->key, name);
396 }
398 if ((ud->ts_type == NULL) != (ud->ts_base == NULL)) {
399 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Both options, "
400 "TimeseriesBackend and TimeseriesBaseURL, have to be "
401 "specified.");
402 ud->ts_type = ud->ts_base = NULL;
403 user_data_destroy(ud);
404 return -1;
405 }
407 if (ud->ts_type) {
408 ud->ts_type = strdup(ud->ts_type);
409 ud->ts_base = strdup(ud->ts_base);
410 if ((! ud->ts_type) || (! ud->ts_base)) {
411 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed "
412 "to duplicate a string");
413 user_data_destroy(ud);
414 return -1;
415 }
416 }
418 if (! socket_path) {
419 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance '%s' "
420 "missing the 'Socket' option.", name);
421 user_data_destroy(ud);
422 return -1;
423 }
425 ud->client = sdb_unixsock_client_create(socket_path);
426 if (! ud->client) {
427 char errbuf[1024];
428 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to create "
429 "unixsock client: %s",
430 sdb_strerror(errno, errbuf, sizeof(errbuf)));
431 user_data_destroy(ud);
432 return -1;
433 }
435 user_data = sdb_object_create_wrapper("collectd-userdata", ud,
436 user_data_destroy);
437 if (! user_data) {
438 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
439 "user-data wrapper object");
440 user_data_destroy(ud);
441 return -1;
442 }
444 sdb_plugin_register_init(name, sdb_collectd_init, user_data);
445 sdb_plugin_register_collector(name, sdb_collectd_collect,
446 /* interval */ NULL, user_data);
448 /* pass control to the list */
449 sdb_object_deref(user_data);
450 return 0;
451 } /* sdb_collectd_config_instance */
453 static int
454 sdb_collectd_config(oconfig_item_t *ci)
455 {
456 int i;
458 if (! ci) /* nothing to do to deconfigure this plugin */
459 return 0;
461 for (i = 0; i < ci->children_num; ++i) {
462 oconfig_item_t *child = ci->children + i;
464 if (! strcasecmp(child->key, "Instance"))
465 sdb_collectd_config_instance(child);
466 else
467 sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
468 "unknown config option '%s'.", child->key);
469 }
470 return 0;
471 } /* sdb_collectd_config */
473 int
474 sdb_module_init(sdb_plugin_info_t *info)
475 {
476 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
477 "backend accessing the system statistics collection daemon "
478 "throught the UNIXSOCK interface");
479 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
480 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
481 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
482 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
483 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
485 sdb_plugin_register_config(sdb_collectd_config);
486 return 0;
487 } /* sdb_version_extra */
489 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */