Code

8af5327624e135c15796b5e86c1bc6a7878cbedd
[sysdb.git] / src / plugins / backend / collectd / unixsock.c
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;
94         free(ud);
95 } /* user_data_destroy */
97 /* store the specified host-name (once per iteration) */
98 static int
99 store_host(state_t *state, const char *hostname, sdb_time_t last_update)
101         int status;
103         if (last_update > state->current_timestamp)
104                 state->current_timestamp = last_update;
106         if (state->current_host && (! strcasecmp(state->current_host, hostname)))
107                 return 0;
108         /* else: first/new host */
110         if (state->current_host) {
111                 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
112                                 "%i metric%s (%i failed) for host '%s'.",
113                                 state->metrics_updated, state->metrics_updated == 1 ? "" : "s",
114                                 state->metrics_failed, state->current_host);
115                 state->metrics_updated = state->metrics_failed = 0;
116                 free(state->current_host);
117         }
119         state->current_host = strdup(hostname);
120         if (! state->current_host) {
121                 char errbuf[1024];
122                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
123                                 "string buffer: %s",
124                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
125                 return -1;
126         }
128         status = sdb_store_host(hostname, last_update);
130         if (status < 0) {
131                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
132                                 "store/update host '%s'.", hostname);
133                 return -1;
134         }
135         else if (status > 0) /* value too old */
136                 return 0;
138         sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
139                         "host '%s' (last update timestamp = %"PRIsdbTIME").",
140                         hostname, last_update);
141         return 0;
142 } /* store_host */
144 static int
145 add_metrics(const char *hostname, char *plugin, char *type,
146                 sdb_time_t last_update, user_data_t *ud)
148         char  name[strlen(plugin) + strlen(type) + 2];
149         char *plugin_instance, *type_instance;
151         char metric_id[(ud->ts_base ? strlen(ud->ts_base) : 0)
152                 + strlen(hostname) + sizeof(name) + 7];
153         sdb_metric_store_t store = { ud->ts_type, metric_id };
155         sdb_data_t data = { SDB_TYPE_STRING, { .string = NULL } };
157         int status;
159         snprintf(name, sizeof(name), "%s/%s", plugin, type);
161         if (ud->ts_base) {
162                 snprintf(metric_id, sizeof(metric_id), "%s/%s/%s.rrd",
163                                 ud->ts_base, hostname, name);
164                 status = sdb_store_metric(hostname, name, &store, last_update);
165         }
166         else
167                 status = sdb_store_metric(hostname, name, NULL, last_update);
168         if (status < 0) {
169                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
170                                 "store/update metric '%s/%s'.", hostname, name);
171                 return -1;
172         }
174         plugin_instance = strchr(plugin, '-');
175         if (plugin_instance) {
176                 *plugin_instance = '\0';
177                 ++plugin_instance;
179                 data.data.string = plugin_instance;
180                 sdb_store_metric_attr(hostname, name,
181                                 "plugin_instance", &data, last_update);
182         }
184         type_instance = strchr(type, '-');
185         if (type_instance) {
186                 *type_instance = '\0';
187                 ++type_instance;
189                 data.data.string = type_instance;
190                 sdb_store_metric_attr(hostname, name,
191                                 "type_instance", &data, last_update);
192         }
194         data.data.string = plugin;
195         sdb_store_metric_attr(hostname, name, "plugin", &data, last_update);
196         data.data.string = type;
197         sdb_store_metric_attr(hostname, name, "type", &data, last_update);
198         return 0;
199 } /* add_metrics */
201 static int
202 get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
203                 size_t n, sdb_data_t *data, sdb_object_t *user_data)
205         state_t *state;
206         sdb_data_t last_update;
208         char *hostname;
209         char *plugin;
210         char *type;
212         assert(user_data);
214         /* 0: <last_update> <hostname>
215          * 1: <plugin>
216          * 2: <type> */
217         assert(n == 3);
218         assert((data[0].type == SDB_TYPE_STRING)
219                         && (data[1].type == SDB_TYPE_STRING)
220                         && (data[2].type == SDB_TYPE_STRING));
222         hostname = data[0].data.string;
223         plugin   = data[1].data.string;
224         type     = data[2].data.string;
226         hostname = strchr(hostname, ' ');
227         if (! hostname) {
228                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Expected to find "
229                                 "a space character in the LISTVAL response");
230                 return -1;
231         }
232         *hostname = '\0';
233         ++hostname;
235         if (sdb_data_parse(data[0].data.string, SDB_TYPE_DATETIME, &last_update)) {
236                 char errbuf[1024];
237                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
238                                 "timestamp '%s' returned by LISTVAL: %s", data[0].data.string,
239                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
240                 return -1;
241         }
243         state = SDB_OBJ_WRAPPER(user_data)->data;
244         if (store_host(state, hostname, last_update.data.datetime))
245                 return -1;
247         if (add_metrics(hostname, plugin, type,
248                                 last_update.data.datetime, state->ud))
249                 ++state->metrics_failed;
250         else
251                 ++state->metrics_updated;
252         return 0;
253 } /* get_data */
255 /*
256  * plugin API
257  */
259 static int
260 sdb_collectd_init(sdb_object_t *user_data)
262         user_data_t *ud;
264         if (! user_data)
265                 return -1;
267         ud = SDB_OBJ_WRAPPER(user_data)->data;
268         if (sdb_unixsock_client_connect(ud->client)) {
269                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: "
270                                 "Failed to connect to collectd.");
271                 return -1;
272         }
274         sdb_log(SDB_LOG_INFO, "collectd::unixsock backend: Successfully "
275                         "connected to collectd @ %s.",
276                         sdb_unixsock_client_path(ud->client));
277         return 0;
278 } /* sdb_collectd_init */
280 static int
281 sdb_collectd_collect(sdb_object_t *user_data)
283         user_data_t *ud;
285         char  buffer[1024];
286         char *line;
287         char *msg;
289         char *endptr = NULL;
290         long int count;
292         state_t state = STATE_INIT;
293         sdb_object_wrapper_t state_obj = SDB_OBJECT_WRAPPER_STATIC(&state);
295         if (! user_data)
296                 return -1;
298         ud = SDB_OBJ_WRAPPER(user_data)->data;
299         state.ud = ud;
301         if (sdb_unixsock_client_send(ud->client, "LISTVAL") <= 0) {
302                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to send "
303                                 "LISTVAL command to collectd @ %s.",
304                                 sdb_unixsock_client_path(ud->client));
305                 return -1;
306         }
308         line = sdb_unixsock_client_recv(ud->client, buffer, sizeof(buffer));
309         if (! line) {
310                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to read "
311                                 "status of LISTVAL command from collectd @ %s.",
312                                 sdb_unixsock_client_path(ud->client));
313                 return -1;
314         }
316         msg = strchr(line, ' ');
317         if (msg) {
318                 *msg = '\0';
319                 ++msg;
320         }
322         errno = 0;
323         count = strtol(line, &endptr, /* base */ 0);
324         if (errno || (line == endptr)) {
325                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
326                                 "status of LISTVAL command from collectd @ %s.",
327                                 sdb_unixsock_client_path(ud->client));
328                 return -1;
329         }
331         if (count < 0) {
332                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to get "
333                                 "value list from collectd @ %s: %s",
334                                 sdb_unixsock_client_path(ud->client),
335                                 msg ? msg : line);
336                 return -1;
337         }
339         if (sdb_unixsock_client_process_lines(ud->client, get_data,
340                                 SDB_OBJ(&state_obj), count, /* delim */ "/",
341                                 /* column count = */ 3,
342                                 SDB_TYPE_STRING, SDB_TYPE_STRING, SDB_TYPE_STRING)) {
343                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed "
344                                 "to read response from collectd @ %s.",
345                                 sdb_unixsock_client_path(ud->client));
346                 return -1;
347         }
349         if (state.current_host) {
350                 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
351                                 "%i metric%s (%i failed) for host '%s'.",
352                                 state.metrics_updated, state.metrics_updated == 1 ? "" : "s",
353                                 state.metrics_failed, state.current_host);
354                 free(state.current_host);
355         }
356         return 0;
357 } /* collect */
359 static int
360 sdb_collectd_config_instance(oconfig_item_t *ci)
362         char *name = NULL;
363         char *socket_path = NULL;
365         sdb_object_t *user_data;
366         user_data_t *ud;
368         int i;
370         if (oconfig_get_string(ci, &name)) {
371                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance requires "
372                                 "a single string argument\n\tUsage: <Instance NAME>");
373                 return -1;
374         }
376         ud = calloc(1, sizeof(*ud));
377         if (! ud) {
378                 char errbuf[1024];
379                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
380                                 "allocate user-data object: %s",
381                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
382                 return -1;
383         }
385         for (i = 0; i < ci->children_num; ++i) {
386                 oconfig_item_t *child = ci->children + i;
388                 if (! strcasecmp(child->key, "Socket"))
389                         oconfig_get_string(child, &socket_path);
390                 else if (! strcasecmp(child->key, "TimeseriesBackend"))
391                         oconfig_get_string(child, &ud->ts_type);
392                 else if (! strcasecmp(child->key, "TimeseriesBaseURL"))
393                         oconfig_get_string(child, &ud->ts_base);
394                 else
395                         sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
396                                         "unknown config option '%s' inside <Instance %s>.",
397                                         child->key, name);
398         }
400         if ((ud->ts_type == NULL) != (ud->ts_base == NULL)) {
401                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Both options, "
402                                 "TimeseriesBackend and TimeseriesBaseURL, have to be "
403                                 "specified.");
404                 ud->ts_type = ud->ts_base = NULL;
405                 user_data_destroy(ud);
406                 return -1;
407         }
409         if (ud->ts_type) {
410                 /* TODO: add support for other backend types
411                  * -> will require different ID generation */
412                 if (strcasecmp(ud->ts_type, "rrdtool")
413                                 && strcasecmp(ud->ts_type, "rrdcached")) {
414                         sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: "
415                                         "TimeseriesBackend '%s' is not supported - "
416                                         "use 'rrdtool' instead.", ud->ts_type);
417                         ud->ts_type = ud->ts_base = NULL;
418                         user_data_destroy(ud);
419                         return -1;
420                 }
422                 ud->ts_type = strdup(ud->ts_type);
423                 ud->ts_base = strdup(ud->ts_base);
424                 if ((! ud->ts_type) || (! ud->ts_base)) {
425                         sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed "
426                                         "to duplicate a string");
427                         user_data_destroy(ud);
428                         return -1;
429                 }
430         }
432         if (! socket_path) {
433                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance '%s' "
434                                 "missing the 'Socket' option.", name);
435                 user_data_destroy(ud);
436                 return -1;
437         }
439         ud->client = sdb_unixsock_client_create(socket_path);
440         if (! ud->client) {
441                 char errbuf[1024];
442                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to create "
443                                 "unixsock client: %s",
444                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
445                 user_data_destroy(ud);
446                 return -1;
447         }
449         user_data = sdb_object_create_wrapper("collectd-userdata", ud,
450                         user_data_destroy);
451         if (! user_data) {
452                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
453                                 "user-data wrapper object");
454                 user_data_destroy(ud);
455                 return -1;
456         }
458         sdb_plugin_register_init(name, sdb_collectd_init, user_data);
459         sdb_plugin_register_collector(name, sdb_collectd_collect,
460                         /* interval */ NULL, user_data);
462         /* pass control to the list */
463         sdb_object_deref(user_data);
464         return 0;
465 } /* sdb_collectd_config_instance */
467 static int
468 sdb_collectd_config(oconfig_item_t *ci)
470         int i;
472         if (! ci) /* nothing to do to deconfigure this plugin */
473                 return 0;
475         for (i = 0; i < ci->children_num; ++i) {
476                 oconfig_item_t *child = ci->children + i;
478                 if (! strcasecmp(child->key, "Instance"))
479                         sdb_collectd_config_instance(child);
480                 else
481                         sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
482                                         "unknown config option '%s'.", child->key);
483         }
484         return 0;
485 } /* sdb_collectd_config */
487 int
488 sdb_module_init(sdb_plugin_info_t *info)
490         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
491                         "backend accessing the system statistics collection daemon "
492                         "throught the UNIXSOCK interface");
493         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
494                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
495         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
496         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
497         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
499         sdb_plugin_register_config(sdb_collectd_config);
500         return 0;
501 } /* sdb_version_extra */
503 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */