inteface plugin: add formatting string
[collectd.git] / src / interface.c
index 90dc139..a91c13b 100644 (file)
@@ -84,12 +84,15 @@ static const char *config_keys[] =
 {
        "Interface",
        "IgnoreSelected",
+       "ActiveInterfaceOnly",
        NULL
 };
-static int config_keys_num = 2;
+static int config_keys_num = 3;
 
 static ignorelist_t *ignorelist = NULL;
 
+static _Bool active_interface_only = 0;
+
 #ifdef HAVE_LIBKSTAT
 #define MAX_NUMIF 256
 extern kstat_ctl_t *kc;
@@ -114,6 +117,13 @@ static int interface_config (const char *key, const char *value)
                        invert = 0;
                ignorelist_set_invert (ignorelist, invert);
        }
+       else if (strcasecmp (key, "ActiveInterfaceOnly") == 0)
+       {
+               if (IS_TRUE (value))
+                       active_interface_only = 1;
+               else
+                       active_interface_only = 0;
+       }
        else if (strcasecmp (key, "UniqueName") == 0)
        {
                #ifdef HAVE_LIBKSTAT
@@ -223,6 +233,9 @@ static int interface_read (void)
                if (if_ptr->ifa_addr != NULL && if_ptr->ifa_addr->sa_family == AF_LINK) {
                        if_data = (struct IFA_DATA *) if_ptr->ifa_data;
 
+                       if ( active_interface_only && if_data->IFA_RX_PACKT == 0 && if_data->IFA_TX_PACKT == 0 )
+                               continue;
+
                        if_submit (if_ptr->ifa_name, "if_octets",
                                if_data->IFA_RX_BYTES,
                                if_data->IFA_TX_BYTES);
@@ -275,14 +288,17 @@ static int interface_read (void)
                if (numfields < 11)
                        continue;
 
-               incoming = atoll (fields[0]);
-               outgoing = atoll (fields[8]);
-               if_submit (device, "if_octets", incoming, outgoing);
-
                incoming = atoll (fields[1]);
                outgoing = atoll (fields[9]);
+               if ( active_interface_only && incoming == 0 && outgoing == 0 )
+                       continue;
+
                if_submit (device, "if_packets", incoming, outgoing);
 
+               incoming = atoll (fields[0]);
+               outgoing = atoll (fields[8]);
+               if_submit (device, "if_octets", incoming, outgoing);
+
                incoming = atoll (fields[2]);
                outgoing = atoll (fields[10]);
                if_submit (device, "if_errors", incoming, outgoing);
@@ -315,26 +331,28 @@ static int interface_read (void)
                        sstrncpy(iname, ksp[i]->ks_name, sizeof(iname));
 
                /* try to get 64bit counters */
-               rx = get_kstat_value (ksp[i], "rbytes64");
-               tx = get_kstat_value (ksp[i], "obytes64");
+               rx = get_kstat_value (ksp[i], "ipackets64");
+               tx = get_kstat_value (ksp[i], "opackets64");
                /* or fallback to 32bit */
                if (rx == -1LL)
-                       rx = get_kstat_value (ksp[i], "rbytes");
+                       rx = get_kstat_value (ksp[i], "ipackets");
                if (tx == -1LL)
-                       tx = get_kstat_value (ksp[i], "obytes");
+                       tx = get_kstat_value (ksp[i], "opackets");
+               if ( active_interface_only && rx == 0 && tx == 0 )
+                       continue;
                if ((rx != -1LL) || (tx != -1LL))
-                       if_submit (iname, "if_octets", rx, tx);
+                       if_submit (iname, "if_packets", rx, tx);
 
                /* try to get 64bit counters */
-               rx = get_kstat_value (ksp[i], "ipackets64");
-               tx = get_kstat_value (ksp[i], "opackets64");
+               rx = get_kstat_value (ksp[i], "rbytes64");
+               tx = get_kstat_value (ksp[i], "obytes64");
                /* or fallback to 32bit */
                if (rx == -1LL)
-                       rx = get_kstat_value (ksp[i], "ipackets");
+                       rx = get_kstat_value (ksp[i], "rbytes");
                if (tx == -1LL)
-                       tx = get_kstat_value (ksp[i], "opackets");
+                       tx = get_kstat_value (ksp[i], "obytes");
                if ((rx != -1LL) || (tx != -1LL))
-                       if_submit (iname, "if_packets", rx, tx);
+                       if_submit (iname, "if_octets", rx, tx);
 
                /* no 64bit error counters yet */
                rx = get_kstat_value (ksp[i], "ierrors");
@@ -350,8 +368,11 @@ static int interface_read (void)
 
        ios = sg_get_network_io_stats (&num);
 
-       for (i = 0; i < num; i++)
+       for (i = 0; i < num; i++) {
+               if ( active_interface_only && ios[i].rx == 0 && ios[i].tx == 0 )
+                       continue;
                if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
+       }
 /* #endif HAVE_LIBSTATGRAB */
 
 #elif defined(HAVE_PERFSTAT)
@@ -368,9 +389,8 @@ static int interface_read (void)
 
        if (pnif != nif || ifstat == NULL)
        {
-               if (ifstat != NULL)
-                       free(ifstat);
-               ifstat = malloc(nif * sizeof(perfstat_netinterface_t));
+               free(ifstat);
+               ifstat = malloc(nif * sizeof (*ifstat));
        }
        pnif = nif;
 
@@ -385,6 +405,9 @@ static int interface_read (void)
 
        for (i = 0; i < ifs; i++)
        {
+               if ( active_interface_only && ifstat[i].ipackets == 0 && ifstat[i].opackets == 0 )
+                       continue;
+
                if_submit (ifstat[i].name, "if_octets", ifstat[i].ibytes, ifstat[i].obytes);
                if_submit (ifstat[i].name, "if_packets", ifstat[i].ipackets ,ifstat[i].opackets);
                if_submit (ifstat[i].name, "if_errors", ifstat[i].ierrors, ifstat[i].oerrors );
@@ -403,3 +426,13 @@ void module_register (void)
 #endif
        plugin_register_read ("interface", interface_read);
 } /* void module_register */
+
+/*
+ * Local variables:
+ *  c-file-style: "linux"
+ *  indent-tabs-mode: t
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ *  tab-width: 4
+ * End:
+ */