Merge branch 'collectd-5.8'
[collectd.git] / src / interface.c
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  **/
24
25 #include "collectd.h"
26
27 #include "common.h"
28 #include "plugin.h"
29 #include "utils_ignorelist.h"
30
31 #if HAVE_SYS_TYPES_H
32 #include <sys/types.h>
33 #endif
34
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
41
42 #if HAVE_LINUX_NETDEVICE_H
43 #include <linux/netdevice.h>
44 #endif
45 #if HAVE_IFADDRS_H
46 #include <ifaddrs.h>
47 #endif
48
49 #if HAVE_STATGRAB_H
50 #include <statgrab.h>
51 #endif
52
53 #if HAVE_PERFSTAT
54 #include <libperfstat.h>
55 #include <sys/protosw.h>
56 #endif
57
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 */
69
70 #if HAVE_PERFSTAT
71 static perfstat_netinterface_t *ifstat;
72 static int nif;
73 static int pnif;
74 #endif /* HAVE_PERFSTAT */
75
76 #if !HAVE_GETIFADDRS && !KERNEL_LINUX && !HAVE_LIBKSTAT &&                     \
77     !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
78 #error "No applicable input method."
79 #endif
80
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);
88
89 static ignorelist_t *ignorelist;
90
91 static bool report_inactive = true;
92
93 #ifdef HAVE_LIBKSTAT
94 #if HAVE_KSTAT_H
95 #include <kstat.h>
96 #endif
97 #define MAX_NUMIF 256
98 extern kstat_ctl_t *kc;
99 static kstat_t *ksp[MAX_NUMIF];
100 static int numif;
101 static bool unique_name;
102 #endif /* HAVE_LIBKSTAT */
103
104 static int interface_config(const char *key, const char *value) {
105   if (ignorelist == NULL)
106     ignorelist = ignorelist_create(/* invert = */ 1);
107
108   if (strcasecmp(key, "Interface") == 0) {
109     ignorelist_add(ignorelist, value);
110   } else if (strcasecmp(key, "IgnoreSelected") == 0) {
111     int invert = 1;
112     if (IS_TRUE(value))
113       invert = 0;
114     ignorelist_set_invert(ignorelist, invert);
115   } else if (strcasecmp(key, "ReportInactive") == 0)
116     report_inactive = IS_TRUE(value);
117   else if (strcasecmp(key, "UniqueName") == 0) {
118 #ifdef HAVE_LIBKSTAT
119     if (IS_TRUE(value))
120       unique_name = true;
121 #else
122     WARNING("interface plugin: the \"UniqueName\" option is only valid on "
123             "Solaris.");
124 #endif /* HAVE_LIBKSTAT */
125   } else {
126     return -1;
127   }
128
129   return 0;
130 }
131
132 #if HAVE_LIBKSTAT
133 static int interface_init(void) {
134   kstat_t *ksp_chain;
135
136   numif = 0;
137
138   if (kc == NULL)
139     return -1;
140
141   for (numif = 0, ksp_chain = kc->kc_chain;
142        (numif < MAX_NUMIF) && (ksp_chain != NULL);
143        ksp_chain = ksp_chain->ks_next) {
144     if (strncmp(ksp_chain->ks_class, "net", 3))
145       continue;
146     if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
147       continue;
148     if (kstat_read(kc, ksp_chain, NULL) == -1)
149       continue;
150     if (get_kstat_value(ksp_chain, "obytes") == -1LL)
151       continue;
152     ksp[numif++] = ksp_chain;
153   }
154
155   return 0;
156 } /* int interface_init */
157 #endif /* HAVE_LIBKSTAT */
158
159 static void if_submit(const char *dev, const char *type, derive_t rx,
160                       derive_t tx) {
161   value_list_t vl = VALUE_LIST_INIT;
162   value_t values[] = {
163       {.derive = rx}, {.derive = tx},
164   };
165
166   if (ignorelist_match(ignorelist, dev) != 0)
167     return;
168
169   vl.values = values;
170   vl.values_len = STATIC_ARRAY_SIZE(values);
171   sstrncpy(vl.plugin, "interface", sizeof(vl.plugin));
172   sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
173   sstrncpy(vl.type, type, sizeof(vl.type));
174
175   plugin_dispatch_values(&vl);
176 } /* void if_submit */
177
178 static int interface_read(void) {
179 #if HAVE_GETIFADDRS
180   struct ifaddrs *if_list;
181
182 /* Darwin/Mac OS X and possible other *BSDs */
183 #if HAVE_STRUCT_IF_DATA
184 #define IFA_DATA if_data
185 #define IFA_RX_BYTES ifi_ibytes
186 #define IFA_TX_BYTES ifi_obytes
187 #define IFA_RX_PACKT ifi_ipackets
188 #define IFA_TX_PACKT ifi_opackets
189 #define IFA_RX_ERROR ifi_ierrors
190 #define IFA_TX_ERROR ifi_oerrors
191 /* #endif HAVE_STRUCT_IF_DATA */
192
193 #elif HAVE_STRUCT_NET_DEVICE_STATS
194 #define IFA_DATA net_device_stats
195 #define IFA_RX_BYTES rx_bytes
196 #define IFA_TX_BYTES tx_bytes
197 #define IFA_RX_PACKT rx_packets
198 #define IFA_TX_PACKT tx_packets
199 #define IFA_RX_ERROR rx_errors
200 #define IFA_TX_ERROR tx_errors
201 #else
202 #error "No suitable type for `struct ifaddrs->ifa_data' found."
203 #endif
204
205   struct IFA_DATA *if_data;
206
207   if (getifaddrs(&if_list) != 0)
208     return -1;
209
210   for (struct ifaddrs *if_ptr = if_list; if_ptr != NULL;
211        if_ptr = if_ptr->ifa_next) {
212     if (if_ptr->ifa_addr != NULL && if_ptr->ifa_addr->sa_family == AF_LINK) {
213       if_data = (struct IFA_DATA *)if_ptr->ifa_data;
214
215       if (!report_inactive && if_data->IFA_RX_PACKT == 0 &&
216           if_data->IFA_TX_PACKT == 0)
217         continue;
218
219       if_submit(if_ptr->ifa_name, "if_octets", if_data->IFA_RX_BYTES,
220                 if_data->IFA_TX_BYTES);
221       if_submit(if_ptr->ifa_name, "if_packets", if_data->IFA_RX_PACKT,
222                 if_data->IFA_TX_PACKT);
223       if_submit(if_ptr->ifa_name, "if_errors", if_data->IFA_RX_ERROR,
224                 if_data->IFA_TX_ERROR);
225     }
226   }
227
228   freeifaddrs(if_list);
229 /* #endif HAVE_GETIFADDRS */
230
231 #elif KERNEL_LINUX
232   FILE *fh;
233   char buffer[1024];
234   derive_t incoming, outgoing;
235   char *device;
236
237   char *dummy;
238   char *fields[16];
239   int numfields;
240
241   if ((fh = fopen("/proc/net/dev", "r")) == NULL) {
242     WARNING("interface plugin: fopen: %s", STRERRNO);
243     return -1;
244   }
245
246   while (fgets(buffer, 1024, fh) != NULL) {
247     if (!(dummy = strchr(buffer, ':')))
248       continue;
249     dummy[0] = '\0';
250     dummy++;
251
252     device = buffer;
253     while (device[0] == ' ')
254       device++;
255
256     if (device[0] == '\0')
257       continue;
258
259     numfields = strsplit(dummy, fields, 16);
260
261     if (numfields < 11)
262       continue;
263
264     incoming = atoll(fields[1]);
265     outgoing = atoll(fields[9]);
266     if (!report_inactive && incoming == 0 && outgoing == 0)
267       continue;
268
269     if_submit(device, "if_packets", incoming, outgoing);
270
271     incoming = atoll(fields[0]);
272     outgoing = atoll(fields[8]);
273     if_submit(device, "if_octets", incoming, outgoing);
274
275     incoming = atoll(fields[2]);
276     outgoing = atoll(fields[10]);
277     if_submit(device, "if_errors", incoming, outgoing);
278
279     incoming = atoll(fields[3]);
280     outgoing = atoll(fields[11]);
281     if_submit(device, "if_dropped", incoming, outgoing);
282   }
283
284   fclose(fh);
285 /* #endif KERNEL_LINUX */
286
287 #elif HAVE_LIBKSTAT
288   derive_t rx;
289   derive_t tx;
290   char iname[DATA_MAX_NAME_LEN];
291
292   if (kc == NULL)
293     return -1;
294
295   for (int i = 0; i < numif; i++) {
296     if (kstat_read(kc, ksp[i], NULL) == -1)
297       continue;
298
299     if (unique_name)
300       snprintf(iname, sizeof(iname), "%s_%d_%s", ksp[i]->ks_module,
301                ksp[i]->ks_instance, ksp[i]->ks_name);
302     else
303       sstrncpy(iname, ksp[i]->ks_name, sizeof(iname));
304
305     /* try to get 64bit counters */
306     rx = get_kstat_value(ksp[i], "ipackets64");
307     tx = get_kstat_value(ksp[i], "opackets64");
308     /* or fallback to 32bit */
309     if (rx == -1LL)
310       rx = get_kstat_value(ksp[i], "ipackets");
311     if (tx == -1LL)
312       tx = get_kstat_value(ksp[i], "opackets");
313     if (!report_inactive && rx == 0 && tx == 0)
314       continue;
315     if ((rx != -1LL) || (tx != -1LL))
316       if_submit(iname, "if_packets", rx, tx);
317
318     /* try to get 64bit counters */
319     rx = get_kstat_value(ksp[i], "rbytes64");
320     tx = get_kstat_value(ksp[i], "obytes64");
321     /* or fallback to 32bit */
322     if (rx == -1LL)
323       rx = get_kstat_value(ksp[i], "rbytes");
324     if (tx == -1LL)
325       tx = get_kstat_value(ksp[i], "obytes");
326     if ((rx != -1LL) || (tx != -1LL))
327       if_submit(iname, "if_octets", rx, tx);
328
329     /* no 64bit error counters yet */
330     rx = get_kstat_value(ksp[i], "ierrors");
331     tx = get_kstat_value(ksp[i], "oerrors");
332     if ((rx != -1LL) || (tx != -1LL))
333       if_submit(iname, "if_errors", rx, tx);
334   }
335 /* #endif HAVE_LIBKSTAT */
336
337 #elif defined(HAVE_LIBSTATGRAB)
338   sg_network_io_stats *ios;
339   int num;
340
341   ios = sg_get_network_io_stats(&num);
342
343   for (int i = 0; i < num; i++) {
344     if (!report_inactive && ios[i].rx == 0 && ios[i].tx == 0)
345       continue;
346     if_submit(ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
347   }
348 /* #endif HAVE_LIBSTATGRAB */
349
350 #elif defined(HAVE_PERFSTAT)
351   perfstat_id_t id;
352   int ifs;
353
354   if ((nif = perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t),
355                                    0)) < 0) {
356     WARNING("interface plugin: perfstat_netinterface: %s", STRERRNO);
357     return -1;
358   }
359
360   if (pnif != nif || ifstat == NULL) {
361     free(ifstat);
362     ifstat = malloc(nif * sizeof(*ifstat));
363   }
364   pnif = nif;
365
366   id.name[0] = '\0';
367   if ((ifs = perfstat_netinterface(&id, ifstat, sizeof(perfstat_netinterface_t),
368                                    nif)) < 0) {
369     WARNING("interface plugin: perfstat_netinterface (interfaces=%d): %s", nif,
370             STRERRNO);
371     return -1;
372   }
373
374   for (int i = 0; i < ifs; i++) {
375     if (!report_inactive && ifstat[i].ipackets == 0 && ifstat[i].opackets == 0)
376       continue;
377
378     if_submit(ifstat[i].name, "if_octets", ifstat[i].ibytes, ifstat[i].obytes);
379     if_submit(ifstat[i].name, "if_packets", ifstat[i].ipackets,
380               ifstat[i].opackets);
381     if_submit(ifstat[i].name, "if_errors", ifstat[i].ierrors,
382               ifstat[i].oerrors);
383   }
384 #endif /* HAVE_PERFSTAT */
385
386   return 0;
387 } /* int interface_read */
388
389 void module_register(void) {
390   plugin_register_config("interface", interface_config, config_keys,
391                          config_keys_num);
392 #if HAVE_LIBKSTAT
393   plugin_register_init("interface", interface_init);
394 #endif
395   plugin_register_read("interface", interface_read);
396 } /* void module_register */