1 /**
2 * collectd - src/interface.c
3 * Copyright (C) 2005-2010 Florian octo Forster
4 * Copyright (C) 2009 Manuel Sanmartin
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian octo Forster <octo at collectd.org>
21 * Sune Marcher <sm at flork.dk>
22 * Manuel Sanmartin
23 **/
25 #include "collectd.h"
27 #include "common.h"
28 #include "plugin.h"
29 #include "utils_ignorelist.h"
31 #if HAVE_SYS_TYPES_H
32 #include <sys/types.h>
33 #endif
35 /* One cannot include both. This sucks. */
36 #if HAVE_LINUX_IF_H
37 #include <linux/if.h>
38 #elif HAVE_NET_IF_H
39 #include <net/if.h>
40 #endif
42 #if HAVE_LINUX_NETDEVICE_H
43 #include <linux/netdevice.h>
44 #endif
45 #if HAVE_IFADDRS_H
46 #include <ifaddrs.h>
47 #endif
49 #if HAVE_STATGRAB_H
50 #include <statgrab.h>
51 #endif
53 #if HAVE_PERFSTAT
54 #include <libperfstat.h>
55 #include <sys/protosw.h>
56 #endif
58 /*
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
63 */
64 #if KERNEL_LINUX
65 #if !COLLECT_GETIFADDRS
66 #undef HAVE_GETIFADDRS
67 #endif /* !COLLECT_GETIFADDRS */
68 #endif /* KERNEL_LINUX */
70 #if HAVE_PERFSTAT
71 static perfstat_netinterface_t *ifstat;
72 static int nif;
73 static int pnif;
74 #endif /* HAVE_PERFSTAT */
76 #if !HAVE_GETIFADDRS && !KERNEL_LINUX && !HAVE_LIBKSTAT && \
77 !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
78 #error "No applicable input method."
79 #endif
81 /*
82 * (Module-)Global variables
83 */
84 static const char *config_keys[] = {
85 "Interface", "IgnoreSelected", "ReportInactive",
86 };
87 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
89 static ignorelist_t *ignorelist = NULL;
91 static _Bool report_inactive = 1;
93 #ifdef HAVE_LIBKSTAT
94 #define MAX_NUMIF 256
95 extern kstat_ctl_t *kc;
96 static kstat_t *ksp[MAX_NUMIF];
97 static int numif = 0;
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) {
108 int invert = 1;
109 if (IS_TRUE(value))
110 invert = 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) {
115 #ifdef HAVE_LIBKSTAT
116 if (IS_TRUE(value))
117 unique_name = 1;
118 #else
119 WARNING("interface plugin: the \"UniqueName\" option is only valid on "
120 "Solaris.");
121 #endif /* HAVE_LIBKSTAT */
122 } else {
123 return (-1);
124 }
126 return (0);
127 }
129 #if HAVE_LIBKSTAT
130 static int interface_init(void) {
131 kstat_t *ksp_chain;
133 numif = 0;
135 if (kc == NULL)
136 return (-1);
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))
142 continue;
143 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
144 continue;
145 if (kstat_read(kc, ksp_chain, NULL) == -1)
146 continue;
147 if (get_kstat_value(ksp_chain, "obytes") == -1LL)
148 continue;
149 ksp[numif++] = ksp_chain;
150 }
152 return (0);
153 } /* int interface_init */
154 #endif /* HAVE_LIBKSTAT */
156 static void if_submit(const char *dev, const char *type, derive_t rx,
157 derive_t tx) {
158 value_t values[2];
159 value_list_t vl = VALUE_LIST_INIT;
161 if (ignorelist_match(ignorelist, dev) != 0)
162 return;
164 values[0].derive = rx;
165 values[1].derive = tx;
167 vl.values = values;
168 vl.values_len = 2;
169 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
170 sstrncpy(vl.plugin, "interface", sizeof(vl.plugin));
171 sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
172 sstrncpy(vl.type, type, sizeof(vl.type));
174 plugin_dispatch_values(&vl);
175 } /* void if_submit */
177 static int interface_read(void) {
178 #if HAVE_GETIFADDRS
179 struct ifaddrs *if_list;
181 /* Darwin/Mac OS X and possible other *BSDs */
182 #if HAVE_STRUCT_IF_DATA
183 #define IFA_DATA if_data
184 #define IFA_RX_BYTES ifi_ibytes
185 #define IFA_TX_BYTES ifi_obytes
186 #define IFA_RX_PACKT ifi_ipackets
187 #define IFA_TX_PACKT ifi_opackets
188 #define IFA_RX_ERROR ifi_ierrors
189 #define IFA_TX_ERROR ifi_oerrors
190 /* #endif HAVE_STRUCT_IF_DATA */
192 #elif HAVE_STRUCT_NET_DEVICE_STATS
193 #define IFA_DATA net_device_stats
194 #define IFA_RX_BYTES rx_bytes
195 #define IFA_TX_BYTES tx_bytes
196 #define IFA_RX_PACKT rx_packets
197 #define IFA_TX_PACKT tx_packets
198 #define IFA_RX_ERROR rx_errors
199 #define IFA_TX_ERROR tx_errors
200 #else
201 #error "No suitable type for `struct ifaddrs->ifa_data' found."
202 #endif
204 struct IFA_DATA *if_data;
206 if (getifaddrs(&if_list) != 0)
207 return (-1);
209 for (struct ifaddrs *if_ptr = if_list; if_ptr != NULL;
210 if_ptr = if_ptr->ifa_next) {
211 if (if_ptr->ifa_addr != NULL && if_ptr->ifa_addr->sa_family == AF_LINK) {
212 if_data = (struct IFA_DATA *)if_ptr->ifa_data;
214 if (!report_inactive && if_data->IFA_RX_PACKT == 0 &&
215 if_data->IFA_TX_PACKT == 0)
216 continue;
218 if_submit(if_ptr->ifa_name, "if_octets", if_data->IFA_RX_BYTES,
219 if_data->IFA_TX_BYTES);
220 if_submit(if_ptr->ifa_name, "if_packets", if_data->IFA_RX_PACKT,
221 if_data->IFA_TX_PACKT);
222 if_submit(if_ptr->ifa_name, "if_errors", if_data->IFA_RX_ERROR,
223 if_data->IFA_TX_ERROR);
224 }
225 }
227 freeifaddrs(if_list);
228 /* #endif HAVE_GETIFADDRS */
230 #elif KERNEL_LINUX
231 FILE *fh;
232 char buffer[1024];
233 derive_t incoming, outgoing;
234 char *device;
236 char *dummy;
237 char *fields[16];
238 int numfields;
240 if ((fh = fopen("/proc/net/dev", "r")) == NULL) {
241 char errbuf[1024];
242 WARNING("interface plugin: fopen: %s",
243 sstrerror(errno, errbuf, sizeof(errbuf)));
244 return (-1);
245 }
247 while (fgets(buffer, 1024, fh) != NULL) {
248 if (!(dummy = strchr(buffer, ':')))
249 continue;
250 dummy[0] = '\0';
251 dummy++;
253 device = buffer;
254 while (device[0] == ' ')
255 device++;
257 if (device[0] == '\0')
258 continue;
260 numfields = strsplit(dummy, fields, 16);
262 if (numfields < 11)
263 continue;
265 incoming = atoll(fields[1]);
266 outgoing = atoll(fields[9]);
267 if (!report_inactive && incoming == 0 && outgoing == 0)
268 continue;
270 if_submit(device, "if_packets", incoming, outgoing);
272 incoming = atoll(fields[0]);
273 outgoing = atoll(fields[8]);
274 if_submit(device, "if_octets", incoming, outgoing);
276 incoming = atoll(fields[2]);
277 outgoing = atoll(fields[10]);
278 if_submit(device, "if_errors", incoming, outgoing);
280 incoming = atoll(fields[3]);
281 outgoing = atoll(fields[11]);
282 if_submit(device, "if_dropped", incoming, outgoing);
283 }
285 fclose(fh);
286 /* #endif KERNEL_LINUX */
288 #elif HAVE_LIBKSTAT
289 derive_t rx;
290 derive_t tx;
291 char iname[DATA_MAX_NAME_LEN];
293 if (kc == NULL)
294 return (-1);
296 for (int i = 0; i < numif; i++) {
297 if (kstat_read(kc, ksp[i], NULL) == -1)
298 continue;
300 if (unique_name)
301 ssnprintf(iname, sizeof(iname), "%s_%d_%s", ksp[i]->ks_module,
302 ksp[i]->ks_instance, ksp[i]->ks_name);
303 else
304 sstrncpy(iname, ksp[i]->ks_name, sizeof(iname));
306 /* try to get 64bit counters */
307 rx = get_kstat_value(ksp[i], "ipackets64");
308 tx = get_kstat_value(ksp[i], "opackets64");
309 /* or fallback to 32bit */
310 if (rx == -1LL)
311 rx = get_kstat_value(ksp[i], "ipackets");
312 if (tx == -1LL)
313 tx = get_kstat_value(ksp[i], "opackets");
314 if (!report_inactive && rx == 0 && tx == 0)
315 continue;
316 if ((rx != -1LL) || (tx != -1LL))
317 if_submit(iname, "if_packets", rx, tx);
319 /* try to get 64bit counters */
320 rx = get_kstat_value(ksp[i], "rbytes64");
321 tx = get_kstat_value(ksp[i], "obytes64");
322 /* or fallback to 32bit */
323 if (rx == -1LL)
324 rx = get_kstat_value(ksp[i], "rbytes");
325 if (tx == -1LL)
326 tx = get_kstat_value(ksp[i], "obytes");
327 if ((rx != -1LL) || (tx != -1LL))
328 if_submit(iname, "if_octets", rx, tx);
330 /* no 64bit error counters yet */
331 rx = get_kstat_value(ksp[i], "ierrors");
332 tx = get_kstat_value(ksp[i], "oerrors");
333 if ((rx != -1LL) || (tx != -1LL))
334 if_submit(iname, "if_errors", rx, tx);
335 }
336 /* #endif HAVE_LIBKSTAT */
338 #elif defined(HAVE_LIBSTATGRAB)
339 sg_network_io_stats *ios;
340 int num;
342 ios = sg_get_network_io_stats(&num);
344 for (int i = 0; i < num; i++) {
345 if (!report_inactive && ios[i].rx == 0 && ios[i].tx == 0)
346 continue;
347 if_submit(ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
348 }
349 /* #endif HAVE_LIBSTATGRAB */
351 #elif defined(HAVE_PERFSTAT)
352 perfstat_id_t id;
353 int ifs;
355 if ((nif = perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t),
356 0)) < 0) {
357 char errbuf[1024];
358 WARNING("interface plugin: perfstat_netinterface: %s",
359 sstrerror(errno, errbuf, sizeof(errbuf)));
360 return (-1);
361 }
363 if (pnif != nif || ifstat == NULL) {
364 free(ifstat);
365 ifstat = malloc(nif * sizeof(*ifstat));
366 }
367 pnif = nif;
369 id.name[0] = '\0';
370 if ((ifs = perfstat_netinterface(&id, ifstat, sizeof(perfstat_netinterface_t),
371 nif)) < 0) {
372 char errbuf[1024];
373 WARNING("interface plugin: perfstat_netinterface (interfaces=%d): %s", nif,
374 sstrerror(errno, errbuf, sizeof(errbuf)));
375 return (-1);
376 }
378 for (int i = 0; i < ifs; i++) {
379 if (!report_inactive && ifstat[i].ipackets == 0 && ifstat[i].opackets == 0)
380 continue;
382 if_submit(ifstat[i].name, "if_octets", ifstat[i].ibytes, ifstat[i].obytes);
383 if_submit(ifstat[i].name, "if_packets", ifstat[i].ipackets,
384 ifstat[i].opackets);
385 if_submit(ifstat[i].name, "if_errors", ifstat[i].ierrors,
386 ifstat[i].oerrors);
387 }
388 #endif /* HAVE_PERFSTAT */
390 return (0);
391 } /* int interface_read */
393 void module_register(void) {
394 plugin_register_config("interface", interface_config, config_keys,
395 config_keys_num);
396 #if HAVE_LIBKSTAT
397 plugin_register_init("interface", interface_init);
398 #endif
399 plugin_register_read("interface", interface_read);
400 } /* void module_register */