[collectd] [PATCH] make traffic plugin only create rrds for selected interfaces

ZHUANG YUYAO zhuangyy at xianan.com.cn
Tue Jun 27 08:11:28 CEST 2006


hi,

here is a patch to make traffic plugin create rrds only for selected 
interfaces. there are so many device entry in /proc/net/dev to which I 
don't care.
to name a few: lo, imq0, imq1, shaper0, gre0, tunl0, teql0 and those 
interfaces which are bridged by brctl.

advantage:
less disk space usage and traffic plugin runs a litter faster.

limitation:
only tested under linux, but should work with others.

usage:
<plugin traffic>
    dev eth0
    dev eth1
</plugin>

Best regards,

    Zhuang Yuyao
-------------- next part --------------
--- collectd-3.9.2/src/traffic.c.orig	2006-06-27 09:19:04.000000000 +0800
+++ collectd-3.9.2/src/traffic.c	2006-06-27 09:55:23.000000000 +0800
@@ -23,6 +23,7 @@
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
+#include "configfile.h"
 
 #if HAVE_SYS_TYPES_H
 #  include <sys/types.h>
@@ -65,6 +66,19 @@
 };
 static int ds_num = 2;
 
+static char *config_keys[] =
+{
+	"DEV",
+	NULL
+};
+static int config_keys_num = 1;
+
+#define TRAFFIC_MAX_DEVICES 32
+#define TRAFFIC_DEVICE_SIZE 32
+
+static char traffic_devices[TRAFFIC_MAX_DEVICES][TRAFFIC_DEVICE_SIZE];
+static int traffic_devices_num = 0;
+
 #ifdef HAVE_LIBKSTAT
 #define MAX_NUMIF 256
 extern kstat_ctl_t *kc;
@@ -72,6 +86,54 @@
 static int numif = 0;
 #endif /* HAVE_LIBKSTAT */
 
+
+static int traffic_devices_add(char* v) {
+	int v_len = 0;
+	if (traffic_devices_num >= TRAFFIC_MAX_DEVICES) {
+		syslog (LOG_WARNING, "The traffic device buffer is full. maxinum traffic device number is %d", TRAFFIC_MAX_DEVICES);
+		return -1;
+	}
+	v_len = strlen(v);
+	if (v_len >= TRAFFIC_DEVICE_SIZE) {
+		syslog (LOG_WARNING, "The traffic device name is too long to fit in buffer, the maxinum is %d", TRAFFIC_DEVICE_SIZE);
+		return -1;
+	}
+	strncpy(traffic_devices[traffic_devices_num], v, v_len);
+	traffic_devices[traffic_devices_num][v_len] = '\0';
+	traffic_devices_num ++;
+	return 0;
+}
+
+static int traffic_devices_set(char* v) {
+	int n = 0;
+	if (0 == traffic_devices_num) {
+		return 1;
+	}
+	for (n = 0; n < traffic_devices_num; n ++) {
+		if (strcasecmp(v, traffic_devices[n]) == 0) {
+			return 1;
+		}
+	}
+	return 0;
+}
+
+static int traffic_config (char *key, char *value)
+{
+	if (strcasecmp (key, "dev") == 0)
+	{
+		if (traffic_devices_add (value) < 0)
+		{
+			return (1);
+		}
+	}
+	else
+	{
+		return (-1);
+	}
+
+	return (0);
+}
+
 static void traffic_init (void)
 {
 #if HAVE_GETIFADDRS
@@ -166,6 +228,10 @@
 
 	for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
 	{
+		if (traffic_devices_set(if_ptr->ifa_name) == 0) {
+			continue;
+		}
+
 		if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
 			continue;
 
@@ -206,6 +272,10 @@
 		if (device[0] == '\0')
 			continue;
 		
+		if (traffic_devices_set(device) == 0) {
+			continue;
+		}
+		
 		dummy = buffer + 7;
 		numfields = strsplit (dummy, fields, 16);
 
@@ -233,6 +303,10 @@
 		if (kstat_read (kc, ksp[i], NULL) == -1)
 			continue;
 
+		if (traffic_devices_set(ksp[i]->ks_name) == 0) {
+			continue;
+		}
+
 		if ((incoming = get_kstat_value (ksp[i], "rbytes")) == -1LL)
 			continue;
 		if ((outgoing = get_kstat_value (ksp[i], "obytes")) == -1LL)
@@ -248,8 +322,13 @@
 
 	ios = sg_get_network_io_stats (&num);
 
-	for (i = 0; i < num; i++)
+	for (i = 0; i < num; i++) {
+		if (traffic_devices_set(ios[i].interface_name) == 0) {
+			continue;
+		}
+		
 		traffic_submit (ios[i].interface_name, ios[i].rx, ios[i].tx);
+	}
 #endif /* HAVE_LIBSTATGRAB */
 }
 #else
@@ -259,6 +338,7 @@
 void module_register (void)
 {
 	plugin_register (MODULE_NAME, traffic_init, traffic_read, traffic_write);
+	cf_register (MODULE_NAME, traffic_config, config_keys, config_keys_num);
 }
 
 #undef BUFSIZE


More information about the collectd mailing list