1 /**
2 * collectd - src/olsrd.c
3 * Copyright (C) 2009 Florian octo Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
32 #include <sys/types.h>
33 #include <netdb.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[] =
41 {
42 "Host",
43 "Port",
44 "CollectLinks",
45 "CollectRoutes",
46 "CollectTopology"
47 };
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) /* {{{ */
61 {
62 if (config_node != NULL)
63 return (config_node);
64 return (OLSRD_DEFAULT_NODE);
65 } /* }}} const char *olsrd_get_node */
67 static const char *olsrd_get_service (void) /* {{{ */
68 {
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) /* {{{ */
75 {
76 char *tmp;
77 if (node == NULL)
78 return;
79 tmp = strdup (node);
80 if (tmp == NULL)
81 return;
82 config_node = tmp;
83 } /* }}} void olsrd_set_node */
85 static void olsrd_set_service (const char *service) /* {{{ */
86 {
87 char *tmp;
88 if (service == NULL)
89 return;
90 tmp = strdup (service);
91 if (tmp == NULL)
92 return;
93 config_service = tmp;
94 } /* }}} void olsrd_set_service */
96 static void olsrd_set_detail (int *varptr, const char *detail, /* {{{ */
97 const char *key)
98 {
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;
105 else
106 {
107 ERROR ("olsrd plugin: Invalid argument given to the `%s' configuration "
108 "option: `%s'. Expected: `No', `Summary', or `Detail'.",
109 key, detail);
110 }
111 } /* }}} void olsrd_set_detail */
113 /* Strip trailing newline characters. Returns length of string. */
114 static size_t strchomp (char *buffer) /* {{{ */
115 {
116 size_t buffer_len;
118 buffer_len = strlen (buffer);
119 while ((buffer_len > 0)
120 && ((buffer[buffer_len - 1] == '\r')
121 || (buffer[buffer_len - 1] == '\n')))
122 {
123 buffer_len--;
124 buffer[buffer_len] = 0;
125 }
127 return (buffer_len);
128 } /* }}} size_t strchomp */
130 static size_t strtabsplit (char *string, char **fields, size_t size) /* {{{ */
131 {
132 size_t i;
133 char *ptr;
134 char *saveptr;
136 i = 0;
137 ptr = string;
138 saveptr = NULL;
139 while ((fields[i] = strtok_r (ptr, " \t\r\n", &saveptr)) != NULL)
140 {
141 ptr = NULL;
142 i++;
144 if (i >= size)
145 break;
146 }
148 return (i);
149 } /* }}} size_t strtabsplit */
151 static FILE *olsrd_connect (void) /* {{{ */
152 {
153 struct addrinfo *ai_list;
154 int ai_return;
156 FILE *fh;
158 struct addrinfo ai_hints = {
159 .ai_family = AF_UNSPEC,
160 .ai_flags = AI_ADDRCONFIG,
161 .ai_protocol = IPPROTO_TCP,
162 .ai_socktype = SOCK_STREAM
163 };
165 ai_return = getaddrinfo (olsrd_get_node (), olsrd_get_service (),
166 &ai_hints, &ai_list);
167 if (ai_return != 0)
168 {
169 ERROR ("olsrd plugin: getaddrinfo (%s, %s) failed: %s",
170 olsrd_get_node (), olsrd_get_service (),
171 gai_strerror (ai_return));
172 return (NULL);
173 }
175 fh = NULL;
176 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
177 {
178 int fd;
179 int status;
180 char errbuf[1024];
182 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
183 if (fd < 0)
184 {
185 ERROR ("olsrd plugin: socket failed: %s",
186 sstrerror (errno, errbuf, sizeof (errbuf)));
187 continue;
188 }
190 status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
191 if (status != 0)
192 {
193 ERROR ("olsrd plugin: connect failed: %s",
194 sstrerror (errno, errbuf, sizeof (errbuf)));
195 close (fd);
196 continue;
197 }
199 fh = fdopen (fd, "r+");
200 if (fh == NULL)
201 {
202 ERROR ("olsrd plugin: fdopen failed.");
203 close (fd);
204 continue;
205 }
207 break;
208 } /* for (ai_ptr) */
210 freeaddrinfo (ai_list);
212 return (fh);
213 } /* }}} FILE *olsrd_connect */
215 __attribute__ ((nonnull(2)))
216 static void olsrd_submit (const char *plugin_instance, /* {{{ */
217 const char *type, const char *type_instance, gauge_t value)
218 {
219 value_list_t vl = VALUE_LIST_INIT;
221 vl.values = &(value_t) { .gauge = value };
222 vl.values_len = 1;
224 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
225 sstrncpy (vl.plugin, "olsrd", sizeof (vl.plugin));
226 if (plugin_instance != NULL)
227 sstrncpy (vl.plugin_instance, plugin_instance,
228 sizeof (vl.plugin_instance));
229 sstrncpy (vl.type, type, sizeof (vl.type));
230 if (type_instance != NULL)
231 sstrncpy (vl.type_instance, type_instance,
232 sizeof (vl.type_instance));
234 plugin_dispatch_values (&vl);
235 } /* }}} void olsrd_submit */
237 static int olsrd_cb_ignore (int lineno, /* {{{ */
238 size_t fields_num, char **fields)
239 {
240 return (0);
241 } /* }}} int olsrd_cb_ignore */
243 static int olsrd_cb_links (int lineno, /* {{{ */
244 size_t fields_num, char **fields)
245 {
246 /* Fields:
247 * 0 = Local IP
248 * 1 = Remote IP
249 * 2 = Hyst.
250 * 3 = LQ
251 * 4 = NLQ
252 * 5 = Cost */
254 static uint32_t links_num;
255 static double lq_sum;
256 static uint32_t lq_num;
257 static double nlq_sum;
258 static uint32_t nlq_num;
260 double lq;
261 double nlq;
263 char *endptr;
265 if (config_want_links == OLSRD_WANT_NOT)
266 return (0);
268 /* Special handling of the first line. */
269 if (lineno <= 0)
270 {
271 links_num = 0;
272 lq_sum = 0.0;
273 lq_num = 0;
274 nlq_sum = 0.0;
275 nlq_num = 0;
277 return (0);
278 }
280 /* Special handling of the last line. */
281 if (fields_num == 0)
282 {
283 DEBUG ("olsrd plugin: Number of links: %"PRIu32, links_num);
284 olsrd_submit (/* p.-inst = */ "links", /* type = */ "links",
285 /* t.-inst = */ NULL, (gauge_t) links_num);
287 lq = NAN;
288 if (lq_num > 0)
289 lq = lq_sum / ((double) lq_num);
290 DEBUG ("olsrd plugin: Average LQ: %g", lq);
291 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
292 "average-lq", lq);
294 nlq = NAN;
295 if (nlq_num > 0)
296 nlq = nlq_sum / ((double) nlq_num);
297 DEBUG ("olsrd plugin: Average NLQ: %g", nlq);
298 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
299 "average-nlq", nlq);
301 return (0);
302 }
304 if (fields_num != 6)
305 return (-1);
307 links_num++;
309 errno = 0;
310 endptr = NULL;
311 lq = strtod (fields[3], &endptr);
312 if ((errno != 0) || (endptr == fields[3]))
313 {
314 ERROR ("olsrd plugin: Cannot parse link quality: %s", fields[3]);
315 }
316 else
317 {
318 if (!isnan (lq))
319 {
320 lq_sum += lq;
321 lq_num++;
322 }
324 if (config_want_links == OLSRD_WANT_DETAIL)
325 {
326 char type_instance[DATA_MAX_NAME_LEN];
328 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-lq",
329 fields[0], fields[1]);
331 DEBUG ("olsrd plugin: links: type_instance = %s; lq = %g;",
332 type_instance, lq);
333 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
334 type_instance, lq);
335 }
336 }
338 errno = 0;
339 endptr = NULL;
340 nlq = strtod (fields[4], &endptr);
341 if ((errno != 0) || (endptr == fields[4]))
342 {
343 ERROR ("olsrd plugin: Cannot parse neighbor link quality: %s", fields[4]);
344 }
345 else
346 {
347 if (!isnan (nlq))
348 {
349 nlq_sum += nlq;
350 nlq_num++;
351 }
353 if (config_want_links == OLSRD_WANT_DETAIL)
354 {
355 char type_instance[DATA_MAX_NAME_LEN];
357 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
358 fields[0], fields[1]);
360 DEBUG ("olsrd plugin: links: type_instance = %s; nlq = %g;",
361 type_instance, lq);
362 olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
363 type_instance, nlq);
364 }
365 }
367 return (0);
368 } /* }}} int olsrd_cb_links */
370 static int olsrd_cb_routes (int lineno, /* {{{ */
371 size_t fields_num, char **fields)
372 {
373 /* Fields:
374 * 0 = Destination
375 * 1 = Gateway IP
376 * 2 = Metric
377 * 3 = ETX
378 * 4 = Interface */
380 static uint32_t routes_num;
381 static uint32_t metric_sum;
382 static uint32_t metric_num;
383 static double etx_sum;
384 static uint32_t etx_num;
386 uint32_t metric;
387 double etx;
388 char *endptr;
390 if (config_want_routes == OLSRD_WANT_NOT)
391 return (0);
393 /* Special handling of the first line */
394 if (lineno <= 0)
395 {
396 routes_num = 0;
397 metric_num = 0;
398 metric_sum = 0;
399 etx_sum = 0.0;
400 etx_num = 0;
402 return (0);
403 }
405 /* Special handling after the last line */
406 if (fields_num == 0)
407 {
408 double metric_avg;
410 DEBUG ("olsrd plugin: Number of routes: %"PRIu32, routes_num);
411 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "routes",
412 /* t.-inst = */ NULL, (gauge_t) routes_num);
414 metric_avg = NAN;
415 if (metric_num > 0)
416 metric_avg = ((double) metric_sum) / ((double) metric_num);
417 DEBUG ("olsrd plugin: Average metric: %g", metric_avg);
418 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_metric",
419 "average", metric_avg);
421 etx = NAN;
422 if (etx_num > 0)
423 etx = etx_sum / ((double) etx_sum);
424 DEBUG ("olsrd plugin: Average ETX: %g", etx);
425 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_etx",
426 "average", etx);
428 return (0);
429 }
431 if (fields_num != 5)
432 return (-1);
434 routes_num++;
436 errno = 0;
437 endptr = NULL;
438 metric = (uint32_t) strtoul (fields[2], &endptr, 0);
439 if ((errno != 0) || (endptr == fields[2]))
440 {
441 ERROR ("olsrd plugin: Unable to parse metric: %s", fields[2]);
442 }
443 else
444 {
445 metric_num++;
446 metric_sum += metric;
448 if (config_want_routes == OLSRD_WANT_DETAIL)
449 {
450 DEBUG ("olsrd plugin: destination = %s; metric = %"PRIu32";",
451 fields[0], metric);
452 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_metric",
453 /* t.-inst = */ fields[0], (gauge_t) metric);
454 }
455 }
457 errno = 0;
458 endptr = NULL;
459 etx = strtod (fields[3], &endptr);
460 if ((errno != 0) || (endptr == fields[3]))
461 {
462 ERROR ("olsrd plugin: Unable to parse ETX: %s", fields[3]);
463 }
464 else
465 {
466 if (!isnan (etx))
467 {
468 etx_sum += etx;
469 etx_num++;
470 }
472 if (config_want_routes == OLSRD_WANT_DETAIL)
473 {
474 DEBUG ("olsrd plugin: destination = %s; etx = %g;",
475 fields[0], etx);
476 olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_etx",
477 /* t.-inst = */ fields[0], etx);
478 }
479 }
481 return (0);
482 } /* }}} int olsrd_cb_routes */
484 static int olsrd_cb_topology (int lineno, /* {{{ */
485 size_t fields_num, char **fields)
486 {
487 /* Fields:
488 * 0 = Dest. IP
489 * 1 = Last hop IP
490 * 2 = LQ
491 * 3 = NLQ
492 * 4 = Cost */
494 static double lq_sum;
495 static uint32_t lq_num;
497 static uint32_t links_num;
499 double lq;
500 char *endptr;
502 if (config_want_topology == OLSRD_WANT_NOT)
503 return (0);
505 /* Special handling of the first line */
506 if (lineno <= 0)
507 {
508 lq_sum = 0.0;
509 lq_num = 0;
510 links_num = 0;
512 return (0);
513 }
515 /* Special handling after the last line */
516 if (fields_num == 0)
517 {
518 DEBUG ("olsrd plugin: topology: Number of links: %"PRIu32, links_num);
519 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "links",
520 /* t.-inst = */ NULL, (gauge_t) links_num);
522 lq = NAN;
523 if (lq_num > 0)
524 lq = lq_sum / ((double) lq_sum);
525 DEBUG ("olsrd plugin: topology: Average link quality: %g", lq);
526 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
527 /* t.-inst = */ "average", lq);
529 return (0);
530 }
532 if (fields_num != 5)
533 return (-1);
535 links_num++;
537 errno = 0;
538 endptr = NULL;
539 lq = strtod (fields[2], &endptr);
540 if ((errno != 0) || (endptr == fields[2]))
541 {
542 ERROR ("olsrd plugin: Unable to parse LQ: %s", fields[2]);
543 }
544 else
545 {
546 if (!isnan (lq))
547 {
548 lq_sum += lq;
549 lq_num++;
550 }
552 if (config_want_topology == OLSRD_WANT_DETAIL)
553 {
554 char type_instance[DATA_MAX_NAME_LEN] = { 0 };
556 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-lq",
557 fields[0], fields[1]);
558 DEBUG ("olsrd plugin: type_instance = %s; lq = %g;", type_instance, lq);
559 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
560 type_instance, lq);
561 }
562 }
564 if (config_want_topology == OLSRD_WANT_DETAIL)
565 {
566 double nlq;
568 errno = 0;
569 endptr = NULL;
570 nlq = strtod (fields[3], &endptr);
571 if ((errno != 0) || (endptr == fields[3]))
572 {
573 ERROR ("olsrd plugin: Unable to parse NLQ: %s", fields[3]);
574 }
575 else
576 {
577 char type_instance[DATA_MAX_NAME_LEN] = { 0 };
579 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-nlq",
580 fields[0], fields[1]);
581 DEBUG ("olsrd plugin: type_instance = %s; nlq = %g;", type_instance, nlq);
582 olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
583 type_instance, nlq);
584 }
585 }
587 return (0);
588 } /* }}} int olsrd_cb_topology */
590 static int olsrd_read_table (FILE *fh, /* {{{ */
591 int (*callback) (int lineno, size_t fields_num, char **fields))
592 {
593 char buffer[1024];
594 size_t buffer_len;
596 char *fields[32];
597 size_t fields_num;
599 int lineno;
601 lineno = 0;
602 while (fgets (buffer, sizeof (buffer), fh) != NULL)
603 {
604 /* An empty line ends the table. */
605 buffer_len = strchomp (buffer);
606 if (buffer_len == 0)
607 {
608 (*callback) (lineno, /* fields_num = */ 0, /* fields = */ NULL);
609 break;
610 }
612 fields_num = strtabsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
614 (*callback) (lineno, fields_num, fields);
615 lineno++;
616 } /* while (fgets) */
618 return (0);
619 } /* }}} int olsrd_read_table */
621 static int olsrd_config (const char *key, const char *value) /* {{{ */
622 {
623 if (strcasecmp ("Host", key) == 0)
624 olsrd_set_node (value);
625 else if (strcasecmp ("Port", key) == 0)
626 olsrd_set_service (value);
627 else if (strcasecmp ("CollectLinks", key) == 0)
628 olsrd_set_detail (&config_want_links, value, key);
629 else if (strcasecmp ("CollectRoutes", key) == 0)
630 olsrd_set_detail (&config_want_routes, value, key);
631 else if (strcasecmp ("CollectTopology", key) == 0)
632 olsrd_set_detail (&config_want_topology, value, key);
633 else
634 {
635 ERROR ("olsrd plugin: Unknown configuration option given: %s", key);
636 return (-1);
637 }
639 return (0);
640 } /* }}} int olsrd_config */
642 static int olsrd_read (void) /* {{{ */
643 {
644 FILE *fh;
645 char buffer[1024];
646 size_t buffer_len;
648 fh = olsrd_connect ();
649 if (fh == NULL)
650 return (-1);
652 fputs ("\r\n", fh);
653 fflush (fh);
655 while (fgets (buffer, sizeof (buffer), fh) != NULL)
656 {
657 buffer_len = strchomp (buffer);
658 if (buffer_len == 0)
659 continue;
661 if (strcmp ("Table: Links", buffer) == 0)
662 olsrd_read_table (fh, olsrd_cb_links);
663 else if (strcmp ("Table: Neighbors", buffer) == 0)
664 olsrd_read_table (fh, olsrd_cb_ignore);
665 else if (strcmp ("Table: Topology", buffer) == 0)
666 olsrd_read_table (fh, olsrd_cb_topology);
667 else if (strcmp ("Table: HNA", buffer) == 0)
668 olsrd_read_table (fh, olsrd_cb_ignore);
669 else if (strcmp ("Table: MID", buffer) == 0)
670 olsrd_read_table (fh, olsrd_cb_ignore);
671 else if (strcmp ("Table: Routes", buffer) == 0)
672 olsrd_read_table (fh, olsrd_cb_routes);
673 else if ((strcmp ("HTTP/1.0 200 OK", buffer) == 0)
674 || (strcmp ("Content-type: text/plain", buffer) == 0))
675 {
676 /* ignore */
677 }
678 else
679 {
680 DEBUG ("olsrd plugin: Unable to handle line: %s", buffer);
681 }
682 } /* while (fgets) */
684 fclose (fh);
686 return (0);
687 } /* }}} int olsrd_read */
689 static int olsrd_shutdown (void) /* {{{ */
690 {
691 sfree (config_node);
692 sfree (config_service);
694 return (0);
695 } /* }}} int olsrd_shutdown */
697 void module_register (void)
698 {
699 plugin_register_config ("olsrd", olsrd_config,
700 config_keys, config_keys_num);
701 plugin_register_read ("olsrd", olsrd_read);
702 plugin_register_shutdown ("olsrd", olsrd_shutdown);
703 } /* void module_register */
705 /* vim: set sw=2 sts=2 et fdm=marker : */