Various plugins: Change various plugins to use "derive" internally.
[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 verplant.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 #if HAVE_SYS_SOCKET_H
35 #  include <sys/socket.h>
36 #endif
37
38 /* One cannot include both. This sucks. */
39 #if HAVE_LINUX_IF_H
40 #  include <linux/if.h>
41 #elif HAVE_NET_IF_H
42 #  include <net/if.h>
43 #endif
44
45 #if HAVE_LINUX_NETDEVICE_H
46 #  include <linux/netdevice.h>
47 #endif
48 #if HAVE_IFADDRS_H
49 #  include <ifaddrs.h>
50 #endif
51
52 #if HAVE_STATGRAB_H
53 # include <statgrab.h>
54 #endif
55
56 #if HAVE_PERFSTAT
57 # include <sys/protosw.h>
58 # include <libperfstat.h>
59 #endif
60
61 /*
62  * Various people have reported problems with `getifaddrs' and varying versions
63  * of `glibc'. That's why it's disabled by default. Since more statistics are
64  * available this way one may enable it using the `--enable-getifaddrs' option
65  * of the configure script. -octo
66  */
67 #if KERNEL_LINUX
68 # if !COLLECT_GETIFADDRS
69 #  undef HAVE_GETIFADDRS
70 # endif /* !COLLECT_GETIFADDRS */
71 #endif /* KERNEL_LINUX */
72
73 #if HAVE_PERFSTAT
74 static perfstat_netinterface_t *ifstat;
75 static int nif;
76 static int pnif;
77 #endif /* HAVE_PERFSTAT */
78
79 #if !HAVE_GETIFADDRS && !KERNEL_LINUX && !HAVE_LIBKSTAT && !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
80 # error "No applicable input method."
81 #endif
82
83 /*
84  * (Module-)Global variables
85  */
86 static const char *config_keys[] =
87 {
88         "Interface",
89         "IgnoreSelected",
90         NULL
91 };
92 static int config_keys_num = 2;
93
94 static ignorelist_t *ignorelist = NULL;
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 #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
120         {
121                 return (-1);
122         }
123
124         return (0);
125 }
126
127 #if HAVE_LIBKSTAT
128 static int interface_init (void)
129 {
130         kstat_t *ksp_chain;
131         derive_t val;
132
133         numif = 0;
134
135         if (kc == NULL)
136                 return (-1);
137
138         for (numif = 0, ksp_chain = kc->kc_chain;
139                         (numif < MAX_NUMIF) && (ksp_chain != NULL);
140                         ksp_chain = ksp_chain->ks_next)
141         {
142                 if (strncmp (ksp_chain->ks_class, "net", 3))
143                         continue;
144                 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
145                         continue;
146                 if (kstat_read (kc, ksp_chain, NULL) == -1)
147                         continue;
148                 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
149                         continue;
150                 ksp[numif++] = ksp_chain;
151         }
152
153         return (0);
154 } /* int interface_init */
155 #endif /* HAVE_LIBKSTAT */
156
157 static void if_submit (const char *dev, const char *type,
158                 derive_t rx,
159                 derive_t tx)
160 {
161         value_t values[2];
162         value_list_t vl = VALUE_LIST_INIT;
163
164         if (ignorelist_match (ignorelist, dev) != 0)
165                 return;
166
167         values[0].derive = rx;
168         values[1].derive = tx;
169
170         vl.values = values;
171         vl.values_len = 2;
172         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
173         sstrncpy (vl.plugin, "interface", sizeof (vl.plugin));
174         sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
175         sstrncpy (vl.type, type, sizeof (vl.type));
176
177         plugin_dispatch_values (&vl);
178 } /* void if_submit */
179
180 static int interface_read (void)
181 {
182 #if HAVE_GETIFADDRS
183         struct ifaddrs *if_list;
184         struct ifaddrs *if_ptr;
185
186 /* Darin/Mac OS X and possible other *BSDs */
187 #if HAVE_STRUCT_IF_DATA
188 #  define IFA_DATA if_data
189 #  define IFA_RX_BYTES ifi_ibytes
190 #  define IFA_TX_BYTES ifi_obytes
191 #  define IFA_RX_PACKT ifi_ipackets
192 #  define IFA_TX_PACKT ifi_opackets
193 #  define IFA_RX_ERROR ifi_ierrors
194 #  define IFA_TX_ERROR ifi_oerrors
195 /* #endif HAVE_STRUCT_IF_DATA */
196
197 #elif HAVE_STRUCT_NET_DEVICE_STATS
198 #  define IFA_DATA net_device_stats
199 #  define IFA_RX_BYTES rx_bytes
200 #  define IFA_TX_BYTES tx_bytes
201 #  define IFA_RX_PACKT rx_packets
202 #  define IFA_TX_PACKT tx_packets
203 #  define IFA_RX_ERROR rx_errors
204 #  define IFA_TX_ERROR tx_errors
205 #else
206 #  error "No suitable type for `struct ifaddrs->ifa_data' found."
207 #endif
208
209         struct IFA_DATA *if_data;
210
211         if (getifaddrs (&if_list) != 0)
212                 return (-1);
213
214         for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
215         {
216                 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
217                         continue;
218
219                 if_submit (if_ptr->ifa_name, "if_octets",
220                                 if_data->IFA_RX_BYTES,
221                                 if_data->IFA_TX_BYTES);
222                 if_submit (if_ptr->ifa_name, "if_packets",
223                                 if_data->IFA_RX_PACKT,
224                                 if_data->IFA_TX_PACKT);
225                 if_submit (if_ptr->ifa_name, "if_errors",
226                                 if_data->IFA_RX_ERROR,
227                                 if_data->IFA_TX_ERROR);
228         }
229
230         freeifaddrs (if_list);
231 /* #endif HAVE_GETIFADDRS */
232
233 #elif KERNEL_LINUX
234         FILE *fh;
235         char buffer[1024];
236         derive_t incoming, outgoing;
237         char *device;
238
239         char *dummy;
240         char *fields[16];
241         int numfields;
242
243         if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
244         {
245                 char errbuf[1024];
246                 WARNING ("interface plugin: fopen: %s",
247                                 sstrerror (errno, errbuf, sizeof (errbuf)));
248                 return (-1);
249         }
250
251         while (fgets (buffer, 1024, fh) != NULL)
252         {
253                 if (!(dummy = strchr(buffer, ':')))
254                         continue;
255                 dummy[0] = '\0';
256                 dummy++;
257
258                 device = buffer;
259                 while (device[0] == ' ')
260                         device++;
261
262                 if (device[0] == '\0')
263                         continue;
264
265                 numfields = strsplit (dummy, fields, 16);
266
267                 if (numfields < 11)
268                         continue;
269
270                 incoming = atoll (fields[0]);
271                 outgoing = atoll (fields[8]);
272                 if_submit (device, "if_octets", incoming, outgoing);
273
274                 incoming = atoll (fields[1]);
275                 outgoing = atoll (fields[9]);
276                 if_submit (device, "if_packets", incoming, outgoing);
277
278                 incoming = atoll (fields[2]);
279                 outgoing = atoll (fields[10]);
280                 if_submit (device, "if_errors", incoming, outgoing);
281         }
282
283         fclose (fh);
284 /* #endif KERNEL_LINUX */
285
286 #elif HAVE_LIBKSTAT
287         int i;
288         derive_t rx;
289         derive_t tx;
290
291         if (kc == NULL)
292                 return (-1);
293
294         for (i = 0; i < numif; i++)
295         {
296                 if (kstat_read (kc, ksp[i], NULL) == -1)
297                         continue;
298
299                 rx = get_kstat_value (ksp[i], "rbytes");
300                 tx = get_kstat_value (ksp[i], "obytes");
301                 if ((rx != -1LL) || (tx != -1LL))
302                         if_submit (ksp[i]->ks_name, "if_octets", rx, tx);
303
304                 rx = get_kstat_value (ksp[i], "ipackets");
305                 tx = get_kstat_value (ksp[i], "opackets");
306                 if ((rx != -1LL) || (tx != -1LL))
307                         if_submit (ksp[i]->ks_name, "if_packets", rx, tx);
308
309                 rx = get_kstat_value (ksp[i], "ierrors");
310                 tx = get_kstat_value (ksp[i], "oerrors");
311                 if ((rx != -1LL) || (tx != -1LL))
312                         if_submit (ksp[i]->ks_name, "if_errors", rx, tx);
313         }
314 /* #endif HAVE_LIBKSTAT */
315
316 #elif defined(HAVE_LIBSTATGRAB)
317         sg_network_io_stats *ios;
318         int i, num;
319
320         ios = sg_get_network_io_stats (&num);
321
322         for (i = 0; i < num; i++)
323                 if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
324 /* #endif HAVE_LIBSTATGRAB */
325
326 #elif defined(HAVE_PERFSTAT)
327         perfstat_id_t id;
328         int i, ifs;
329
330         if ((nif =  perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t), 0)) < 0)
331         {
332                 char errbuf[1024];
333                 WARNING ("interface plugin: perfstat_netinterface: %s",
334                         sstrerror (errno, errbuf, sizeof (errbuf)));
335                 return (-1);
336         }
337
338         if (pnif != nif || ifstat == NULL)
339         {
340                 if (ifstat != NULL)
341                         free(ifstat);
342                 ifstat = malloc(nif * sizeof(perfstat_netinterface_t));
343         }
344         pnif = nif;
345
346         id.name[0]='\0';
347         if ((ifs = perfstat_netinterface(&id, ifstat, sizeof(perfstat_netinterface_t), nif)) < 0)
348         {
349                 char errbuf[1024];
350                 WARNING ("interface plugin: perfstat_netinterface (interfaces=%d): %s",
351                         nif, sstrerror (errno, errbuf, sizeof (errbuf)));
352                 return (-1);
353         }
354
355         for (i = 0; i < ifs; i++)
356         {
357                 if_submit (ifstat[i].name, "if_octets", ifstat[i].ibytes, ifstat[i].obytes);
358                 if_submit (ifstat[i].name, "if_packets", ifstat[i].ipackets ,ifstat[i].opackets);
359                 if_submit (ifstat[i].name, "if_errors", ifstat[i].ierrors, ifstat[i].oerrors );
360         }
361 #endif /* HAVE_PERFSTAT */
362
363         return (0);
364 } /* int interface_read */
365
366 void module_register (void)
367 {
368         plugin_register_config ("interface", interface_config,
369                         config_keys, config_keys_num);
370 #if HAVE_LIBKSTAT
371         plugin_register_init ("interface", interface_init);
372 #endif
373         plugin_register_read ("interface", interface_read);
374 } /* void module_register */