2 * collectd - src/interface.c
3 * Copyright (C) 2005-2010 Florian octo Forster
4 * Copyright (C) 2009 Manuel Sanmartin
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at collectd.org>
21 * Sune Marcher <sm at flork.dk>
29 #include "configfile.h"
30 #include "utils_ignorelist.h"
33 # include <sys/types.h>
36 /* One cannot include both. This sucks. */
38 # include <linux/if.h>
43 #if HAVE_LINUX_NETDEVICE_H
44 # include <linux/netdevice.h>
51 # include <statgrab.h>
55 # include <sys/protosw.h>
56 # include <libperfstat.h>
60 * Various people have reported problems with `getifaddrs' and varying versions
61 * of `glibc'. That's why it's disabled by default. Since more statistics are
62 * available this way one may enable it using the `--enable-getifaddrs' option
63 * of the configure script. -octo
66 # if !COLLECT_GETIFADDRS
67 # undef HAVE_GETIFADDRS
68 # endif /* !COLLECT_GETIFADDRS */
69 #endif /* KERNEL_LINUX */
72 static perfstat_netinterface_t *ifstat;
75 #endif /* HAVE_PERFSTAT */
77 #if !HAVE_GETIFADDRS && !KERNEL_LINUX && !HAVE_LIBKSTAT && !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
78 # error "No applicable input method."
82 * (Module-)Global variables
84 static const char *config_keys[] =
90 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
92 static ignorelist_t *ignorelist = NULL;
94 static _Bool report_inactive = 1;
98 extern kstat_ctl_t *kc;
99 static kstat_t *ksp[MAX_NUMIF];
100 static int numif = 0;
101 static _Bool unique_name = 0;
102 #endif /* HAVE_LIBKSTAT */
104 static int interface_config (const char *key, const char *value)
106 if (ignorelist == NULL)
107 ignorelist = ignorelist_create (/* invert = */ 1);
109 if (strcasecmp (key, "Interface") == 0)
111 ignorelist_add (ignorelist, value);
113 else if (strcasecmp (key, "IgnoreSelected") == 0)
118 ignorelist_set_invert (ignorelist, invert);
120 else if (strcasecmp (key, "ReportInactive") == 0)
121 report_inactive = IS_TRUE (value);
122 else if (strcasecmp (key, "UniqueName") == 0)
128 WARNING ("interface plugin: the \"UniqueName\" option is only valid on Solaris.");
129 #endif /* HAVE_LIBKSTAT */
140 static int interface_init (void)
149 for (numif = 0, ksp_chain = kc->kc_chain;
150 (numif < MAX_NUMIF) && (ksp_chain != NULL);
151 ksp_chain = ksp_chain->ks_next)
153 if (strncmp (ksp_chain->ks_class, "net", 3))
155 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
157 if (kstat_read (kc, ksp_chain, NULL) == -1)
159 if (get_kstat_value (ksp_chain, "obytes") == -1LL)
161 ksp[numif++] = ksp_chain;
165 } /* int interface_init */
166 #endif /* HAVE_LIBKSTAT */
168 static void if_submit (const char *dev, const char *type,
173 value_list_t vl = VALUE_LIST_INIT;
175 if (ignorelist_match (ignorelist, dev) != 0)
178 values[0].derive = rx;
179 values[1].derive = tx;
183 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
184 sstrncpy (vl.plugin, "interface", sizeof (vl.plugin));
185 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
186 sstrncpy (vl.type, type, sizeof (vl.type));
188 plugin_dispatch_values (&vl);
189 } /* void if_submit */
191 static int interface_read (void)
194 struct ifaddrs *if_list;
195 struct ifaddrs *if_ptr;
197 /* Darwin/Mac OS X and possible other *BSDs */
198 #if HAVE_STRUCT_IF_DATA
199 # define IFA_DATA if_data
200 # define IFA_RX_BYTES ifi_ibytes
201 # define IFA_TX_BYTES ifi_obytes
202 # define IFA_RX_PACKT ifi_ipackets
203 # define IFA_TX_PACKT ifi_opackets
204 # define IFA_RX_ERROR ifi_ierrors
205 # define IFA_TX_ERROR ifi_oerrors
206 /* #endif HAVE_STRUCT_IF_DATA */
208 #elif HAVE_STRUCT_NET_DEVICE_STATS
209 # define IFA_DATA net_device_stats
210 # define IFA_RX_BYTES rx_bytes
211 # define IFA_TX_BYTES tx_bytes
212 # define IFA_RX_PACKT rx_packets
213 # define IFA_TX_PACKT tx_packets
214 # define IFA_RX_ERROR rx_errors
215 # define IFA_TX_ERROR tx_errors
217 # error "No suitable type for `struct ifaddrs->ifa_data' found."
220 struct IFA_DATA *if_data;
222 if (getifaddrs (&if_list) != 0)
225 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
227 if (if_ptr->ifa_addr != NULL && if_ptr->ifa_addr->sa_family == AF_LINK) {
228 if_data = (struct IFA_DATA *) if_ptr->ifa_data;
230 if (!report_inactive && if_data->IFA_RX_PACKT == 0 && if_data->IFA_TX_PACKT == 0)
233 if_submit (if_ptr->ifa_name, "if_octets",
234 if_data->IFA_RX_BYTES,
235 if_data->IFA_TX_BYTES);
236 if_submit (if_ptr->ifa_name, "if_packets",
237 if_data->IFA_RX_PACKT,
238 if_data->IFA_TX_PACKT);
239 if_submit (if_ptr->ifa_name, "if_errors",
240 if_data->IFA_RX_ERROR,
241 if_data->IFA_TX_ERROR);
245 freeifaddrs (if_list);
246 /* #endif HAVE_GETIFADDRS */
251 derive_t incoming, outgoing;
258 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
261 WARNING ("interface plugin: fopen: %s",
262 sstrerror (errno, errbuf, sizeof (errbuf)));
266 while (fgets (buffer, 1024, fh) != NULL)
268 if (!(dummy = strchr(buffer, ':')))
274 while (device[0] == ' ')
277 if (device[0] == '\0')
280 numfields = strsplit (dummy, fields, 16);
285 incoming = atoll (fields[1]);
286 outgoing = atoll (fields[9]);
287 if (!report_inactive && incoming == 0 && outgoing == 0)
290 if_submit (device, "if_packets", incoming, outgoing);
292 incoming = atoll (fields[0]);
293 outgoing = atoll (fields[8]);
294 if_submit (device, "if_octets", incoming, outgoing);
296 incoming = atoll (fields[2]);
297 outgoing = atoll (fields[10]);
298 if_submit (device, "if_errors", incoming, outgoing);
300 incoming = atoll (fields[3]);
301 outgoing = atoll (fields[11]);
302 if_submit (device, "if_dropped", incoming, outgoing);
306 /* #endif KERNEL_LINUX */
312 char iname[DATA_MAX_NAME_LEN];
317 for (i = 0; i < numif; i++)
319 if (kstat_read (kc, ksp[i], NULL) == -1)
323 ssnprintf(iname, sizeof(iname), "%s_%d_%s", ksp[i]->ks_module, ksp[i]->ks_instance, ksp[i]->ks_name);
325 sstrncpy(iname, ksp[i]->ks_name, sizeof(iname));
327 /* try to get 64bit counters */
328 rx = get_kstat_value (ksp[i], "ipackets64");
329 tx = get_kstat_value (ksp[i], "opackets64");
330 /* or fallback to 32bit */
332 rx = get_kstat_value (ksp[i], "ipackets");
334 tx = get_kstat_value (ksp[i], "opackets");
335 if (!report_inactive && rx == 0 && tx == 0)
337 if ((rx != -1LL) || (tx != -1LL))
338 if_submit (iname, "if_packets", rx, tx);
340 /* try to get 64bit counters */
341 rx = get_kstat_value (ksp[i], "rbytes64");
342 tx = get_kstat_value (ksp[i], "obytes64");
343 /* or fallback to 32bit */
345 rx = get_kstat_value (ksp[i], "rbytes");
347 tx = get_kstat_value (ksp[i], "obytes");
348 if ((rx != -1LL) || (tx != -1LL))
349 if_submit (iname, "if_octets", rx, tx);
351 /* no 64bit error counters yet */
352 rx = get_kstat_value (ksp[i], "ierrors");
353 tx = get_kstat_value (ksp[i], "oerrors");
354 if ((rx != -1LL) || (tx != -1LL))
355 if_submit (iname, "if_errors", rx, tx);
357 /* #endif HAVE_LIBKSTAT */
359 #elif defined(HAVE_LIBSTATGRAB)
360 sg_network_io_stats *ios;
363 ios = sg_get_network_io_stats (&num);
365 for (i = 0; i < num; i++) {
366 if (!report_inactive && ios[i].rx == 0 && ios[i].tx == 0)
368 if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
370 /* #endif HAVE_LIBSTATGRAB */
372 #elif defined(HAVE_PERFSTAT)
376 if ((nif = perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t), 0)) < 0)
379 WARNING ("interface plugin: perfstat_netinterface: %s",
380 sstrerror (errno, errbuf, sizeof (errbuf)));
384 if (pnif != nif || ifstat == NULL)
387 ifstat = malloc(nif * sizeof (*ifstat));
392 if ((ifs = perfstat_netinterface(&id, ifstat, sizeof(perfstat_netinterface_t), nif)) < 0)
395 WARNING ("interface plugin: perfstat_netinterface (interfaces=%d): %s",
396 nif, sstrerror (errno, errbuf, sizeof (errbuf)));
400 for (i = 0; i < ifs; i++)
402 if (!report_inactive && ifstat[i].ipackets == 0 && ifstat[i].opackets == 0)
405 if_submit (ifstat[i].name, "if_octets", ifstat[i].ibytes, ifstat[i].obytes);
406 if_submit (ifstat[i].name, "if_packets", ifstat[i].ipackets ,ifstat[i].opackets);
407 if_submit (ifstat[i].name, "if_errors", ifstat[i].ierrors, ifstat[i].oerrors );
409 #endif /* HAVE_PERFSTAT */
412 } /* int interface_read */
414 void module_register (void)
416 plugin_register_config ("interface", interface_config,
417 config_keys, config_keys_num);
419 plugin_register_init ("interface", interface_init);
421 plugin_register_read ("interface", interface_read);
422 } /* void module_register */