Fixing compile-warnings with braces in battery.c
[collectd.git] / src / traffic.c
index 66f2b13..929589e 100644 (file)
@@ -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
  *   Florian octo Forster <octo at verplant.org>
  **/
 
-#include "traffic.h"
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+
+#if HAVE_SYS_TYPES_H
+#  include <sys/types.h>
+#endif
+#if HAVE_SYS_SOCKET_H
+#  include <sys/socket.h>
+#endif
+
+/* One cannot include both. This sucks. */
+#if HAVE_LINUX_IF_H
+#  include <linux/if.h>
+#elif HAVE_NET_IF_H
+#  include <net/if.h>
+#endif
+
+#if HAVE_LINUX_NETDEVICE_H
+#  include <linux/netdevice.h>
+#endif
+#if HAVE_IFADDRS_H
+#  include <ifaddrs.h>
+#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;
 
-extern time_t curtime;
+#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 */
 
-void traffic_init (void)
+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;
-       kstat_named_t *kn;
        unsigned long long val;
 
        numif = 0;
@@ -73,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)
 {
@@ -102,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;
@@ -181,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 */