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