Code

f745fc9316d66e9849c219c4ebe2e1d3769a78a9
[sysdb.git] / src / backend / collectd.c
1 /*
2  * SysDB - src/backend/collectd.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/string.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         sdb_host_t host = SDB_HOST_INIT;
66         char name[strlen(hostname) + 1];
68         int status;
70         strncpy(name, hostname, sizeof(name));
72         host.host_name = name;
73         host.host_last_update = last_update;
75         status = sdb_store_host(&host);
77         if (status < 0) {
78                 fprintf(stderr, "collectd backend: Failed to store/update "
79                                 "host '%s'.\n", name);
80                 return -1;
81         }
82         else if (status > 0) /* value too old */
83                 return 0;
85         fprintf(stderr, "collectd backend: Added/updated host '%s' "
86                         "(last update timestamp = %"PRIscTIME").\n",
87                         name, last_update);
88         return 0;
89 } /* sdb_collectd_add_host */
91 static int
92 sdb_collectd_add_svc(const char *hostname, const char *plugin,
93                 const char *type, sdb_time_t last_update)
94 {
95         sdb_service_t svc = SDB_SVC_INIT;
96         char host[strlen(hostname) + 1];
97         char name[strlen(plugin) + strlen(type) + 2];
99         int status;
101         strncpy(host, hostname, sizeof(host));
102         snprintf(name, sizeof(name), "%s/%s", plugin, type);
104         svc.hostname = host;
105         svc.svc_name = name;
106         svc.svc_last_update = last_update;
108         status = sdb_store_service(&svc);
109         if (status < 0) {
110                 fprintf(stderr, "collectd backend: Failed to store/update "
111                                 "service '%s/%s'.\n", host, name);
112                 return -1;
113         }
114         return 0;
115 } /* sdb_collectd_add_svc */
117 static int
118 sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
119                 size_t n, sdb_data_t *data, sdb_object_t *user_data)
121         sdb_collectd_state_t *state;
123         const char *hostname;
124         const char *plugin;
125         const char *type;
126         sdb_time_t last_update;
128         assert(user_data);
130         assert(n == 4);
131         assert((data[0].type == SDB_TYPE_DATETIME)
132                         && (data[1].type == SDB_TYPE_STRING)
133                         && (data[2].type == SDB_TYPE_STRING)
134                         && (data[3].type == SDB_TYPE_STRING));
136         last_update = data[0].data.datetime;
137         hostname = data[1].data.string;
138         plugin   = data[2].data.string;
139         type     = data[3].data.string;
141         state = SDB_OBJ_WRAPPER(user_data)->data;
143         if (! state->current_host) {
144                 state->current_host = strdup(hostname);
145                 state->current_timestamp = last_update;
146         }
148         if (! state->current_host) {
149                 char errbuf[1024];
150                 fprintf(stderr, "collectd backend: Failed to allocate "
151                                 "string buffer: %s\n",
152                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
153                 return -1;
154         }
156         if (! sdb_store_get_host(hostname))
157                 sdb_collectd_add_host(hostname, last_update);
159         if (sdb_collectd_add_svc(hostname, plugin, type, last_update))
160                 ++state->svc_failed;
161         else
162                 ++state->svc_updated;
164         if (! strcasecmp(state->current_host, hostname)) {
165                 if (last_update > state->current_timestamp)
166                         state->current_timestamp = last_update;
167                 return 0;
168         }
170         /* new host */
171         sdb_collectd_add_host(hostname, last_update);
173         fprintf(stderr, "collectd backend: Added/updated "
174                         "%i service%s (%i failed) for host '%s'.\n",
175                         state->svc_updated, state->svc_updated == 1 ? "" : "s",
176                         state->svc_failed, state->current_host);
177         state->svc_updated = state->svc_failed = 0;
179         free(state->current_host);
180         state->current_host = strdup(hostname);
181         state->current_timestamp = last_update;
182         return 0;
183 } /* sdb_collectd_get_data */
185 /*
186  * plugin API
187  */
189 static int
190 sdb_collectd_init(sdb_object_t *user_data)
192         sdb_unixsock_client_t *client;
194         if (! user_data)
195                 return -1;
197         client = SDB_OBJ_WRAPPER(user_data)->data;
198         if (sdb_unixsock_client_connect(client)) {
199                 fprintf(stderr, "collectd backend: "
200                                 "Failed to connect to collectd.\n");
201                 return -1;
202         }
204         fprintf(stderr, "collectd backend: Successfully "
205                         "connected to collectd @ %s.\n",
206                         sdb_unixsock_client_path(client));
207         return 0;
208 } /* sdb_collectd_init */
210 static int
211 sdb_collectd_shutdown(__attribute__((unused)) sdb_object_t *user_data)
213         return 0;
214 } /* sdb_collectd_shutdown */
216 static int
217 sdb_collectd_collect(sdb_object_t *user_data)
219         sdb_unixsock_client_t *client;
221         char  buffer[1024];
222         char *line;
223         char *msg;
225         char *endptr = NULL;
226         long int count;
228         sdb_collectd_state_t state = SDB_COLLECTD_STATE_INIT;
229         sdb_object_wrapper_t state_obj = SDB_OBJECT_WRAPPER_STATIC(&state,
230                         /* destructor = */ NULL);
232         if (! user_data)
233                 return -1;
235         client = SDB_OBJ_WRAPPER(user_data)->data;
237         if (sdb_unixsock_client_send(client, "LISTVAL") <= 0) {
238                 fprintf(stderr, "collectd backend: Failed to send LISTVAL command "
239                                 "to collectd @ %s.\n", sdb_unixsock_client_path(client));
240                 return -1;
241         }
243         line = sdb_unixsock_client_recv(client, buffer, sizeof(buffer));
244         if (! line) {
245                 fprintf(stderr, "collectd backend: Failed to read status "
246                                 "of LISTVAL command from collectd @ %s.\n",
247                                 sdb_unixsock_client_path(client));
248                 return -1;
249         }
251         msg = strchr(line, ' ');
252         if (msg) {
253                 *msg = '\0';
254                 ++msg;
255         }
257         errno = 0;
258         count = strtol(line, &endptr, /* base */ 0);
259         if (errno || (line == endptr)) {
260                 fprintf(stderr, "collectd backend: Failed to parse status "
261                                 "of LISTVAL command from collectd @ %s.\n",
262                                 sdb_unixsock_client_path(client));
263                 return -1;
264         }
266         if (count < 0) {
267                 fprintf(stderr, "collectd backend: Failed to get value list "
268                                 "from collectd @ %s: %s\n", sdb_unixsock_client_path(client),
269                                 msg ? msg : line);
270                 return -1;
271         }
273         if (sdb_unixsock_client_process_lines(client, sdb_collectd_get_data,
274                                 SDB_OBJ(&state_obj), count, /* delim */ " /",
275                                 /* column count = */ 4,
276                                 SDB_TYPE_DATETIME, SDB_TYPE_STRING,
277                                 SDB_TYPE_STRING, SDB_TYPE_STRING)) {
278                 fprintf(stderr, "collectd backend: Failed to read response "
279                                 "from collectd @ %s.\n", sdb_unixsock_client_path(client));
280                 return -1;
281         }
283         if (state.current_host) {
284                 sdb_collectd_add_host(state.current_host, state.current_timestamp);
285                 fprintf(stderr, "collectd backend: Added/updated "
286                                 "%i service%s (%i failed) for host '%s'.\n",
287                                 state.svc_updated, state.svc_updated == 1 ? "" : "s",
288                                 state.svc_failed, state.current_host);
289         }
290         return 0;
291 } /* sdb_collectd_collect */
293 static int
294 sdb_collectd_config_instance(oconfig_item_t *ci)
296         char *name = NULL;
297         char *socket = NULL;
299         char cb_name[1024];
301         sdb_object_t *user_data;
302         sdb_unixsock_client_t *client;
304         int i;
306         if (oconfig_get_string(ci, &name)) {
307                 fprintf(stderr, "collectd backend: Instance requires a single "
308                                 "string argument\n\tUsage: <Instance NAME>\n");
309                 return -1;
310         }
312         for (i = 0; i < ci->children_num; ++i) {
313                 oconfig_item_t *child = ci->children + i;
315                 if (! strcasecmp(child->key, "Socket"))
316                         oconfig_get_string(child, &socket);
317                 else
318                         fprintf(stderr, "collectd backend: Ignoring unknown config "
319                                         "option '%s' inside <Instance %s>.\n",
320                                         child->key, name);
321         }
323         if (! socket) {
324                 fprintf(stderr, "collectd backend: Instance '%s' missing "
325                                 "the 'Socket' option.\n", name);
326                 return -1;
327         }
329         snprintf(cb_name, sizeof(cb_name), "collectd-%s", name);
330         cb_name[sizeof(cb_name) - 1] = '\0';
332         client = sdb_unixsock_client_create(socket);
333         if (! client) {
334                 char errbuf[1024];
335                 fprintf(stderr, "collectd backend: Failed to create unixsock client: "
336                                 "%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
337                 return -1;
338         }
340         user_data = sdb_object_create_wrapper(client,
341                         (void (*)(void *))sdb_unixsock_client_destroy);
342         if (! user_data) {
343                 sdb_unixsock_client_destroy(client);
344                 fprintf(stderr, "collectd backend: Failed to allocate sdb_object_t\n");
345                 return -1;
346         }
348         sdb_plugin_register_init(cb_name, sdb_collectd_init, user_data);
349         sdb_plugin_register_shutdown(cb_name, sdb_collectd_shutdown, user_data);
351         sdb_plugin_register_collector(cb_name, sdb_collectd_collect,
352                         /* interval */ NULL, user_data);
354         /* pass control to the list */
355         sdb_object_deref(user_data);
356         return 0;
357 } /* sdb_collectd_config_instance */
359 static int
360 sdb_collectd_config(oconfig_item_t *ci)
362         int i;
364         for (i = 0; i < ci->children_num; ++i) {
365                 oconfig_item_t *child = ci->children + i;
367                 if (! strcasecmp(child->key, "Instance"))
368                         sdb_collectd_config_instance(child);
369                 else
370                         fprintf(stderr, "collectd backend: Ignoring unknown config "
371                                         "option '%s'.\n", child->key);
372         }
373         return 0;
374 } /* sdb_collectd_config */
376 int
377 sdb_module_init(sdb_plugin_info_t *info)
379         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "collectd");
380         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
381                         "backend accessing the system statistics collection daemon");
382         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
383                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
384         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
385         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
386         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
388         sdb_plugin_register_config("collectd", sdb_collectd_config);
389         return 0;
390 } /* sdb_version_extra */
392 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */