From: Marco van Tol Date: Thu, 20 Jun 2019 21:01:55 +0000 (+0200) Subject: create plugin ipstats X-Git-Tag: collectd-5.11.0~35^2~9 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=1f7a78f479b82e6ff912ecd0f3f1a3a7a8f46371;p=thirdparty%2Fcollectd.git create plugin ipstats --- diff --git a/Makefile.am b/Makefile.am index 8b37b494b..931f956ec 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 877605489..f7b45accb 100644 --- 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. diff --git a/configure.ac b/configure.ac index f6f9acbf0..e71e2be1d 100644 --- a/configure.ac +++ b/configure.ac @@ -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 index 000000000..1fd27dda0 --- /dev/null +++ b/src/ipstats.c @@ -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 + **/ + +#include "collectd.h" +#include "plugin.h" +#include "utils/common/common.h" + +#if KERNEL_FREEBSD +#include +#include + +#include +#include +#include +#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); } diff --git a/src/types.db b/src/types.db index 69f59b065..6dd53b81f 100644 --- a/src/types.db +++ b/src/types.db @@ -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