X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Ftraffic.c;h=929589e32baedc0864c7a26e1426e70286eefccf;hb=1f8b06df4f907658bea49c47dd4e8329fdbc8099;hp=5a276a825dec02a7faa3d2a56cc823add5cb73f5;hpb=a82ff6683f0011a498b4fc0947833d2e28925b76;p=collectd.git diff --git a/src/traffic.c b/src/traffic.c index 5a276a82..929589e3 100644 --- a/src/traffic.c +++ b/src/traffic.c @@ -1,6 +1,6 @@ /** * collectd - src/traffic.c - * Copyright (C) 2005 Florian octo Forster + * Copyright (C) 2005,2006 Florian octo Forster * * 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 @@ -20,36 +20,70 @@ * Florian octo Forster **/ -#include "traffic.h" +#include "collectd.h" +#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 -#if COLLECT_TRAFFIC #define MODULE_NAME "traffic" -#include "plugin.h" -#include "common.h" +#if HAVE_GETIFADDRS || defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) || defined(HAVE_LIBSTATGRAB) +# define TRAFFIC_HAVE_READ 1 +#else +# define TRAFFIC_HAVE_READ 0 +#endif -#ifdef HAVE_LIBKSTAT -#define MAX_NUMIF 256 -extern kstat_ctl_t *kc; -static kstat_t *ksp[MAX_NUMIF]; -static int numif = 0; -#endif /* HAVE_LIBKSTAT */ +#define BUFSIZE 512 static char *traffic_filename_template = "traffic-%s.rrd"; static char *ds_def[] = { - "DS:incoming:COUNTER:25:0:U", - "DS:outgoing:COUNTER:25:0:U", + "DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U", + "DS:outgoing:COUNTER:"COLLECTD_HEARTBEAT":0:U", NULL }; static int ds_num = 2; -void traffic_init (void) -{ #ifdef HAVE_LIBKSTAT +#define MAX_NUMIF 256 +extern kstat_ctl_t *kc; +static kstat_t *ksp[MAX_NUMIF]; +static int numif = 0; +#endif /* HAVE_LIBKSTAT */ + +static void traffic_init (void) +{ +#if HAVE_GETIFADDRS + /* nothing */ +/* #endif HAVE_GETIFADDRS */ + +#elif KERNEL_LINUX + /* nothing */ +/* #endif KERNEL_LINUX */ + +#elif HAVE_LIBKSTAT kstat_t *ksp_chain; - kstat_named_t *kn; unsigned long long val; numif = 0; @@ -71,25 +105,31 @@ void traffic_init (void) continue; ksp[numif++] = ksp_chain; } -#endif /* HAVE_LIBKSTAT */ +/* #endif HAVE_LIBKSTAT */ + +#elif HAVE_LIBSTATG + /* nothing */ +#endif /* HAVE_LIBSTATG */ + + return; } -void traffic_write (char *host, char *inst, char *val) +static void traffic_write (char *host, char *inst, char *val) { - char file[512]; + char file[BUFSIZE]; int status; - status = snprintf (file, 512, traffic_filename_template, inst); + status = snprintf (file, BUFSIZE, traffic_filename_template, inst); if (status < 1) return; - else if (status >= 512) + else if (status >= BUFSIZE) return; rrd_update_file (host, file, val, ds_def, ds_num); } -#define BUFSIZE 512 -void traffic_submit (char *device, +#if TRAFFIC_HAVE_READ +static void traffic_submit (char *device, unsigned long long incoming, unsigned long long outgoing) { @@ -100,11 +140,44 @@ void traffic_submit (char *device, plugin_submit (MODULE_NAME, device, buf); } -#undef BUFSIZE -void traffic_read (void) +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; @@ -179,11 +252,14 @@ void traffic_read (void) traffic_submit (ios[i].interface_name, ios[i].rx, ios[i].tx); #endif /* HAVE_LIBSTATGRAB */ } +#else +#define traffic_read NULL +#endif /* TRAFFIC_HAVE_READ */ void module_register (void) { plugin_register (MODULE_NAME, traffic_init, traffic_read, traffic_write); } +#undef BUFSIZE #undef MODULE_NAME -#endif /* COLLECT_TRAFFIC */