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>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <netinet/tcp.h>
32 #define OLSRD_DEFAULT_NODE "localhost"
33 #define OLSRD_DEFAULT_SERVICE "2006"
35 static const char *config_keys[] =
43 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
45 static char *config_node = NULL;
46 static char *config_service = NULL;
48 #define OLSRD_WANT_NOT 0
49 #define OLSRD_WANT_SUMMARY 1
50 #define OLSRD_WANT_DETAIL 2
51 static int config_want_links = OLSRD_WANT_DETAIL;
52 static int config_want_routes = OLSRD_WANT_SUMMARY;
53 static int config_want_topology = OLSRD_WANT_SUMMARY;
55 static const char *olsrd_get_node (void) /* {{{ */
57 if (config_node != NULL)
59 return (OLSRD_DEFAULT_NODE);
60 } /* }}} const char *olsrd_get_node */
62 static const char *olsrd_get_service (void) /* {{{ */
64 if (config_service != NULL)
65 return (config_service);
66 return (OLSRD_DEFAULT_SERVICE);
67 } /* }}} const char *olsrd_get_service */
69 static void olsrd_set_node (const char *node) /* {{{ */
78 } /* }}} void olsrd_set_node */
80 static void olsrd_set_service (const char *service) /* {{{ */
85 tmp = strdup (service);
89 } /* }}} void olsrd_set_service */
91 static void olsrd_set_detail (int *varptr, const char *detail, /* {{{ */
94 if (strcasecmp ("No", detail) == 0)
95 *varptr = OLSRD_WANT_NOT;
96 else if (strcasecmp ("Summary", detail) == 0)
97 *varptr = OLSRD_WANT_SUMMARY;
98 else if (strcasecmp ("Detail", detail) == 0)
99 *varptr = OLSRD_WANT_DETAIL;
102 ERROR ("olsrd plugin: Invalid argument given to the `%s' configuration "
103 "option: `%s'. Expected: `No', `Summary', or `Detail'.",
106 } /* }}} void olsrd_set_detail */
108 /* Strip trailing newline characters. Returns length of string. */
109 static size_t strchomp (char *buffer) /* {{{ */
113 buffer_len = strlen (buffer);
114 while ((buffer_len > 0)
115 && ((buffer[buffer_len - 1] == '\r')
116 || (buffer[buffer_len - 1] == '\n')))
119 buffer[buffer_len] = 0;
123 } /* }}} size_t strchomp */
125 static size_t strtabsplit (char *string, char **fields, size_t size) /* {{{ */
134 while ((fields[i] = strtok_r (ptr, " \t\r\n", &saveptr)) != NULL)
144 } /* }}} size_t strtabsplit */
146 static FILE *olsrd_connect (void) /* {{{ */
148 struct addrinfo ai_hints;
149 struct addrinfo *ai_list, *ai_ptr;
154 memset (&ai_hints, 0, sizeof (ai_hints));
155 ai_hints.ai_flags = 0;
157 ai_hints.ai_flags |= AI_ADDRCONFIG;
159 ai_hints.ai_family = PF_UNSPEC;
160 ai_hints.ai_socktype = SOCK_STREAM;
161 ai_hints.ai_protocol = IPPROTO_TCP;
164 ai_return = getaddrinfo (olsrd_get_node (), olsrd_get_service (),
165 &ai_hints, &ai_list);
168 ERROR ("olsrd plugin: getaddrinfo (%s, %s) failed: %s",
169 olsrd_get_node (), olsrd_get_service (),
170 gai_strerror (ai_return));
175 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
181 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
184 ERROR ("olsrd plugin: socket failed: %s",
185 sstrerror (errno, errbuf, sizeof (errbuf)));
189 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
192 ERROR ("olsrd plugin: connect failed: %s",
193 sstrerror (errno, errbuf, sizeof (errbuf)));
198 fh = fdopen (fd, "r+");
201 ERROR ("olsrd plugin: fdopen failed.");
209 freeaddrinfo (ai_list);
212 } /* }}} FILE *olsrd_connect */
214 __attribute__ ((nonnull(2)))
215 static void olsrd_submit (const char *plugin_instance, /* {{{ */
216 const char *type, const char *type_instance, gauge_t value)
219 value_list_t vl = VALUE_LIST_INIT;
221 values[0].gauge = value;
226 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
227 sstrncpy (vl.plugin, "olsrd", sizeof (vl.plugin));
228 if (plugin_instance != NULL)
229 sstrncpy (vl.plugin_instance, plugin_instance,
230 sizeof (vl.plugin_instance));
231 sstrncpy (vl.type, type, sizeof (vl.type));
232 if (type_instance != NULL)
233 sstrncpy (vl.type_instance, type_instance,
234 sizeof (vl.type_instance));
236 plugin_dispatch_values (&vl);
237 } /* }}} void olsrd_submit */
239 static int olsrd_cb_ignore (int lineno, /* {{{ */
240 size_t fields_num, char **fields)
243 } /* }}} int olsrd_cb_ignore */
245 static int olsrd_cb_links (int lineno, /* {{{ */
246 size_t fields_num, char **fields)
256 static uint32_t links_num;
257 static double lq_sum;
258 static uint32_t lq_num;
259 static double nlq_sum;
260 static uint32_t nlq_num;
267 if (config_want_links == OLSRD_WANT_NOT)
270 /* Special handling of the first line. */
282 /* Special handling of the last line. */
285 DEBUG ("olsrd plugin: Number of links: %"PRIu32, links_num);
286 olsrd_submit (/* p.-inst = */ "links", /* type = */ "links",
287 /* t.-inst = */ NULL, (gauge_t) links_num);
291 lq = lq_sum / ((double) lq_num);
292 DEBUG ("olsrd plugin: Average LQ: %g", lq);
293 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
298 nlq = nlq_sum / ((double) nlq_num);
299 DEBUG ("olsrd plugin: Average NLQ: %g", nlq);
300 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
313 lq = strtod (fields[3], &endptr);
314 if ((errno != 0) || (endptr == fields[3]))
316 ERROR ("olsrd plugin: Cannot parse link quality: %s", fields[3]);
326 if (config_want_links == OLSRD_WANT_DETAIL)
328 char type_instance[DATA_MAX_NAME_LEN];
330 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-lq",
331 fields[0], fields[1]);
333 DEBUG ("olsrd plugin: links: type_instance = %s; lq = %g;",
335 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
342 nlq = strtod (fields[4], &endptr);
343 if ((errno != 0) || (endptr == fields[4]))
345 ERROR ("olsrd plugin: Cannot parse neighbor link quality: %s", fields[4]);
355 if (config_want_links == OLSRD_WANT_DETAIL)
357 char type_instance[DATA_MAX_NAME_LEN];
359 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
360 fields[0], fields[1]);
362 DEBUG ("olsrd plugin: links: type_instance = %s; nlq = %g;",
364 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
370 } /* }}} int olsrd_cb_links */
372 static int olsrd_cb_routes (int lineno, /* {{{ */
373 size_t fields_num, char **fields)
382 static uint32_t routes_num;
383 static uint32_t metric_sum;
384 static uint32_t metric_num;
385 static double etx_sum;
386 static uint32_t etx_num;
392 if (config_want_routes == OLSRD_WANT_NOT)
395 /* Special handling of the first line */
407 /* Special handling after the last line */
412 DEBUG ("olsrd plugin: Number of routes: %"PRIu32, routes_num);
413 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "routes",
414 /* t.-inst = */ NULL, (gauge_t) routes_num);
418 metric_avg = ((double) metric_sum) / ((double) metric_num);
419 DEBUG ("olsrd plugin: Average metric: %g", metric_avg);
420 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_metric",
421 "average", metric_avg);
425 etx = etx_sum / ((double) etx_sum);
426 DEBUG ("olsrd plugin: Average ETX: %g", etx);
427 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_etx",
440 metric = (uint32_t) strtoul (fields[2], &endptr, 0);
441 if ((errno != 0) || (endptr == fields[2]))
443 ERROR ("olsrd plugin: Unable to parse metric: %s", fields[2]);
448 metric_sum += metric;
450 if (config_want_routes == OLSRD_WANT_DETAIL)
452 DEBUG ("olsrd plugin: destination = %s; metric = %"PRIu32";",
454 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_metric",
455 /* t.-inst = */ fields[0], (gauge_t) metric);
461 etx = strtod (fields[3], &endptr);
462 if ((errno != 0) || (endptr == fields[3]))
464 ERROR ("olsrd plugin: Unable to parse ETX: %s", fields[3]);
474 if (config_want_routes == OLSRD_WANT_DETAIL)
476 DEBUG ("olsrd plugin: destination = %s; etx = %g;",
478 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_etx",
479 /* t.-inst = */ fields[0], etx);
484 } /* }}} int olsrd_cb_routes */
486 static int olsrd_cb_topology (int lineno, /* {{{ */
487 size_t fields_num, char **fields)
496 static double lq_sum;
497 static uint32_t lq_num;
499 static uint32_t links_num;
504 if (config_want_topology == OLSRD_WANT_NOT)
507 /* Special handling of the first line */
517 /* Special handling after the last line */
520 DEBUG ("olsrd plugin: topology: Number of links: %"PRIu32, links_num);
521 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "links",
522 /* t.-inst = */ NULL, (gauge_t) links_num);
526 lq = lq_sum / ((double) lq_sum);
527 DEBUG ("olsrd plugin: topology: Average link quality: %g", lq);
528 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
529 /* t.-inst = */ "average", lq);
541 lq = strtod (fields[2], &endptr);
542 if ((errno != 0) || (endptr == fields[2]))
544 ERROR ("olsrd plugin: Unable to parse LQ: %s", fields[2]);
554 if (config_want_topology == OLSRD_WANT_DETAIL)
556 char type_instance[DATA_MAX_NAME_LEN];
558 memset (type_instance, 0, sizeof (type_instance));
559 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-lq",
560 fields[0], fields[1]);
561 DEBUG ("olsrd plugin: type_instance = %s; lq = %g;", type_instance, lq);
562 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
567 if (config_want_topology == OLSRD_WANT_DETAIL)
573 nlq = strtod (fields[3], &endptr);
574 if ((errno != 0) || (endptr == fields[3]))
576 ERROR ("olsrd plugin: Unable to parse NLQ: %s", fields[3]);
580 char type_instance[DATA_MAX_NAME_LEN];
582 memset (type_instance, 0, sizeof (type_instance));
583 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-nlq",
584 fields[0], fields[1]);
585 DEBUG ("olsrd plugin: type_instance = %s; nlq = %g;", type_instance, nlq);
586 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
592 } /* }}} int olsrd_cb_topology */
594 static int olsrd_read_table (FILE *fh, /* {{{ */
595 int (*callback) (int lineno, size_t fields_num, char **fields))
606 while (fgets (buffer, sizeof (buffer), fh) != NULL)
608 /* An empty line ends the table. */
609 buffer_len = strchomp (buffer);
612 (*callback) (lineno, /* fields_num = */ 0, /* fields = */ NULL);
616 fields_num = strtabsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
618 (*callback) (lineno, fields_num, fields);
620 } /* while (fgets) */
623 } /* }}} int olsrd_read_table */
625 static int olsrd_config (const char *key, const char *value) /* {{{ */
627 if (strcasecmp ("Host", key) == 0)
628 olsrd_set_node (value);
629 else if (strcasecmp ("Port", key) == 0)
630 olsrd_set_service (value);
631 else if (strcasecmp ("CollectLinks", key) == 0)
632 olsrd_set_detail (&config_want_links, value, key);
633 else if (strcasecmp ("CollectRoutes", key) == 0)
634 olsrd_set_detail (&config_want_routes, value, key);
635 else if (strcasecmp ("CollectTopology", key) == 0)
636 olsrd_set_detail (&config_want_topology, value, key);
639 ERROR ("olsrd plugin: Unknown configuration option given: %s", key);
644 } /* }}} int olsrd_config */
646 static int olsrd_read (void) /* {{{ */
652 fh = olsrd_connect ();
659 while (fgets (buffer, sizeof (buffer), fh) != NULL)
661 buffer_len = strchomp (buffer);
665 if (strcmp ("Table: Links", buffer) == 0)
666 olsrd_read_table (fh, olsrd_cb_links);
667 else if (strcmp ("Table: Neighbors", buffer) == 0)
668 olsrd_read_table (fh, olsrd_cb_ignore);
669 else if (strcmp ("Table: Topology", buffer) == 0)
670 olsrd_read_table (fh, olsrd_cb_topology);
671 else if (strcmp ("Table: HNA", buffer) == 0)
672 olsrd_read_table (fh, olsrd_cb_ignore);
673 else if (strcmp ("Table: MID", buffer) == 0)
674 olsrd_read_table (fh, olsrd_cb_ignore);
675 else if (strcmp ("Table: Routes", buffer) == 0)
676 olsrd_read_table (fh, olsrd_cb_routes);
677 else if ((strcmp ("HTTP/1.0 200 OK", buffer) == 0)
678 || (strcmp ("Content-type: text/plain", buffer) == 0))
684 DEBUG ("olsrd plugin: Unable to handle line: %s", buffer);
686 } /* while (fgets) */
691 } /* }}} int olsrd_read */
693 static int olsrd_shutdown (void) /* {{{ */
696 sfree (config_service);
699 } /* }}} int olsrd_shutdown */
701 void module_register (void)
703 plugin_register_config ("olsrd", olsrd_config,
704 config_keys, config_keys_num);
705 plugin_register_read ("olsrd", olsrd_read);
706 plugin_register_shutdown ("olsrd", olsrd_shutdown);
707 } /* void module_register */
709 /* vim: set sw=2 sts=2 et fdm=marker : */