diff --git a/src/traffic.c b/src/traffic.c
index 713b01a4158e41640baa04787e4f1c5d4289b055..14b7dc32b1c6ac9ca33caab29466f5b2a16fd3c2 100644 (file)
--- a/src/traffic.c
+++ b/src/traffic.c
*
* Authors:
* Florian octo Forster <octo at verplant.org>
*
* Authors:
* Florian octo Forster <octo at verplant.org>
+ * Sune Marcher <sm at flork.dk>
**/
#include "collectd.h"
#include "common.h"
#include "plugin.h"
**/
#include "collectd.h"
#include "common.h"
#include "plugin.h"
+#include "configfile.h"
#if HAVE_SYS_TYPES_H
# include <sys/types.h>
#if HAVE_SYS_TYPES_H
# include <sys/types.h>
#define MODULE_NAME "traffic"
#define MODULE_NAME "traffic"
+/*
+ * Various people have reported problems with `getifaddrs' and varying versions
+ * of `glibc'. That's why it's disabled by default. Since more statistics are
+ * available this way one may enable it using the `--enable-getifaddrs' option
+ * of the configure script. -octo
+ */
+#if KERNEL_LINUX
+# if !COLLECT_GETIFADDRS
+# undef HAVE_GETIFADDRS
+# endif /* !COLLECT_GETIFADDRS */
+#endif /* KERNEL_LINUX */
+
#if HAVE_GETIFADDRS || KERNEL_LINUX || HAVE_LIBKSTAT || HAVE_LIBSTATGRAB
# define TRAFFIC_HAVE_READ 1
#else
#if HAVE_GETIFADDRS || KERNEL_LINUX || HAVE_LIBKSTAT || HAVE_LIBSTATGRAB
# define TRAFFIC_HAVE_READ 1
#else
#define BUFSIZE 512
#define BUFSIZE 512
+/*
+ * (Module-)Global variables
+ */
/* TODO: Move this to `interface-%s/<blah>.rrd' in version 4. */
static char *bytes_file = "traffic-%s.rrd";
/* TODO: Move this to `interface-%s/<blah>.rrd' in version 4. */
static char *bytes_file = "traffic-%s.rrd";
-static char *packets_file = "if_packets-%s.rrd";
-static char *errors_file = "if_errors-%s.rrd";
+static char *packets_file = "interface-%s/if_packets.rrd";
+static char *errors_file = "interface-%s/if_errors.rrd";
/* TODO: Maybe implement multicast and broadcast counters */
/* TODO: Maybe implement multicast and broadcast counters */
+static char *config_keys[] =
+{
+ "Interface",
+ "IgnoreSelected",
+ NULL
+};
+static int config_keys_num = 2;
+
static char *bytes_ds_def[] =
{
"DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U",
static char *bytes_ds_def[] =
{
"DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U",
};
static int errors_ds_num = 2;
};
static int errors_ds_num = 2;
+static char **if_list = NULL;
+static int if_list_num = 0;
+/*
+ * if_list_action:
+ * 0 => default is to collect selected interface
+ * 1 => ignore selcted interfaces
+ */
+static int if_list_action = 0;
+
#ifdef HAVE_LIBKSTAT
#define MAX_NUMIF 256
extern kstat_ctl_t *kc;
#ifdef HAVE_LIBKSTAT
#define MAX_NUMIF 256
extern kstat_ctl_t *kc;
static int numif = 0;
#endif /* HAVE_LIBKSTAT */
static int numif = 0;
#endif /* HAVE_LIBKSTAT */
+static int traffic_config (char *key, char *value)
+{
+ char **temp;
+
+ if (strcasecmp (key, "Interface") == 0)
+ {
+ temp = (char **) realloc (if_list, (if_list_num + 1) * sizeof (char *));
+ if (temp == NULL)
+ {
+ syslog (LOG_EMERG, "Cannot allocate more memory.");
+ return (1);
+ }
+ if_list = temp;
+
+ if ((if_list[if_list_num] = strdup (value)) == NULL)
+ {
+ syslog (LOG_EMERG, "Cannot allocate memory.");
+ return (1);
+ }
+ if_list_num++;
+ }
+ else if (strcasecmp (key, "IgnoreSelected") == 0)
+ {
+ if ((strcasecmp (value, "True") == 0)
+ || (strcasecmp (value, "Yes") == 0)
+ || (strcasecmp (value, "On") == 0))
+ if_list_action = 1;
+ else
+ if_list_action = 0;
+ }
+ else
+ {
+ return (-1);
+ }
+
+ return (0);
+}
+
static void traffic_init (void)
{
#if HAVE_GETIFADDRS
static void traffic_init (void)
{
#if HAVE_GETIFADDRS
return;
}
return;
}
+/*
+ * Check if this interface/instance should be ignored. This is called from
+ * both, `submit' and `write' to give client and server the ability to
+ * ignore certain stuff..
+ */
+static int check_ignore_if (const char *interface)
+{
+ int i;
+
+ /* If no interfaces are given collect all interfaces. Mostly to be
+ * backwards compatible, but also because this is much easier. */
+ if (if_list_num < 1)
+ return (0);
+
+ for (i = 0; i < if_list_num; i++)
+ if (strcasecmp (interface, if_list[i]) == 0)
+ return (if_list_action);
+ return (1 - if_list_action);
+}
+
static void generic_write (char *host, char *inst, char *val,
char *file_template,
char **ds_def, int ds_num)
static void generic_write (char *host, char *inst, char *val,
char *file_template,
char **ds_def, int ds_num)
char file[512];
int status;
char file[512];
int status;
+ if (check_ignore_if (inst))
+ return;
+
status = snprintf (file, BUFSIZE, file_template, inst);
if (status < 1)
return;
status = snprintf (file, BUFSIZE, file_template, inst);
if (status < 1)
return;
}
#if TRAFFIC_HAVE_READ
}
#if TRAFFIC_HAVE_READ
-static void bytes_submit (char *device,
- unsigned long long incoming,
- unsigned long long outgoing)
+static void bytes_submit (char *dev,
+ unsigned long long rx,
+ unsigned long long tx)
{
{
- char buf[BUFSIZE];
+ char buf[512];
+ int status;
+
+ if (check_ignore_if (dev))
+ return;
- if (snprintf (buf, BUFSIZE, "%u:%lld:%lld", (unsigned int) curtime, incoming, outgoing) >= BUFSIZE)
+ status = snprintf (buf, 512, "%u:%lld:%lld",
+ (unsigned int) curtime,
+ rx, tx);
+ if ((status >= 512) || (status < 1))
return;
return;
- plugin_submit (MODULE_NAME, device, buf);
+ plugin_submit (MODULE_NAME, dev, buf);
}
}
-#if HAVE_GETIFADDRS || HAVE_LIBKSTAT
+#if HAVE_GETIFADDRS || KERNEL_LINUX || HAVE_LIBKSTAT
static void packets_submit (char *dev,
unsigned long long rx,
unsigned long long tx)
static void packets_submit (char *dev,
unsigned long long rx,
unsigned long long tx)
char buf[512];
int status;
char buf[512];
int status;
+ if (check_ignore_if (dev))
+ return;
+
status = snprintf (buf, 512, "%u:%lld:%lld",
(unsigned int) curtime,
rx, tx);
status = snprintf (buf, 512, "%u:%lld:%lld",
(unsigned int) curtime,
rx, tx);
char buf[512];
int status;
char buf[512];
int status;
+ if (check_ignore_if (dev))
+ return;
+
status = snprintf (buf, 512, "%u:%lld:%lld",
(unsigned int) curtime,
rx, tx);
status = snprintf (buf, 512, "%u:%lld:%lld",
(unsigned int) curtime,
rx, tx);
return;
plugin_submit ("if_errors", dev, buf);
}
return;
plugin_submit ("if_errors", dev, buf);
}
-#endif /* HAVE_GETIFADDRS || HAVE_LIBKSTAT */
+#endif /* HAVE_GETIFADDRS || KERNEL_LINUX || HAVE_LIBKSTAT */
static void traffic_read (void)
{
static void traffic_read (void)
{
while (fgets (buffer, 1024, fh) != NULL)
{
while (fgets (buffer, 1024, fh) != NULL)
{
- if (buffer[6] != ':')
+ if (!(dummy = strchr(buffer, ':')))
continue;
continue;
- buffer[6] = '\0';
+ dummy[0] = '\0';
+ dummy++;
device = buffer;
while (device[0] == ' ')
device = buffer;
while (device[0] == ' ')
if (device[0] == '\0')
continue;
if (device[0] == '\0')
continue;
- dummy = buffer + 7;
numfields = strsplit (dummy, fields, 16);
numfields = strsplit (dummy, fields, 16);
- if (numfields < 9)
+ if (numfields < 11)
continue;
incoming = atoll (fields[0]);
outgoing = atoll (fields[8]);
continue;
incoming = atoll (fields[0]);
outgoing = atoll (fields[8]);
-
bytes_submit (device, incoming, outgoing);
bytes_submit (device, incoming, outgoing);
+
+ incoming = atoll (fields[1]);
+ outgoing = atoll (fields[9]);
+ packets_submit (device, incoming, outgoing);
+
+ incoming = atoll (fields[2]);
+ outgoing = atoll (fields[10]);
+ errors_submit (device, incoming, outgoing);
}
fclose (fh);
}
fclose (fh);
plugin_register (MODULE_NAME, traffic_init, traffic_read, bytes_write);
plugin_register ("if_packets", NULL, NULL, packets_write);
plugin_register ("if_errors", NULL, NULL, errors_write);
plugin_register (MODULE_NAME, traffic_init, traffic_read, bytes_write);
plugin_register ("if_packets", NULL, NULL, packets_write);
plugin_register ("if_errors", NULL, NULL, errors_write);
+ cf_register (MODULE_NAME, traffic_config, config_keys, config_keys_num);
}
#undef BUFSIZE
}
#undef BUFSIZE