07da8c022fb55393a253bf8a89c2c68dfc154f0d
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 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
33 #include "core/plugin.h"
34 #include "core/store.h"
35 #include "utils/error.h"
36 #include "utils/unixsock.h"
38 #include "liboconfig/utils.h"
40 #include <assert.h>
42 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
48 SDB_PLUGIN_MAGIC;
50 /*
51 * private data types
52 */
54 typedef struct {
55 char *current_host;
56 sdb_time_t current_timestamp;
57 int svc_updated;
58 int svc_failed;
59 } sdb_collectd_state_t;
60 #define SDB_COLLECTD_STATE_INIT { NULL, 0, 0, 0 }
62 /*
63 * private helper functions
64 */
66 /* store the specified host-name (once per iteration) */
67 static int
68 sdb_collectd_store_host(sdb_collectd_state_t *state,
69 const char *hostname, sdb_time_t last_update)
70 {
71 int status;
73 if (last_update > state->current_timestamp)
74 state->current_timestamp = last_update;
76 if (state->current_host && (! strcasecmp(state->current_host, hostname)))
77 return 0;
78 /* else: first/new host */
80 if (state->current_host) {
81 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
82 "%i service%s (%i failed) for host '%s'.",
83 state->svc_updated, state->svc_updated == 1 ? "" : "s",
84 state->svc_failed, state->current_host);
85 state->svc_updated = state->svc_failed = 0;
86 free(state->current_host);
87 }
89 state->current_host = strdup(hostname);
90 if (! state->current_host) {
91 char errbuf[1024];
92 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
93 "string buffer: %s",
94 sdb_strerror(errno, errbuf, sizeof(errbuf)));
95 return -1;
96 }
98 status = sdb_store_host(hostname, last_update);
100 if (status < 0) {
101 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
102 "store/update host '%s'.", hostname);
103 return -1;
104 }
105 else if (status > 0) /* value too old */
106 return 0;
108 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
109 "host '%s' (last update timestamp = %"PRIsdbTIME").",
110 hostname, last_update);
111 return 0;
112 } /* sdb_collectd_store_host */
114 static int
115 sdb_collectd_add_svc(const char *hostname, char *plugin, char *type,
116 sdb_time_t last_update)
117 {
118 char name[strlen(plugin) + strlen(type) + 2];
119 char *plugin_instance, *type_instance;
121 sdb_data_t data = { SDB_TYPE_STRING, { .string = NULL } };
123 int status;
125 snprintf(name, sizeof(name), "%s/%s", plugin, type);
127 status = sdb_store_service(hostname, name, last_update);
128 if (status < 0) {
129 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
130 "store/update service '%s/%s'.", hostname, name);
131 return -1;
132 }
134 plugin_instance = strchr(plugin, '-');
135 if (plugin_instance) {
136 *plugin_instance = '\0';
137 ++plugin_instance;
139 data.data.string = plugin_instance;
140 sdb_store_service_attr(hostname, name,
141 "plugin_instance", &data, last_update);
142 }
144 type_instance = strchr(type, '-');
145 if (type_instance) {
146 *type_instance = '\0';
147 ++type_instance;
149 data.data.string = type_instance;
150 sdb_store_service_attr(hostname, name,
151 "type_instance", &data, last_update);
152 }
154 data.data.string = plugin;
155 sdb_store_service_attr(hostname, name, "plugin", &data, last_update);
156 data.data.string = type;
157 sdb_store_service_attr(hostname, name, "type", &data, last_update);
158 return 0;
159 } /* sdb_collectd_add_svc */
161 static int
162 sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
163 size_t n, sdb_data_t *data, sdb_object_t *user_data)
164 {
165 sdb_collectd_state_t *state;
166 sdb_data_t last_update;
168 char *hostname;
169 char *plugin;
170 char *type;
172 assert(user_data);
174 /* 0: <last_update> <hostname>
175 * 1: <plugin>
176 * 2: <type> */
177 assert(n == 3);
178 assert((data[0].type == SDB_TYPE_STRING)
179 && (data[1].type == SDB_TYPE_STRING)
180 && (data[2].type == SDB_TYPE_STRING));
182 hostname = data[0].data.string;
183 plugin = data[1].data.string;
184 type = data[2].data.string;
186 hostname = strchr(hostname, ' ');
187 if (! hostname) {
188 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Expected to find "
189 "a space character in the LISTVAL response");
190 return -1;
191 }
192 *hostname = '\0';
193 ++hostname;
195 if (sdb_data_parse(data[0].data.string, SDB_TYPE_DATETIME, &last_update)) {
196 char errbuf[1024];
197 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
198 "timestamp '%s' returned by LISTVAL: %s", data[0].data.string,
199 sdb_strerror(errno, errbuf, sizeof(errbuf)));
200 return -1;
201 }
203 state = SDB_OBJ_WRAPPER(user_data)->data;
204 if (sdb_collectd_store_host(state, hostname, last_update.data.datetime))
205 return -1;
207 if (sdb_collectd_add_svc(hostname, plugin, type,
208 last_update.data.datetime))
209 ++state->svc_failed;
210 else
211 ++state->svc_updated;
212 return 0;
213 } /* sdb_collectd_get_data */
215 /*
216 * plugin API
217 */
219 static int
220 sdb_collectd_init(sdb_object_t *user_data)
221 {
222 sdb_unixsock_client_t *client;
224 if (! user_data)
225 return -1;
227 client = SDB_OBJ_WRAPPER(user_data)->data;
228 if (sdb_unixsock_client_connect(client)) {
229 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: "
230 "Failed to connect to collectd.");
231 return -1;
232 }
234 sdb_log(SDB_LOG_INFO, "collectd::unixsock backend: Successfully "
235 "connected to collectd @ %s.",
236 sdb_unixsock_client_path(client));
237 return 0;
238 } /* sdb_collectd_init */
240 static int
241 sdb_collectd_shutdown(__attribute__((unused)) sdb_object_t *user_data)
242 {
243 if (! user_data)
244 return -1;
246 sdb_unixsock_client_destroy(SDB_OBJ_WRAPPER(user_data)->data);
247 SDB_OBJ_WRAPPER(user_data)->data = NULL;
248 return 0;
249 } /* sdb_collectd_shutdown */
251 static int
252 sdb_collectd_collect(sdb_object_t *user_data)
253 {
254 sdb_unixsock_client_t *client;
256 char buffer[1024];
257 char *line;
258 char *msg;
260 char *endptr = NULL;
261 long int count;
263 sdb_collectd_state_t state = SDB_COLLECTD_STATE_INIT;
264 sdb_object_wrapper_t state_obj = SDB_OBJECT_WRAPPER_STATIC(&state);
266 if (! user_data)
267 return -1;
269 client = SDB_OBJ_WRAPPER(user_data)->data;
271 if (sdb_unixsock_client_send(client, "LISTVAL") <= 0) {
272 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to send "
273 "LISTVAL command to collectd @ %s.",
274 sdb_unixsock_client_path(client));
275 return -1;
276 }
278 line = sdb_unixsock_client_recv(client, buffer, sizeof(buffer));
279 if (! line) {
280 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to read "
281 "status of LISTVAL command from collectd @ %s.",
282 sdb_unixsock_client_path(client));
283 return -1;
284 }
286 msg = strchr(line, ' ');
287 if (msg) {
288 *msg = '\0';
289 ++msg;
290 }
292 errno = 0;
293 count = strtol(line, &endptr, /* base */ 0);
294 if (errno || (line == endptr)) {
295 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
296 "status of LISTVAL command from collectd @ %s.",
297 sdb_unixsock_client_path(client));
298 return -1;
299 }
301 if (count < 0) {
302 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to get "
303 "value list from collectd @ %s: %s",
304 sdb_unixsock_client_path(client),
305 msg ? msg : line);
306 return -1;
307 }
309 if (sdb_unixsock_client_process_lines(client, sdb_collectd_get_data,
310 SDB_OBJ(&state_obj), count, /* delim */ "/",
311 /* column count = */ 3,
312 SDB_TYPE_STRING, SDB_TYPE_STRING, SDB_TYPE_STRING)) {
313 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed "
314 "to read response from collectd @ %s.",
315 sdb_unixsock_client_path(client));
316 return -1;
317 }
319 if (state.current_host) {
320 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
321 "%i service%s (%i failed) for host '%s'.",
322 state.svc_updated, state.svc_updated == 1 ? "" : "s",
323 state.svc_failed, state.current_host);
324 free(state.current_host);
325 }
326 return 0;
327 } /* sdb_collectd_collect */
329 static int
330 sdb_collectd_config_instance(oconfig_item_t *ci)
331 {
332 char *name = NULL;
333 char *socket_path = NULL;
335 sdb_object_t *user_data;
336 sdb_unixsock_client_t *client;
338 int i;
340 if (oconfig_get_string(ci, &name)) {
341 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance requires "
342 "a single string argument\n\tUsage: <Instance NAME>");
343 return -1;
344 }
346 for (i = 0; i < ci->children_num; ++i) {
347 oconfig_item_t *child = ci->children + i;
349 if (! strcasecmp(child->key, "Socket"))
350 oconfig_get_string(child, &socket_path);
351 else
352 sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
353 "unknown config option '%s' inside <Instance %s>.",
354 child->key, name);
355 }
357 if (! socket_path) {
358 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance '%s' "
359 "missing the 'Socket' option.", name);
360 return -1;
361 }
363 client = sdb_unixsock_client_create(socket_path);
364 if (! client) {
365 char errbuf[1024];
366 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to create "
367 "unixsock client: %s",
368 sdb_strerror(errno, errbuf, sizeof(errbuf)));
369 return -1;
370 }
372 user_data = sdb_object_create_wrapper("unixsock-client", client,
373 (void (*)(void *))sdb_unixsock_client_destroy);
374 if (! user_data) {
375 sdb_unixsock_client_destroy(client);
376 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
377 "sdb_object_t");
378 return -1;
379 }
381 sdb_plugin_register_init(name, sdb_collectd_init, user_data);
382 sdb_plugin_register_shutdown(name, sdb_collectd_shutdown, user_data);
384 sdb_plugin_register_collector(name, sdb_collectd_collect,
385 /* interval */ NULL, user_data);
387 /* pass control to the list */
388 sdb_object_deref(user_data);
389 return 0;
390 } /* sdb_collectd_config_instance */
392 static int
393 sdb_collectd_config(oconfig_item_t *ci)
394 {
395 int i;
397 if (! ci) /* nothing to do to deconfigure this plugin */
398 return 0;
400 for (i = 0; i < ci->children_num; ++i) {
401 oconfig_item_t *child = ci->children + i;
403 if (! strcasecmp(child->key, "Instance"))
404 sdb_collectd_config_instance(child);
405 else
406 sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
407 "unknown config option '%s'.", child->key);
408 }
409 return 0;
410 } /* sdb_collectd_config */
412 int
413 sdb_module_init(sdb_plugin_info_t *info)
414 {
415 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
416 "backend accessing the system statistics collection daemon "
417 "throught the UNIXSOCK interface");
418 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
419 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
420 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
421 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
422 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
424 sdb_plugin_register_config(sdb_collectd_config);
425 return 0;
426 } /* sdb_version_extra */
428 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */