Code

Store a separate last_update timestamp for metric stores.
[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>
47 #include <strings.h>
49 SDB_PLUGIN_MAGIC;
51 /*
52  * private data types
53  */
55 typedef struct {
56         sdb_unixsock_client_t *client;
58         char *ts_type;
59         char *ts_base;
60 } user_data_t;
61 #define UD(obj) ((user_data_t *)(obj))
63 typedef struct {
64         char *current_host;
65         sdb_time_t current_timestamp;
66         int metrics_updated;
67         int metrics_failed;
69         user_data_t *ud;
70 } state_t;
71 #define STATE_INIT { NULL, 0, 0, 0, NULL }
73 /*
74  * private helper functions
75  */
77 static void
78 user_data_destroy(void *obj)
79 {
80         user_data_t *ud = UD(obj);
82         if (! obj)
83                 return;
85         if (ud->client)
86                 sdb_unixsock_client_destroy(ud->client);
87         ud->client = NULL;
89         if (ud->ts_type)
90                 free(ud->ts_type);
91         if (ud->ts_base)
92                 free(ud->ts_base);
93         ud->ts_type = ud->ts_base = NULL;
95         free(ud);
96 } /* user_data_destroy */
98 /* store the specified host-name (once per iteration) */
99 static int
100 store_host(state_t *state, const char *hostname, sdb_time_t last_update)
102         int status;
104         if (last_update > state->current_timestamp)
105                 state->current_timestamp = last_update;
107         if (state->current_host && (! strcasecmp(state->current_host, hostname)))
108                 return 0;
109         /* else: first/new host */
111         if (state->current_host) {
112                 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
113                                 "%i metric%s (%i failed) for host '%s'.",
114                                 state->metrics_updated, state->metrics_updated == 1 ? "" : "s",
115                                 state->metrics_failed, state->current_host);
116                 state->metrics_updated = state->metrics_failed = 0;
117                 free(state->current_host);
118         }
120         state->current_host = strdup(hostname);
121         if (! state->current_host) {
122                 char errbuf[1024];
123                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
124                                 "string buffer: %s",
125                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
126                 return -1;
127         }
129         status = sdb_plugin_store_host(hostname, last_update);
131         if (status < 0) {
132                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
133                                 "store/update host '%s'.", hostname);
134                 return -1;
135         }
136         else if (status > 0) /* value too old */
137                 return 0;
139         sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
140                         "host '%s' (last update timestamp = %"PRIsdbTIME").",
141                         hostname, last_update);
142         return 0;
143 } /* store_host */
145 static int
146 add_metrics(const char *hostname, char *plugin, char *type,
147                 sdb_time_t last_update, user_data_t *ud)
149         char  name[strlen(plugin) + strlen(type) + 2];
150         char *plugin_instance, *type_instance;
152         char metric_id[(ud->ts_base ? strlen(ud->ts_base) : 0)
153                 + strlen(hostname) + sizeof(name) + 7];
154         sdb_metric_store_t store = { ud->ts_type, metric_id, last_update };
156         sdb_data_t data = { SDB_TYPE_STRING, { .string = NULL } };
158         int status;
160         snprintf(name, sizeof(name), "%s/%s", plugin, type);
162         if (ud->ts_base) {
163                 snprintf(metric_id, sizeof(metric_id), "%s/%s/%s.rrd",
164                                 ud->ts_base, hostname, name);
165                 status = sdb_plugin_store_metric(hostname, name, &store, last_update);
166         }
167         else
168                 status = sdb_plugin_store_metric(hostname, name, NULL, last_update);
169         if (status < 0) {
170                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
171                                 "store/update metric '%s/%s'.", hostname, name);
172                 return -1;
173         }
175         plugin_instance = strchr(plugin, '-');
176         if (plugin_instance) {
177                 *plugin_instance = '\0';
178                 ++plugin_instance;
180                 data.data.string = plugin_instance;
181                 sdb_plugin_store_metric_attribute(hostname, name,
182                                 "plugin_instance", &data, last_update);
183         }
185         type_instance = strchr(type, '-');
186         if (type_instance) {
187                 *type_instance = '\0';
188                 ++type_instance;
190                 data.data.string = type_instance;
191                 sdb_plugin_store_metric_attribute(hostname, name,
192                                 "type_instance", &data, last_update);
193         }
195         data.data.string = plugin;
196         sdb_plugin_store_metric_attribute(hostname, name, "plugin", &data, last_update);
197         data.data.string = type;
198         sdb_plugin_store_metric_attribute(hostname, name, "type", &data, last_update);
199         return 0;
200 } /* add_metrics */
202 static int
203 get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
204                 size_t n, sdb_data_t *data, sdb_object_t *user_data)
206         state_t *state;
207         sdb_data_t last_update;
209         char *hostname;
210         char *plugin;
211         char *type;
213         assert(user_data);
215         /* 0: <last_update> <hostname>
216          * 1: <plugin>
217          * 2: <type> */
218         assert(n == 3);
219         assert((data[0].type == SDB_TYPE_STRING)
220                         && (data[1].type == SDB_TYPE_STRING)
221                         && (data[2].type == SDB_TYPE_STRING));
223         hostname = data[0].data.string;
224         plugin   = data[1].data.string;
225         type     = data[2].data.string;
227         hostname = strchr(hostname, ' ');
228         if (! hostname) {
229                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Expected to find "
230                                 "a space character in the LISTVAL response");
231                 return -1;
232         }
233         *hostname = '\0';
234         ++hostname;
236         if (sdb_data_parse(data[0].data.string, SDB_TYPE_DATETIME, &last_update)) {
237                 char errbuf[1024];
238                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
239                                 "timestamp '%s' returned by LISTVAL: %s", data[0].data.string,
240                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
241                 return -1;
242         }
244         state = SDB_OBJ_WRAPPER(user_data)->data;
245         if (store_host(state, hostname, last_update.data.datetime))
246                 return -1;
248         if (add_metrics(hostname, plugin, type,
249                                 last_update.data.datetime, state->ud))
250                 ++state->metrics_failed;
251         else
252                 ++state->metrics_updated;
253         return 0;
254 } /* get_data */
256 /*
257  * plugin API
258  */
260 static int
261 sdb_collectd_init(sdb_object_t *user_data)
263         user_data_t *ud;
265         if (! user_data)
266                 return -1;
268         ud = SDB_OBJ_WRAPPER(user_data)->data;
269         if (sdb_unixsock_client_connect(ud->client)) {
270                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: "
271                                 "Failed to connect to collectd.");
272                 return -1;
273         }
275         sdb_log(SDB_LOG_INFO, "collectd::unixsock backend: Successfully "
276                         "connected to collectd @ %s.",
277                         sdb_unixsock_client_path(ud->client));
278         return 0;
279 } /* sdb_collectd_init */
281 static int
282 sdb_collectd_collect(sdb_object_t *user_data)
284         user_data_t *ud;
286         char  buffer[1024];
287         char *line;
288         char *msg;
290         char *endptr = NULL;
291         long int count;
293         state_t state = STATE_INIT;
294         sdb_object_wrapper_t state_obj = SDB_OBJECT_WRAPPER_STATIC(&state);
296         if (! user_data)
297                 return -1;
299         ud = SDB_OBJ_WRAPPER(user_data)->data;
300         state.ud = ud;
302         if (sdb_unixsock_client_send(ud->client, "LISTVAL") <= 0) {
303                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to send "
304                                 "LISTVAL command to collectd @ %s.",
305                                 sdb_unixsock_client_path(ud->client));
306                 return -1;
307         }
309         line = sdb_unixsock_client_recv(ud->client, buffer, sizeof(buffer));
310         if (! line) {
311                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to read "
312                                 "status of LISTVAL command from collectd @ %s.",
313                                 sdb_unixsock_client_path(ud->client));
314                 return -1;
315         }
317         msg = strchr(line, ' ');
318         if (msg) {
319                 *msg = '\0';
320                 ++msg;
321         }
323         errno = 0;
324         count = strtol(line, &endptr, /* base */ 0);
325         if (errno || (line == endptr)) {
326                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
327                                 "status of LISTVAL command from collectd @ %s.",
328                                 sdb_unixsock_client_path(ud->client));
329                 return -1;
330         }
332         if (count < 0) {
333                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to get "
334                                 "value list from collectd @ %s: %s",
335                                 sdb_unixsock_client_path(ud->client),
336                                 msg ? msg : line);
337                 return -1;
338         }
340         if (sdb_unixsock_client_process_lines(ud->client, get_data,
341                                 SDB_OBJ(&state_obj), count, /* delim */ "/",
342                                 /* column count = */ 3,
343                                 SDB_TYPE_STRING, SDB_TYPE_STRING, SDB_TYPE_STRING)) {
344                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed "
345                                 "to read response from collectd @ %s.",
346                                 sdb_unixsock_client_path(ud->client));
347                 return -1;
348         }
350         if (state.current_host) {
351                 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
352                                 "%i metric%s (%i failed) for host '%s'.",
353                                 state.metrics_updated, state.metrics_updated == 1 ? "" : "s",
354                                 state.metrics_failed, state.current_host);
355                 free(state.current_host);
356         }
357         return 0;
358 } /* collect */
360 static int
361 sdb_collectd_config_instance(oconfig_item_t *ci)
363         char *name = NULL;
364         char *socket_path = NULL;
366         sdb_object_t *user_data;
367         user_data_t *ud;
369         int i;
371         if (oconfig_get_string(ci, &name)) {
372                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance requires "
373                                 "a single string argument\n\tUsage: <Instance NAME>");
374                 return -1;
375         }
377         ud = calloc(1, sizeof(*ud));
378         if (! ud) {
379                 char errbuf[1024];
380                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
381                                 "allocate user-data object: %s",
382                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
383                 return -1;
384         }
386         for (i = 0; i < ci->children_num; ++i) {
387                 oconfig_item_t *child = ci->children + i;
389                 if (! strcasecmp(child->key, "Socket"))
390                         oconfig_get_string(child, &socket_path);
391                 else if (! strcasecmp(child->key, "TimeseriesBackend"))
392                         oconfig_get_string(child, &ud->ts_type);
393                 else if (! strcasecmp(child->key, "TimeseriesBaseURL"))
394                         oconfig_get_string(child, &ud->ts_base);
395                 else
396                         sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
397                                         "unknown config option '%s' inside <Instance %s>.",
398                                         child->key, name);
399         }
401         if ((ud->ts_type == NULL) != (ud->ts_base == NULL)) {
402                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Both options, "
403                                 "TimeseriesBackend and TimeseriesBaseURL, have to be "
404                                 "specified.");
405                 ud->ts_type = ud->ts_base = NULL;
406                 user_data_destroy(ud);
407                 return -1;
408         }
410         if (ud->ts_type) {
411                 /* TODO: add support for other backend types
412                  * -> will require different ID generation */
413                 if (strcasecmp(ud->ts_type, "rrdtool")
414                                 && strcasecmp(ud->ts_type, "rrdcached")) {
415                         sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: "
416                                         "TimeseriesBackend '%s' is not supported - "
417                                         "use 'rrdtool' instead.", ud->ts_type);
418                         ud->ts_type = ud->ts_base = NULL;
419                         user_data_destroy(ud);
420                         return -1;
421                 }
423                 ud->ts_type = strdup(ud->ts_type);
424                 ud->ts_base = strdup(ud->ts_base);
425                 if ((! ud->ts_type) || (! ud->ts_base)) {
426                         sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed "
427                                         "to duplicate a string");
428                         user_data_destroy(ud);
429                         return -1;
430                 }
431         }
433         if (! socket_path) {
434                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance '%s' "
435                                 "missing the 'Socket' option.", name);
436                 user_data_destroy(ud);
437                 return -1;
438         }
440         ud->client = sdb_unixsock_client_create(socket_path);
441         if (! ud->client) {
442                 char errbuf[1024];
443                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to create "
444                                 "unixsock client: %s",
445                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
446                 user_data_destroy(ud);
447                 return -1;
448         }
450         user_data = sdb_object_create_wrapper("collectd-userdata", ud,
451                         user_data_destroy);
452         if (! user_data) {
453                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
454                                 "user-data wrapper object");
455                 user_data_destroy(ud);
456                 return -1;
457         }
459         sdb_plugin_register_init(name, sdb_collectd_init, user_data);
460         sdb_plugin_register_collector(name, sdb_collectd_collect,
461                         /* interval */ NULL, user_data);
463         /* pass control to the list */
464         sdb_object_deref(user_data);
465         return 0;
466 } /* sdb_collectd_config_instance */
468 static int
469 sdb_collectd_config(oconfig_item_t *ci)
471         int i;
473         if (! ci) /* nothing to do to deconfigure this plugin */
474                 return 0;
476         for (i = 0; i < ci->children_num; ++i) {
477                 oconfig_item_t *child = ci->children + i;
479                 if (! strcasecmp(child->key, "Instance"))
480                         sdb_collectd_config_instance(child);
481                 else
482                         sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
483                                         "unknown config option '%s'.", child->key);
484         }
485         return 0;
486 } /* sdb_collectd_config */
488 int
489 sdb_module_init(sdb_plugin_info_t *info)
491         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
492                         "backend accessing the system statistics collection daemon "
493                         "throught the UNIXSOCK interface");
494         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
495                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
496         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
497         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
498         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
500         sdb_plugin_register_config(sdb_collectd_config);
501         return 0;
502 } /* sdb_version_extra */
504 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */