Merge remote-tracking branch 'origin/pr/598'
[collectd.git] / src / olsrd.c
index 8b73214..6d0576c 100644 (file)
@@ -1,22 +1,27 @@
 /**
  * collectd - src/olsrd.c
- * Copyright (C) 2009  Florian octo Forster
+ * Copyright (C) 2009       Florian octo Forster
  *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; only version 2 of the License is applicable.
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
  *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
  *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
  *
  * Authors:
- *   Florian octo Forster <octo at verplant.org>
+ *   Florian octo Forster <octo at collectd.org>
  **/
 
 #include "collectd.h"
 #include "plugin.h"
 
 #include <sys/types.h>
-#include <sys/socket.h>
 #include <netdb.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
 
 #define OLSRD_DEFAULT_NODE "localhost"
 #define OLSRD_DEFAULT_SERVICE "2006"
@@ -209,6 +216,31 @@ static FILE *olsrd_connect (void) /* {{{ */
   return (fh);
 } /* }}} FILE *olsrd_connect */
 
+__attribute__ ((nonnull(2)))
+static void olsrd_submit (const char *plugin_instance, /* {{{ */
+    const char *type, const char *type_instance, gauge_t value)
+{
+  value_t values[1];
+  value_list_t vl = VALUE_LIST_INIT;
+
+  values[0].gauge = value;
+
+  vl.values = values;
+  vl.values_len = 1;
+
+  sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+  sstrncpy (vl.plugin, "olsrd", sizeof (vl.plugin));
+  if (plugin_instance != NULL)
+    sstrncpy (vl.plugin_instance, plugin_instance,
+        sizeof (vl.plugin_instance));
+  sstrncpy (vl.type, type, sizeof (vl.type));
+  if (type_instance != NULL)
+    sstrncpy (vl.type_instance, type_instance,
+        sizeof (vl.type_instance));
+
+  plugin_dispatch_values (&vl);
+} /* }}} void olsrd_submit */
+
 static int olsrd_cb_ignore (int lineno, /* {{{ */
     size_t fields_num, char **fields)
 {
@@ -232,9 +264,8 @@ static int olsrd_cb_links (int lineno, /* {{{ */
   static double   nlq_sum;
   static uint32_t nlq_num;
 
-  char link_name[DATA_MAX_NAME_LEN];
-  double lq;  /* tx */
-  double nlq; /* rx */
+  double lq;
+  double nlq;
 
   char *endptr;
 
@@ -256,17 +287,23 @@ static int olsrd_cb_links (int lineno, /* {{{ */
   /* Special handling of the last line. */
   if (fields_num == 0)
   {
-    DEBUG ("olsrd plugin: fields_num = %"PRIu32";", links_num);
+    DEBUG ("olsrd plugin: Number of links: %"PRIu32, links_num);
+    olsrd_submit (/* p.-inst = */ "links", /* type = */ "links",
+        /* t.-inst = */ NULL, (gauge_t) links_num);
 
     lq = NAN;
     if (lq_num > 0)
       lq = lq_sum / ((double) lq_num);
     DEBUG ("olsrd plugin: Average  LQ: %g", lq);
+    olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
+        "average-lq", lq);
 
     nlq = NAN;
     if (nlq_num > 0)
       nlq = nlq_sum / ((double) nlq_num);
     DEBUG ("olsrd plugin: Average NLQ: %g", nlq);
+    olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
+        "average-nlq", nlq);
 
     return (0);
   }
@@ -276,9 +313,6 @@ static int olsrd_cb_links (int lineno, /* {{{ */
 
   links_num++;
 
-  memset (link_name, 0, sizeof (link_name));
-  ssnprintf (link_name, sizeof (link_name), "%s-%s", fields[0], fields[1]);
-
   errno = 0;
   endptr = NULL;
   lq = strtod (fields[3], &endptr);
@@ -296,7 +330,15 @@ static int olsrd_cb_links (int lineno, /* {{{ */
 
     if (config_want_links == OLSRD_WANT_DETAIL)
     {
-      DEBUG ("olsrd plugin: %s:  LQ = %g.", link_name, lq);
+      char type_instance[DATA_MAX_NAME_LEN];
+
+      ssnprintf (type_instance, sizeof (type_instance), "%s-%s-lq",
+          fields[0], fields[1]);
+
+      DEBUG ("olsrd plugin: links: type_instance = %s;  lq = %g;",
+          type_instance, lq);
+      olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
+          type_instance, lq);
     }
   }
 
@@ -317,7 +359,15 @@ static int olsrd_cb_links (int lineno, /* {{{ */
 
     if (config_want_links == OLSRD_WANT_DETAIL)
     {
-      DEBUG ("olsrd plugin: %s: NLQ = %g.", link_name, lq);
+      char type_instance[DATA_MAX_NAME_LEN];
+
+      ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
+          fields[0], fields[1]);
+
+      DEBUG ("olsrd plugin: links: type_instance = %s; nlq = %g;",
+          type_instance, lq);
+      olsrd_submit (/* p.-inst = */ "links", /* type = */ "signal_quality",
+          type_instance, nlq);
     }
   }
 
@@ -334,9 +384,9 @@ static int olsrd_cb_routes (int lineno, /* {{{ */
    *  3 = ETX
    *  4 = Interface */
 
+  static uint32_t routes_num;
   static uint32_t metric_sum;
   static uint32_t metric_num;
-
   static double   etx_sum;
   static uint32_t etx_num;
 
@@ -350,8 +400,12 @@ static int olsrd_cb_routes (int lineno, /* {{{ */
   /* Special handling of the first line */
   if (lineno <= 0)
   {
+    routes_num = 0;
     metric_num = 0;
     metric_sum = 0;
+    etx_sum = 0.0;
+    etx_num = 0;
+
     return (0);
   }
 
@@ -360,22 +414,32 @@ static int olsrd_cb_routes (int lineno, /* {{{ */
   {
     double metric_avg;
 
+    DEBUG ("olsrd plugin: Number of routes: %"PRIu32, routes_num);
+    olsrd_submit (/* p.-inst = */ "routes", /* type = */ "routes",
+        /* t.-inst = */ NULL, (gauge_t) routes_num);
+
     metric_avg = NAN;
     if (metric_num > 0)
       metric_avg = ((double) metric_sum) / ((double) metric_num);
+    DEBUG ("olsrd plugin: Average metric: %g", metric_avg);
+    olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_metric",
+        "average", metric_avg);
 
     etx = NAN;
     if (etx_num > 0)
       etx = etx_sum / ((double) etx_sum);
+    DEBUG ("olsrd plugin: Average ETX: %g", etx);
+    olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_etx",
+        "average", etx);
 
-    DEBUG ("olsrd plugin: Number of routes: %"PRIu32"; Average metric: %g",
-        metric_num, metric_avg);
     return (0);
   }
 
   if (fields_num != 5)
     return (-1);
 
+  routes_num++;
+
   errno = 0;
   endptr = NULL;
   metric = (uint32_t) strtoul (fields[2], &endptr, 0);
@@ -390,7 +454,10 @@ static int olsrd_cb_routes (int lineno, /* {{{ */
 
     if (config_want_routes == OLSRD_WANT_DETAIL)
     {
-      DEBUG ("olsrd plugin: Route with metric %"PRIu32".", metric);
+      DEBUG ("olsrd plugin: destination = %s; metric = %"PRIu32";",
+          fields[0], metric);
+      olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_metric",
+          /* t.-inst = */ fields[0], (gauge_t) metric);
     }
   }
 
@@ -411,7 +478,10 @@ static int olsrd_cb_routes (int lineno, /* {{{ */
 
     if (config_want_routes == OLSRD_WANT_DETAIL)
     {
-      DEBUG ("olsrd plugin: Route with ETX %g.", etx);
+      DEBUG ("olsrd plugin: destination = %s; etx = %g;",
+          fields[0], etx);
+      olsrd_submit (/* p.-inst = */ "routes", /* type = */ "route_etx",
+          /* t.-inst = */ fields[0], etx);
     }
   }
 
@@ -433,7 +503,6 @@ static int olsrd_cb_topology (int lineno, /* {{{ */
 
   static uint32_t links_num;
 
-  char link_name[DATA_MAX_NAME_LEN];
   double lq;
   char *endptr;
 
@@ -453,12 +522,16 @@ static int olsrd_cb_topology (int lineno, /* {{{ */
   /* Special handling after the last line */
   if (fields_num == 0)
   {
+    DEBUG ("olsrd plugin: topology: Number of links: %"PRIu32, links_num);
+    olsrd_submit (/* p.-inst = */ "topology", /* type = */ "links",
+        /* t.-inst = */ NULL, (gauge_t) links_num);
+
     lq = NAN;
     if (lq_num > 0)
       lq = lq_sum / ((double) lq_sum);
-
-    DEBUG ("olsrd plugin: Number of links: %"PRIu32, links_num);
-    DEBUG ("olsrd plugin: Average link quality: %g", lq);
+    DEBUG ("olsrd plugin: topology: Average link quality: %g", lq);
+    olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
+        /* t.-inst = */ "average", lq);
 
     return (0);
   }
@@ -466,8 +539,6 @@ static int olsrd_cb_topology (int lineno, /* {{{ */
   if (fields_num != 5)
     return (-1);
 
-  memset (link_name, 0, sizeof (link_name));
-  ssnprintf (link_name, sizeof (link_name), "%s-%s", fields[0], fields[1]);
   links_num++;
 
   errno = 0;
@@ -487,7 +558,14 @@ static int olsrd_cb_topology (int lineno, /* {{{ */
 
     if (config_want_topology == OLSRD_WANT_DETAIL)
     {
-      DEBUG ("olsrd plugin: link_name = %s; lq = %g;", link_name, lq);
+      char type_instance[DATA_MAX_NAME_LEN];
+
+      memset (type_instance, 0, sizeof (type_instance));
+      ssnprintf (type_instance, sizeof (type_instance), "%s-%s-lq",
+          fields[0], fields[1]);
+      DEBUG ("olsrd plugin: type_instance = %s; lq = %g;", type_instance, lq);
+      olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
+          type_instance, lq);
     }
   }
 
@@ -504,7 +582,14 @@ static int olsrd_cb_topology (int lineno, /* {{{ */
     }
     else
     {
-      DEBUG ("olsrd plugin: link_name = %s; nlq = %g;", link_name, nlq);
+      char type_instance[DATA_MAX_NAME_LEN];
+
+      memset (type_instance, 0, sizeof (type_instance));
+      ssnprintf (type_instance, sizeof (type_instance), "%s-%s-nlq",
+          fields[0], fields[1]);
+      DEBUG ("olsrd plugin: type_instance = %s; nlq = %g;", type_instance, nlq);
+      olsrd_submit (/* p.-inst = */ "topology", /* type = */ "signal_quality",
+          type_instance, nlq);
     }
   }
 
@@ -573,6 +658,9 @@ static int olsrd_read (void) /* {{{ */
   if (fh == NULL)
     return (-1);
 
+  fputs ("\r\n", fh);
+  fflush (fh);
+
   while (fgets (buffer, sizeof (buffer), fh) != NULL)
   {
     buffer_len = strchomp (buffer);