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 sdb_host_t host = SDB_HOST_INIT;
66 char name[strlen(hostname) + 1];
68 int status;
70 strncpy(name, hostname, sizeof(name));
72 host._name = name;
73 host._last_update = last_update;
75 status = sdb_store_host(&host);
77 if (status < 0) {
78 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
79 "store/update host '%s'.", name);
80 return -1;
81 }
82 else if (status > 0) /* value too old */
83 return 0;
85 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
86 "host '%s' (last update timestamp = %"PRIscTIME").",
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._name = name;
106 svc._last_update = last_update;
108 status = sdb_store_service(&svc);
109 if (status < 0) {
110 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
111 "store/update service '%s/%s'.", 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::unixsock backend: Failed to allocate "
151 "string buffer: %s",
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::unixsock backend: Added/updated "
174 "%i service%s (%i failed) for host '%s'.",
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::unixsock backend: "
200 "Failed to connect to collectd.");
201 return -1;
202 }
204 sdb_log(SDB_LOG_INFO, "collectd::unixsock backend: Successfully "
205 "connected to collectd @ %s.",
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::unixsock backend: Failed to send "
239 "LISTVAL command to collectd @ %s.",
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::unixsock backend: Failed to read "
247 "status of LISTVAL command from collectd @ %s.",
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::unixsock backend: Failed to parse "
262 "status of LISTVAL command from collectd @ %s.",
263 sdb_unixsock_client_path(client));
264 return -1;
265 }
267 if (count < 0) {
268 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to get "
269 "value list from collectd @ %s: %s",
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::unixsock backend: Failed "
281 "to read response from collectd @ %s.",
282 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_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
289 "%i service%s (%i failed) for host '%s'.",
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_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance requires "
311 "a single string argument\n\tUsage: <Instance NAME>");
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_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
322 "unknown config option '%s' inside <Instance %s>.",
323 child->key, name);
324 }
326 if (! socket_path) {
327 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance '%s' "
328 "missing the 'Socket' option.", name);
329 return -1;
330 }
332 snprintf(cb_name, sizeof(cb_name), "collectd::unixsock::%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_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to create "
339 "unixsock client: %s",
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_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
349 "sdb_object_t");
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_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
376 "unknown config option '%s'.", 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::unixsock");
385 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
386 "backend accessing the system statistics collection daemon "
387 "throught the UNIXSOCK interface");
388 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
389 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
390 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
391 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
392 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
394 sdb_plugin_register_config("collectd::unixsock", sdb_collectd_config);
395 return 0;
396 } /* sdb_version_extra */
398 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */