Merge pull request #1831 from rubenk/ai_hints-cleanup
[collectd.git] / src / olsrd.c
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  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30
31 #include <sys/types.h>
32 #include <netdb.h>
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
35
36 #define OLSRD_DEFAULT_NODE "localhost"
37 #define OLSRD_DEFAULT_SERVICE "2006"
38
39 static const char *config_keys[] =
40 {
41   "Host",
42   "Port",
43   "CollectLinks",
44   "CollectRoutes",
45   "CollectTopology"
46 };
47 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
48
49 static char *config_node = NULL;
50 static char *config_service = NULL;
51
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;
58
59 static const char *olsrd_get_node (void) /* {{{ */
60 {
61   if (config_node != NULL)
62     return (config_node);
63   return (OLSRD_DEFAULT_NODE);
64 } /* }}} const char *olsrd_get_node */
65
66 static const char *olsrd_get_service (void) /* {{{ */
67 {
68   if (config_service != NULL)
69     return (config_service);
70   return (OLSRD_DEFAULT_SERVICE);
71 } /* }}} const char *olsrd_get_service */
72
73 static void olsrd_set_node (const char *node) /* {{{ */
74 {
75   char *tmp;
76   if (node == NULL)
77     return;
78   tmp = strdup (node);
79   if (tmp == NULL)
80     return;
81   config_node = tmp;
82 } /* }}} void olsrd_set_node */
83
84 static void olsrd_set_service (const char *service) /* {{{ */
85 {
86   char *tmp;
87   if (service == NULL)
88     return;
89   tmp = strdup (service);
90   if (tmp == NULL)
91     return;
92   config_service = tmp;
93 } /* }}} void olsrd_set_service */
94
95 static void olsrd_set_detail (int *varptr, const char *detail, /* {{{ */
96     const char *key)
97 {
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;
104   else
105   {
106     ERROR ("olsrd plugin: Invalid argument given to the `%s' configuration "
107         "option: `%s'. Expected: `No', `Summary', or `Detail'.",
108         key, detail);
109   }
110 } /* }}} void olsrd_set_detail */
111
112 /* Strip trailing newline characters. Returns length of string. */
113 static size_t strchomp (char *buffer) /* {{{ */
114 {
115   size_t buffer_len;
116
117   buffer_len = strlen (buffer);
118   while ((buffer_len > 0)
119       && ((buffer[buffer_len - 1] == '\r')
120         || (buffer[buffer_len - 1] == '\n')))
121   {
122     buffer_len--;
123     buffer[buffer_len] = 0;
124   }
125
126   return (buffer_len);
127 } /* }}} size_t strchomp */
128
129 static size_t strtabsplit (char *string, char **fields, size_t size) /* {{{ */
130 {
131   size_t i;
132   char *ptr;
133   char *saveptr;
134
135   i = 0;
136   ptr = string;
137   saveptr = NULL;
138   while ((fields[i] = strtok_r (ptr, " \t\r\n", &saveptr)) != NULL)
139   {
140     ptr = NULL;
141     i++;
142
143     if (i >= size)
144       break;
145   }
146
147   return (i);
148 } /* }}} size_t strtabsplit */
149
150 static FILE *olsrd_connect (void) /* {{{ */
151 {
152   struct addrinfo *ai_list, *ai_ptr;
153   int              ai_return;
154
155   FILE *fh;
156
157   struct addrinfo ai_hints = {
158     .ai_family   = AF_UNSPEC,
159     .ai_flags    = AI_ADDRCONFIG,
160     .ai_protocol = IPPROTO_TCP,
161     .ai_socktype = SOCK_STREAM
162   };
163
164   ai_return = getaddrinfo (olsrd_get_node (), olsrd_get_service (),
165       &ai_hints, &ai_list);
166   if (ai_return != 0)
167   {
168     ERROR ("olsrd plugin: getaddrinfo (%s, %s) failed: %s",
169         olsrd_get_node (), olsrd_get_service (),
170         gai_strerror (ai_return));
171     return (NULL);
172   }
173
174   fh = NULL;
175   for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
176   {
177     int fd;
178     int status;
179     char errbuf[1024];
180
181     fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
182     if (fd < 0)
183     {
184       ERROR ("olsrd plugin: socket failed: %s",
185           sstrerror (errno, errbuf, sizeof (errbuf)));
186       continue;
187     }
188
189     status = connect (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
190     if (status != 0)
191     {
192       ERROR ("olsrd plugin: connect failed: %s",
193           sstrerror (errno, errbuf, sizeof (errbuf)));
194       close (fd);
195       continue;
196     }
197
198     fh = fdopen (fd, "r+");
199     if (fh == NULL)
200     {
201       ERROR ("olsrd plugin: fdopen failed.");
202       close (fd);
203       continue;
204     }
205
206     break;
207   } /* for (ai_ptr) */
208
209   freeaddrinfo (ai_list);
210
211   return (fh);
212 } /* }}} FILE *olsrd_connect */
213
214 __attribute__ ((nonnull(2)))
215 static void olsrd_submit (const char *plugin_instance, /* {{{ */
216     const char *type, const char *type_instance, gauge_t value)
217 {
218   value_t values[1];
219   value_list_t vl = VALUE_LIST_INIT;
220
221   values[0].gauge = value;
222
223   vl.values = values;
224   vl.values_len = 1;
225
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));
235
236   plugin_dispatch_values (&vl);
237 } /* }}} void olsrd_submit */
238
239 static int olsrd_cb_ignore (int lineno, /* {{{ */
240     size_t fields_num, char **fields)
241 {
242   return (0);
243 } /* }}} int olsrd_cb_ignore */
244
245 static int olsrd_cb_links (int lineno, /* {{{ */
246     size_t fields_num, char **fields)
247 {
248   /* Fields:
249    *  0 = Local IP
250    *  1 = Remote IP
251    *  2 = Hyst.
252    *  3 = LQ
253    *  4 = NLQ
254    *  5 = Cost */
255
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;
261
262   double lq;
263   double nlq;
264
265   char *endptr;
266
267   if (config_want_links == OLSRD_WANT_NOT)
268     return (0);
269
270   /* Special handling of the first line. */
271   if (lineno <= 0)
272   {
273     links_num = 0;
274     lq_sum = 0.0;
275     lq_num = 0;
276     nlq_sum = 0.0;
277     nlq_num = 0;
278
279     return (0);
280   }
281
282   /* Special handling of the last line. */
283   if (fields_num == 0)
284   {
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);
288
289     lq = NAN;
290     if (lq_num > 0)
291       lq = lq_sum / ((double) lq_num);
292     DEBUG ("olsrd plugin: Average  LQ: %g", lq);
293     olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
294         "average-lq", lq);
295
296     nlq = NAN;
297     if (nlq_num > 0)
298       nlq = nlq_sum / ((double) nlq_num);
299     DEBUG ("olsrd plugin: Average NLQ: %g", nlq);
300     olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
301         "average-nlq", nlq);
302
303     return (0);
304   }
305
306   if (fields_num != 6)
307     return (-1);
308
309   links_num++;
310
311   errno = 0;
312   endptr = NULL;
313   lq = strtod (fields[3], &endptr);
314   if ((errno != 0) || (endptr == fields[3]))
315   {
316     ERROR ("olsrd plugin: Cannot parse link quality: %s", fields[3]);
317   }
318   else
319   {
320     if (!isnan (lq))
321     {
322       lq_sum += lq;
323       lq_num++;
324     }
325
326     if (config_want_links == OLSRD_WANT_DETAIL)
327     {
328       char type_instance[DATA_MAX_NAME_LEN];
329
330       ssnprintf (type_instance, sizeof (type_instance), "%s-%s-lq",
331           fields[0], fields[1]);
332
333       DEBUG ("olsrd plugin: links: type_instance = %s;  lq = %g;",
334           type_instance, lq);
335       olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
336           type_instance, lq);
337     }
338   }
339
340   errno = 0;
341   endptr = NULL;
342   nlq = strtod (fields[4], &endptr);
343   if ((errno != 0) || (endptr == fields[4]))
344   {
345     ERROR ("olsrd plugin: Cannot parse neighbor link quality: %s", fields[4]);
346   }
347   else
348   {
349     if (!isnan (nlq))
350     {
351       nlq_sum += nlq;
352       nlq_num++;
353     }
354
355     if (config_want_links == OLSRD_WANT_DETAIL)
356     {
357       char type_instance[DATA_MAX_NAME_LEN];
358
359       ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
360           fields[0], fields[1]);
361
362       DEBUG ("olsrd plugin: links: type_instance = %s; nlq = %g;",
363           type_instance, lq);
364       olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
365           type_instance, nlq);
366     }
367   }
368
369   return (0);
370 } /* }}} int olsrd_cb_links */
371
372 static int olsrd_cb_routes (int lineno, /* {{{ */
373     size_t fields_num, char **fields)
374 {
375   /* Fields:
376    *  0 = Destination
377    *  1 = Gateway IP
378    *  2 = Metric
379    *  3 = ETX
380    *  4 = Interface */
381
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;
387
388   uint32_t metric;
389   double etx;
390   char *endptr;
391
392   if (config_want_routes == OLSRD_WANT_NOT)
393     return (0);
394
395   /* Special handling of the first line */
396   if (lineno <= 0)
397   {
398     routes_num = 0;
399     metric_num = 0;
400     metric_sum = 0;
401     etx_sum = 0.0;
402     etx_num = 0;
403
404     return (0);
405   }
406
407   /* Special handling after the last line */
408   if (fields_num == 0)
409   {
410     double metric_avg;
411
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);
415
416     metric_avg = NAN;
417     if (metric_num > 0)
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);
422
423     etx = NAN;
424     if (etx_num > 0)
425       etx = etx_sum / ((double) etx_sum);
426     DEBUG ("olsrd plugin: Average ETX: %g", etx);
427     olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_etx",
428         "average", etx);
429
430     return (0);
431   }
432
433   if (fields_num != 5)
434     return (-1);
435
436   routes_num++;
437
438   errno = 0;
439   endptr = NULL;
440   metric = (uint32_t) strtoul (fields[2], &endptr, 0);
441   if ((errno != 0) || (endptr == fields[2]))
442   {
443     ERROR ("olsrd plugin: Unable to parse metric: %s", fields[2]);
444   }
445   else
446   {
447     metric_num++;
448     metric_sum += metric;
449
450     if (config_want_routes == OLSRD_WANT_DETAIL)
451     {
452       DEBUG ("olsrd plugin: destination = %s; metric = %"PRIu32";",
453           fields[0], metric);
454       olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_metric",
455           /* t.-inst = */ fields[0], (gauge_t) metric);
456     }
457   }
458
459   errno = 0;
460   endptr = NULL;
461   etx = strtod (fields[3], &endptr);
462   if ((errno != 0) || (endptr == fields[3]))
463   {
464     ERROR ("olsrd plugin: Unable to parse ETX: %s", fields[3]);
465   }
466   else
467   {
468     if (!isnan (etx))
469     {
470       etx_sum += etx;
471       etx_num++;
472     }
473
474     if (config_want_routes == OLSRD_WANT_DETAIL)
475     {
476       DEBUG ("olsrd plugin: destination = %s; etx = %g;",
477           fields[0], etx);
478       olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_etx",
479           /* t.-inst = */ fields[0], etx);
480     }
481   }
482
483   return (0);
484 } /* }}} int olsrd_cb_routes */
485
486 static int olsrd_cb_topology (int lineno, /* {{{ */
487     size_t fields_num, char **fields)
488 {
489   /* Fields:
490    *  0 = Dest. IP
491    *  1 = Last hop IP
492    *  2 = LQ
493    *  3 = NLQ
494    *  4 = Cost */
495
496   static double   lq_sum;
497   static uint32_t lq_num;
498
499   static uint32_t links_num;
500
501   double lq;
502   char *endptr;
503
504   if (config_want_topology == OLSRD_WANT_NOT)
505     return (0);
506
507   /* Special handling of the first line */
508   if (lineno <= 0)
509   {
510     lq_sum = 0.0;
511     lq_num = 0;
512     links_num = 0;
513
514     return (0);
515   }
516
517   /* Special handling after the last line */
518   if (fields_num == 0)
519   {
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);
523
524     lq = NAN;
525     if (lq_num > 0)
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);
530
531     return (0);
532   }
533
534   if (fields_num != 5)
535     return (-1);
536
537   links_num++;
538
539   errno = 0;
540   endptr = NULL;
541   lq = strtod (fields[2], &endptr);
542   if ((errno != 0) || (endptr == fields[2]))
543   {
544     ERROR ("olsrd plugin: Unable to parse LQ: %s", fields[2]);
545   }
546   else
547   {
548     if (!isnan (lq))
549     {
550       lq_sum += lq;
551       lq_num++;
552     }
553
554     if (config_want_topology == OLSRD_WANT_DETAIL)
555     {
556       char type_instance[DATA_MAX_NAME_LEN] = { 0 };
557
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",
562           type_instance, lq);
563     }
564   }
565
566   if (config_want_topology == OLSRD_WANT_DETAIL)
567   {
568     double nlq;
569
570     errno = 0;
571     endptr = NULL;
572     nlq = strtod (fields[3], &endptr);
573     if ((errno != 0) || (endptr == fields[3]))
574     {
575       ERROR ("olsrd plugin: Unable to parse NLQ: %s", fields[3]);
576     }
577     else
578     {
579       char type_instance[DATA_MAX_NAME_LEN] = { 0 };
580
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",
585           type_instance, nlq);
586     }
587   }
588
589   return (0);
590 } /* }}} int olsrd_cb_topology */
591
592 static int olsrd_read_table (FILE *fh, /* {{{ */
593     int (*callback) (int lineno, size_t fields_num, char **fields))
594 {
595   char buffer[1024];
596   size_t buffer_len;
597
598   char *fields[32];
599   size_t fields_num;
600
601   int lineno;
602
603   lineno = 0;
604   while (fgets (buffer, sizeof (buffer), fh) != NULL)
605   {
606     /* An empty line ends the table. */
607     buffer_len = strchomp (buffer);
608     if (buffer_len == 0)
609     {
610       (*callback) (lineno, /* fields_num = */ 0, /* fields = */ NULL);
611       break;
612     }
613
614     fields_num = strtabsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
615
616     (*callback) (lineno, fields_num, fields);
617     lineno++;
618   } /* while (fgets) */
619
620   return (0);
621 } /* }}} int olsrd_read_table */
622
623 static int olsrd_config (const char *key, const char *value) /* {{{ */
624 {
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);
635   else
636   {
637     ERROR ("olsrd plugin: Unknown configuration option given: %s", key);
638     return (-1);
639   }
640
641   return (0);
642 } /* }}} int olsrd_config */
643
644 static int olsrd_read (void) /* {{{ */
645 {
646   FILE *fh;
647   char buffer[1024];
648   size_t buffer_len;
649
650   fh = olsrd_connect ();
651   if (fh == NULL)
652     return (-1);
653
654   fputs ("\r\n", fh);
655   fflush (fh);
656
657   while (fgets (buffer, sizeof (buffer), fh) != NULL)
658   {
659     buffer_len = strchomp (buffer);
660     if (buffer_len == 0)
661       continue;
662
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))
677     {
678       /* ignore */
679     }
680     else
681     {
682       DEBUG ("olsrd plugin: Unable to handle line: %s", buffer);
683     }
684   } /* while (fgets) */
685
686   fclose (fh);
687
688   return (0);
689 } /* }}} int olsrd_read */
690
691 static int olsrd_shutdown (void) /* {{{ */
692 {
693   sfree (config_node);
694   sfree (config_service);
695
696   return (0);
697 } /* }}} int olsrd_shutdown */
698
699 void module_register (void)
700 {
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 */
706
707 /* vim: set sw=2 sts=2 et fdm=marker : */