2 * collectd - src/olsrd.c
3 * Copyright (C) 2009 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
26 #include <sys/types.h>
27 #include <sys/socket.h>
30 #define OLSRD_DEFAULT_NODE "localhost"
31 #define OLSRD_DEFAULT_SERVICE "2006"
33 static const char *config_keys[] =
41 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
43 static char *config_node = NULL;
44 static char *config_service = NULL;
46 #define OLSRD_WANT_NOT 0
47 #define OLSRD_WANT_SUMMARY 1
48 #define OLSRD_WANT_DETAIL 2
49 static int config_want_links = OLSRD_WANT_DETAIL;
50 static int config_want_routes = OLSRD_WANT_SUMMARY;
51 static int config_want_topology = OLSRD_WANT_SUMMARY;
53 static const char *olsrd_get_node (void) /* {{{ */
55 if (config_node != NULL)
57 return (OLSRD_DEFAULT_NODE);
58 } /* }}} const char *olsrd_get_node */
60 static const char *olsrd_get_service (void) /* {{{ */
62 if (config_service != NULL)
63 return (config_service);
64 return (OLSRD_DEFAULT_SERVICE);
65 } /* }}} const char *olsrd_get_service */
67 static void olsrd_set_node (const char *node) /* {{{ */
76 } /* }}} void olsrd_set_node */
78 static void olsrd_set_service (const char *service) /* {{{ */
83 tmp = strdup (service);
87 } /* }}} void olsrd_set_service */
89 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;
100 ERROR ("olsrd plugin: Invalid argument given to the `%s' configuration "
101 "option: `%s'. Expected: `No', `Summary', or `Detail'.",
104 } /* }}} void olsrd_set_detail */
106 /* Strip trailing newline characters. Returns length of string. */
107 static size_t strchomp (char *buffer) /* {{{ */
111 buffer_len = strlen (buffer);
112 while ((buffer_len > 0)
113 && ((buffer[buffer_len - 1] == '\r')
114 || (buffer[buffer_len - 1] == '\n')))
117 buffer[buffer_len] = 0;
121 } /* }}} size_t strchomp */
123 static size_t strtabsplit (char *string, char **fields, size_t size) /* {{{ */
132 while ((fields[i] = strtok_r (ptr, " \t\r\n", &saveptr)) != NULL)
142 } /* }}} size_t strtabsplit */
144 static FILE *olsrd_connect (void) /* {{{ */
146 struct addrinfo ai_hints;
147 struct addrinfo *ai_list, *ai_ptr;
152 memset (&ai_hints, 0, sizeof (ai_hints));
153 ai_hints.ai_flags = 0;
155 ai_hints.ai_flags |= AI_ADDRCONFIG;
157 ai_hints.ai_family = PF_UNSPEC;
158 ai_hints.ai_socktype = SOCK_STREAM;
159 ai_hints.ai_protocol = IPPROTO_TCP;
162 ai_return = getaddrinfo (olsrd_get_node (), olsrd_get_service (),
163 &ai_hints, &ai_list);
166 ERROR ("olsrd plugin: getaddrinfo (%s, %s) failed: %s",
167 olsrd_get_node (), olsrd_get_service (),
168 gai_strerror (ai_return));
173 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
179 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
182 ERROR ("olsrd plugin: socket failed: %s",
183 sstrerror (errno, errbuf, sizeof (errbuf)));
187 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
190 ERROR ("olsrd plugin: connect failed: %s",
191 sstrerror (errno, errbuf, sizeof (errbuf)));
196 fh = fdopen (fd, "r+");
199 ERROR ("olsrd plugin: fdopen failed.");
207 freeaddrinfo (ai_list);
210 } /* }}} FILE *olsrd_connect */
212 __attribute__ ((nonnull(2)))
213 static void olsrd_submit (const char *plugin_instance, /* {{{ */
214 const char *type, const char *type_instance, gauge_t value)
217 value_list_t vl = VALUE_LIST_INIT;
219 values[0].gauge = value;
224 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
225 sstrncpy (vl.plugin, "olsrd", sizeof (vl.plugin));
226 if (plugin_instance != NULL)
227 sstrncpy (vl.plugin_instance, plugin_instance,
228 sizeof (vl.plugin_instance));
229 sstrncpy (vl.type, type, sizeof (vl.type));
230 if (type_instance != NULL)
231 sstrncpy (vl.type_instance, type_instance,
232 sizeof (vl.type_instance));
234 plugin_dispatch_values (&vl);
235 } /* }}} void olsrd_submit */
237 static int olsrd_cb_ignore (int lineno, /* {{{ */
238 size_t fields_num, char **fields)
241 } /* }}} int olsrd_cb_ignore */
243 static int olsrd_cb_links (int lineno, /* {{{ */
244 size_t fields_num, char **fields)
254 static uint32_t links_num;
255 static double lq_sum;
256 static uint32_t lq_num;
257 static double nlq_sum;
258 static uint32_t nlq_num;
265 if (config_want_links == OLSRD_WANT_NOT)
268 /* Special handling of the first line. */
280 /* Special handling of the last line. */
283 DEBUG ("olsrd plugin: Number of links: %"PRIu32, links_num);
284 olsrd_submit (/* p.-inst = */ "links", /* type = */ "links",
285 /* t.-inst = */ NULL, (gauge_t) links_num);
289 lq = lq_sum / ((double) lq_num);
290 DEBUG ("olsrd plugin: Average LQ: %g", lq);
291 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
296 nlq = nlq_sum / ((double) nlq_num);
297 DEBUG ("olsrd plugin: Average NLQ: %g", nlq);
298 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
311 lq = strtod (fields[3], &endptr);
312 if ((errno != 0) || (endptr == fields[3]))
314 ERROR ("olsrd plugin: Cannot parse link quality: %s", fields[3]);
324 if (config_want_links == OLSRD_WANT_DETAIL)
326 char type_instance[DATA_MAX_NAME_LEN];
328 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-tx",
329 fields[0], fields[1]);
331 DEBUG ("olsrd plugin: links: type_instance = %s; lq = %g;",
333 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
340 nlq = strtod (fields[4], &endptr);
341 if ((errno != 0) || (endptr == fields[4]))
343 ERROR ("olsrd plugin: Cannot parse neighbor link quality: %s", fields[4]);
353 if (config_want_links == OLSRD_WANT_DETAIL)
355 char type_instance[DATA_MAX_NAME_LEN];
357 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
358 fields[0], fields[1]);
360 DEBUG ("olsrd plugin: links: type_instance = %s; nlq = %g;",
362 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
368 } /* }}} int olsrd_cb_links */
370 static int olsrd_cb_routes (int lineno, /* {{{ */
371 size_t fields_num, char **fields)
380 static uint32_t routes_num;
381 static uint32_t metric_sum;
382 static uint32_t metric_num;
383 static double etx_sum;
384 static uint32_t etx_num;
390 if (config_want_routes == OLSRD_WANT_NOT)
393 /* Special handling of the first line */
405 /* Special handling after the last line */
410 DEBUG ("olsrd plugin: Number of routes: %"PRIu32, routes_num);
411 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "routes",
412 /* t.-inst = */ NULL, (gauge_t) routes_num);
416 metric_avg = ((double) metric_sum) / ((double) metric_num);
417 DEBUG ("olsrd plugin: Average metric: %g", metric_avg);
418 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_metric",
419 "average", metric_avg);
423 etx = etx_sum / ((double) etx_sum);
424 DEBUG ("olsrd plugin: Average ETX: %g", etx);
425 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_etx",
438 metric = (uint32_t) strtoul (fields[2], &endptr, 0);
439 if ((errno != 0) || (endptr == fields[2]))
441 ERROR ("olsrd plugin: Unable to parse metric: %s", fields[2]);
446 metric_sum += metric;
448 if (config_want_routes == OLSRD_WANT_DETAIL)
450 DEBUG ("olsrd plugin: destination = %s; metric = %"PRIu32";",
452 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_metric",
453 /* t.-inst = */ fields[0], (gauge_t) metric);
459 etx = strtod (fields[3], &endptr);
460 if ((errno != 0) || (endptr == fields[3]))
462 ERROR ("olsrd plugin: Unable to parse ETX: %s", fields[3]);
472 if (config_want_routes == OLSRD_WANT_DETAIL)
474 DEBUG ("olsrd plugin: destination = %s; etx = %g;",
476 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_etx",
477 /* t.-inst = */ fields[0], etx);
482 } /* }}} int olsrd_cb_routes */
484 static int olsrd_cb_topology (int lineno, /* {{{ */
485 size_t fields_num, char **fields)
494 static double lq_sum;
495 static uint32_t lq_num;
497 static uint32_t links_num;
502 if (config_want_topology == OLSRD_WANT_NOT)
505 /* Special handling of the first line */
515 /* Special handling after the last line */
518 DEBUG ("olsrd plugin: topology: Number of links: %"PRIu32, links_num);
519 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "links",
520 /* t.-inst = */ NULL, (gauge_t) links_num);
524 lq = lq_sum / ((double) lq_sum);
525 DEBUG ("olsrd plugin: topology: Average link quality: %g", lq);
526 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
527 /* t.-inst = */ "average", lq);
539 lq = strtod (fields[2], &endptr);
540 if ((errno != 0) || (endptr == fields[2]))
542 ERROR ("olsrd plugin: Unable to parse LQ: %s", fields[2]);
552 if (config_want_topology == OLSRD_WANT_DETAIL)
554 char type_instance[DATA_MAX_NAME_LEN];
556 memset (type_instance, 0, sizeof (type_instance));
557 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
558 fields[0], fields[1]);
559 DEBUG ("olsrd plugin: type_instance = %s; lq = %g;", type_instance, lq);
560 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
565 if (config_want_topology == OLSRD_WANT_DETAIL)
571 nlq = strtod (fields[3], &endptr);
572 if ((errno != 0) || (endptr == fields[3]))
574 ERROR ("olsrd plugin: Unable to parse NLQ: %s", fields[3]);
578 char type_instance[DATA_MAX_NAME_LEN];
580 memset (type_instance, 0, sizeof (type_instance));
581 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-tx",
582 fields[0], fields[1]);
583 DEBUG ("olsrd plugin: type_instance = %s; nlq = %g;", type_instance, nlq);
584 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
590 } /* }}} int olsrd_cb_topology */
592 static int olsrd_read_table (FILE *fh, /* {{{ */
593 int (*callback) (int lineno, size_t fields_num, char **fields))
604 while (fgets (buffer, sizeof (buffer), fh) != NULL)
606 /* An empty line ends the table. */
607 buffer_len = strchomp (buffer);
610 (*callback) (lineno, /* fields_num = */ 0, /* fields = */ NULL);
614 fields_num = strtabsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
616 (*callback) (lineno, fields_num, fields);
618 } /* while (fgets) */
621 } /* }}} int olsrd_read_table */
623 static int olsrd_config (const char *key, const char *value) /* {{{ */
625 if (strcasecmp ("Host", key) == 0)
626 olsrd_set_node (value);
627 else if (strcasecmp ("Port", key) == 0)
628 olsrd_set_service (value);
629 else if (strcasecmp ("CollectLinks", key) == 0)
630 olsrd_set_detail (&config_want_links, value, key);
631 else if (strcasecmp ("CollectRoutes", key) == 0)
632 olsrd_set_detail (&config_want_routes, value, key);
633 else if (strcasecmp ("CollectTopology", key) == 0)
634 olsrd_set_detail (&config_want_topology, value, key);
637 ERROR ("olsrd plugin: Unknown configuration option given: %s", key);
642 } /* }}} int olsrd_config */
644 static int olsrd_read (void) /* {{{ */
650 fh = olsrd_connect ();
657 while (fgets (buffer, sizeof (buffer), fh) != NULL)
659 buffer_len = strchomp (buffer);
663 if (strcmp ("Table: Links", buffer) == 0)
664 olsrd_read_table (fh, olsrd_cb_links);
665 else if (strcmp ("Table: Neighbors", buffer) == 0)
666 olsrd_read_table (fh, olsrd_cb_ignore);
667 else if (strcmp ("Table: Topology", buffer) == 0)
668 olsrd_read_table (fh, olsrd_cb_topology);
669 else if (strcmp ("Table: HNA", buffer) == 0)
670 olsrd_read_table (fh, olsrd_cb_ignore);
671 else if (strcmp ("Table: MID", buffer) == 0)
672 olsrd_read_table (fh, olsrd_cb_ignore);
673 else if (strcmp ("Table: Routes", buffer) == 0)
674 olsrd_read_table (fh, olsrd_cb_routes);
675 else if ((strcmp ("HTTP/1.0 200 OK", buffer) == 0)
676 || (strcmp ("Content-type: text/plain", buffer) == 0))
682 DEBUG ("olsrd plugin: Unable to handle line: %s", buffer);
684 } /* while (fgets) */
689 } /* }}} int olsrd_read */
691 static int olsrd_shutdown (void) /* {{{ */
694 sfree (config_service);
697 } /* }}} int olsrd_shutdown */
699 void module_register (void)
701 plugin_register_config ("olsrd", olsrd_config,
702 config_keys, config_keys_num);
703 plugin_register_read ("olsrd", olsrd_read);
704 plugin_register_shutdown ("olsrd", olsrd_shutdown);
705 } /* void module_register */
707 /* vim: set sw=2 sts=2 et fdm=marker : */