2 * collectd - src/interface.c
3 * Copyright (C) 2005-2008 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 verplant.org>
21 * Sune Marcher <sm at flork.dk>
28 #include "configfile.h"
29 #include "utils_ignorelist.h"
32 # include <sys/types.h>
35 # include <sys/socket.h>
38 /* One cannot include both. This sucks. */
40 # include <linux/if.h>
45 #if HAVE_LINUX_NETDEVICE_H
46 # include <linux/netdevice.h>
53 # include <statgrab.h>
57 # include <sys/protosw.h>
58 # include <libperfstat.h>
62 * Various people have reported problems with `getifaddrs' and varying versions
63 * of `glibc'. That's why it's disabled by default. Since more statistics are
64 * available this way one may enable it using the `--enable-getifaddrs' option
65 * of the configure script. -octo
68 # if !COLLECT_GETIFADDRS
69 # undef HAVE_GETIFADDRS
70 # endif /* !COLLECT_GETIFADDRS */
71 #endif /* KERNEL_LINUX */
74 static perfstat_netinterface_t *ifstat;
77 #endif /* HAVE_PERFSTAT */
79 #if !HAVE_GETIFADDRS && !KERNEL_LINUX && !HAVE_LIBKSTAT && !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
80 # error "No applicable input method."
84 * (Module-)Global variables
86 static const char *config_keys[] =
92 static int config_keys_num = 2;
94 static ignorelist_t *ignorelist = NULL;
98 extern kstat_ctl_t *kc;
99 static kstat_t *ksp[MAX_NUMIF];
100 static int numif = 0;
101 #endif /* HAVE_LIBKSTAT */
103 static int interface_config (const char *key, const char *value)
105 if (ignorelist == NULL)
106 ignorelist = ignorelist_create (/* invert = */ 1);
108 if (strcasecmp (key, "Interface") == 0)
110 ignorelist_add (ignorelist, value);
112 else if (strcasecmp (key, "IgnoreSelected") == 0)
117 ignorelist_set_invert (ignorelist, invert);
128 static int interface_init (void)
131 unsigned long long val;
138 for (numif = 0, ksp_chain = kc->kc_chain;
139 (numif < MAX_NUMIF) && (ksp_chain != NULL);
140 ksp_chain = ksp_chain->ks_next)
142 if (strncmp (ksp_chain->ks_class, "net", 3))
144 /* Ignore kstat entry if not the regular statistic set. This
145 * avoids problems with "bogus" interfaces, such as
147 if (strncmp (ksp_chain->ks_name, ksp_chain->ks_module,
148 strlen (ksp_chain->ks_module)) != 0)
150 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
152 if (kstat_read (kc, ksp_chain, NULL) == -1)
154 if ((val = get_kstat_value (ksp_chain, "ifspeed")) == -1LL)
156 ksp[numif++] = ksp_chain;
160 } /* int interface_init */
161 #endif /* HAVE_LIBKSTAT */
163 static void if_submit (const char *dev, const char *type,
164 unsigned long long rx,
165 unsigned long long tx)
168 value_list_t vl = VALUE_LIST_INIT;
170 if (ignorelist_match (ignorelist, dev) != 0)
173 values[0].counter = rx;
174 values[1].counter = tx;
178 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
179 sstrncpy (vl.plugin, "interface", sizeof (vl.plugin));
180 sstrncpy (vl.type, type, sizeof (vl.type));
181 sstrncpy (vl.type_instance, dev, sizeof (vl.type_instance));
183 plugin_dispatch_values (&vl);
184 } /* void if_submit */
186 static int interface_read (void)
189 struct ifaddrs *if_list;
190 struct ifaddrs *if_ptr;
192 /* Darin/Mac OS X and possible other *BSDs */
193 #if HAVE_STRUCT_IF_DATA
194 # define IFA_DATA if_data
195 # define IFA_RX_BYTES ifi_ibytes
196 # define IFA_TX_BYTES ifi_obytes
197 # define IFA_RX_PACKT ifi_ipackets
198 # define IFA_TX_PACKT ifi_opackets
199 # define IFA_RX_ERROR ifi_ierrors
200 # define IFA_TX_ERROR ifi_oerrors
201 /* #endif HAVE_STRUCT_IF_DATA */
203 #elif HAVE_STRUCT_NET_DEVICE_STATS
204 # define IFA_DATA net_device_stats
205 # define IFA_RX_BYTES rx_bytes
206 # define IFA_TX_BYTES tx_bytes
207 # define IFA_RX_PACKT rx_packets
208 # define IFA_TX_PACKT tx_packets
209 # define IFA_RX_ERROR rx_errors
210 # define IFA_TX_ERROR tx_errors
212 # error "No suitable type for `struct ifaddrs->ifa_data' found."
215 struct IFA_DATA *if_data;
217 if (getifaddrs (&if_list) != 0)
220 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
222 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
225 if_submit (if_ptr->ifa_name, "if_octets",
226 if_data->IFA_RX_BYTES,
227 if_data->IFA_TX_BYTES);
228 if_submit (if_ptr->ifa_name, "if_packets",
229 if_data->IFA_RX_PACKT,
230 if_data->IFA_TX_PACKT);
231 if_submit (if_ptr->ifa_name, "if_errors",
232 if_data->IFA_RX_ERROR,
233 if_data->IFA_TX_ERROR);
236 freeifaddrs (if_list);
237 /* #endif HAVE_GETIFADDRS */
242 unsigned long long incoming, outgoing;
249 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
252 WARNING ("interface plugin: fopen: %s",
253 sstrerror (errno, errbuf, sizeof (errbuf)));
257 while (fgets (buffer, 1024, fh) != NULL)
259 if (!(dummy = strchr(buffer, ':')))
265 while (device[0] == ' ')
268 if (device[0] == '\0')
271 numfields = strsplit (dummy, fields, 16);
276 incoming = atoll (fields[0]);
277 outgoing = atoll (fields[8]);
278 if_submit (device, "if_octets", incoming, outgoing);
280 incoming = atoll (fields[1]);
281 outgoing = atoll (fields[9]);
282 if_submit (device, "if_packets", incoming, outgoing);
284 incoming = atoll (fields[2]);
285 outgoing = atoll (fields[10]);
286 if_submit (device, "if_errors", incoming, outgoing);
290 /* #endif KERNEL_LINUX */
294 unsigned long long rx;
295 unsigned long long tx;
300 for (i = 0; i < numif; i++)
302 if (kstat_read (kc, ksp[i], NULL) == -1)
305 /* try to get 64bit counters */
306 rx = get_kstat_value (ksp[i], "rbytes64");
307 tx = get_kstat_value (ksp[i], "obytes64");
308 /* or fallback to 32bit */
310 rx = get_kstat_value (ksp[i], "rbytes");
312 tx = get_kstat_value (ksp[i], "obytes");
313 if ((rx != -1LL) || (tx != -1LL))
314 if_submit (ksp[i]->ks_name, "if_octets", rx, tx);
316 /* try to get 64bit counters */
317 rx = get_kstat_value (ksp[i], "ipackets64");
318 tx = get_kstat_value (ksp[i], "opackets64");
319 /* or fallback to 32bit */
321 rx = get_kstat_value (ksp[i], "ipackets");
323 tx = get_kstat_value (ksp[i], "opackets");
324 if ((rx != -1LL) || (tx != -1LL))
325 if_submit (ksp[i]->ks_name, "if_packets", rx, tx);
327 /* no 64bit error counters yet */
328 rx = get_kstat_value (ksp[i], "ierrors");
329 tx = get_kstat_value (ksp[i], "oerrors");
330 if ((rx != -1LL) || (tx != -1LL))
331 if_submit (ksp[i]->ks_name, "if_errors", rx, tx);
333 /* #endif HAVE_LIBKSTAT */
335 #elif defined(HAVE_LIBSTATGRAB)
336 sg_network_io_stats *ios;
339 ios = sg_get_network_io_stats (&num);
341 for (i = 0; i < num; i++)
342 if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
343 /* #endif HAVE_LIBSTATGRAB */
345 #elif defined(HAVE_PERFSTAT)
349 if ((nif = perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t), 0)) < 0)
352 WARNING ("interface plugin: perfstat_netinterface: %s",
353 sstrerror (errno, errbuf, sizeof (errbuf)));
357 if (pnif != nif || ifstat == NULL)
361 ifstat = malloc(nif * sizeof(perfstat_netinterface_t));
366 if ((ifs = perfstat_netinterface(&id, ifstat, sizeof(perfstat_netinterface_t), nif)) < 0)
369 WARNING ("interface plugin: perfstat_netinterface (interfaces=%d): %s",
370 nif, sstrerror (errno, errbuf, sizeof (errbuf)));
374 for (i = 0; i < ifs; i++)
376 if_submit (ifstat[i].name, "if_octets", ifstat[i].ibytes, ifstat[i].obytes);
377 if_submit (ifstat[i].name, "if_packets", ifstat[i].ipackets ,ifstat[i].opackets);
378 if_submit (ifstat[i].name, "if_errors", ifstat[i].ierrors, ifstat[i].oerrors );
380 #endif /* HAVE_PERFSTAT */
383 } /* int interface_read */
385 void module_register (void)
387 plugin_register_config ("interface", interface_config,
388 config_keys, config_keys_num);
390 plugin_register_init ("interface", interface_init);
392 plugin_register_read ("interface", interface_read);
393 } /* void module_register */