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 = { 0 };
153 struct addrinfo *ai_list, *ai_ptr;
158 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] = { 0 };
558 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-lq",
559 fields[0], fields[1]);
560 DEBUG ("olsrd plugin: type_instance = %s; lq = %g;", type_instance, lq);
561 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
566 if (config_want_topology == OLSRD_WANT_DETAIL)
572 nlq = strtod (fields[3], &endptr);
573 if ((errno != 0) || (endptr == fields[3]))
575 ERROR ("olsrd plugin: Unable to parse NLQ: %s", fields[3]);
579 char type_instance[DATA_MAX_NAME_LEN] = { 0 };
581 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-nlq",
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 : */