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