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;
159 ai_hints.ai_flags |= AI_ADDRCONFIG;
161 ai_hints.ai_family = PF_UNSPEC;
162 ai_hints.ai_socktype = SOCK_STREAM;
163 ai_hints.ai_protocol = IPPROTO_TCP;
166 ai_return = getaddrinfo (olsrd_get_node (), olsrd_get_service (),
167 &ai_hints, &ai_list);
170 ERROR ("olsrd plugin: getaddrinfo (%s, %s) failed: %s",
171 olsrd_get_node (), olsrd_get_service (),
172 gai_strerror (ai_return));
177 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
183 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
186 ERROR ("olsrd plugin: socket failed: %s",
187 sstrerror (errno, errbuf, sizeof (errbuf)));
191 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
194 ERROR ("olsrd plugin: connect failed: %s",
195 sstrerror (errno, errbuf, sizeof (errbuf)));
200 fh = fdopen (fd, "r+");
203 ERROR ("olsrd plugin: fdopen failed.");
211 freeaddrinfo (ai_list);
214 } /* }}} FILE *olsrd_connect */
216 __attribute__ ((nonnull(2)))
217 static void olsrd_submit (const char *plugin_instance, /* {{{ */
218 const char *type, const char *type_instance, gauge_t value)
221 value_list_t vl = VALUE_LIST_INIT;
223 values[0].gauge = value;
228 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
229 sstrncpy (vl.plugin, "olsrd", sizeof (vl.plugin));
230 if (plugin_instance != NULL)
231 sstrncpy (vl.plugin_instance, plugin_instance,
232 sizeof (vl.plugin_instance));
233 sstrncpy (vl.type, type, sizeof (vl.type));
234 if (type_instance != NULL)
235 sstrncpy (vl.type_instance, type_instance,
236 sizeof (vl.type_instance));
238 plugin_dispatch_values (&vl);
239 } /* }}} void olsrd_submit */
241 static int olsrd_cb_ignore (int lineno, /* {{{ */
242 size_t fields_num, char **fields)
245 } /* }}} int olsrd_cb_ignore */
247 static int olsrd_cb_links (int lineno, /* {{{ */
248 size_t fields_num, char **fields)
258 static uint32_t links_num;
259 static double lq_sum;
260 static uint32_t lq_num;
261 static double nlq_sum;
262 static uint32_t nlq_num;
269 if (config_want_links == OLSRD_WANT_NOT)
272 /* Special handling of the first line. */
284 /* Special handling of the last line. */
287 DEBUG ("olsrd plugin: Number of links: %"PRIu32, links_num);
288 olsrd_submit (/* p.-inst = */ "links", /* type = */ "links",
289 /* t.-inst = */ NULL, (gauge_t) links_num);
293 lq = lq_sum / ((double) lq_num);
294 DEBUG ("olsrd plugin: Average LQ: %g", lq);
295 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
300 nlq = nlq_sum / ((double) nlq_num);
301 DEBUG ("olsrd plugin: Average NLQ: %g", nlq);
302 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
315 lq = strtod (fields[3], &endptr);
316 if ((errno != 0) || (endptr == fields[3]))
318 ERROR ("olsrd plugin: Cannot parse link quality: %s", fields[3]);
328 if (config_want_links == OLSRD_WANT_DETAIL)
330 char type_instance[DATA_MAX_NAME_LEN];
332 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-lq",
333 fields[0], fields[1]);
335 DEBUG ("olsrd plugin: links: type_instance = %s; lq = %g;",
337 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
344 nlq = strtod (fields[4], &endptr);
345 if ((errno != 0) || (endptr == fields[4]))
347 ERROR ("olsrd plugin: Cannot parse neighbor link quality: %s", fields[4]);
357 if (config_want_links == OLSRD_WANT_DETAIL)
359 char type_instance[DATA_MAX_NAME_LEN];
361 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
362 fields[0], fields[1]);
364 DEBUG ("olsrd plugin: links: type_instance = %s; nlq = %g;",
366 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
372 } /* }}} int olsrd_cb_links */
374 static int olsrd_cb_routes (int lineno, /* {{{ */
375 size_t fields_num, char **fields)
384 static uint32_t routes_num;
385 static uint32_t metric_sum;
386 static uint32_t metric_num;
387 static double etx_sum;
388 static uint32_t etx_num;
394 if (config_want_routes == OLSRD_WANT_NOT)
397 /* Special handling of the first line */
409 /* Special handling after the last line */
414 DEBUG ("olsrd plugin: Number of routes: %"PRIu32, routes_num);
415 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "routes",
416 /* t.-inst = */ NULL, (gauge_t) routes_num);
420 metric_avg = ((double) metric_sum) / ((double) metric_num);
421 DEBUG ("olsrd plugin: Average metric: %g", metric_avg);
422 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_metric",
423 "average", metric_avg);
427 etx = etx_sum / ((double) etx_sum);
428 DEBUG ("olsrd plugin: Average ETX: %g", etx);
429 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_etx",
442 metric = (uint32_t) strtoul (fields[2], &endptr, 0);
443 if ((errno != 0) || (endptr == fields[2]))
445 ERROR ("olsrd plugin: Unable to parse metric: %s", fields[2]);
450 metric_sum += metric;
452 if (config_want_routes == OLSRD_WANT_DETAIL)
454 DEBUG ("olsrd plugin: destination = %s; metric = %"PRIu32";",
456 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_metric",
457 /* t.-inst = */ fields[0], (gauge_t) metric);
463 etx = strtod (fields[3], &endptr);
464 if ((errno != 0) || (endptr == fields[3]))
466 ERROR ("olsrd plugin: Unable to parse ETX: %s", fields[3]);
476 if (config_want_routes == OLSRD_WANT_DETAIL)
478 DEBUG ("olsrd plugin: destination = %s; etx = %g;",
480 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_etx",
481 /* t.-inst = */ fields[0], etx);
486 } /* }}} int olsrd_cb_routes */
488 static int olsrd_cb_topology (int lineno, /* {{{ */
489 size_t fields_num, char **fields)
498 static double lq_sum;
499 static uint32_t lq_num;
501 static uint32_t links_num;
506 if (config_want_topology == OLSRD_WANT_NOT)
509 /* Special handling of the first line */
519 /* Special handling after the last line */
522 DEBUG ("olsrd plugin: topology: Number of links: %"PRIu32, links_num);
523 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "links",
524 /* t.-inst = */ NULL, (gauge_t) links_num);
528 lq = lq_sum / ((double) lq_sum);
529 DEBUG ("olsrd plugin: topology: Average link quality: %g", lq);
530 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
531 /* t.-inst = */ "average", lq);
543 lq = strtod (fields[2], &endptr);
544 if ((errno != 0) || (endptr == fields[2]))
546 ERROR ("olsrd plugin: Unable to parse LQ: %s", fields[2]);
556 if (config_want_topology == OLSRD_WANT_DETAIL)
558 char type_instance[DATA_MAX_NAME_LEN] = { 0 };
560 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-lq",
561 fields[0], fields[1]);
562 DEBUG ("olsrd plugin: type_instance = %s; lq = %g;", type_instance, lq);
563 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
568 if (config_want_topology == OLSRD_WANT_DETAIL)
574 nlq = strtod (fields[3], &endptr);
575 if ((errno != 0) || (endptr == fields[3]))
577 ERROR ("olsrd plugin: Unable to parse NLQ: %s", fields[3]);
581 char type_instance[DATA_MAX_NAME_LEN] = { 0 };
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 : */