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 <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
37 #define OLSRD_DEFAULT_NODE "localhost"
38 #define OLSRD_DEFAULT_SERVICE "2006"
40 static const char *config_keys[] =
48 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
50 static char *config_node = NULL;
51 static char *config_service = NULL;
53 #define OLSRD_WANT_NOT 0
54 #define OLSRD_WANT_SUMMARY 1
55 #define OLSRD_WANT_DETAIL 2
56 static int config_want_links = OLSRD_WANT_DETAIL;
57 static int config_want_routes = OLSRD_WANT_SUMMARY;
58 static int config_want_topology = OLSRD_WANT_SUMMARY;
60 static const char *olsrd_get_node (void) /* {{{ */
62 if (config_node != NULL)
64 return (OLSRD_DEFAULT_NODE);
65 } /* }}} const char *olsrd_get_node */
67 static const char *olsrd_get_service (void) /* {{{ */
69 if (config_service != NULL)
70 return (config_service);
71 return (OLSRD_DEFAULT_SERVICE);
72 } /* }}} const char *olsrd_get_service */
74 static void olsrd_set_node (const char *node) /* {{{ */
83 } /* }}} void olsrd_set_node */
85 static void olsrd_set_service (const char *service) /* {{{ */
90 tmp = strdup (service);
94 } /* }}} void olsrd_set_service */
96 static void olsrd_set_detail (int *varptr, const char *detail, /* {{{ */
99 if (strcasecmp ("No", detail) == 0)
100 *varptr = OLSRD_WANT_NOT;
101 else if (strcasecmp ("Summary", detail) == 0)
102 *varptr = OLSRD_WANT_SUMMARY;
103 else if (strcasecmp ("Detail", detail) == 0)
104 *varptr = OLSRD_WANT_DETAIL;
107 ERROR ("olsrd plugin: Invalid argument given to the `%s' configuration "
108 "option: `%s'. Expected: `No', `Summary', or `Detail'.",
111 } /* }}} void olsrd_set_detail */
113 /* Strip trailing newline characters. Returns length of string. */
114 static size_t strchomp (char *buffer) /* {{{ */
118 buffer_len = strlen (buffer);
119 while ((buffer_len > 0)
120 && ((buffer[buffer_len - 1] == '\r')
121 || (buffer[buffer_len - 1] == '\n')))
124 buffer[buffer_len] = 0;
128 } /* }}} size_t strchomp */
130 static size_t strtabsplit (char *string, char **fields, size_t size) /* {{{ */
139 while ((fields[i] = strtok_r (ptr, " \t\r\n", &saveptr)) != NULL)
149 } /* }}} size_t strtabsplit */
151 static FILE *olsrd_connect (void) /* {{{ */
153 struct addrinfo ai_hints;
154 struct addrinfo *ai_list, *ai_ptr;
159 memset (&ai_hints, 0, sizeof (ai_hints));
160 ai_hints.ai_flags = 0;
162 ai_hints.ai_flags |= AI_ADDRCONFIG;
164 ai_hints.ai_family = PF_UNSPEC;
165 ai_hints.ai_socktype = SOCK_STREAM;
166 ai_hints.ai_protocol = IPPROTO_TCP;
169 ai_return = getaddrinfo (olsrd_get_node (), olsrd_get_service (),
170 &ai_hints, &ai_list);
173 ERROR ("olsrd plugin: getaddrinfo (%s, %s) failed: %s",
174 olsrd_get_node (), olsrd_get_service (),
175 gai_strerror (ai_return));
180 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
186 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
189 ERROR ("olsrd plugin: socket failed: %s",
190 sstrerror (errno, errbuf, sizeof (errbuf)));
194 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
197 ERROR ("olsrd plugin: connect failed: %s",
198 sstrerror (errno, errbuf, sizeof (errbuf)));
203 fh = fdopen (fd, "r+");
206 ERROR ("olsrd plugin: fdopen failed.");
214 freeaddrinfo (ai_list);
217 } /* }}} FILE *olsrd_connect */
219 __attribute__ ((nonnull(2)))
220 static void olsrd_submit (const char *plugin_instance, /* {{{ */
221 const char *type, const char *type_instance, gauge_t value)
224 value_list_t vl = VALUE_LIST_INIT;
226 values[0].gauge = value;
231 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
232 sstrncpy (vl.plugin, "olsrd", sizeof (vl.plugin));
233 if (plugin_instance != NULL)
234 sstrncpy (vl.plugin_instance, plugin_instance,
235 sizeof (vl.plugin_instance));
236 sstrncpy (vl.type, type, sizeof (vl.type));
237 if (type_instance != NULL)
238 sstrncpy (vl.type_instance, type_instance,
239 sizeof (vl.type_instance));
241 plugin_dispatch_values (&vl);
242 } /* }}} void olsrd_submit */
244 static int olsrd_cb_ignore (int lineno, /* {{{ */
245 size_t fields_num, char **fields)
248 } /* }}} int olsrd_cb_ignore */
250 static int olsrd_cb_links (int lineno, /* {{{ */
251 size_t fields_num, char **fields)
261 static uint32_t links_num;
262 static double lq_sum;
263 static uint32_t lq_num;
264 static double nlq_sum;
265 static uint32_t nlq_num;
272 if (config_want_links == OLSRD_WANT_NOT)
275 /* Special handling of the first line. */
287 /* Special handling of the last line. */
290 DEBUG ("olsrd plugin: Number of links: %"PRIu32, links_num);
291 olsrd_submit (/* p.-inst = */ "links", /* type = */ "links",
292 /* t.-inst = */ NULL, (gauge_t) links_num);
296 lq = lq_sum / ((double) lq_num);
297 DEBUG ("olsrd plugin: Average LQ: %g", lq);
298 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
303 nlq = nlq_sum / ((double) nlq_num);
304 DEBUG ("olsrd plugin: Average NLQ: %g", nlq);
305 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
318 lq = strtod (fields[3], &endptr);
319 if ((errno != 0) || (endptr == fields[3]))
321 ERROR ("olsrd plugin: Cannot parse link quality: %s", fields[3]);
331 if (config_want_links == OLSRD_WANT_DETAIL)
333 char type_instance[DATA_MAX_NAME_LEN];
335 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-lq",
336 fields[0], fields[1]);
338 DEBUG ("olsrd plugin: links: type_instance = %s; lq = %g;",
340 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
347 nlq = strtod (fields[4], &endptr);
348 if ((errno != 0) || (endptr == fields[4]))
350 ERROR ("olsrd plugin: Cannot parse neighbor link quality: %s", fields[4]);
360 if (config_want_links == OLSRD_WANT_DETAIL)
362 char type_instance[DATA_MAX_NAME_LEN];
364 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
365 fields[0], fields[1]);
367 DEBUG ("olsrd plugin: links: type_instance = %s; nlq = %g;",
369 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
375 } /* }}} int olsrd_cb_links */
377 static int olsrd_cb_routes (int lineno, /* {{{ */
378 size_t fields_num, char **fields)
387 static uint32_t routes_num;
388 static uint32_t metric_sum;
389 static uint32_t metric_num;
390 static double etx_sum;
391 static uint32_t etx_num;
397 if (config_want_routes == OLSRD_WANT_NOT)
400 /* Special handling of the first line */
412 /* Special handling after the last line */
417 DEBUG ("olsrd plugin: Number of routes: %"PRIu32, routes_num);
418 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "routes",
419 /* t.-inst = */ NULL, (gauge_t) routes_num);
423 metric_avg = ((double) metric_sum) / ((double) metric_num);
424 DEBUG ("olsrd plugin: Average metric: %g", metric_avg);
425 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_metric",
426 "average", metric_avg);
430 etx = etx_sum / ((double) etx_sum);
431 DEBUG ("olsrd plugin: Average ETX: %g", etx);
432 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_etx",
445 metric = (uint32_t) strtoul (fields[2], &endptr, 0);
446 if ((errno != 0) || (endptr == fields[2]))
448 ERROR ("olsrd plugin: Unable to parse metric: %s", fields[2]);
453 metric_sum += metric;
455 if (config_want_routes == OLSRD_WANT_DETAIL)
457 DEBUG ("olsrd plugin: destination = %s; metric = %"PRIu32";",
459 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_metric",
460 /* t.-inst = */ fields[0], (gauge_t) metric);
466 etx = strtod (fields[3], &endptr);
467 if ((errno != 0) || (endptr == fields[3]))
469 ERROR ("olsrd plugin: Unable to parse ETX: %s", fields[3]);
479 if (config_want_routes == OLSRD_WANT_DETAIL)
481 DEBUG ("olsrd plugin: destination = %s; etx = %g;",
483 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_etx",
484 /* t.-inst = */ fields[0], etx);
489 } /* }}} int olsrd_cb_routes */
491 static int olsrd_cb_topology (int lineno, /* {{{ */
492 size_t fields_num, char **fields)
501 static double lq_sum;
502 static uint32_t lq_num;
504 static uint32_t links_num;
509 if (config_want_topology == OLSRD_WANT_NOT)
512 /* Special handling of the first line */
522 /* Special handling after the last line */
525 DEBUG ("olsrd plugin: topology: Number of links: %"PRIu32, links_num);
526 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "links",
527 /* t.-inst = */ NULL, (gauge_t) links_num);
531 lq = lq_sum / ((double) lq_sum);
532 DEBUG ("olsrd plugin: topology: Average link quality: %g", lq);
533 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
534 /* t.-inst = */ "average", lq);
546 lq = strtod (fields[2], &endptr);
547 if ((errno != 0) || (endptr == fields[2]))
549 ERROR ("olsrd plugin: Unable to parse LQ: %s", fields[2]);
559 if (config_want_topology == OLSRD_WANT_DETAIL)
561 char type_instance[DATA_MAX_NAME_LEN];
563 memset (type_instance, 0, sizeof (type_instance));
564 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-lq",
565 fields[0], fields[1]);
566 DEBUG ("olsrd plugin: type_instance = %s; lq = %g;", type_instance, lq);
567 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
572 if (config_want_topology == OLSRD_WANT_DETAIL)
578 nlq = strtod (fields[3], &endptr);
579 if ((errno != 0) || (endptr == fields[3]))
581 ERROR ("olsrd plugin: Unable to parse NLQ: %s", fields[3]);
585 char type_instance[DATA_MAX_NAME_LEN];
587 memset (type_instance, 0, sizeof (type_instance));
588 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-nlq",
589 fields[0], fields[1]);
590 DEBUG ("olsrd plugin: type_instance = %s; nlq = %g;", type_instance, nlq);
591 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
597 } /* }}} int olsrd_cb_topology */
599 static int olsrd_read_table (FILE *fh, /* {{{ */
600 int (*callback) (int lineno, size_t fields_num, char **fields))
611 while (fgets (buffer, sizeof (buffer), fh) != NULL)
613 /* An empty line ends the table. */
614 buffer_len = strchomp (buffer);
617 (*callback) (lineno, /* fields_num = */ 0, /* fields = */ NULL);
621 fields_num = strtabsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
623 (*callback) (lineno, fields_num, fields);
625 } /* while (fgets) */
628 } /* }}} int olsrd_read_table */
630 static int olsrd_config (const char *key, const char *value) /* {{{ */
632 if (strcasecmp ("Host", key) == 0)
633 olsrd_set_node (value);
634 else if (strcasecmp ("Port", key) == 0)
635 olsrd_set_service (value);
636 else if (strcasecmp ("CollectLinks", key) == 0)
637 olsrd_set_detail (&config_want_links, value, key);
638 else if (strcasecmp ("CollectRoutes", key) == 0)
639 olsrd_set_detail (&config_want_routes, value, key);
640 else if (strcasecmp ("CollectTopology", key) == 0)
641 olsrd_set_detail (&config_want_topology, value, key);
644 ERROR ("olsrd plugin: Unknown configuration option given: %s", key);
649 } /* }}} int olsrd_config */
651 static int olsrd_read (void) /* {{{ */
657 fh = olsrd_connect ();
664 while (fgets (buffer, sizeof (buffer), fh) != NULL)
666 buffer_len = strchomp (buffer);
670 if (strcmp ("Table: Links", buffer) == 0)
671 olsrd_read_table (fh, olsrd_cb_links);
672 else if (strcmp ("Table: Neighbors", buffer) == 0)
673 olsrd_read_table (fh, olsrd_cb_ignore);
674 else if (strcmp ("Table: Topology", buffer) == 0)
675 olsrd_read_table (fh, olsrd_cb_topology);
676 else if (strcmp ("Table: HNA", buffer) == 0)
677 olsrd_read_table (fh, olsrd_cb_ignore);
678 else if (strcmp ("Table: MID", buffer) == 0)
679 olsrd_read_table (fh, olsrd_cb_ignore);
680 else if (strcmp ("Table: Routes", buffer) == 0)
681 olsrd_read_table (fh, olsrd_cb_routes);
682 else if ((strcmp ("HTTP/1.0 200 OK", buffer) == 0)
683 || (strcmp ("Content-type: text/plain", buffer) == 0))
689 DEBUG ("olsrd plugin: Unable to handle line: %s", buffer);
691 } /* while (fgets) */
696 } /* }}} int olsrd_read */
698 static int olsrd_shutdown (void) /* {{{ */
701 sfree (config_service);
704 } /* }}} int olsrd_shutdown */
706 void module_register (void)
708 plugin_register_config ("olsrd", olsrd_config,
709 config_keys, config_keys_num);
710 plugin_register_read ("olsrd", olsrd_read);
711 plugin_register_shutdown ("olsrd", olsrd_shutdown);
712 } /* void module_register */
714 /* vim: set sw=2 sts=2 et fdm=marker : */