From a05e34a0d4fdb2c15db8226689268a27f6f7163d Mon Sep 17 00:00:00 2001 From: octo Date: Fri, 7 Apr 2006 14:02:47 +0000 Subject: [PATCH] The traffic plugin has been extended to use `getifaddrs' which seems to be available on *BSD as well as under the glibc.. The configure-script needed quite some changes for this. --- configure.in | 43 +++++++++++++++++++++++++++++++++ src/traffic.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 116 insertions(+), 4 deletions(-) diff --git a/configure.in b/configure.in index 829fb761..5150146e 100644 --- a/configure.in +++ b/configure.in @@ -151,6 +151,31 @@ AC_CHECK_HEADERS(sys/loadavg.h) AC_CHECK_HEADERS(utmp.h) AC_CHECK_HEADERS(utmpx.h) +# For traffic plugin +AC_CHECK_HEADERS(net/if.h) +AC_CHECK_HEADERS(ifaddrs.h) +AC_CHECK_HEADERS(linux/if.h, [], [], +[ +#if HAVE_SYS_TYPES_H +# include +#endif +#if HAVE_SYS_SOCKET_H +# include +#endif +]) +AC_CHECK_HEADERS(linux/netdevice.h, [], [], +[ +#if HAVE_SYS_TYPES_H +# include +#endif +#if HAVE_SYS_SOCKET_H +# include +#endif +#if HAVE_LINUX_IF_H +# include +#endif +]) + # For apache plugin AC_CHECK_HEADERS(curl/curl.h) @@ -219,6 +244,9 @@ AC_CHECK_FUNCS(getutent getutxent) AC_CHECK_FUNCS(quotactl) AC_CHECK_FUNCS(getgrgid getpwuid) +# For traffic module +AC_CHECK_FUNCS(getifaddrs) + # For mount interface AC_CHECK_FUNCS(getfsent getvfsent listmntent) AC_CHECK_FUNCS(getfsstat) @@ -289,6 +317,21 @@ if test "x$fu_cv_getmntent2" = "xyes"; then ) fi +# Check for structures +AC_CHECK_MEMBERS([struct if_data.ifi_ibytes, struct if_data.ifi_obytes], + [AC_DEFINE(HAVE_STRUCT_IF_DATA, 1, [Define if struct if_data exists and is usable.])], + [], + [#include ]) +AC_CHECK_MEMBERS([struct net_device_stats.rx_bytes, struct net_device_stats.tx_bytes], + [AC_DEFINE(HAVE_STRUCT_NET_DEVICE_STATS, 1, [Define if struct net_device_stats exists and is usable.])], + [], + [ + #include + #include + #include + #include + ]) + AC_MSG_CHECKING([for kernel type ($host_os)]) case $host_os in *linux*) diff --git a/src/traffic.c b/src/traffic.c index 45e4cfee..b5e7887f 100644 --- a/src/traffic.c +++ b/src/traffic.c @@ -24,9 +24,30 @@ #include "common.h" #include "plugin.h" +#if HAVE_SYS_TYPES_H +# include +#endif +#if HAVE_SYS_SOCKET_H +# include +#endif + +/* One cannot include both. This sucks. */ +#if HAVE_LINUX_IF_H +# include +#elif HAVE_NET_IF_H +# include +#endif + +#if HAVE_LINUX_NETDEVICE_H +# include +#endif +#if HAVE_IFADDRS_H +# include +#endif + #define MODULE_NAME "traffic" -#if defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) || defined(HAVE_LIBSTATGRAB) +#if HAVE_GETIFADDRS || defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) || defined(HAVE_LIBSTATGRAB) # define TRAFFIC_HAVE_READ 1 #else # define TRAFFIC_HAVE_READ 0 @@ -53,7 +74,15 @@ static int numif = 0; static void traffic_init (void) { -#ifdef HAVE_LIBKSTAT +#if HAVE_GETIFADDRS + /* nothing */ +/* #endif HAVE_GETIFADDRS */ + +#elif KERNEL_LINUX + /* nothing */ +/* #endif KERNEL_LINUX */ + +#elif HAVE_LIBKSTAT kstat_t *ksp_chain; unsigned long long val; @@ -76,7 +105,13 @@ static void traffic_init (void) continue; ksp[numif++] = ksp_chain; } -#endif /* HAVE_LIBKSTAT */ +/* #endif HAVE_LIBKSTAT */ + +#elif HAVE_LIBSTATG + /* nothing */ +#endif /* HAVE_LIBSTATG */ + + return; } static void traffic_write (char *host, char *inst, char *val) @@ -108,7 +143,41 @@ static void traffic_submit (char *device, static void traffic_read (void) { -#ifdef KERNEL_LINUX +#if HAVE_GETIFADDRS + struct ifaddrs *if_list; + struct ifaddrs *if_ptr; + +#if HAVE_STRUCT_IF_DATA +# define IFA_DATA if_data +# define IFA_INCOMING ifi_ibytes +# define IFA_OUTGOING ifi_obytes +#elif HAVE_STRUCT_NET_DEVICE_STATS +# define IFA_DATA net_device_stats +# define IFA_INCOMING rx_bytes +# define IFA_OUTGOING tx_bytes +#else +# error "No suitable type for `struct ifaddrs->ifa_data' found." +#endif + + struct IFA_DATA *if_data; + + if (getifaddrs (&if_list) != 0) + return; + + for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next) + { + if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL) + continue; + + traffic_submit (if_ptr->ifa_name, + if_data->IFA_INCOMING, + if_data->IFA_OUTGOING); + } + + freeifaddrs (if_list); +/* #endif HAVE_GETIFADDRS */ + +#elif KERNEL_LINUX FILE *fh; char buffer[1024]; unsigned long long incoming, outgoing; -- 2.11.0