Code

d7cbf661d1d44677257f2bd0c186587cff184956
[sysdb.git] / src / backend / collectd.c
1 /*
2  * syscollector - 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 "syscollector.h"
29 #include "core/plugin.h"
30 #include "core/store.h"
31 #include "utils/string.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 SC_PLUGIN_MAGIC;
46 /*
47  * private helper functions
48  */
50 static int
51 sc_collectd_parse_value(char *line,
52                 sc_time_t *timestamp, char **host, char **svc)
53 {
54         char *timestamp_str;
55         double timestamp_dbl;
57         char *hostname, *service;
59         char *endptr = NULL;
61         timestamp_str = line;
62         hostname = strchr(timestamp_str, ' ');
64         if (! hostname) {
65                 fprintf(stderr, "collectd backend: Failed to find hostname "
66                                 "in LISTVAL output, line: %s\n", line);
67                 return -1;
68         }
70         *hostname = '\0';
71         ++hostname;
73         service = strchr(hostname, '/');
75         if (! service)
76                 fprintf(stderr, "collectd backend: Failed to find service name "
77                                 "in LISTVAL output, line: %s\n", line);
78         else {
79                 *service = '\0';
80                 ++service;
81         }
83         errno = 0;
84         timestamp_dbl = strtod(timestamp_str, &endptr);
85         if (errno || (timestamp_str == endptr)) {
86                 char errbuf[1024];
87                 fprintf(stderr, "collectd backend: Failed to "
88                                 "parse timestamp (%s): %s\n", timestamp_str,
89                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
90                 return -1;
91         }
92         if (endptr && (*endptr != '\0'))
93                 fprintf(stderr, "collectd backend: Ignoring garbage "
94                                 "after number when parsing timestamp: %s.\n",
95                                 endptr);
97         *timestamp = DOUBLE_TO_SC_TIME(timestamp_dbl);
98         *host = hostname;
99         *svc  = service;
100         return 0;
101 } /* sc_collectd_parse_value */
103 static int
104 sc_collectd_add_host(char *hostname, sc_time_t last_update)
106         sc_host_t host = SC_HOST_INIT;
108         int status;
110         host.host_name = hostname;
111         host.host_last_update = last_update;
113         status = sc_store_host(&host);
115         if (status < 0) {
116                 fprintf(stderr, "collectd backend: Failed to store/update "
117                                 "host '%s'.\n", hostname);
118                 return -1;
119         }
120         else if (status > 0) /* value too old */
121                 return 0;
123         fprintf(stderr, "collectd backend: Added/updated host '%s' "
124                         "(last update timestamp = %"PRIscTIME").\n",
125                         hostname, last_update);
126         return 0;
127 } /* sc_collectd_add_host */
129 static int
130 sc_collectd_add_svc(char *hostname, char *name, sc_time_t last_update)
132         sc_service_t svc = SC_SVC_INIT;
134         int status;
136         svc.hostname = hostname;
137         svc.svc_name = name;
138         svc.svc_last_update = last_update;
140         status = sc_store_service(&svc);
141         if (status < 0) {
142                 fprintf(stderr, "collectd backend: Failed to store/update "
143                                 "service '%s/%s'.\n", hostname, name);
144                 return -1;
145         }
146         return 0;
147 } /* sc_collectd_add_svc */
149 /*
150  * plugin API
151  */
153 static int
154 sc_collectd_init(sc_object_t *user_data)
156         sc_unixsock_client_t *client;
158         if (! user_data)
159                 return -1;
161         client = SC_OBJ_WRAPPER(user_data)->data;
162         if (sc_unixsock_client_connect(client)) {
163                 fprintf(stderr, "collectd backend: "
164                                 "Failed to connect to collectd.\n");
165                 return -1;
166         }
168         fprintf(stderr, "collectd backend: Successfully "
169                         "connected to collectd @ %s.\n",
170                         sc_unixsock_client_path(client));
171         return 0;
172 } /* sc_collectd_init */
174 static int
175 sc_collectd_shutdown(__attribute__((unused)) sc_object_t *user_data)
177         return 0;
178 } /* sc_collectd_shutdown */
180 static int
181 sc_collectd_collect(sc_object_t *user_data)
183         sc_unixsock_client_t *client;
185         char  buffer[1024];
186         char *line;
187         char *msg;
189         char *endptr = NULL;
190         long int count, i;
192         char *current_host = NULL;
193         sc_time_t current_timestamp = 0;
195         int svc_updated = 0;
196         int svc_failed  = 0;
198         if (! user_data)
199                 return -1;
201         client = SC_OBJ_WRAPPER(user_data)->data;
203         if (sc_unixsock_client_send(client, "LISTVAL") <= 0) {
204                 fprintf(stderr, "collectd backend: Failed to send LISTVAL command "
205                                 "to collectd @ %s.\n", sc_unixsock_client_path(client));
206                 return -1;
207         }
209         line = sc_unixsock_client_recv(client, buffer, sizeof(buffer));
210         if (! line) {
211                 fprintf(stderr, "collectd backend: Failed to read status "
212                                 "of LISTVAL command from collectd @ %s.\n",
213                                 sc_unixsock_client_path(client));
214                 return -1;
215         }
217         msg = strchr(line, ' ');
218         if (msg) {
219                 *msg = '\0';
220                 ++msg;
221         }
223         errno = 0;
224         count = strtol(line, &endptr, /* base */ 0);
225         if (errno || (line == endptr)) {
226                 fprintf(stderr, "collectd backend: Failed to parse status "
227                                 "of LISTVAL command from collectd @ %s.\n",
228                                 sc_unixsock_client_path(client));
229                 return -1;
230         }
232         if (count < 0) {
233                 fprintf(stderr, "collectd backend: Failed to get value list "
234                                 "from collectd @ %s: %s\n", sc_unixsock_client_path(client),
235                                 msg ? msg : line);
236                 return -1;
237         }
239         for (i = 0; i < count; ++i) {
240                 char *hostname = NULL, *service = NULL;
241                 sc_time_t last_update = 0;
243                 line = sc_unixsock_client_recv(client, buffer, sizeof(buffer));
245                 if (sc_collectd_parse_value(line, &last_update, &hostname, &service))
246                         continue;
248                 if (! current_host)
249                         current_host = strdup(hostname);
250                 if (! current_host) {
251                         char errbuf[1024];
252                         fprintf(stderr, "collectd backend: Failed to allocate "
253                                         "string buffer: %s\n",
254                                         sc_strerror(errno, errbuf, sizeof(errbuf)));
255                         return -1;
256                 }
258                 if (! sc_store_get_host(hostname))
259                         sc_collectd_add_host(hostname, last_update);
261                 if (sc_collectd_add_svc(hostname, service, last_update))
262                         ++svc_failed;
263                 else
264                         ++svc_updated;
266                 assert(hostname && service);
267                 if (! strcasecmp(current_host, hostname)) {
268                         if (last_update > current_timestamp)
269                                 current_timestamp = last_update;
270                         continue;
271                 }
273                 /* new host */
274                 sc_collectd_add_host(current_host, current_timestamp);
276                 fprintf(stderr, "collectd backend: Added/updated "
277                                 "%i service%s (%i failed) for host '%s'.\n",
278                                 svc_updated, svc_updated == 1 ? "" : "s",
279                                 svc_failed, current_host);
280                 svc_updated = svc_failed = 0;
282                 free(current_host);
283                 current_host = strdup(hostname);
284                 current_timestamp = last_update;
285         }
287         if (current_host) {
288                 sc_collectd_add_host(current_host, current_timestamp);
289                 fprintf(stderr, "collectd backend: Added/updated "
290                                 "%i service%s (%i failed) for host '%s'.\n",
291                                 svc_updated, svc_updated == 1 ? "" : "s",
292                                 svc_failed, current_host);
293         }
294         return 0;
295 } /* sc_collectd_collect */
297 static int
298 sc_collectd_config_instance(oconfig_item_t *ci)
300         char *name = NULL;
301         char *socket = NULL;
303         char cb_name[1024];
305         sc_object_t *user_data;
306         sc_unixsock_client_t *client;
308         int i;
310         if (oconfig_get_string(ci, &name)) {
311                 fprintf(stderr, "collectd backend: Instance requires a single "
312                                 "string argument\n\tUsage: <Instance NAME>\n");
313                 return -1;
314         }
316         for (i = 0; i < ci->children_num; ++i) {
317                 oconfig_item_t *child = ci->children + i;
319                 if (! strcasecmp(child->key, "Socket"))
320                         oconfig_get_string(child, &socket);
321                 else
322                         fprintf(stderr, "collectd backend: Ignoring unknown config "
323                                         "option '%s' inside <Instance %s>.\n",
324                                         child->key, name);
325         }
327         if (! socket) {
328                 fprintf(stderr, "collectd backend: Instance '%s' missing "
329                                 "the 'Socket' option.\n", name);
330                 return -1;
331         }
333         snprintf(cb_name, sizeof(cb_name), "collectd-%s", name);
334         cb_name[sizeof(cb_name) - 1] = '\0';
336         client = sc_unixsock_client_create(socket);
337         if (! client) {
338                 char errbuf[1024];
339                 fprintf(stderr, "collectd backend: Failed to create unixsock client: "
340                                 "%s\n", sc_strerror(errno, errbuf, sizeof(errbuf)));
341                 return -1;
342         }
344         user_data = sc_object_create_wrapper(client,
345                         (void (*)(void *))sc_unixsock_client_destroy);
346         if (! user_data) {
347                 sc_unixsock_client_destroy(client);
348                 fprintf(stderr, "collectd backend: Failed to allocate sc_object_t\n");
349                 return -1;
350         }
352         sc_plugin_register_init(cb_name, sc_collectd_init, user_data);
353         sc_plugin_register_shutdown(cb_name, sc_collectd_shutdown, user_data);
355         sc_plugin_register_collector(cb_name, sc_collectd_collect,
356                         /* interval */ NULL, user_data);
358         /* pass control to the list */
359         sc_object_deref(user_data);
360         return 0;
361 } /* sc_collectd_config_instance */
363 static int
364 sc_collectd_config(oconfig_item_t *ci)
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                         sc_collectd_config_instance(child);
373                 else
374                         fprintf(stderr, "collectd backend: Ignoring unknown config "
375                                         "option '%s'.\n", child->key);
376         }
377         return 0;
378 } /* sc_collectd_config */
380 int
381 sc_module_init(sc_plugin_info_t *info)
383         sc_plugin_set_info(info, SC_PLUGIN_INFO_NAME, "collectd");
384         sc_plugin_set_info(info, SC_PLUGIN_INFO_DESC,
385                         "backend accessing the system statistics collection daemon");
386         sc_plugin_set_info(info, SC_PLUGIN_INFO_COPYRIGHT,
387                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
388         sc_plugin_set_info(info, SC_PLUGIN_INFO_LICENSE, "BSD");
389         sc_plugin_set_info(info, SC_PLUGIN_INFO_VERSION, SC_VERSION);
390         sc_plugin_set_info(info, SC_PLUGIN_INFO_PLUGIN_VERSION, SC_VERSION);
392         sc_plugin_register_config("collectd", sc_collectd_config);
393         return 0;
394 } /* sc_version_extra */
396 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */