2a834f51982ca1248763674cecb34c41370215ac
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/error.h"
32 #include "utils/string.h"
33 #include "utils/unixsock.h"
35 #include "liboconfig/utils.h"
37 #include <assert.h>
39 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
45 SDB_PLUGIN_MAGIC;
47 /*
48 * private data types
49 */
51 typedef struct {
52 char *current_host;
53 sdb_time_t current_timestamp;
54 int svc_updated;
55 int svc_failed;
56 } sdb_collectd_state_t;
57 #define SDB_COLLECTD_STATE_INIT { NULL, 0, 0, 0 }
59 /*
60 * private helper functions
61 */
63 static int
64 sdb_collectd_add_host(const char *hostname, sdb_time_t last_update)
65 {
66 sdb_host_t host = SDB_HOST_INIT;
67 char name[strlen(hostname) + 1];
69 int status;
71 strncpy(name, hostname, sizeof(name));
73 host.host_name = name;
74 host.host_last_update = last_update;
76 status = sdb_store_host(&host);
78 if (status < 0) {
79 sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to store/update "
80 "host '%s'.\n", name);
81 return -1;
82 }
83 else if (status > 0) /* value too old */
84 return 0;
86 sdb_error_set(SDB_LOG_DEBUG, "collectd backend: Added/updated host '%s' "
87 "(last update timestamp = %"PRIscTIME").\n",
88 name, last_update);
89 return 0;
90 } /* sdb_collectd_add_host */
92 static int
93 sdb_collectd_add_svc(const char *hostname, const char *plugin,
94 const char *type, sdb_time_t last_update)
95 {
96 sdb_service_t svc = SDB_SVC_INIT;
97 char host[strlen(hostname) + 1];
98 char name[strlen(plugin) + strlen(type) + 2];
100 int status;
102 strncpy(host, hostname, sizeof(host));
103 snprintf(name, sizeof(name), "%s/%s", plugin, type);
105 svc.hostname = host;
106 svc.svc_name = name;
107 svc.svc_last_update = last_update;
109 status = sdb_store_service(&svc);
110 if (status < 0) {
111 sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to store/update "
112 "service '%s/%s'.\n", host, name);
113 return -1;
114 }
115 return 0;
116 } /* sdb_collectd_add_svc */
118 static int
119 sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
120 size_t n, sdb_data_t *data, sdb_object_t *user_data)
121 {
122 sdb_collectd_state_t *state;
124 const char *hostname;
125 const char *plugin;
126 const char *type;
127 sdb_time_t last_update;
129 assert(user_data);
131 assert(n == 4);
132 assert((data[0].type == SDB_TYPE_DATETIME)
133 && (data[1].type == SDB_TYPE_STRING)
134 && (data[2].type == SDB_TYPE_STRING)
135 && (data[3].type == SDB_TYPE_STRING));
137 last_update = data[0].data.datetime;
138 hostname = data[1].data.string;
139 plugin = data[2].data.string;
140 type = data[3].data.string;
142 state = SDB_OBJ_WRAPPER(user_data)->data;
144 if (! state->current_host) {
145 state->current_host = strdup(hostname);
146 state->current_timestamp = last_update;
147 }
149 if (! state->current_host) {
150 char errbuf[1024];
151 sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to allocate "
152 "string buffer: %s\n",
153 sdb_strerror(errno, errbuf, sizeof(errbuf)));
154 return -1;
155 }
157 if (! sdb_store_get_host(hostname))
158 sdb_collectd_add_host(hostname, last_update);
160 if (sdb_collectd_add_svc(hostname, plugin, type, last_update))
161 ++state->svc_failed;
162 else
163 ++state->svc_updated;
165 if (! strcasecmp(state->current_host, hostname)) {
166 if (last_update > state->current_timestamp)
167 state->current_timestamp = last_update;
168 return 0;
169 }
171 /* new host */
172 sdb_collectd_add_host(hostname, last_update);
174 sdb_error_set(SDB_LOG_DEBUG, "collectd backend: Added/updated "
175 "%i service%s (%i failed) for host '%s'.\n",
176 state->svc_updated, state->svc_updated == 1 ? "" : "s",
177 state->svc_failed, state->current_host);
178 state->svc_updated = state->svc_failed = 0;
180 free(state->current_host);
181 state->current_host = strdup(hostname);
182 state->current_timestamp = last_update;
183 return 0;
184 } /* sdb_collectd_get_data */
186 /*
187 * plugin API
188 */
190 static int
191 sdb_collectd_init(sdb_object_t *user_data)
192 {
193 sdb_unixsock_client_t *client;
195 if (! user_data)
196 return -1;
198 client = SDB_OBJ_WRAPPER(user_data)->data;
199 if (sdb_unixsock_client_connect(client)) {
200 sdb_error_set(SDB_LOG_ERR, "collectd backend: "
201 "Failed to connect to collectd.\n");
202 return -1;
203 }
205 sdb_error_set(SDB_LOG_INFO, "collectd backend: Successfully "
206 "connected to collectd @ %s.\n",
207 sdb_unixsock_client_path(client));
208 return 0;
209 } /* sdb_collectd_init */
211 static int
212 sdb_collectd_shutdown(__attribute__((unused)) sdb_object_t *user_data)
213 {
214 return 0;
215 } /* sdb_collectd_shutdown */
217 static int
218 sdb_collectd_collect(sdb_object_t *user_data)
219 {
220 sdb_unixsock_client_t *client;
222 char buffer[1024];
223 char *line;
224 char *msg;
226 char *endptr = NULL;
227 long int count;
229 sdb_collectd_state_t state = SDB_COLLECTD_STATE_INIT;
230 sdb_object_wrapper_t state_obj = SDB_OBJECT_WRAPPER_STATIC(&state,
231 /* destructor = */ NULL);
233 if (! user_data)
234 return -1;
236 client = SDB_OBJ_WRAPPER(user_data)->data;
238 if (sdb_unixsock_client_send(client, "LISTVAL") <= 0) {
239 sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to send LISTVAL "
240 "command to collectd @ %s.\n",
241 sdb_unixsock_client_path(client));
242 return -1;
243 }
245 line = sdb_unixsock_client_recv(client, buffer, sizeof(buffer));
246 if (! line) {
247 sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to read status "
248 "of LISTVAL command from collectd @ %s.\n",
249 sdb_unixsock_client_path(client));
250 return -1;
251 }
253 msg = strchr(line, ' ');
254 if (msg) {
255 *msg = '\0';
256 ++msg;
257 }
259 errno = 0;
260 count = strtol(line, &endptr, /* base */ 0);
261 if (errno || (line == endptr)) {
262 sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to parse status "
263 "of LISTVAL command from collectd @ %s.\n",
264 sdb_unixsock_client_path(client));
265 return -1;
266 }
268 if (count < 0) {
269 sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to get value "
270 "list from collectd @ %s: %s\n",
271 sdb_unixsock_client_path(client),
272 msg ? msg : line);
273 return -1;
274 }
276 if (sdb_unixsock_client_process_lines(client, sdb_collectd_get_data,
277 SDB_OBJ(&state_obj), count, /* delim */ " /",
278 /* column count = */ 4,
279 SDB_TYPE_DATETIME, SDB_TYPE_STRING,
280 SDB_TYPE_STRING, SDB_TYPE_STRING)) {
281 sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to read response "
282 "from collectd @ %s.\n", sdb_unixsock_client_path(client));
283 return -1;
284 }
286 if (state.current_host) {
287 sdb_collectd_add_host(state.current_host, state.current_timestamp);
288 sdb_error_set(SDB_LOG_DEBUG, "collectd backend: Added/updated "
289 "%i service%s (%i failed) for host '%s'.\n",
290 state.svc_updated, state.svc_updated == 1 ? "" : "s",
291 state.svc_failed, state.current_host);
292 }
293 return 0;
294 } /* sdb_collectd_collect */
296 static int
297 sdb_collectd_config_instance(oconfig_item_t *ci)
298 {
299 char *name = NULL;
300 char *socket_path = NULL;
302 char cb_name[1024];
304 sdb_object_t *user_data;
305 sdb_unixsock_client_t *client;
307 int i;
309 if (oconfig_get_string(ci, &name)) {
310 sdb_error_set(SDB_LOG_ERR, "collectd backend: Instance requires a "
311 "single string argument\n\tUsage: <Instance NAME>\n");
312 return -1;
313 }
315 for (i = 0; i < ci->children_num; ++i) {
316 oconfig_item_t *child = ci->children + i;
318 if (! strcasecmp(child->key, "Socket"))
319 oconfig_get_string(child, &socket_path);
320 else
321 sdb_error_set(SDB_LOG_WARNING, "collectd backend: Ignoring "
322 "unknown config option '%s' inside <Instance %s>.\n",
323 child->key, name);
324 }
326 if (! socket_path) {
327 sdb_error_set(SDB_LOG_ERR, "collectd backend: Instance '%s' missing "
328 "the 'Socket' option.\n", name);
329 return -1;
330 }
332 snprintf(cb_name, sizeof(cb_name), "collectd-%s", name);
333 cb_name[sizeof(cb_name) - 1] = '\0';
335 client = sdb_unixsock_client_create(socket_path);
336 if (! client) {
337 char errbuf[1024];
338 sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to create "
339 "unixsock client: %s\n",
340 sdb_strerror(errno, errbuf, sizeof(errbuf)));
341 return -1;
342 }
344 user_data = sdb_object_create_wrapper(client,
345 (void (*)(void *))sdb_unixsock_client_destroy);
346 if (! user_data) {
347 sdb_unixsock_client_destroy(client);
348 sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to allocate "
349 "sdb_object_t\n");
350 return -1;
351 }
353 sdb_plugin_register_init(cb_name, sdb_collectd_init, user_data);
354 sdb_plugin_register_shutdown(cb_name, sdb_collectd_shutdown, user_data);
356 sdb_plugin_register_collector(cb_name, sdb_collectd_collect,
357 /* interval */ NULL, user_data);
359 /* pass control to the list */
360 sdb_object_deref(user_data);
361 return 0;
362 } /* sdb_collectd_config_instance */
364 static int
365 sdb_collectd_config(oconfig_item_t *ci)
366 {
367 int i;
369 for (i = 0; i < ci->children_num; ++i) {
370 oconfig_item_t *child = ci->children + i;
372 if (! strcasecmp(child->key, "Instance"))
373 sdb_collectd_config_instance(child);
374 else
375 sdb_error_set(SDB_LOG_WARNING, "collectd backend: Ignoring "
376 "unknown config option '%s'.\n", child->key);
377 }
378 return 0;
379 } /* sdb_collectd_config */
381 int
382 sdb_module_init(sdb_plugin_info_t *info)
383 {
384 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "collectd");
385 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
386 "backend accessing the system statistics collection daemon");
387 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
388 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
389 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
390 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
391 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
393 sdb_plugin_register_config("collectd", sdb_collectd_config);
394 return 0;
395 } /* sdb_version_extra */
397 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */