Merge branch 'collectd-4.5'
[collectd.git] / src / interface.c
1 /**
2  * collectd - src/interface.c
3  * Copyright (C) 2005-2007  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  *   Sune Marcher <sm at flork.dk>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27 #include "utils_ignorelist.h"
28
29 #if HAVE_SYS_TYPES_H
30 #  include <sys/types.h>
31 #endif
32 #if HAVE_SYS_SOCKET_H
33 #  include <sys/socket.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 /*
55  * Various people have reported problems with `getifaddrs' and varying versions
56  * of `glibc'. That's why it's disabled by default. Since more statistics are
57  * available this way one may enable it using the `--enable-getifaddrs' option
58  * of the configure script. -octo
59  */
60 #if KERNEL_LINUX
61 # if !COLLECT_GETIFADDRS
62 #  undef HAVE_GETIFADDRS
63 # endif /* !COLLECT_GETIFADDRS */
64 #endif /* KERNEL_LINUX */
65
66 #if !HAVE_GETIFADDRS && !KERNEL_LINUX && !HAVE_LIBKSTAT && !HAVE_LIBSTATGRAB
67 # error "No applicable input method."
68 #endif
69
70 /*
71  * (Module-)Global variables
72  */
73 static const char *config_keys[] =
74 {
75         "Interface",
76         "IgnoreSelected",
77         NULL
78 };
79 static int config_keys_num = 2;
80
81 static ignorelist_t *ignorelist = NULL;
82
83 #ifdef HAVE_LIBKSTAT
84 #define MAX_NUMIF 256
85 extern kstat_ctl_t *kc;
86 static kstat_t *ksp[MAX_NUMIF];
87 static int numif = 0;
88 #endif /* HAVE_LIBKSTAT */
89
90 static int interface_config (const char *key, const char *value)
91 {
92         if (ignorelist == NULL)
93                 ignorelist = ignorelist_create (/* invert = */ 1);
94
95         if (strcasecmp (key, "Interface") == 0)
96         {
97                 ignorelist_add (ignorelist, value);
98         }
99         else if (strcasecmp (key, "IgnoreSelected") == 0)
100         {
101                 int invert = 1;
102                 if ((strcasecmp (value, "True") == 0)
103                                 || (strcasecmp (value, "Yes") == 0)
104                                 || (strcasecmp (value, "On") == 0))
105                         invert = 0;
106                 ignorelist_set_invert (ignorelist, invert);
107         }
108         else
109         {
110                 return (-1);
111         }
112
113         return (0);
114 }
115
116 #if HAVE_LIBKSTAT
117 static int interface_init (void)
118 {
119         kstat_t *ksp_chain;
120         unsigned long long val;
121
122         numif = 0;
123
124         if (kc == NULL)
125                 return (-1);
126
127         for (numif = 0, ksp_chain = kc->kc_chain;
128                         (numif < MAX_NUMIF) && (ksp_chain != NULL);
129                         ksp_chain = ksp_chain->ks_next)
130         {
131                 if (strncmp (ksp_chain->ks_class, "net", 3))
132                         continue;
133                 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
134                         continue;
135                 if (kstat_read (kc, ksp_chain, NULL) == -1)
136                         continue;
137                 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
138                         continue;
139                 ksp[numif++] = ksp_chain;
140         }
141
142         return (0);
143 } /* int interface_init */
144 #endif /* HAVE_LIBKSTAT */
145
146 static void if_submit (const char *dev, const char *type,
147                 unsigned long long rx,
148                 unsigned long long tx)
149 {
150         value_t values[2];
151         value_list_t vl = VALUE_LIST_INIT;
152
153         if (ignorelist_match (ignorelist, dev) != 0)
154                 return;
155
156         values[0].counter = rx;
157         values[1].counter = tx;
158
159         vl.values = values;
160         vl.values_len = 2;
161         vl.time = time (NULL);
162         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
163         sstrncpy (vl.plugin, "interface", sizeof (vl.plugin));
164         sstrncpy (vl.type, type, sizeof (vl.type));
165         sstrncpy (vl.type_instance, dev, sizeof (vl.type_instance));
166
167         plugin_dispatch_values (&vl);
168 } /* void if_submit */
169
170 static int interface_read (void)
171 {
172 #if HAVE_GETIFADDRS
173         struct ifaddrs *if_list;
174         struct ifaddrs *if_ptr;
175
176 /* Darin/Mac OS X and possible other *BSDs */
177 #if HAVE_STRUCT_IF_DATA
178 #  define IFA_DATA if_data
179 #  define IFA_RX_BYTES ifi_ibytes
180 #  define IFA_TX_BYTES ifi_obytes
181 #  define IFA_RX_PACKT ifi_ipackets
182 #  define IFA_TX_PACKT ifi_opackets
183 #  define IFA_RX_ERROR ifi_ierrors
184 #  define IFA_TX_ERROR ifi_oerrors
185 /* #endif HAVE_STRUCT_IF_DATA */
186
187 #elif HAVE_STRUCT_NET_DEVICE_STATS
188 #  define IFA_DATA net_device_stats
189 #  define IFA_RX_BYTES rx_bytes
190 #  define IFA_TX_BYTES tx_bytes
191 #  define IFA_RX_PACKT rx_packets
192 #  define IFA_TX_PACKT tx_packets
193 #  define IFA_RX_ERROR rx_errors
194 #  define IFA_TX_ERROR tx_errors
195 #else
196 #  error "No suitable type for `struct ifaddrs->ifa_data' found."
197 #endif
198
199         struct IFA_DATA *if_data;
200
201         if (getifaddrs (&if_list) != 0)
202                 return (-1);
203
204         for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
205         {
206                 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
207                         continue;
208
209                 if_submit (if_ptr->ifa_name, "if_octets",
210                                 if_data->IFA_RX_BYTES,
211                                 if_data->IFA_TX_BYTES);
212                 if_submit (if_ptr->ifa_name, "if_packets",
213                                 if_data->IFA_RX_PACKT,
214                                 if_data->IFA_TX_PACKT);
215                 if_submit (if_ptr->ifa_name, "if_errors",
216                                 if_data->IFA_RX_ERROR,
217                                 if_data->IFA_TX_ERROR);
218         }
219
220         freeifaddrs (if_list);
221 /* #endif HAVE_GETIFADDRS */
222
223 #elif KERNEL_LINUX
224         FILE *fh;
225         char buffer[1024];
226         unsigned long long incoming, outgoing;
227         char *device;
228         
229         char *dummy;
230         char *fields[16];
231         int numfields;
232
233         if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
234         {
235                 char errbuf[1024];
236                 WARNING ("interface plugin: fopen: %s",
237                                 sstrerror (errno, errbuf, sizeof (errbuf)));
238                 return (-1);
239         }
240
241         while (fgets (buffer, 1024, fh) != NULL)
242         {
243                 if (!(dummy = strchr(buffer, ':')))
244                         continue;
245                 dummy[0] = '\0';
246                 dummy++;
247
248                 device = buffer;
249                 while (device[0] == ' ')
250                         device++;
251
252                 if (device[0] == '\0')
253                         continue;
254                 
255                 numfields = strsplit (dummy, fields, 16);
256
257                 if (numfields < 11)
258                         continue;
259
260                 incoming = atoll (fields[0]);
261                 outgoing = atoll (fields[8]);
262                 if_submit (device, "if_octets", incoming, outgoing);
263
264                 incoming = atoll (fields[1]);
265                 outgoing = atoll (fields[9]);
266                 if_submit (device, "if_packets", incoming, outgoing);
267
268                 incoming = atoll (fields[2]);
269                 outgoing = atoll (fields[10]);
270                 if_submit (device, "if_errors", incoming, outgoing);
271         }
272
273         fclose (fh);
274 /* #endif KERNEL_LINUX */
275
276 #elif HAVE_LIBKSTAT
277         int i;
278         unsigned long long rx;
279         unsigned long long tx;
280
281         if (kc == NULL)
282                 return (-1);
283
284         for (i = 0; i < numif; i++)
285         {
286                 if (kstat_read (kc, ksp[i], NULL) == -1)
287                         continue;
288
289                 rx = get_kstat_value (ksp[i], "rbytes");
290                 tx = get_kstat_value (ksp[i], "obytes");
291                 if ((rx != -1LL) || (tx != -1LL))
292                         if_submit (ksp[i]->ks_name, "if_octets", rx, tx);
293
294                 rx = get_kstat_value (ksp[i], "ipackets");
295                 tx = get_kstat_value (ksp[i], "opackets");
296                 if ((rx != -1LL) || (tx != -1LL))
297                         if_submit (ksp[i]->ks_name, "if_packets", rx, tx);
298
299                 rx = get_kstat_value (ksp[i], "ierrors");
300                 tx = get_kstat_value (ksp[i], "oerrors");
301                 if ((rx != -1LL) || (tx != -1LL))
302                         if_submit (ksp[i]->ks_name, "if_errors", rx, tx);
303         }
304 /* #endif HAVE_LIBKSTAT */
305
306 #elif defined(HAVE_LIBSTATGRAB)
307         sg_network_io_stats *ios;
308         int i, num;
309
310         ios = sg_get_network_io_stats (&num);
311
312         for (i = 0; i < num; i++)
313                 if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
314 #endif /* HAVE_LIBSTATGRAB */
315
316         return (0);
317 } /* int interface_read */
318
319 void module_register (void)
320 {
321         plugin_register_config ("interface", interface_config,
322                         config_keys, config_keys_num);
323 #if HAVE_LIBKSTAT
324         plugin_register_init ("interface", interface_init);
325 #endif
326         plugin_register_read ("interface", interface_read);
327 } /* void module_register */