2ace8e11b83a889f167123ba06dbc1b3857e86ee
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/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 sdb_log(SDB_LOG_ERR, "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 sdb_log(SDB_LOG_DEBUG, "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 sdb_log(SDB_LOG_ERR, "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)
120 {
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 sdb_log(SDB_LOG_ERR, "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 sdb_log(SDB_LOG_DEBUG, "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)
191 {
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 sdb_log(SDB_LOG_ERR, "collectd backend: "
200 "Failed to connect to collectd.\n");
201 return -1;
202 }
204 sdb_log(SDB_LOG_INFO, "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)
212 {
213 return 0;
214 } /* sdb_collectd_shutdown */
216 static int
217 sdb_collectd_collect(sdb_object_t *user_data)
218 {
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 sdb_log(SDB_LOG_ERR, "collectd backend: Failed to send LISTVAL "
239 "command to collectd @ %s.\n",
240 sdb_unixsock_client_path(client));
241 return -1;
242 }
244 line = sdb_unixsock_client_recv(client, buffer, sizeof(buffer));
245 if (! line) {
246 sdb_log(SDB_LOG_ERR, "collectd backend: Failed to read status "
247 "of LISTVAL command from collectd @ %s.\n",
248 sdb_unixsock_client_path(client));
249 return -1;
250 }
252 msg = strchr(line, ' ');
253 if (msg) {
254 *msg = '\0';
255 ++msg;
256 }
258 errno = 0;
259 count = strtol(line, &endptr, /* base */ 0);
260 if (errno || (line == endptr)) {
261 sdb_log(SDB_LOG_ERR, "collectd backend: Failed to parse status "
262 "of LISTVAL command from collectd @ %s.\n",
263 sdb_unixsock_client_path(client));
264 return -1;
265 }
267 if (count < 0) {
268 sdb_log(SDB_LOG_ERR, "collectd backend: Failed to get value "
269 "list from collectd @ %s: %s\n",
270 sdb_unixsock_client_path(client),
271 msg ? msg : line);
272 return -1;
273 }
275 if (sdb_unixsock_client_process_lines(client, sdb_collectd_get_data,
276 SDB_OBJ(&state_obj), count, /* delim */ " /",
277 /* column count = */ 4,
278 SDB_TYPE_DATETIME, SDB_TYPE_STRING,
279 SDB_TYPE_STRING, SDB_TYPE_STRING)) {
280 sdb_log(SDB_LOG_ERR, "collectd backend: Failed to read response "
281 "from collectd @ %s.\n", sdb_unixsock_client_path(client));
282 return -1;
283 }
285 if (state.current_host) {
286 sdb_collectd_add_host(state.current_host, state.current_timestamp);
287 sdb_log(SDB_LOG_DEBUG, "collectd backend: Added/updated "
288 "%i service%s (%i failed) for host '%s'.\n",
289 state.svc_updated, state.svc_updated == 1 ? "" : "s",
290 state.svc_failed, state.current_host);
291 }
292 return 0;
293 } /* sdb_collectd_collect */
295 static int
296 sdb_collectd_config_instance(oconfig_item_t *ci)
297 {
298 char *name = NULL;
299 char *socket_path = NULL;
301 char cb_name[1024];
303 sdb_object_t *user_data;
304 sdb_unixsock_client_t *client;
306 int i;
308 if (oconfig_get_string(ci, &name)) {
309 sdb_log(SDB_LOG_ERR, "collectd backend: Instance requires a "
310 "single string argument\n\tUsage: <Instance NAME>\n");
311 return -1;
312 }
314 for (i = 0; i < ci->children_num; ++i) {
315 oconfig_item_t *child = ci->children + i;
317 if (! strcasecmp(child->key, "Socket"))
318 oconfig_get_string(child, &socket_path);
319 else
320 sdb_log(SDB_LOG_WARNING, "collectd backend: Ignoring "
321 "unknown config option '%s' inside <Instance %s>.\n",
322 child->key, name);
323 }
325 if (! socket_path) {
326 sdb_log(SDB_LOG_ERR, "collectd backend: Instance '%s' missing "
327 "the 'Socket' option.\n", name);
328 return -1;
329 }
331 snprintf(cb_name, sizeof(cb_name), "collectd-%s", name);
332 cb_name[sizeof(cb_name) - 1] = '\0';
334 client = sdb_unixsock_client_create(socket_path);
335 if (! client) {
336 char errbuf[1024];
337 sdb_log(SDB_LOG_ERR, "collectd backend: Failed to create "
338 "unixsock client: %s\n",
339 sdb_strerror(errno, errbuf, sizeof(errbuf)));
340 return -1;
341 }
343 user_data = sdb_object_create_wrapper(client,
344 (void (*)(void *))sdb_unixsock_client_destroy);
345 if (! user_data) {
346 sdb_unixsock_client_destroy(client);
347 sdb_log(SDB_LOG_ERR, "collectd backend: Failed to allocate "
348 "sdb_object_t\n");
349 return -1;
350 }
352 sdb_plugin_register_init(cb_name, sdb_collectd_init, user_data);
353 sdb_plugin_register_shutdown(cb_name, sdb_collectd_shutdown, user_data);
355 sdb_plugin_register_collector(cb_name, sdb_collectd_collect,
356 /* interval */ NULL, user_data);
358 /* pass control to the list */
359 sdb_object_deref(user_data);
360 return 0;
361 } /* sdb_collectd_config_instance */
363 static int
364 sdb_collectd_config(oconfig_item_t *ci)
365 {
366 int i;
368 for (i = 0; i < ci->children_num; ++i) {
369 oconfig_item_t *child = ci->children + i;
371 if (! strcasecmp(child->key, "Instance"))
372 sdb_collectd_config_instance(child);
373 else
374 sdb_log(SDB_LOG_WARNING, "collectd backend: Ignoring "
375 "unknown config option '%s'.\n", child->key);
376 }
377 return 0;
378 } /* sdb_collectd_config */
380 int
381 sdb_module_init(sdb_plugin_info_t *info)
382 {
383 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "collectd");
384 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
385 "backend accessing the system statistics collection daemon");
386 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
387 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
388 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
389 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
390 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
392 sdb_plugin_register_config("collectd", sdb_collectd_config);
393 return 0;
394 } /* sdb_version_extra */
396 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */