Unified string handling.
[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
28 #if HAVE_SYS_TYPES_H
29 #  include <sys/types.h>
30 #endif
31 #if HAVE_SYS_SOCKET_H
32 #  include <sys/socket.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 /*
50  * Various people have reported problems with `getifaddrs' and varying versions
51  * of `glibc'. That's why it's disabled by default. Since more statistics are
52  * available this way one may enable it using the `--enable-getifaddrs' option
53  * of the configure script. -octo
54  */
55 #if KERNEL_LINUX
56 # if !COLLECT_GETIFADDRS
57 #  undef HAVE_GETIFADDRS
58 # endif /* !COLLECT_GETIFADDRS */
59 #endif /* KERNEL_LINUX */
60
61 #if !HAVE_GETIFADDRS && !KERNEL_LINUX && !HAVE_LIBKSTAT && !HAVE_LIBSTATGRAB
62 # error "No applicable input method."
63 #endif
64
65 /*
66  * (Module-)Global variables
67  */
68 static const char *config_keys[] =
69 {
70         "Interface",
71         "IgnoreSelected",
72         NULL
73 };
74 static int config_keys_num = 2;
75
76 static char **if_list = NULL;
77 static int    if_list_num = 0;
78 /* 
79  * if_list_action:
80  * 0 => default is to collect selected interface
81  * 1 => ignore selcted interfaces
82  */
83 static int    if_list_action = 0;
84
85 #ifdef HAVE_LIBKSTAT
86 #define MAX_NUMIF 256
87 extern kstat_ctl_t *kc;
88 static kstat_t *ksp[MAX_NUMIF];
89 static int numif = 0;
90 #endif /* HAVE_LIBKSTAT */
91
92 static int interface_config (const char *key, const char *value)
93 {
94         char **temp;
95
96         if (strcasecmp (key, "Interface") == 0)
97         {
98                 temp = (char **) realloc (if_list, (if_list_num + 1) * sizeof (char *));
99                 if (temp == NULL)
100                 {
101                         ERROR ("Cannot allocate more memory.");
102                         return (1);
103                 }
104                 if_list = temp;
105
106                 if ((if_list[if_list_num] = strdup (value)) == NULL)
107                 {
108                         ERROR ("Cannot allocate memory.");
109                         return (1);
110                 }
111                 if_list_num++;
112         }
113         else if (strcasecmp (key, "IgnoreSelected") == 0)
114         {
115                 if ((strcasecmp (value, "True") == 0)
116                                 || (strcasecmp (value, "Yes") == 0)
117                                 || (strcasecmp (value, "On") == 0))
118                         if_list_action = 1;
119                 else
120                         if_list_action = 0;
121         }
122         else
123         {
124                 return (-1);
125         }
126
127         return (0);
128 }
129
130 #if HAVE_LIBKSTAT
131 static int interface_init (void)
132 {
133         kstat_t *ksp_chain;
134         unsigned long long val;
135
136         numif = 0;
137
138         if (kc == NULL)
139                 return (-1);
140
141         for (numif = 0, ksp_chain = kc->kc_chain;
142                         (numif < MAX_NUMIF) && (ksp_chain != NULL);
143                         ksp_chain = ksp_chain->ks_next)
144         {
145                 if (strncmp (ksp_chain->ks_class, "net", 3))
146                         continue;
147                 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
148                         continue;
149                 if (kstat_read (kc, ksp_chain, NULL) == -1)
150                         continue;
151                 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
152                         continue;
153                 ksp[numif++] = ksp_chain;
154         }
155
156         return (0);
157 } /* int interface_init */
158 #endif /* HAVE_LIBKSTAT */
159
160 /*
161  * Check if this interface/instance should be ignored. This is called from
162  * both, `submit' and `write' to give client and server the ability to
163  * ignore certain stuff..
164  */
165 static int check_ignore_if (const char *interface)
166 {
167         int i;
168
169         /* If no interfaces are given collect all interfaces. Mostly to be
170          * backwards compatible, but also because this is much easier. */
171         if (if_list_num < 1)
172                 return (0);
173
174         for (i = 0; i < if_list_num; i++)
175                 if (strcasecmp (interface, if_list[i]) == 0)
176                         return (if_list_action);
177         return (1 - if_list_action);
178 } /* int check_ignore_if */
179
180 static void if_submit (const char *dev, const char *type,
181                 unsigned long long rx,
182                 unsigned long long tx)
183 {
184         value_t values[2];
185         value_list_t vl = VALUE_LIST_INIT;
186
187         if (check_ignore_if (dev))
188                 return;
189
190         values[0].counter = rx;
191         values[1].counter = tx;
192
193         vl.values = values;
194         vl.values_len = 2;
195         vl.time = time (NULL);
196         strcpy (vl.host, hostname_g);
197         strcpy (vl.plugin, "interface");
198         sstrncpy (vl.type, type, sizeof (vl.type));
199         sstrncpy (vl.type_instance, dev, sizeof (vl.type_instance));
200
201         plugin_dispatch_values (&vl);
202 } /* void if_submit */
203
204 static int interface_read (void)
205 {
206 #if HAVE_GETIFADDRS
207         struct ifaddrs *if_list;
208         struct ifaddrs *if_ptr;
209
210 /* Darin/Mac OS X and possible other *BSDs */
211 #if HAVE_STRUCT_IF_DATA
212 #  define IFA_DATA if_data
213 #  define IFA_RX_BYTES ifi_ibytes
214 #  define IFA_TX_BYTES ifi_obytes
215 #  define IFA_RX_PACKT ifi_ipackets
216 #  define IFA_TX_PACKT ifi_opackets
217 #  define IFA_RX_ERROR ifi_ierrors
218 #  define IFA_TX_ERROR ifi_oerrors
219 /* #endif HAVE_STRUCT_IF_DATA */
220
221 #elif HAVE_STRUCT_NET_DEVICE_STATS
222 #  define IFA_DATA net_device_stats
223 #  define IFA_RX_BYTES rx_bytes
224 #  define IFA_TX_BYTES tx_bytes
225 #  define IFA_RX_PACKT rx_packets
226 #  define IFA_TX_PACKT tx_packets
227 #  define IFA_RX_ERROR rx_errors
228 #  define IFA_TX_ERROR tx_errors
229 #else
230 #  error "No suitable type for `struct ifaddrs->ifa_data' found."
231 #endif
232
233         struct IFA_DATA *if_data;
234
235         if (getifaddrs (&if_list) != 0)
236                 return (-1);
237
238         for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
239         {
240                 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
241                         continue;
242
243                 if_submit (if_ptr->ifa_name, "if_octets",
244                                 if_data->IFA_RX_BYTES,
245                                 if_data->IFA_TX_BYTES);
246                 if_submit (if_ptr->ifa_name, "if_packets",
247                                 if_data->IFA_RX_PACKT,
248                                 if_data->IFA_TX_PACKT);
249                 if_submit (if_ptr->ifa_name, "if_errors",
250                                 if_data->IFA_RX_ERROR,
251                                 if_data->IFA_TX_ERROR);
252         }
253
254         freeifaddrs (if_list);
255 /* #endif HAVE_GETIFADDRS */
256
257 #elif KERNEL_LINUX
258         FILE *fh;
259         char buffer[1024];
260         unsigned long long incoming, outgoing;
261         char *device;
262         
263         char *dummy;
264         char *fields[16];
265         int numfields;
266
267         if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
268         {
269                 char errbuf[1024];
270                 WARNING ("interface plugin: fopen: %s",
271                                 sstrerror (errno, errbuf, sizeof (errbuf)));
272                 return (-1);
273         }
274
275         while (fgets (buffer, 1024, fh) != NULL)
276         {
277                 if (!(dummy = strchr(buffer, ':')))
278                         continue;
279                 dummy[0] = '\0';
280                 dummy++;
281
282                 device = buffer;
283                 while (device[0] == ' ')
284                         device++;
285
286                 if (device[0] == '\0')
287                         continue;
288                 
289                 numfields = strsplit (dummy, fields, 16);
290
291                 if (numfields < 11)
292                         continue;
293
294                 incoming = atoll (fields[0]);
295                 outgoing = atoll (fields[8]);
296                 if_submit (device, "if_octets", incoming, outgoing);
297
298                 incoming = atoll (fields[1]);
299                 outgoing = atoll (fields[9]);
300                 if_submit (device, "if_packets", incoming, outgoing);
301
302                 incoming = atoll (fields[2]);
303                 outgoing = atoll (fields[10]);
304                 if_submit (device, "if_errors", incoming, outgoing);
305         }
306
307         fclose (fh);
308 /* #endif KERNEL_LINUX */
309
310 #elif HAVE_LIBKSTAT
311         int i;
312         unsigned long long rx;
313         unsigned long long tx;
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                 rx = get_kstat_value (ksp[i], "rbytes");
324                 tx = get_kstat_value (ksp[i], "obytes");
325                 if ((rx != -1LL) || (tx != -1LL))
326                         if_submit (ksp[i]->ks_name, "if_octets", rx, tx);
327
328                 rx = get_kstat_value (ksp[i], "ipackets");
329                 tx = get_kstat_value (ksp[i], "opackets");
330                 if ((rx != -1LL) || (tx != -1LL))
331                         if_submit (ksp[i]->ks_name, "if_packets", rx, tx);
332
333                 rx = get_kstat_value (ksp[i], "ierrors");
334                 tx = get_kstat_value (ksp[i], "oerrors");
335                 if ((rx != -1LL) || (tx != -1LL))
336                         if_submit (ksp[i]->ks_name, "if_errors", rx, tx);
337         }
338 /* #endif HAVE_LIBKSTAT */
339
340 #elif defined(HAVE_LIBSTATGRAB)
341         sg_network_io_stats *ios;
342         int i, num;
343
344         ios = sg_get_network_io_stats (&num);
345
346         for (i = 0; i < num; i++)
347                 if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
348 #endif /* HAVE_LIBSTATGRAB */
349
350         return (0);
351 } /* int interface_read */
352
353 void module_register (void)
354 {
355         plugin_register_config ("interface", interface_config,
356                         config_keys, config_keys_num);
357 #if HAVE_LIBKSTAT
358         plugin_register_init ("interface", interface_init);
359 #endif
360         plugin_register_read ("interface", interface_read);
361 } /* void module_register */