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 "utils_ignorelist.h"
32 #include <sys/types.h>
35 /* One cannot include both. This sucks. */
42 #if HAVE_LINUX_NETDEVICE_H
43 #include <linux/netdevice.h>
54 #include <libperfstat.h>
55 #include <sys/protosw.h>
59 * Various people have reported problems with `getifaddrs' and varying versions
60 * of `glibc'. That's why it's disabled by default. Since more statistics are
61 * available this way one may enable it using the `--enable-getifaddrs' option
62 * of the configure script. -octo
65 #if !COLLECT_GETIFADDRS
66 #undef HAVE_GETIFADDRS
67 #endif /* !COLLECT_GETIFADDRS */
68 #endif /* KERNEL_LINUX */
71 static perfstat_netinterface_t *ifstat;
74 #endif /* HAVE_PERFSTAT */
76 #if !HAVE_GETIFADDRS && !KERNEL_LINUX && !HAVE_LIBKSTAT && \
77 !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
78 #error "No applicable input method."
82 * (Module-)Global variables
84 static const char *config_keys[] = {
85 "Interface", "IgnoreSelected", "ReportInactive",
87 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
89 static ignorelist_t *ignorelist = NULL;
91 static _Bool report_inactive = 1;
95 extern kstat_ctl_t *kc;
96 static kstat_t *ksp[MAX_NUMIF];
98 static _Bool unique_name = 0;
99 #endif /* HAVE_LIBKSTAT */
101 static int interface_config(const char *key, const char *value) {
102 if (ignorelist == NULL)
103 ignorelist = ignorelist_create(/* invert = */ 1);
105 if (strcasecmp(key, "Interface") == 0) {
106 ignorelist_add(ignorelist, value);
107 } else if (strcasecmp(key, "IgnoreSelected") == 0) {
111 ignorelist_set_invert(ignorelist, invert);
112 } else if (strcasecmp(key, "ReportInactive") == 0)
113 report_inactive = IS_TRUE(value);
114 else if (strcasecmp(key, "UniqueName") == 0) {
119 WARNING("interface plugin: the \"UniqueName\" option is only valid on "
121 #endif /* HAVE_LIBKSTAT */
130 static int interface_init(void) {
138 for (numif = 0, ksp_chain = kc->kc_chain;
139 (numif < MAX_NUMIF) && (ksp_chain != NULL);
140 ksp_chain = ksp_chain->ks_next) {
141 if (strncmp(ksp_chain->ks_class, "net", 3))
143 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
145 if (kstat_read(kc, ksp_chain, NULL) == -1)
147 if (get_kstat_value(ksp_chain, "obytes") == -1LL)
149 ksp[numif++] = ksp_chain;
153 } /* int interface_init */
154 #endif /* HAVE_LIBKSTAT */
156 static void if_submit(const char *dev, const char *type, derive_t rx,
158 value_list_t vl = VALUE_LIST_INIT;
160 {.derive = rx}, {.derive = tx},
163 if (ignorelist_match(ignorelist, dev) != 0)
167 vl.values_len = STATIC_ARRAY_SIZE(values);
168 sstrncpy(vl.plugin, "interface", sizeof(vl.plugin));
169 sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
170 sstrncpy(vl.type, type, sizeof(vl.type));
172 plugin_dispatch_values(&vl);
173 } /* void if_submit */
175 static int interface_read(void) {
177 struct ifaddrs *if_list;
179 /* Darwin/Mac OS X and possible other *BSDs */
180 #if HAVE_STRUCT_IF_DATA
181 #define IFA_DATA if_data
182 #define IFA_RX_BYTES ifi_ibytes
183 #define IFA_TX_BYTES ifi_obytes
184 #define IFA_RX_PACKT ifi_ipackets
185 #define IFA_TX_PACKT ifi_opackets
186 #define IFA_RX_ERROR ifi_ierrors
187 #define IFA_TX_ERROR ifi_oerrors
188 /* #endif HAVE_STRUCT_IF_DATA */
190 #elif HAVE_STRUCT_NET_DEVICE_STATS
191 #define IFA_DATA net_device_stats
192 #define IFA_RX_BYTES rx_bytes
193 #define IFA_TX_BYTES tx_bytes
194 #define IFA_RX_PACKT rx_packets
195 #define IFA_TX_PACKT tx_packets
196 #define IFA_RX_ERROR rx_errors
197 #define IFA_TX_ERROR tx_errors
199 #error "No suitable type for `struct ifaddrs->ifa_data' found."
202 struct IFA_DATA *if_data;
204 if (getifaddrs(&if_list) != 0)
207 for (struct ifaddrs *if_ptr = if_list; if_ptr != NULL;
208 if_ptr = if_ptr->ifa_next) {
209 if (if_ptr->ifa_addr != NULL && if_ptr->ifa_addr->sa_family == AF_LINK) {
210 if_data = (struct IFA_DATA *)if_ptr->ifa_data;
212 if (!report_inactive && if_data->IFA_RX_PACKT == 0 &&
213 if_data->IFA_TX_PACKT == 0)
216 if_submit(if_ptr->ifa_name, "if_octets", if_data->IFA_RX_BYTES,
217 if_data->IFA_TX_BYTES);
218 if_submit(if_ptr->ifa_name, "if_packets", if_data->IFA_RX_PACKT,
219 if_data->IFA_TX_PACKT);
220 if_submit(if_ptr->ifa_name, "if_errors", if_data->IFA_RX_ERROR,
221 if_data->IFA_TX_ERROR);
225 freeifaddrs(if_list);
226 /* #endif HAVE_GETIFADDRS */
231 derive_t incoming, outgoing;
238 if ((fh = fopen("/proc/net/dev", "r")) == NULL) {
240 WARNING("interface plugin: fopen: %s",
241 sstrerror(errno, errbuf, sizeof(errbuf)));
245 while (fgets(buffer, 1024, fh) != NULL) {
246 if (!(dummy = strchr(buffer, ':')))
252 while (device[0] == ' ')
255 if (device[0] == '\0')
258 numfields = strsplit(dummy, fields, 16);
263 incoming = atoll(fields[1]);
264 outgoing = atoll(fields[9]);
265 if (!report_inactive && incoming == 0 && outgoing == 0)
268 if_submit(device, "if_packets", incoming, outgoing);
270 incoming = atoll(fields[0]);
271 outgoing = atoll(fields[8]);
272 if_submit(device, "if_octets", incoming, outgoing);
274 incoming = atoll(fields[2]);
275 outgoing = atoll(fields[10]);
276 if_submit(device, "if_errors", incoming, outgoing);
278 incoming = atoll(fields[3]);
279 outgoing = atoll(fields[11]);
280 if_submit(device, "if_dropped", incoming, outgoing);
284 /* #endif KERNEL_LINUX */
289 char iname[DATA_MAX_NAME_LEN];
294 for (int i = 0; i < numif; i++) {
295 if (kstat_read(kc, ksp[i], NULL) == -1)
299 ssnprintf(iname, sizeof(iname), "%s_%d_%s", ksp[i]->ks_module,
300 ksp[i]->ks_instance, ksp[i]->ks_name);
302 sstrncpy(iname, ksp[i]->ks_name, sizeof(iname));
304 /* try to get 64bit counters */
305 rx = get_kstat_value(ksp[i], "ipackets64");
306 tx = get_kstat_value(ksp[i], "opackets64");
307 /* or fallback to 32bit */
309 rx = get_kstat_value(ksp[i], "ipackets");
311 tx = get_kstat_value(ksp[i], "opackets");
312 if (!report_inactive && rx == 0 && tx == 0)
314 if ((rx != -1LL) || (tx != -1LL))
315 if_submit(iname, "if_packets", rx, tx);
317 /* try to get 64bit counters */
318 rx = get_kstat_value(ksp[i], "rbytes64");
319 tx = get_kstat_value(ksp[i], "obytes64");
320 /* or fallback to 32bit */
322 rx = get_kstat_value(ksp[i], "rbytes");
324 tx = get_kstat_value(ksp[i], "obytes");
325 if ((rx != -1LL) || (tx != -1LL))
326 if_submit(iname, "if_octets", rx, tx);
328 /* no 64bit error counters yet */
329 rx = get_kstat_value(ksp[i], "ierrors");
330 tx = get_kstat_value(ksp[i], "oerrors");
331 if ((rx != -1LL) || (tx != -1LL))
332 if_submit(iname, "if_errors", rx, tx);
334 /* #endif HAVE_LIBKSTAT */
336 #elif defined(HAVE_LIBSTATGRAB)
337 sg_network_io_stats *ios;
340 ios = sg_get_network_io_stats(&num);
342 for (int i = 0; i < num; i++) {
343 if (!report_inactive && ios[i].rx == 0 && ios[i].tx == 0)
345 if_submit(ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
347 /* #endif HAVE_LIBSTATGRAB */
349 #elif defined(HAVE_PERFSTAT)
353 if ((nif = perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t),
356 WARNING("interface plugin: perfstat_netinterface: %s",
357 sstrerror(errno, errbuf, sizeof(errbuf)));
361 if (pnif != nif || ifstat == NULL) {
363 ifstat = malloc(nif * sizeof(*ifstat));
368 if ((ifs = perfstat_netinterface(&id, ifstat, sizeof(perfstat_netinterface_t),
371 WARNING("interface plugin: perfstat_netinterface (interfaces=%d): %s", nif,
372 sstrerror(errno, errbuf, sizeof(errbuf)));
376 for (int i = 0; i < ifs; i++) {
377 if (!report_inactive && ifstat[i].ipackets == 0 && ifstat[i].opackets == 0)
380 if_submit(ifstat[i].name, "if_octets", ifstat[i].ibytes, ifstat[i].obytes);
381 if_submit(ifstat[i].name, "if_packets", ifstat[i].ipackets,
383 if_submit(ifstat[i].name, "if_errors", ifstat[i].ierrors,
386 #endif /* HAVE_PERFSTAT */
389 } /* int interface_read */
391 void module_register(void) {
392 plugin_register_config("interface", interface_config, config_keys,
395 plugin_register_init("interface", interface_init);
397 plugin_register_read("interface", interface_read);
398 } /* void module_register */