]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
create plugin ipstats
authorMarco van Tol <marco@tols.org>
Thu, 20 Jun 2019 21:01:55 +0000 (23:01 +0200)
committerMarco van Tol <marco@tols.org>
Thu, 20 Jun 2019 21:01:55 +0000 (23:01 +0200)
Makefile.am
README
configure.ac
src/ipstats.c [new file with mode: 0644]
src/types.db

index 8b37b494becde25f837bcbde2d71d3afec6518e5..931f956ece202de283c527ea579cb1bfbd715ffc 100644 (file)
@@ -1136,6 +1136,12 @@ ipmi_la_LDFLAGS = $(PLUGIN_LDFLAGS)
 ipmi_la_LIBADD = libignorelist.la $(BUILD_WITH_OPENIPMI_LIBS)
 endif
 
+if BUILD_PLUGIN_IPSTATS
+pkglib_LTLIBRARIES += ipstats.la
+ipstats_la_SOURCES = src/ipstats.c
+ipstats_la_LDFLAGS = $(PLUGIN_LDFLAGS)
+endif
+
 if BUILD_PLUGIN_IPVS
 pkglib_LTLIBRARIES += ipvs.la
 ipvs_la_SOURCES = src/ipvs.c
diff --git a/README b/README
index 877605489f699c7e7ba0d102dd5764b11ec3f30c..f7b45accb9675417246a16a32177885fe93a4568 100644 (file)
--- a/README
+++ b/README
@@ -174,6 +174,9 @@ Features
     - ipmi
       IPMI (Intelligent Platform Management Interface) sensors information.
 
+    - ipstats
+      IPv4 and IPv6; incoming, outgoing, forwarded counters
+
     - iptables
       Iptables' counters: Number of bytes that were matched by a certain
       iptables rule.
index f6f9acbf0f83f4419adb9d5c9c3b9f6a57c4f9fc..e71e2be1dd2018d541d4b0018bdf2ebb74f300f1 100644 (file)
@@ -6394,6 +6394,7 @@ plugin_intel_rdt="no"
 plugin_interface="no"
 plugin_ipc="no"
 plugin_ipmi="no"
+plugin_ipstats="no"
 plugin_ipvs="no"
 plugin_irq="no"
 plugin_load="no"
@@ -6520,6 +6521,7 @@ fi
 if test "x$ac_system" = "xFreeBSD"; then
   plugin_cpufreq="yes"
   plugin_disk="yes"
+  plugin_ipstats="yes"
   plugin_zfs_arc="yes"
 fi
 
@@ -6828,6 +6830,7 @@ AC_PLUGIN([interface],           [$plugin_interface],         [Interface traffic
 AC_PLUGIN([ipc],                 [$plugin_ipc],               [IPC statistics])
 AC_PLUGIN([ipmi],                [$plugin_ipmi],              [IPMI sensor statistics])
 AC_PLUGIN([iptables],            [$with_libiptc],             [IPTables rule counters])
+AC_PLUGIN([ipstats],             [$plugin_ipstats],           [IP packet statistics])
 AC_PLUGIN([ipvs],                [$plugin_ipvs],              [IPVS connection statistics])
 AC_PLUGIN([irq],                 [$plugin_irq],               [IRQ statistics])
 AC_PLUGIN([java],                [$with_java],                [Embed the Java Virtual Machine])
@@ -7254,6 +7257,7 @@ AC_MSG_RESULT([    interface . . . . . . $enable_interface])
 AC_MSG_RESULT([    ipc . . . . . . . . . $enable_ipc])
 AC_MSG_RESULT([    ipmi  . . . . . . . . $enable_ipmi])
 AC_MSG_RESULT([    iptables  . . . . . . $enable_iptables])
+AC_MSG_RESULT([    ipstats . . . . . . . $enable_ipstats])
 AC_MSG_RESULT([    ipvs  . . . . . . . . $enable_ipvs])
 AC_MSG_RESULT([    irq . . . . . . . . . $enable_irq])
 AC_MSG_RESULT([    java  . . . . . . . . $enable_java])
diff --git a/src/ipstats.c b/src/ipstats.c
new file mode 100644 (file)
index 0000000..1fd27dd
--- /dev/null
@@ -0,0 +1,81 @@
+/**
+ * collectd - src/ipstats.c
+ * Copyright (C) 2019  Marco van Tol
+ *
+ * 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:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * 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:
+ *   Marco van Tol <marco at tols.org>
+ **/
+
+#include "collectd.h"
+#include "plugin.h"
+#include "utils/common/common.h"
+
+#if KERNEL_FREEBSD
+#include <sys/sysctl.h>
+#include <sys/types.h>
+
+#include <netinet/in.h>
+#include <netinet/ip_var.h>
+#include <netinet6/ip6_var.h>
+#endif
+
+static void ipstats_submit(const char *family, const char *type, derive_t rx,
+                           derive_t tx, derive_t fwd) {
+  value_list_t vl = VALUE_LIST_INIT;
+  value_t values[] = {{.derive = rx}, {.derive = tx}, {.derive = fwd}};
+
+  vl.values = values;
+  vl.values_len = STATIC_ARRAY_SIZE(values);
+
+  sstrncpy(vl.plugin, "ipstats", sizeof(vl.plugin));
+  sstrncpy(vl.plugin_instance, family, sizeof(vl.plugin_instance));
+  sstrncpy(vl.type, type, sizeof(vl.type));
+
+  plugin_dispatch_values(&vl);
+} /* void ipstats_submit */
+
+static int ipstats_read(void) {
+#if KERNEL_FREEBSD
+  struct ipstat ipstat;
+  size_t ipslen = sizeof(ipstat);
+  char mib[] = "net.inet.ip.stats";
+
+  if (sysctlbyname(mib, &ipstat, &ipslen, NULL, 0) != 0)
+    WARNING("ipstats plugin: sysctl \"%s\" failed.", mib);
+  else
+    ipstats_submit("ipv4", "ips_packets", ipstat.ips_total, ipstat.ips_localout,
+                   ipstat.ips_forward);
+
+  struct ip6stat ip6stat;
+  size_t ip6slen = sizeof(ip6stat);
+  char mib6[] = "net.inet6.ip6.stats";
+
+  if (sysctlbyname(mib6, &ip6stat, &ip6slen, NULL, 0) != 0)
+    WARNING("ipstats plugin: sysctl \"%s\" failed.", mib);
+  else
+    ipstats_submit("ipv6", "ips_packets", ip6stat.ip6s_total,
+                   ip6stat.ip6s_localout, ip6stat.ip6s_forward);
+#endif
+
+  return 0;
+} /* int ipstats_read */
+
+void module_register(void) { plugin_register_read("ipstats", ipstats_read); }
index 69f59b06595fab9d40ec5a4abe80d4bb21e2306a..6dd53b81f12ddf01ed0649b7013074f18bc945cf 100644 (file)
@@ -127,6 +127,7 @@ io_octets               rx:DERIVE:0:U, tx:DERIVE:0:U
 io_ops                  read:DERIVE:0:U, write:DERIVE:0:U
 io_packets              rx:DERIVE:0:U, tx:DERIVE:0:U
 ipc                     value:GAUGE:0:U
+ips_packets             rx:DERIVE:0:U, tx:DERIVE:0:U, fwd:DERIVE:0:U
 ipt_bytes               value:DERIVE:0:U
 ipt_packets             value:DERIVE:0:U
 irq                     value:DERIVE:0:U