Code

3a41fdf65e0e7d628d9a7e032412fb8f6f9e8649
[sysdb.git] / src / backend / collectd / unixsock.c
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 #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         char *current_host;
56         sdb_time_t current_timestamp;
57         int svc_updated;
58         int svc_failed;
59 } sdb_collectd_state_t;
60 #define SDB_COLLECTD_STATE_INIT { NULL, 0, 0, 0 }
62 /*
63  * private helper functions
64  */
66 /* store the specified host-name (once per iteration) */
67 static int
68 sdb_collectd_store_host(sdb_collectd_state_t *state,
69                 const char *hostname, sdb_time_t last_update)
70 {
71         int status;
73         if (last_update > state->current_timestamp)
74                 state->current_timestamp = last_update;
76         if (state->current_host && (! strcasecmp(state->current_host, hostname)))
77                 return 0;
78         /* else: first/new host */
80         if (state->current_host) {
81                 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
82                                 "%i service%s (%i failed) for host '%s'.",
83                                 state->svc_updated, state->svc_updated == 1 ? "" : "s",
84                                 state->svc_failed, state->current_host);
85                 state->svc_updated = state->svc_failed = 0;
86                 free(state->current_host);
87         }
89         state->current_host = strdup(hostname);
90         if (! state->current_host) {
91                 char errbuf[1024];
92                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
93                                 "string buffer: %s",
94                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
95                 return -1;
96         }
98         status = sdb_store_host(hostname, last_update);
100         if (status < 0) {
101                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
102                                 "store/update host '%s'.", hostname);
103                 return -1;
104         }
105         else if (status > 0) /* value too old */
106                 return 0;
108         sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
109                         "host '%s' (last update timestamp = %"PRIscTIME").",
110                         hostname, last_update);
111         return 0;
112 } /* sdb_collectd_store_host */
114 static int
115 sdb_collectd_add_svc(const char *hostname, const char *plugin,
116                 const char *type, sdb_time_t last_update)
118         char name[strlen(plugin) + strlen(type) + 2];
119         int  status;
121         snprintf(name, sizeof(name), "%s/%s", plugin, type);
123         status = sdb_store_service(hostname, name, last_update);
124         if (status < 0) {
125                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
126                                 "store/update service '%s/%s'.", hostname, name);
127                 return -1;
128         }
129         return 0;
130 } /* sdb_collectd_add_svc */
132 static int
133 sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
134                 size_t n, sdb_data_t *data, sdb_object_t *user_data)
136         sdb_collectd_state_t *state;
138         const char *hostname;
139         const char *plugin;
140         const char *type;
141         sdb_time_t last_update;
143         assert(user_data);
145         assert(n == 4);
146         assert((data[0].type == SDB_TYPE_DATETIME)
147                         && (data[1].type == SDB_TYPE_STRING)
148                         && (data[2].type == SDB_TYPE_STRING)
149                         && (data[3].type == SDB_TYPE_STRING));
151         last_update = data[0].data.datetime;
152         hostname = data[1].data.string;
153         plugin   = data[2].data.string;
154         type     = data[3].data.string;
156         state = SDB_OBJ_WRAPPER(user_data)->data;
157         if (sdb_collectd_store_host(state, hostname, last_update))
158                 return -1;
160         if (sdb_collectd_add_svc(hostname, plugin, type, last_update))
161                 ++state->svc_failed;
162         else
163                 ++state->svc_updated;
164         return 0;
165 } /* sdb_collectd_get_data */
167 /*
168  * plugin API
169  */
171 static int
172 sdb_collectd_init(sdb_object_t *user_data)
174         sdb_unixsock_client_t *client;
176         if (! user_data)
177                 return -1;
179         client = SDB_OBJ_WRAPPER(user_data)->data;
180         if (sdb_unixsock_client_connect(client)) {
181                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: "
182                                 "Failed to connect to collectd.");
183                 return -1;
184         }
186         sdb_log(SDB_LOG_INFO, "collectd::unixsock backend: Successfully "
187                         "connected to collectd @ %s.",
188                         sdb_unixsock_client_path(client));
189         return 0;
190 } /* sdb_collectd_init */
192 static int
193 sdb_collectd_shutdown(__attribute__((unused)) sdb_object_t *user_data)
195         if (! user_data)
196                 return -1;
198         sdb_unixsock_client_destroy(SDB_OBJ_WRAPPER(user_data)->data);
199         SDB_OBJ_WRAPPER(user_data)->data = NULL;
200         return 0;
201 } /* sdb_collectd_shutdown */
203 static int
204 sdb_collectd_collect(sdb_object_t *user_data)
206         sdb_unixsock_client_t *client;
208         char  buffer[1024];
209         char *line;
210         char *msg;
212         char *endptr = NULL;
213         long int count;
215         sdb_collectd_state_t state = SDB_COLLECTD_STATE_INIT;
216         sdb_object_wrapper_t state_obj = SDB_OBJECT_WRAPPER_STATIC(&state,
217                         /* destructor = */ NULL);
219         if (! user_data)
220                 return -1;
222         client = SDB_OBJ_WRAPPER(user_data)->data;
224         if (sdb_unixsock_client_send(client, "LISTVAL") <= 0) {
225                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to send "
226                                 "LISTVAL command to collectd @ %s.",
227                                 sdb_unixsock_client_path(client));
228                 return -1;
229         }
231         line = sdb_unixsock_client_recv(client, buffer, sizeof(buffer));
232         if (! line) {
233                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to read "
234                                 "status of LISTVAL command from collectd @ %s.",
235                                 sdb_unixsock_client_path(client));
236                 return -1;
237         }
239         msg = strchr(line, ' ');
240         if (msg) {
241                 *msg = '\0';
242                 ++msg;
243         }
245         errno = 0;
246         count = strtol(line, &endptr, /* base */ 0);
247         if (errno || (line == endptr)) {
248                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
249                                 "status of LISTVAL command from collectd @ %s.",
250                                 sdb_unixsock_client_path(client));
251                 return -1;
252         }
254         if (count < 0) {
255                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to get "
256                                 "value list from collectd @ %s: %s",
257                                 sdb_unixsock_client_path(client),
258                                 msg ? msg : line);
259                 return -1;
260         }
262         if (sdb_unixsock_client_process_lines(client, sdb_collectd_get_data,
263                                 SDB_OBJ(&state_obj), count, /* delim */ " /",
264                                 /* column count = */ 4,
265                                 SDB_TYPE_DATETIME, SDB_TYPE_STRING,
266                                 SDB_TYPE_STRING, SDB_TYPE_STRING)) {
267                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed "
268                                 "to read response from collectd @ %s.",
269                                 sdb_unixsock_client_path(client));
270                 return -1;
271         }
273         if (state.current_host) {
274                 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
275                                 "%i service%s (%i failed) for host '%s'.",
276                                 state.svc_updated, state.svc_updated == 1 ? "" : "s",
277                                 state.svc_failed, state.current_host);
278                 free(state.current_host);
279         }
280         return 0;
281 } /* sdb_collectd_collect */
283 static int
284 sdb_collectd_config_instance(oconfig_item_t *ci)
286         char *name = NULL;
287         char *socket_path = NULL;
289         char cb_name[1024];
291         sdb_object_t *user_data;
292         sdb_unixsock_client_t *client;
294         int i;
296         if (oconfig_get_string(ci, &name)) {
297                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance requires "
298                                 "a single string argument\n\tUsage: <Instance NAME>");
299                 return -1;
300         }
302         for (i = 0; i < ci->children_num; ++i) {
303                 oconfig_item_t *child = ci->children + i;
305                 if (! strcasecmp(child->key, "Socket"))
306                         oconfig_get_string(child, &socket_path);
307                 else
308                         sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
309                                         "unknown config option '%s' inside <Instance %s>.",
310                                         child->key, name);
311         }
313         if (! socket_path) {
314                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance '%s' "
315                                 "missing the 'Socket' option.", name);
316                 return -1;
317         }
319         snprintf(cb_name, sizeof(cb_name), "collectd::unixsock::%s", name);
320         cb_name[sizeof(cb_name) - 1] = '\0';
322         client = sdb_unixsock_client_create(socket_path);
323         if (! client) {
324                 char errbuf[1024];
325                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to create "
326                                 "unixsock client: %s",
327                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
328                 return -1;
329         }
331         user_data = sdb_object_create_wrapper("unixsock-client", client,
332                         (void (*)(void *))sdb_unixsock_client_destroy);
333         if (! user_data) {
334                 sdb_unixsock_client_destroy(client);
335                 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
336                                 "sdb_object_t");
337                 return -1;
338         }
340         sdb_plugin_register_init(cb_name, sdb_collectd_init, user_data);
341         sdb_plugin_register_shutdown(cb_name, sdb_collectd_shutdown, user_data);
343         sdb_plugin_register_collector(cb_name, sdb_collectd_collect,
344                         /* interval */ NULL, user_data);
346         /* pass control to the list */
347         sdb_object_deref(user_data);
348         return 0;
349 } /* sdb_collectd_config_instance */
351 static int
352 sdb_collectd_config(oconfig_item_t *ci)
354         int i;
356         if (! ci) /* nothing to do to deconfigure this plugin */
357                 return 0;
359         for (i = 0; i < ci->children_num; ++i) {
360                 oconfig_item_t *child = ci->children + i;
362                 if (! strcasecmp(child->key, "Instance"))
363                         sdb_collectd_config_instance(child);
364                 else
365                         sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
366                                         "unknown config option '%s'.", child->key);
367         }
368         return 0;
369 } /* sdb_collectd_config */
371 int
372 sdb_module_init(sdb_plugin_info_t *info)
374         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "collectd::unixsock");
375         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
376                         "backend accessing the system statistics collection daemon "
377                         "throught the UNIXSOCK interface");
378         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
379                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
380         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
381         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
382         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
384         sdb_plugin_register_config(sdb_collectd_config);
385         return 0;
386 } /* sdb_version_extra */
388 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */