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