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>
31 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
36 #define OLSRD_DEFAULT_NODE "localhost"
37 #define OLSRD_DEFAULT_SERVICE "2006"
39 static const char *config_keys[] =
47 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
49 static char *config_node = NULL;
50 static char *config_service = NULL;
52 #define OLSRD_WANT_NOT 0
53 #define OLSRD_WANT_SUMMARY 1
54 #define OLSRD_WANT_DETAIL 2
55 static int config_want_links = OLSRD_WANT_DETAIL;
56 static int config_want_routes = OLSRD_WANT_SUMMARY;
57 static int config_want_topology = OLSRD_WANT_SUMMARY;
59 static const char *olsrd_get_node (void) /* {{{ */
61 if (config_node != NULL)
63 return (OLSRD_DEFAULT_NODE);
64 } /* }}} const char *olsrd_get_node */
66 static const char *olsrd_get_service (void) /* {{{ */
68 if (config_service != NULL)
69 return (config_service);
70 return (OLSRD_DEFAULT_SERVICE);
71 } /* }}} const char *olsrd_get_service */
73 static void olsrd_set_node (const char *node) /* {{{ */
82 } /* }}} void olsrd_set_node */
84 static void olsrd_set_service (const char *service) /* {{{ */
89 tmp = strdup (service);
93 } /* }}} void olsrd_set_service */
95 static void olsrd_set_detail (int *varptr, const char *detail, /* {{{ */
98 if (strcasecmp ("No", detail) == 0)
99 *varptr = OLSRD_WANT_NOT;
100 else if (strcasecmp ("Summary", detail) == 0)
101 *varptr = OLSRD_WANT_SUMMARY;
102 else if (strcasecmp ("Detail", detail) == 0)
103 *varptr = OLSRD_WANT_DETAIL;
106 ERROR ("olsrd plugin: Invalid argument given to the `%s' configuration "
107 "option: `%s'. Expected: `No', `Summary', or `Detail'.",
110 } /* }}} void olsrd_set_detail */
112 /* Strip trailing newline characters. Returns length of string. */
113 static size_t strchomp (char *buffer) /* {{{ */
117 buffer_len = strlen (buffer);
118 while ((buffer_len > 0)
119 && ((buffer[buffer_len - 1] == '\r')
120 || (buffer[buffer_len - 1] == '\n')))
123 buffer[buffer_len] = 0;
127 } /* }}} size_t strchomp */
129 static size_t strtabsplit (char *string, char **fields, size_t size) /* {{{ */
138 while ((fields[i] = strtok_r (ptr, " \t\r\n", &saveptr)) != NULL)
148 } /* }}} size_t strtabsplit */
150 static FILE *olsrd_connect (void) /* {{{ */
152 struct addrinfo ai_hints;
153 struct addrinfo *ai_list, *ai_ptr;
158 memset (&ai_hints, 0, sizeof (ai_hints));
159 ai_hints.ai_flags = 0;
161 ai_hints.ai_flags |= AI_ADDRCONFIG;
163 ai_hints.ai_family = PF_UNSPEC;
164 ai_hints.ai_socktype = SOCK_STREAM;
165 ai_hints.ai_protocol = IPPROTO_TCP;
168 ai_return = getaddrinfo (olsrd_get_node (), olsrd_get_service (),
169 &ai_hints, &ai_list);
172 ERROR ("olsrd plugin: getaddrinfo (%s, %s) failed: %s",
173 olsrd_get_node (), olsrd_get_service (),
174 gai_strerror (ai_return));
179 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
185 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
188 ERROR ("olsrd plugin: socket failed: %s",
189 sstrerror (errno, errbuf, sizeof (errbuf)));
193 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
196 ERROR ("olsrd plugin: connect failed: %s",
197 sstrerror (errno, errbuf, sizeof (errbuf)));
202 fh = fdopen (fd, "r+");
205 ERROR ("olsrd plugin: fdopen failed.");
213 freeaddrinfo (ai_list);
216 } /* }}} FILE *olsrd_connect */
218 __attribute__ ((nonnull(2)))
219 static void olsrd_submit (const char *plugin_instance, /* {{{ */
220 const char *type, const char *type_instance, gauge_t value)
223 value_list_t vl = VALUE_LIST_INIT;
225 values[0].gauge = value;
230 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
231 sstrncpy (vl.plugin, "olsrd", sizeof (vl.plugin));
232 if (plugin_instance != NULL)
233 sstrncpy (vl.plugin_instance, plugin_instance,
234 sizeof (vl.plugin_instance));
235 sstrncpy (vl.type, type, sizeof (vl.type));
236 if (type_instance != NULL)
237 sstrncpy (vl.type_instance, type_instance,
238 sizeof (vl.type_instance));
240 plugin_dispatch_values (&vl);
241 } /* }}} void olsrd_submit */
243 static int olsrd_cb_ignore (int lineno, /* {{{ */
244 size_t fields_num, char **fields)
247 } /* }}} int olsrd_cb_ignore */
249 static int olsrd_cb_links (int lineno, /* {{{ */
250 size_t fields_num, char **fields)
260 static uint32_t links_num;
261 static double lq_sum;
262 static uint32_t lq_num;
263 static double nlq_sum;
264 static uint32_t nlq_num;
271 if (config_want_links == OLSRD_WANT_NOT)
274 /* Special handling of the first line. */
286 /* Special handling of the last line. */
289 DEBUG ("olsrd plugin: Number of links: %"PRIu32, links_num);
290 olsrd_submit (/* p.-inst = */ "links", /* type = */ "links",
291 /* t.-inst = */ NULL, (gauge_t) links_num);
295 lq = lq_sum / ((double) lq_num);
296 DEBUG ("olsrd plugin: Average LQ: %g", lq);
297 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
302 nlq = nlq_sum / ((double) nlq_num);
303 DEBUG ("olsrd plugin: Average NLQ: %g", nlq);
304 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
317 lq = strtod (fields[3], &endptr);
318 if ((errno != 0) || (endptr == fields[3]))
320 ERROR ("olsrd plugin: Cannot parse link quality: %s", fields[3]);
330 if (config_want_links == OLSRD_WANT_DETAIL)
332 char type_instance[DATA_MAX_NAME_LEN];
334 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-lq",
335 fields[0], fields[1]);
337 DEBUG ("olsrd plugin: links: type_instance = %s; lq = %g;",
339 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
346 nlq = strtod (fields[4], &endptr);
347 if ((errno != 0) || (endptr == fields[4]))
349 ERROR ("olsrd plugin: Cannot parse neighbor link quality: %s", fields[4]);
359 if (config_want_links == OLSRD_WANT_DETAIL)
361 char type_instance[DATA_MAX_NAME_LEN];
363 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
364 fields[0], fields[1]);
366 DEBUG ("olsrd plugin: links: type_instance = %s; nlq = %g;",
368 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
374 } /* }}} int olsrd_cb_links */
376 static int olsrd_cb_routes (int lineno, /* {{{ */
377 size_t fields_num, char **fields)
386 static uint32_t routes_num;
387 static uint32_t metric_sum;
388 static uint32_t metric_num;
389 static double etx_sum;
390 static uint32_t etx_num;
396 if (config_want_routes == OLSRD_WANT_NOT)
399 /* Special handling of the first line */
411 /* Special handling after the last line */
416 DEBUG ("olsrd plugin: Number of routes: %"PRIu32, routes_num);
417 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "routes",
418 /* t.-inst = */ NULL, (gauge_t) routes_num);
422 metric_avg = ((double) metric_sum) / ((double) metric_num);
423 DEBUG ("olsrd plugin: Average metric: %g", metric_avg);
424 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_metric",
425 "average", metric_avg);
429 etx = etx_sum / ((double) etx_sum);
430 DEBUG ("olsrd plugin: Average ETX: %g", etx);
431 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_etx",
444 metric = (uint32_t) strtoul (fields[2], &endptr, 0);
445 if ((errno != 0) || (endptr == fields[2]))
447 ERROR ("olsrd plugin: Unable to parse metric: %s", fields[2]);
452 metric_sum += metric;
454 if (config_want_routes == OLSRD_WANT_DETAIL)
456 DEBUG ("olsrd plugin: destination = %s; metric = %"PRIu32";",
458 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_metric",
459 /* t.-inst = */ fields[0], (gauge_t) metric);
465 etx = strtod (fields[3], &endptr);
466 if ((errno != 0) || (endptr == fields[3]))
468 ERROR ("olsrd plugin: Unable to parse ETX: %s", fields[3]);
478 if (config_want_routes == OLSRD_WANT_DETAIL)
480 DEBUG ("olsrd plugin: destination = %s; etx = %g;",
482 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_etx",
483 /* t.-inst = */ fields[0], etx);
488 } /* }}} int olsrd_cb_routes */
490 static int olsrd_cb_topology (int lineno, /* {{{ */
491 size_t fields_num, char **fields)
500 static double lq_sum;
501 static uint32_t lq_num;
503 static uint32_t links_num;
508 if (config_want_topology == OLSRD_WANT_NOT)
511 /* Special handling of the first line */
521 /* Special handling after the last line */
524 DEBUG ("olsrd plugin: topology: Number of links: %"PRIu32, links_num);
525 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "links",
526 /* t.-inst = */ NULL, (gauge_t) links_num);
530 lq = lq_sum / ((double) lq_sum);
531 DEBUG ("olsrd plugin: topology: Average link quality: %g", lq);
532 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
533 /* t.-inst = */ "average", lq);
545 lq = strtod (fields[2], &endptr);
546 if ((errno != 0) || (endptr == fields[2]))
548 ERROR ("olsrd plugin: Unable to parse LQ: %s", fields[2]);
558 if (config_want_topology == OLSRD_WANT_DETAIL)
560 char type_instance[DATA_MAX_NAME_LEN];
562 memset (type_instance, 0, sizeof (type_instance));
563 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-lq",
564 fields[0], fields[1]);
565 DEBUG ("olsrd plugin: type_instance = %s; lq = %g;", type_instance, lq);
566 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
571 if (config_want_topology == OLSRD_WANT_DETAIL)
577 nlq = strtod (fields[3], &endptr);
578 if ((errno != 0) || (endptr == fields[3]))
580 ERROR ("olsrd plugin: Unable to parse NLQ: %s", fields[3]);
584 char type_instance[DATA_MAX_NAME_LEN];
586 memset (type_instance, 0, sizeof (type_instance));
587 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-nlq",
588 fields[0], fields[1]);
589 DEBUG ("olsrd plugin: type_instance = %s; nlq = %g;", type_instance, nlq);
590 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
596 } /* }}} int olsrd_cb_topology */
598 static int olsrd_read_table (FILE *fh, /* {{{ */
599 int (*callback) (int lineno, size_t fields_num, char **fields))
610 while (fgets (buffer, sizeof (buffer), fh) != NULL)
612 /* An empty line ends the table. */
613 buffer_len = strchomp (buffer);
616 (*callback) (lineno, /* fields_num = */ 0, /* fields = */ NULL);
620 fields_num = strtabsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
622 (*callback) (lineno, fields_num, fields);
624 } /* while (fgets) */
627 } /* }}} int olsrd_read_table */
629 static int olsrd_config (const char *key, const char *value) /* {{{ */
631 if (strcasecmp ("Host", key) == 0)
632 olsrd_set_node (value);
633 else if (strcasecmp ("Port", key) == 0)
634 olsrd_set_service (value);
635 else if (strcasecmp ("CollectLinks", key) == 0)
636 olsrd_set_detail (&config_want_links, value, key);
637 else if (strcasecmp ("CollectRoutes", key) == 0)
638 olsrd_set_detail (&config_want_routes, value, key);
639 else if (strcasecmp ("CollectTopology", key) == 0)
640 olsrd_set_detail (&config_want_topology, value, key);
643 ERROR ("olsrd plugin: Unknown configuration option given: %s", key);
648 } /* }}} int olsrd_config */
650 static int olsrd_read (void) /* {{{ */
656 fh = olsrd_connect ();
663 while (fgets (buffer, sizeof (buffer), fh) != NULL)
665 buffer_len = strchomp (buffer);
669 if (strcmp ("Table: Links", buffer) == 0)
670 olsrd_read_table (fh, olsrd_cb_links);
671 else if (strcmp ("Table: Neighbors", buffer) == 0)
672 olsrd_read_table (fh, olsrd_cb_ignore);
673 else if (strcmp ("Table: Topology", buffer) == 0)
674 olsrd_read_table (fh, olsrd_cb_topology);
675 else if (strcmp ("Table: HNA", buffer) == 0)
676 olsrd_read_table (fh, olsrd_cb_ignore);
677 else if (strcmp ("Table: MID", buffer) == 0)
678 olsrd_read_table (fh, olsrd_cb_ignore);
679 else if (strcmp ("Table: Routes", buffer) == 0)
680 olsrd_read_table (fh, olsrd_cb_routes);
681 else if ((strcmp ("HTTP/1.0 200 OK", buffer) == 0)
682 || (strcmp ("Content-type: text/plain", buffer) == 0))
688 DEBUG ("olsrd plugin: Unable to handle line: %s", buffer);
690 } /* while (fgets) */
695 } /* }}} int olsrd_read */
697 static int olsrd_shutdown (void) /* {{{ */
700 sfree (config_service);
703 } /* }}} int olsrd_shutdown */
705 void module_register (void)
707 plugin_register_config ("olsrd", olsrd_config,
708 config_keys, config_keys_num);
709 plugin_register_read ("olsrd", olsrd_read);
710 plugin_register_shutdown ("olsrd", olsrd_shutdown);
711 } /* void module_register */
713 /* vim: set sw=2 sts=2 et fdm=marker : */