X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Ftcpconns.c;h=08838fbe672a3dda69aa62a08400cb60e9bbb2e3;hb=d3e97e2e0c710c1bbe0cac873bd1c5df004bc740;hp=150a7b73d3a07b2cc02732cc5b6b230be3882f2f;hpb=79963d13c1884d1d92667cc502ad20758b084a12;p=collectd.git diff --git a/src/tcpconns.c b/src/tcpconns.c index 150a7b73..08838fbe 100644 --- a/src/tcpconns.c +++ b/src/tcpconns.c @@ -62,11 +62,15 @@ #include "common.h" #include "plugin.h" -#if defined(__OpenBSD__) || defined(__NetBSD__) +#if defined(__OpenBSD__) +#define HAVE_KVM_GETFILES 1 +#endif + +#if defined(__NetBSD__) #undef HAVE_SYSCTLBYNAME /* force HAVE_LIBKVM_NLIST path */ #endif -#if !KERNEL_LINUX && !HAVE_SYSCTLBYNAME && !HAVE_LIBKVM_NLIST && !KERNEL_AIX +#if !KERNEL_LINUX && !HAVE_SYSCTLBYNAME && !HAVE_KVM_GETFILES && !HAVE_LIBKVM_NLIST && !KERNEL_AIX #error "No applicable input method." #endif @@ -105,15 +109,27 @@ #include /* #endif HAVE_SYSCTLBYNAME */ -/* This is for OpenBSD and NetBSD. */ +#elif HAVE_KVM_GETFILES +#include +#include +#define _KERNEL /* for DTYPE_SOCKET */ +#include +#undef _KERNEL + +#include + +#include +/* #endif HAVE_KVM_GETFILES */ + +/* This is for NetBSD. */ #elif HAVE_LIBKVM_NLIST #include #include #include #include +#include #include #include -#include #include #include #include @@ -169,6 +185,19 @@ static const char *tcp_state[] = {"CLOSED", "LISTEN", "SYN_SENT", #define TCP_STATE_MAX 10 /* #endif HAVE_SYSCTLBYNAME */ +#elif HAVE_KVM_GETFILES +static const char *tcp_state[] = {"CLOSED", "LISTEN", "SYN_SENT", + "SYN_RECV", "ESTABLISHED", "CLOSE_WAIT", + "FIN_WAIT1", "CLOSING", "LAST_ACK", + "FIN_WAIT2", "TIME_WAIT"}; + +#define TCP_STATE_LISTEN 1 +#define TCP_STATE_MIN 0 +#define TCP_STATE_MAX 10 + +static kvm_t *kvmd; +/* #endif HAVE_KVM_GETFILES */ + #elif HAVE_LIBKVM_NLIST static const char *tcp_state[] = {"CLOSED", "LISTEN", "SYN_SENT", "SYN_RECV", "ESTABLISHED", "CLOSE_WAIT", @@ -251,7 +280,6 @@ static enum { SRC_DUNNO, SRC_NETLINK, SRC_PROC } linux_source = SRC_DUNNO; static void conn_prepare_vl(value_list_t *vl, value_t *values) { vl->values = values; vl->values_len = 1; - sstrncpy(vl->host, hostname_g, sizeof(vl->host)); sstrncpy(vl->plugin, "tcpconns", sizeof(vl->plugin)); sstrncpy(vl->type, "tcp_connections", sizeof(vl->type)); } @@ -776,6 +804,49 @@ static int conn_read(void) { } /* int conn_read */ /* #endif HAVE_SYSCTLBYNAME */ +#elif HAVE_KVM_GETFILES + +static int conn_init(void) { + char buf[_POSIX2_LINE_MAX]; + + kvmd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, buf); + if (kvmd == NULL) { + ERROR("tcpconns plugin: kvm_openfiles failed: %s", buf); + return (-1); + } + + return (0); +} /* int conn_init */ + +static int conn_read(void) { + struct kinfo_file *kf; + int i, fcnt; + + conn_reset_port_entry(); + + kf = kvm_getfiles(kvmd, KERN_FILE_BYFILE, DTYPE_SOCKET, + sizeof(*kf), &fcnt); + if (kf == NULL) { + ERROR("tcpconns plugin: kvm_getfiles failed."); + return (-1); + } + + for (i = 0; i < fcnt; i++) { + if (kf[i].so_protocol != IPPROTO_TCP) + continue; + if (kf[i].inp_fport == 0) + continue; + conn_handle_ports(ntohs(kf[i].inp_lport), ntohs(kf[i].inp_fport), + kf[i].t_state); + } + + conn_submit_all(); + + return (0); +} +/* int conn_read */ +/* #endif HAVE_KVM_GETFILES */ + #elif HAVE_LIBKVM_NLIST static int kread(u_long addr, void *buf, int size) { int status; @@ -959,7 +1030,3 @@ void module_register(void) { #endif plugin_register_read("tcpconns", conn_read); } /* void module_register */ - -/* - * vim: set shiftwidth=2 softtabstop=2 tabstop=8 fdm=marker : - */