Skip to content

Commit

Permalink
Merge pull request #3502 from efuss/netbsd-netstat_udp
Browse files Browse the repository at this point in the history
Add a (currently NetBSD-only) netstat_udp plugin
  • Loading branch information
mrunge committed Jul 17, 2020
2 parents 5e30bc4 + 6c89b62 commit 41b1e33
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
4 changes: 4 additions & 0 deletions configure.ac
Expand Up @@ -6610,6 +6610,7 @@ plugin_mcelog="no"
plugin_mdevents="no"
plugin_memory="no"
plugin_multimeter="no"
plugin_netstat_udp="no"
plugin_nfs="no"
plugin_numa="no"
plugin_ovs_events="no"
Expand Down Expand Up @@ -6724,6 +6725,7 @@ if test "x$ac_system" = "xNetBSD"; then
plugin_irq="yes"
plugin_nfs="yes"
plugin_processes="yes"
plugin_netstat_udp="yes"
fi
# Mac OS X devices
Expand Down Expand Up @@ -7094,6 +7096,7 @@ AC_PLUGIN([multimeter], [$plugin_multimeter], [Read multimeter v
AC_PLUGIN([mysql], [$with_libmysql], [MySQL statistics])
AC_PLUGIN([netapp], [$with_libnetapp], [NetApp plugin])
AC_PLUGIN([netlink], [$with_libmnl], [Enhanced Linux network statistics])
AC_PLUGIN([netstat_udp], [$plugin_netstat_udp], [UDP network statistics])
AC_PLUGIN([network], [yes], [Network communication plugin])
AC_PLUGIN([nfs], [$plugin_nfs], [NFS statistics])
AC_PLUGIN([nginx], [$with_libcurl], [nginx statistics])
Expand Down Expand Up @@ -7539,6 +7542,7 @@ AC_MSG_RESULT([ multimeter . . . . . $enable_multimeter])
AC_MSG_RESULT([ mysql . . . . . . . . $enable_mysql])
AC_MSG_RESULT([ netapp . . . . . . . $enable_netapp])
AC_MSG_RESULT([ netlink . . . . . . . $enable_netlink])
AC_MSG_RESULT([ netstat_udp . . . . . $enable_netstat_udp])
AC_MSG_RESULT([ network . . . . . . . $enable_network])
AC_MSG_RESULT([ nfs . . . . . . . . . $enable_nfs])
AC_MSG_RESULT([ nginx . . . . . . . . $enable_nginx])
Expand Down
122 changes: 122 additions & 0 deletions src/netstat_udp.c
@@ -0,0 +1,122 @@
/**
* collectd - src/netstat-udp.c
* Copyright (C) 2015 Håvard Eidnes
*
* 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.
*
* 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.
*
* 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
*
* Authors:
* Håvard Eidnes <he at NetBSD.org>
**/

#include "collectd.h"
#include "plugin.h"
#include "utils/common/common.h"

#if !defined(KERNEL_NETBSD)
#error "No applicable input method."
#endif

#include <sys/cdefs.h>
#include <sys/sysctl.h>
#include <sys/types.h>

#include <netinet/in.h>
#include <netinet/ip_var.h>
#include <netinet/udp.h>
#include <netinet/udp_var.h>
#include <netinet6/udp6_var.h>

static int netstat_udp_init(void) { return (0); } /* int netstat_udp_init */

#define SUBMIT_VARS(...) \
plugin_dispatch_multivalue(vl, 0, DS_TYPE_DERIVE, __VA_ARGS__, NULL)

static int netstat_udp_internal(value_list_t *vl) {
uint64_t udpstat[UDP_NSTATS];
uint64_t udp6stat[UDP6_NSTATS];
size_t size;
uint64_t delivered, delivered6;
int err;

size = sizeof(udpstat);
if (sysctlbyname("net.inet.udp.stats", udpstat, &size, NULL, 0) == -1) {
ERROR("netstat-udp plugin: could not get udp stats");
return -1;
}

delivered = udpstat[UDP_STAT_IPACKETS] - udpstat[UDP_STAT_HDROPS] -
udpstat[UDP_STAT_BADLEN] - udpstat[UDP_STAT_BADSUM] -
udpstat[UDP_STAT_NOPORT] - udpstat[UDP_STAT_NOPORTBCAST] -
udpstat[UDP_STAT_FULLSOCK];

err = SUBMIT_VARS("udp-received", (derive_t)udpstat[UDP_STAT_IPACKETS],
"udp-bad-header", (derive_t)udpstat[UDP_STAT_HDROPS],
"udp-bad-length", (derive_t)udpstat[UDP_STAT_BADLEN],
"udp-bad-checksum", (derive_t)udpstat[UDP_STAT_BADSUM],
"udp-no-port", (derive_t)udpstat[UDP_STAT_NOPORT],
"udp-no-port-broadcast",
(derive_t)udpstat[UDP_STAT_NOPORTBCAST], "udp-full-socket",
(derive_t)udpstat[UDP_STAT_FULLSOCK], "udp-delivered",
(derive_t)delivered);
if (err != 0) {
ERROR("netstat-udp plugin: could not submit, err=%d\n", err);
}

size = sizeof(udp6stat);
if (sysctlbyname("net.inet6.udp6.stats", udp6stat, &size, NULL, 0) == -1) {
ERROR("netstat-udp plugin: could not get udp6 stats");
return -1;
}

delivered6 = udp6stat[UDP6_STAT_IPACKETS] - udp6stat[UDP6_STAT_HDROPS] -
udp6stat[UDP6_STAT_BADLEN] - udp6stat[UDP6_STAT_BADSUM] -
udp6stat[UDP6_STAT_NOSUM] - udp6stat[UDP6_STAT_NOPORT] -
udp6stat[UDP6_STAT_NOPORTMCAST] - udp6stat[UDP6_STAT_FULLSOCK];

err = SUBMIT_VARS("udp6-received", (derive_t)udp6stat[UDP6_STAT_IPACKETS],
"udp6-bad-header", (derive_t)udp6stat[UDP6_STAT_HDROPS],
"udp6-bad-length", (derive_t)udp6stat[UDP6_STAT_BADLEN],
"udp6-bad-checksum", (derive_t)udp6stat[UDP6_STAT_BADSUM],
"udp6-no-checksum", (derive_t)udp6stat[UDP6_STAT_NOSUM],
"udp6-no-port", (derive_t)udp6stat[UDP6_STAT_NOPORT],
"udp6-no-port-multicast",
(derive_t)udp6stat[UDP6_STAT_NOPORTMCAST],
"udp6-full-socket", (derive_t)udp6stat[UDP6_STAT_FULLSOCK],
"udp6-delivered", (derive_t)delivered6);
if (err != 0) {
ERROR("netstat-udp plugin ipv6: could not submit, err=%d\n", err);
}

return (0);
} /* }}} int netstat_udp_internal */

static int netstat_udp_read(void) /* {{{ */
{
value_t v[1];
value_list_t vl = VALUE_LIST_INIT;

vl.values = v;
vl.values_len = STATIC_ARRAY_SIZE(v);
sstrncpy(vl.host, hostname_g, sizeof(vl.host));
sstrncpy(vl.plugin, "netstat_udp", sizeof(vl.plugin));
sstrncpy(vl.type, "packets", sizeof(vl.type));
vl.time = cdtime();

return (netstat_udp_internal(&vl));
} /* }}} int netstat_udp_read */

void module_register(void) {
plugin_register_init("netstat_udp", netstat_udp_init);
plugin_register_read("netstat_udp", netstat_udp_read);
} /* void module_register */

0 comments on commit 41b1e33

Please sign in to comment.