Various plugins: Use the IS_TRUE and IS_FALSE macros everywhere.
[collectd.git] / src / interface.c
1 /**
2  * collectd - src/interface.c
3  * Copyright (C) 2005-2008  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 (IS_TRUE (value))
103                         invert = 0;
104                 ignorelist_set_invert (ignorelist, invert);
105         }
106         else
107         {
108                 return (-1);
109         }
110
111         return (0);
112 }
113
114 #if HAVE_LIBKSTAT
115 static int interface_init (void)
116 {
117         kstat_t *ksp_chain;
118         unsigned long long val;
119
120         numif = 0;
121
122         if (kc == NULL)
123                 return (-1);
124
125         for (numif = 0, ksp_chain = kc->kc_chain;
126                         (numif < MAX_NUMIF) && (ksp_chain != NULL);
127                         ksp_chain = ksp_chain->ks_next)
128         {
129                 if (strncmp (ksp_chain->ks_class, "net", 3))
130                         continue;
131                 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
132                         continue;
133                 if (kstat_read (kc, ksp_chain, NULL) == -1)
134                         continue;
135                 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
136                         continue;
137                 ksp[numif++] = ksp_chain;
138         }
139
140         return (0);
141 } /* int interface_init */
142 #endif /* HAVE_LIBKSTAT */
143
144 static void if_submit (const char *dev, const char *type,
145                 unsigned long long rx,
146                 unsigned long long tx)
147 {
148         value_t values[2];
149         value_list_t vl = VALUE_LIST_INIT;
150
151         if (ignorelist_match (ignorelist, dev) != 0)
152                 return;
153
154         values[0].counter = rx;
155         values[1].counter = tx;
156
157         vl.values = values;
158         vl.values_len = 2;
159         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
160         sstrncpy (vl.plugin, "interface", sizeof (vl.plugin));
161         sstrncpy (vl.type, type, sizeof (vl.type));
162         sstrncpy (vl.type_instance, dev, sizeof (vl.type_instance));
163
164         plugin_dispatch_values (&vl);
165 } /* void if_submit */
166
167 static int interface_read (void)
168 {
169 #if HAVE_GETIFADDRS
170         struct ifaddrs *if_list;
171         struct ifaddrs *if_ptr;
172
173 /* Darin/Mac OS X and possible other *BSDs */
174 #if HAVE_STRUCT_IF_DATA
175 #  define IFA_DATA if_data
176 #  define IFA_RX_BYTES ifi_ibytes
177 #  define IFA_TX_BYTES ifi_obytes
178 #  define IFA_RX_PACKT ifi_ipackets
179 #  define IFA_TX_PACKT ifi_opackets
180 #  define IFA_RX_ERROR ifi_ierrors
181 #  define IFA_TX_ERROR ifi_oerrors
182 /* #endif HAVE_STRUCT_IF_DATA */
183
184 #elif HAVE_STRUCT_NET_DEVICE_STATS
185 #  define IFA_DATA net_device_stats
186 #  define IFA_RX_BYTES rx_bytes
187 #  define IFA_TX_BYTES tx_bytes
188 #  define IFA_RX_PACKT rx_packets
189 #  define IFA_TX_PACKT tx_packets
190 #  define IFA_RX_ERROR rx_errors
191 #  define IFA_TX_ERROR tx_errors
192 #else
193 #  error "No suitable type for `struct ifaddrs->ifa_data' found."
194 #endif
195
196         struct IFA_DATA *if_data;
197
198         if (getifaddrs (&if_list) != 0)
199                 return (-1);
200
201         for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
202         {
203                 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
204                         continue;
205
206                 if_submit (if_ptr->ifa_name, "if_octets",
207                                 if_data->IFA_RX_BYTES,
208                                 if_data->IFA_TX_BYTES);
209                 if_submit (if_ptr->ifa_name, "if_packets",
210                                 if_data->IFA_RX_PACKT,
211                                 if_data->IFA_TX_PACKT);
212                 if_submit (if_ptr->ifa_name, "if_errors",
213                                 if_data->IFA_RX_ERROR,
214                                 if_data->IFA_TX_ERROR);
215         }
216
217         freeifaddrs (if_list);
218 /* #endif HAVE_GETIFADDRS */
219
220 #elif KERNEL_LINUX
221         FILE *fh;
222         char buffer[1024];
223         unsigned long long incoming, outgoing;
224         char *device;
225         
226         char *dummy;
227         char *fields[16];
228         int numfields;
229
230         if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
231         {
232                 char errbuf[1024];
233                 WARNING ("interface plugin: fopen: %s",
234                                 sstrerror (errno, errbuf, sizeof (errbuf)));
235                 return (-1);
236         }
237
238         while (fgets (buffer, 1024, fh) != NULL)
239         {
240                 if (!(dummy = strchr(buffer, ':')))
241                         continue;
242                 dummy[0] = '\0';
243                 dummy++;
244
245                 device = buffer;
246                 while (device[0] == ' ')
247                         device++;
248
249                 if (device[0] == '\0')
250                         continue;
251                 
252                 numfields = strsplit (dummy, fields, 16);
253
254                 if (numfields < 11)
255                         continue;
256
257                 incoming = atoll (fields[0]);
258                 outgoing = atoll (fields[8]);
259                 if_submit (device, "if_octets", incoming, outgoing);
260
261                 incoming = atoll (fields[1]);
262                 outgoing = atoll (fields[9]);
263                 if_submit (device, "if_packets", incoming, outgoing);
264
265                 incoming = atoll (fields[2]);
266                 outgoing = atoll (fields[10]);
267                 if_submit (device, "if_errors", incoming, outgoing);
268         }
269
270         fclose (fh);
271 /* #endif KERNEL_LINUX */
272
273 #elif HAVE_LIBKSTAT
274         int i;
275         unsigned long long rx;
276         unsigned long long tx;
277
278         if (kc == NULL)
279                 return (-1);
280
281         for (i = 0; i < numif; i++)
282         {
283                 if (kstat_read (kc, ksp[i], NULL) == -1)
284                         continue;
285
286                 rx = get_kstat_value (ksp[i], "rbytes");
287                 tx = get_kstat_value (ksp[i], "obytes");
288                 if ((rx != -1LL) || (tx != -1LL))
289                         if_submit (ksp[i]->ks_name, "if_octets", rx, tx);
290
291                 rx = get_kstat_value (ksp[i], "ipackets");
292                 tx = get_kstat_value (ksp[i], "opackets");
293                 if ((rx != -1LL) || (tx != -1LL))
294                         if_submit (ksp[i]->ks_name, "if_packets", rx, tx);
295
296                 rx = get_kstat_value (ksp[i], "ierrors");
297                 tx = get_kstat_value (ksp[i], "oerrors");
298                 if ((rx != -1LL) || (tx != -1LL))
299                         if_submit (ksp[i]->ks_name, "if_errors", rx, tx);
300         }
301 /* #endif HAVE_LIBKSTAT */
302
303 #elif defined(HAVE_LIBSTATGRAB)
304         sg_network_io_stats *ios;
305         int i, num;
306
307         ios = sg_get_network_io_stats (&num);
308
309         for (i = 0; i < num; i++)
310                 if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
311 #endif /* HAVE_LIBSTATGRAB */
312
313         return (0);
314 } /* int interface_read */
315
316 void module_register (void)
317 {
318         plugin_register_config ("interface", interface_config,
319                         config_keys, config_keys_num);
320 #if HAVE_LIBKSTAT
321         plugin_register_init ("interface", interface_init);
322 #endif
323         plugin_register_read ("interface", interface_read);
324 } /* void module_register */