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 #include "sysdb.h"
29 #include "core/plugin.h"
30 #include "core/store.h"
31 #include "core/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 int status;
67 status = sdb_store_host(hostname, last_update);
69 if (status < 0) {
70 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
71 "store/update host '%s'.", hostname);
72 return -1;
73 }
74 else if (status > 0) /* value too old */
75 return 0;
77 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
78 "host '%s' (last update timestamp = %"PRIscTIME").",
79 hostname, last_update);
80 return 0;
81 } /* sdb_collectd_add_host */
83 static int
84 sdb_collectd_add_svc(const char *hostname, const char *plugin,
85 const char *type, sdb_time_t last_update)
86 {
87 sdb_service_t svc = SDB_SVC_INIT;
88 char host[strlen(hostname) + 1];
89 char name[strlen(plugin) + strlen(type) + 2];
91 int status;
93 strncpy(host, hostname, sizeof(host));
94 snprintf(name, sizeof(name), "%s/%s", plugin, type);
96 svc.hostname = host;
97 SDB_OBJ(&svc)->name = name;
98 svc._last_update = last_update;
100 status = sdb_store_service(&svc);
101 if (status < 0) {
102 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
103 "store/update service '%s/%s'.", host, name);
104 return -1;
105 }
106 return 0;
107 } /* sdb_collectd_add_svc */
109 static int
110 sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
111 size_t n, sdb_data_t *data, sdb_object_t *user_data)
112 {
113 sdb_collectd_state_t *state;
115 const char *hostname;
116 const char *plugin;
117 const char *type;
118 sdb_time_t last_update;
120 assert(user_data);
122 assert(n == 4);
123 assert((data[0].type == SDB_TYPE_DATETIME)
124 && (data[1].type == SDB_TYPE_STRING)
125 && (data[2].type == SDB_TYPE_STRING)
126 && (data[3].type == SDB_TYPE_STRING));
128 last_update = data[0].data.datetime;
129 hostname = data[1].data.string;
130 plugin = data[2].data.string;
131 type = data[3].data.string;
133 state = SDB_OBJ_WRAPPER(user_data)->data;
135 if (! state->current_host) {
136 state->current_host = strdup(hostname);
137 state->current_timestamp = last_update;
138 }
140 if (! state->current_host) {
141 char errbuf[1024];
142 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
143 "string buffer: %s",
144 sdb_strerror(errno, errbuf, sizeof(errbuf)));
145 return -1;
146 }
148 if (! sdb_store_has_host(hostname))
149 sdb_collectd_add_host(hostname, last_update);
151 if (sdb_collectd_add_svc(hostname, plugin, type, last_update))
152 ++state->svc_failed;
153 else
154 ++state->svc_updated;
156 if (! strcasecmp(state->current_host, hostname)) {
157 if (last_update > state->current_timestamp)
158 state->current_timestamp = last_update;
159 return 0;
160 }
162 /* new host */
163 sdb_collectd_add_host(hostname, last_update);
165 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
166 "%i service%s (%i failed) for host '%s'.",
167 state->svc_updated, state->svc_updated == 1 ? "" : "s",
168 state->svc_failed, state->current_host);
169 state->svc_updated = state->svc_failed = 0;
171 free(state->current_host);
172 state->current_host = strdup(hostname);
173 state->current_timestamp = last_update;
174 return 0;
175 } /* sdb_collectd_get_data */
177 /*
178 * plugin API
179 */
181 static int
182 sdb_collectd_init(sdb_object_t *user_data)
183 {
184 sdb_unixsock_client_t *client;
186 if (! user_data)
187 return -1;
189 client = SDB_OBJ_WRAPPER(user_data)->data;
190 if (sdb_unixsock_client_connect(client)) {
191 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: "
192 "Failed to connect to collectd.");
193 return -1;
194 }
196 sdb_log(SDB_LOG_INFO, "collectd::unixsock backend: Successfully "
197 "connected to collectd @ %s.",
198 sdb_unixsock_client_path(client));
199 return 0;
200 } /* sdb_collectd_init */
202 static int
203 sdb_collectd_shutdown(__attribute__((unused)) sdb_object_t *user_data)
204 {
205 return 0;
206 } /* sdb_collectd_shutdown */
208 static int
209 sdb_collectd_collect(sdb_object_t *user_data)
210 {
211 sdb_unixsock_client_t *client;
213 char buffer[1024];
214 char *line;
215 char *msg;
217 char *endptr = NULL;
218 long int count;
220 sdb_collectd_state_t state = SDB_COLLECTD_STATE_INIT;
221 sdb_object_wrapper_t state_obj = SDB_OBJECT_WRAPPER_STATIC(&state,
222 /* destructor = */ NULL);
224 if (! user_data)
225 return -1;
227 client = SDB_OBJ_WRAPPER(user_data)->data;
229 if (sdb_unixsock_client_send(client, "LISTVAL") <= 0) {
230 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to send "
231 "LISTVAL command to collectd @ %s.",
232 sdb_unixsock_client_path(client));
233 return -1;
234 }
236 line = sdb_unixsock_client_recv(client, buffer, sizeof(buffer));
237 if (! line) {
238 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to read "
239 "status of LISTVAL command from collectd @ %s.",
240 sdb_unixsock_client_path(client));
241 return -1;
242 }
244 msg = strchr(line, ' ');
245 if (msg) {
246 *msg = '\0';
247 ++msg;
248 }
250 errno = 0;
251 count = strtol(line, &endptr, /* base */ 0);
252 if (errno || (line == endptr)) {
253 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
254 "status of LISTVAL command from collectd @ %s.",
255 sdb_unixsock_client_path(client));
256 return -1;
257 }
259 if (count < 0) {
260 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to get "
261 "value list from collectd @ %s: %s",
262 sdb_unixsock_client_path(client),
263 msg ? msg : line);
264 return -1;
265 }
267 if (sdb_unixsock_client_process_lines(client, sdb_collectd_get_data,
268 SDB_OBJ(&state_obj), count, /* delim */ " /",
269 /* column count = */ 4,
270 SDB_TYPE_DATETIME, SDB_TYPE_STRING,
271 SDB_TYPE_STRING, SDB_TYPE_STRING)) {
272 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed "
273 "to read response from collectd @ %s.",
274 sdb_unixsock_client_path(client));
275 return -1;
276 }
278 if (state.current_host) {
279 sdb_collectd_add_host(state.current_host, state.current_timestamp);
280 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
281 "%i service%s (%i failed) for host '%s'.",
282 state.svc_updated, state.svc_updated == 1 ? "" : "s",
283 state.svc_failed, state.current_host);
284 }
285 return 0;
286 } /* sdb_collectd_collect */
288 static int
289 sdb_collectd_config_instance(oconfig_item_t *ci)
290 {
291 char *name = NULL;
292 char *socket_path = NULL;
294 char cb_name[1024];
296 sdb_object_t *user_data;
297 sdb_unixsock_client_t *client;
299 int i;
301 if (oconfig_get_string(ci, &name)) {
302 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance requires "
303 "a single string argument\n\tUsage: <Instance NAME>");
304 return -1;
305 }
307 for (i = 0; i < ci->children_num; ++i) {
308 oconfig_item_t *child = ci->children + i;
310 if (! strcasecmp(child->key, "Socket"))
311 oconfig_get_string(child, &socket_path);
312 else
313 sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
314 "unknown config option '%s' inside <Instance %s>.",
315 child->key, name);
316 }
318 if (! socket_path) {
319 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance '%s' "
320 "missing the 'Socket' option.", name);
321 return -1;
322 }
324 snprintf(cb_name, sizeof(cb_name), "collectd::unixsock::%s", name);
325 cb_name[sizeof(cb_name) - 1] = '\0';
327 client = sdb_unixsock_client_create(socket_path);
328 if (! client) {
329 char errbuf[1024];
330 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to create "
331 "unixsock client: %s",
332 sdb_strerror(errno, errbuf, sizeof(errbuf)));
333 return -1;
334 }
336 user_data = sdb_object_create_wrapper("unixsock-client", client,
337 (void (*)(void *))sdb_unixsock_client_destroy);
338 if (! user_data) {
339 sdb_unixsock_client_destroy(client);
340 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
341 "sdb_object_t");
342 return -1;
343 }
345 sdb_plugin_register_init(cb_name, sdb_collectd_init, user_data);
346 sdb_plugin_register_shutdown(cb_name, sdb_collectd_shutdown, user_data);
348 sdb_plugin_register_collector(cb_name, sdb_collectd_collect,
349 /* interval */ NULL, user_data);
351 /* pass control to the list */
352 sdb_object_deref(user_data);
353 return 0;
354 } /* sdb_collectd_config_instance */
356 static int
357 sdb_collectd_config(oconfig_item_t *ci)
358 {
359 int i;
361 for (i = 0; i < ci->children_num; ++i) {
362 oconfig_item_t *child = ci->children + i;
364 if (! strcasecmp(child->key, "Instance"))
365 sdb_collectd_config_instance(child);
366 else
367 sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
368 "unknown config option '%s'.", child->key);
369 }
370 return 0;
371 } /* sdb_collectd_config */
373 int
374 sdb_module_init(sdb_plugin_info_t *info)
375 {
376 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "collectd::unixsock");
377 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
378 "backend accessing the system statistics collection daemon "
379 "throught the UNIXSOCK interface");
380 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
381 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
382 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
383 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
384 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
386 sdb_plugin_register_config("collectd::unixsock", sdb_collectd_config);
387 return 0;
388 } /* sdb_version_extra */
390 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */