2 * collectd - src/olsrd.c
3 * Copyright (C) 2009 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
35 #include <sys/types.h>
37 #define OLSRD_DEFAULT_NODE "localhost"
38 #define OLSRD_DEFAULT_SERVICE "2006"
40 static const char *config_keys[] = {"Host", "Port", "CollectLinks",
41 "CollectRoutes", "CollectTopology"};
42 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
44 static char *config_node = NULL;
45 static char *config_service = NULL;
47 #define OLSRD_WANT_NOT 0
48 #define OLSRD_WANT_SUMMARY 1
49 #define OLSRD_WANT_DETAIL 2
50 static int config_want_links = OLSRD_WANT_DETAIL;
51 static int config_want_routes = OLSRD_WANT_SUMMARY;
52 static int config_want_topology = OLSRD_WANT_SUMMARY;
54 static const char *olsrd_get_node(void) /* {{{ */
56 if (config_node != NULL)
58 return (OLSRD_DEFAULT_NODE);
59 } /* }}} const char *olsrd_get_node */
61 static const char *olsrd_get_service(void) /* {{{ */
63 if (config_service != NULL)
64 return (config_service);
65 return (OLSRD_DEFAULT_SERVICE);
66 } /* }}} const char *olsrd_get_service */
68 static void olsrd_set_node(const char *node) /* {{{ */
77 } /* }}} void olsrd_set_node */
79 static void olsrd_set_service(const char *service) /* {{{ */
84 tmp = strdup(service);
88 } /* }}} void olsrd_set_service */
90 static void olsrd_set_detail(int *varptr, const char *detail, /* {{{ */
92 if (strcasecmp("No", detail) == 0)
93 *varptr = OLSRD_WANT_NOT;
94 else if (strcasecmp("Summary", detail) == 0)
95 *varptr = OLSRD_WANT_SUMMARY;
96 else if (strcasecmp("Detail", detail) == 0)
97 *varptr = OLSRD_WANT_DETAIL;
99 ERROR("olsrd plugin: Invalid argument given to the `%s' configuration "
100 "option: `%s'. Expected: `No', `Summary', or `Detail'.",
103 } /* }}} void olsrd_set_detail */
105 /* Strip trailing newline characters. Returns length of string. */
106 static size_t strchomp(char *buffer) /* {{{ */
110 buffer_len = strlen(buffer);
111 while ((buffer_len > 0) && ((buffer[buffer_len - 1] == '\r') ||
112 (buffer[buffer_len - 1] == '\n'))) {
114 buffer[buffer_len] = 0;
118 } /* }}} size_t strchomp */
120 static size_t strtabsplit(char *string, char **fields, size_t size) /* {{{ */
129 while ((fields[i] = strtok_r(ptr, " \t\r\n", &saveptr)) != NULL) {
138 } /* }}} size_t strtabsplit */
140 static FILE *olsrd_connect(void) /* {{{ */
142 struct addrinfo *ai_list;
147 struct addrinfo ai_hints = {.ai_family = AF_UNSPEC,
148 .ai_flags = AI_ADDRCONFIG,
149 .ai_protocol = IPPROTO_TCP,
150 .ai_socktype = SOCK_STREAM};
153 getaddrinfo(olsrd_get_node(), olsrd_get_service(), &ai_hints, &ai_list);
154 if (ai_return != 0) {
155 ERROR("olsrd plugin: getaddrinfo (%s, %s) failed: %s", olsrd_get_node(),
156 olsrd_get_service(), gai_strerror(ai_return));
161 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
162 ai_ptr = ai_ptr->ai_next) {
167 fd = socket(ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
169 ERROR("olsrd plugin: socket failed: %s",
170 sstrerror(errno, errbuf, sizeof(errbuf)));
174 status = connect(fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
176 ERROR("olsrd plugin: connect failed: %s",
177 sstrerror(errno, errbuf, sizeof(errbuf)));
182 fh = fdopen(fd, "r+");
184 ERROR("olsrd plugin: fdopen failed.");
192 freeaddrinfo(ai_list);
195 } /* }}} FILE *olsrd_connect */
197 __attribute__((nonnull(2))) static void
198 olsrd_submit(const char *plugin_instance, /* {{{ */
199 const char *type, const char *type_instance, gauge_t value) {
201 value_list_t vl = VALUE_LIST_INIT;
203 values[0].gauge = value;
208 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
209 sstrncpy(vl.plugin, "olsrd", sizeof(vl.plugin));
210 if (plugin_instance != NULL)
211 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
212 sstrncpy(vl.type, type, sizeof(vl.type));
213 if (type_instance != NULL)
214 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
216 plugin_dispatch_values(&vl);
217 } /* }}} void olsrd_submit */
219 static int olsrd_cb_ignore(int lineno, /* {{{ */
220 size_t fields_num, char **fields) {
222 } /* }}} int olsrd_cb_ignore */
224 static int olsrd_cb_links(int lineno, /* {{{ */
225 size_t fields_num, char **fields) {
234 static uint32_t links_num;
235 static double lq_sum;
236 static uint32_t lq_num;
237 static double nlq_sum;
238 static uint32_t nlq_num;
245 if (config_want_links == OLSRD_WANT_NOT)
248 /* Special handling of the first line. */
259 /* Special handling of the last line. */
260 if (fields_num == 0) {
261 DEBUG("olsrd plugin: Number of links: %" PRIu32, links_num);
262 olsrd_submit(/* p.-inst = */ "links", /* type = */ "links",
263 /* t.-inst = */ NULL, (gauge_t)links_num);
267 lq = lq_sum / ((double)lq_num);
268 DEBUG("olsrd plugin: Average LQ: %g", lq);
269 olsrd_submit(/* p.-inst = */ "links", /* type = */ "signal_quality",
274 nlq = nlq_sum / ((double)nlq_num);
275 DEBUG("olsrd plugin: Average NLQ: %g", nlq);
276 olsrd_submit(/* p.-inst = */ "links", /* type = */ "signal_quality",
289 lq = strtod(fields[3], &endptr);
290 if ((errno != 0) || (endptr == fields[3])) {
291 ERROR("olsrd plugin: Cannot parse link quality: %s", fields[3]);
298 if (config_want_links == OLSRD_WANT_DETAIL) {
299 char type_instance[DATA_MAX_NAME_LEN];
301 ssnprintf(type_instance, sizeof(type_instance), "%s-%s-lq", fields[0],
304 DEBUG("olsrd plugin: links: type_instance = %s; lq = %g;", type_instance,
306 olsrd_submit(/* p.-inst = */ "links", /* type = */ "signal_quality",
313 nlq = strtod(fields[4], &endptr);
314 if ((errno != 0) || (endptr == fields[4])) {
315 ERROR("olsrd plugin: Cannot parse neighbor link quality: %s", fields[4]);
322 if (config_want_links == OLSRD_WANT_DETAIL) {
323 char type_instance[DATA_MAX_NAME_LEN];
325 ssnprintf(type_instance, sizeof(type_instance), "%s-%s-rx", fields[0],
328 DEBUG("olsrd plugin: links: type_instance = %s; nlq = %g;", type_instance,
330 olsrd_submit(/* p.-inst = */ "links", /* type = */ "signal_quality",
336 } /* }}} int olsrd_cb_links */
338 static int olsrd_cb_routes(int lineno, /* {{{ */
339 size_t fields_num, char **fields) {
347 static uint32_t routes_num;
348 static uint32_t metric_sum;
349 static uint32_t metric_num;
350 static double etx_sum;
351 static uint32_t etx_num;
357 if (config_want_routes == OLSRD_WANT_NOT)
360 /* Special handling of the first line */
371 /* Special handling after the last line */
372 if (fields_num == 0) {
375 DEBUG("olsrd plugin: Number of routes: %" PRIu32, routes_num);
376 olsrd_submit(/* p.-inst = */ "routes", /* type = */ "routes",
377 /* t.-inst = */ NULL, (gauge_t)routes_num);
381 metric_avg = ((double)metric_sum) / ((double)metric_num);
382 DEBUG("olsrd plugin: Average metric: %g", metric_avg);
383 olsrd_submit(/* p.-inst = */ "routes", /* type = */ "route_metric",
384 "average", metric_avg);
388 etx = etx_sum / ((double)etx_sum);
389 DEBUG("olsrd plugin: Average ETX: %g", etx);
390 olsrd_submit(/* p.-inst = */ "routes", /* type = */ "route_etx", "average",
403 metric = (uint32_t)strtoul(fields[2], &endptr, 0);
404 if ((errno != 0) || (endptr == fields[2])) {
405 ERROR("olsrd plugin: Unable to parse metric: %s", fields[2]);
408 metric_sum += metric;
410 if (config_want_routes == OLSRD_WANT_DETAIL) {
411 DEBUG("olsrd plugin: destination = %s; metric = %" PRIu32 ";", fields[0],
413 olsrd_submit(/* p.-inst = */ "routes", /* type = */ "route_metric",
414 /* t.-inst = */ fields[0], (gauge_t)metric);
420 etx = strtod(fields[3], &endptr);
421 if ((errno != 0) || (endptr == fields[3])) {
422 ERROR("olsrd plugin: Unable to parse ETX: %s", fields[3]);
429 if (config_want_routes == OLSRD_WANT_DETAIL) {
430 DEBUG("olsrd plugin: destination = %s; etx = %g;", fields[0], etx);
431 olsrd_submit(/* p.-inst = */ "routes", /* type = */ "route_etx",
432 /* t.-inst = */ fields[0], etx);
437 } /* }}} int olsrd_cb_routes */
439 static int olsrd_cb_topology(int lineno, /* {{{ */
440 size_t fields_num, char **fields) {
448 static double lq_sum;
449 static uint32_t lq_num;
451 static uint32_t links_num;
456 if (config_want_topology == OLSRD_WANT_NOT)
459 /* Special handling of the first line */
468 /* Special handling after the last line */
469 if (fields_num == 0) {
470 DEBUG("olsrd plugin: topology: Number of links: %" PRIu32, links_num);
471 olsrd_submit(/* p.-inst = */ "topology", /* type = */ "links",
472 /* t.-inst = */ NULL, (gauge_t)links_num);
476 lq = lq_sum / ((double)lq_sum);
477 DEBUG("olsrd plugin: topology: Average link quality: %g", lq);
478 olsrd_submit(/* p.-inst = */ "topology", /* type = */ "signal_quality",
479 /* t.-inst = */ "average", lq);
491 lq = strtod(fields[2], &endptr);
492 if ((errno != 0) || (endptr == fields[2])) {
493 ERROR("olsrd plugin: Unable to parse LQ: %s", fields[2]);
500 if (config_want_topology == OLSRD_WANT_DETAIL) {
501 char type_instance[DATA_MAX_NAME_LEN] = {0};
503 ssnprintf(type_instance, sizeof(type_instance), "%s-%s-lq", fields[0],
505 DEBUG("olsrd plugin: type_instance = %s; lq = %g;", type_instance, lq);
506 olsrd_submit(/* p.-inst = */ "topology", /* type = */ "signal_quality",
511 if (config_want_topology == OLSRD_WANT_DETAIL) {
516 nlq = strtod(fields[3], &endptr);
517 if ((errno != 0) || (endptr == fields[3])) {
518 ERROR("olsrd plugin: Unable to parse NLQ: %s", fields[3]);
520 char type_instance[DATA_MAX_NAME_LEN] = {0};
522 ssnprintf(type_instance, sizeof(type_instance), "%s-%s-nlq", fields[0],
524 DEBUG("olsrd plugin: type_instance = %s; nlq = %g;", type_instance, nlq);
525 olsrd_submit(/* p.-inst = */ "topology", /* type = */ "signal_quality",
531 } /* }}} int olsrd_cb_topology */
533 static int olsrd_read_table(FILE *fh, /* {{{ */
534 int (*callback)(int lineno, size_t fields_num,
545 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
546 /* An empty line ends the table. */
547 buffer_len = strchomp(buffer);
548 if (buffer_len == 0) {
549 (*callback)(lineno, /* fields_num = */ 0, /* fields = */ NULL);
553 fields_num = strtabsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
555 (*callback)(lineno, fields_num, fields);
557 } /* while (fgets) */
560 } /* }}} int olsrd_read_table */
562 static int olsrd_config(const char *key, const char *value) /* {{{ */
564 if (strcasecmp("Host", key) == 0)
565 olsrd_set_node(value);
566 else if (strcasecmp("Port", key) == 0)
567 olsrd_set_service(value);
568 else if (strcasecmp("CollectLinks", key) == 0)
569 olsrd_set_detail(&config_want_links, value, key);
570 else if (strcasecmp("CollectRoutes", key) == 0)
571 olsrd_set_detail(&config_want_routes, value, key);
572 else if (strcasecmp("CollectTopology", key) == 0)
573 olsrd_set_detail(&config_want_topology, value, key);
575 ERROR("olsrd plugin: Unknown configuration option given: %s", key);
580 } /* }}} int olsrd_config */
582 static int olsrd_read(void) /* {{{ */
588 fh = olsrd_connect();
595 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
596 buffer_len = strchomp(buffer);
600 if (strcmp("Table: Links", buffer) == 0)
601 olsrd_read_table(fh, olsrd_cb_links);
602 else if (strcmp("Table: Neighbors", buffer) == 0)
603 olsrd_read_table(fh, olsrd_cb_ignore);
604 else if (strcmp("Table: Topology", buffer) == 0)
605 olsrd_read_table(fh, olsrd_cb_topology);
606 else if (strcmp("Table: HNA", buffer) == 0)
607 olsrd_read_table(fh, olsrd_cb_ignore);
608 else if (strcmp("Table: MID", buffer) == 0)
609 olsrd_read_table(fh, olsrd_cb_ignore);
610 else if (strcmp("Table: Routes", buffer) == 0)
611 olsrd_read_table(fh, olsrd_cb_routes);
612 else if ((strcmp("HTTP/1.0 200 OK", buffer) == 0) ||
613 (strcmp("Content-type: text/plain", buffer) == 0)) {
616 DEBUG("olsrd plugin: Unable to handle line: %s", buffer);
618 } /* while (fgets) */
623 } /* }}} int olsrd_read */
625 static int olsrd_shutdown(void) /* {{{ */
628 sfree(config_service);
631 } /* }}} int olsrd_shutdown */
633 void module_register(void) {
634 plugin_register_config("olsrd", olsrd_config, config_keys, config_keys_num);
635 plugin_register_read("olsrd", olsrd_read);
636 plugin_register_shutdown("olsrd", olsrd_shutdown);
637 } /* void module_register */
639 /* vim: set sw=2 sts=2 et fdm=marker : */