load, traffic plugins: Changed license to GPLv2 only.
[collectd.git] / src / traffic.c
1 /**
2  * collectd - src/traffic.c
3  * Copyright (C) 2005,2006  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 # define TRAFFIC_HAVE_READ 1
63 #else
64 # define TRAFFIC_HAVE_READ 0
65 #endif
66
67 /*
68  * (Module-)Global variables
69  */
70 /* 2^32 = 4294967296 = ~4.2GByte/s = ~34GBit/s */
71 static data_source_t octets_dsrc[2] =
72 {
73         {"rx", DS_TYPE_COUNTER, 0, 4294967295.0},
74         {"tx", DS_TYPE_COUNTER, 0, 4294967295.0}
75 };
76
77 static data_set_t octets_ds =
78 {
79         "if_octets", 2, octets_dsrc
80 };
81
82 static data_source_t packets_dsrc[2] =
83 {
84         {"rx", DS_TYPE_COUNTER, 0, 4294967295.0},
85         {"tx", DS_TYPE_COUNTER, 0, 4294967295.0}
86 };
87
88 static data_set_t packets_ds =
89 {
90         "if_packets", 2, packets_dsrc
91 };
92
93 static data_source_t errors_dsrc[2] =
94 {
95         {"rx", DS_TYPE_COUNTER, 0, 4294967295.0},
96         {"tx", DS_TYPE_COUNTER, 0, 4294967295.0}
97 };
98
99 static data_set_t errors_ds =
100 {
101         "if_errors", 2, errors_dsrc
102 };
103
104 static const char *config_keys[] =
105 {
106         "Interface",
107         "IgnoreSelected",
108         NULL
109 };
110 static int config_keys_num = 2;
111
112 static char **if_list = NULL;
113 static int    if_list_num = 0;
114 /* 
115  * if_list_action:
116  * 0 => default is to collect selected interface
117  * 1 => ignore selcted interfaces
118  */
119 static int    if_list_action = 0;
120
121 #ifdef HAVE_LIBKSTAT
122 #define MAX_NUMIF 256
123 extern kstat_ctl_t *kc;
124 static kstat_t *ksp[MAX_NUMIF];
125 static int numif = 0;
126 #endif /* HAVE_LIBKSTAT */
127
128 static int interface_config (const char *key, const char *value)
129 {
130         char **temp;
131
132         if (strcasecmp (key, "Interface") == 0)
133         {
134                 temp = (char **) realloc (if_list, (if_list_num + 1) * sizeof (char *));
135                 if (temp == NULL)
136                 {
137                         syslog (LOG_EMERG, "Cannot allocate more memory.");
138                         return (1);
139                 }
140                 if_list = temp;
141
142                 if ((if_list[if_list_num] = strdup (value)) == NULL)
143                 {
144                         syslog (LOG_EMERG, "Cannot allocate memory.");
145                         return (1);
146                 }
147                 if_list_num++;
148         }
149         else if (strcasecmp (key, "IgnoreSelected") == 0)
150         {
151                 if ((strcasecmp (value, "True") == 0)
152                                 || (strcasecmp (value, "Yes") == 0)
153                                 || (strcasecmp (value, "On") == 0))
154                         if_list_action = 1;
155                 else
156                         if_list_action = 0;
157         }
158         else
159         {
160                 return (-1);
161         }
162
163         return (0);
164 }
165
166 #if HAVE_LIBKSTAT
167 static int traffic_init (void)
168 {
169 #if HAVE_LIBKSTAT
170         kstat_t *ksp_chain;
171         unsigned long long val;
172
173         numif = 0;
174
175         if (kc == NULL)
176                 return (-1);
177
178         for (numif = 0, ksp_chain = kc->kc_chain;
179                         (numif < MAX_NUMIF) && (ksp_chain != NULL);
180                         ksp_chain = ksp_chain->ks_next)
181         {
182                 if (strncmp (ksp_chain->ks_class, "net", 3))
183                         continue;
184                 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
185                         continue;
186                 if (kstat_read (kc, ksp_chain, NULL) == -1)
187                         continue;
188                 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
189                         continue;
190                 ksp[numif++] = ksp_chain;
191         }
192 #endif /* HAVE_LIBKSTAT */
193
194         return (0);
195 } /* int traffic_init */
196 #endif /* HAVE_LIBKSTAT */
197
198 /*
199  * Check if this interface/instance should be ignored. This is called from
200  * both, `submit' and `write' to give client and server the ability to
201  * ignore certain stuff..
202  */
203 static int check_ignore_if (const char *interface)
204 {
205         int i;
206
207         /* If no interfaces are given collect all interfaces. Mostly to be
208          * backwards compatible, but also because this is much easier. */
209         if (if_list_num < 1)
210                 return (0);
211
212         for (i = 0; i < if_list_num; i++)
213                 if (strcasecmp (interface, if_list[i]) == 0)
214                         return (if_list_action);
215         return (1 - if_list_action);
216 } /* int check_ignore_if */
217
218 #if TRAFFIC_HAVE_READ
219 static void if_submit (const char *dev, const char *type,
220                 unsigned long long rx,
221                 unsigned long long tx)
222 {
223         value_t values[2];
224         value_list_t vl = VALUE_LIST_INIT;
225
226         if (check_ignore_if (dev))
227                 return;
228
229         values[0].counter = rx;
230         values[1].counter = tx;
231
232         vl.values = values;
233         vl.values_len = 2;
234         vl.time = time (NULL);
235         strcpy (vl.host, hostname);
236         strcpy (vl.plugin, "interface");
237         strcpy (vl.plugin_instance, "");
238         strncpy (vl.type_instance, dev, sizeof (vl.type_instance));
239
240         plugin_dispatch_values (type, &vl);
241 } /* void if_submit */
242
243 static int traffic_read (void)
244 {
245 #if HAVE_GETIFADDRS
246         struct ifaddrs *if_list;
247         struct ifaddrs *if_ptr;
248
249 /* Darin/Mac OS X and possible other *BSDs */
250 #if HAVE_STRUCT_IF_DATA
251 #  define IFA_DATA if_data
252 #  define IFA_RX_BYTES ifi_ibytes
253 #  define IFA_TX_BYTES ifi_obytes
254 #  define IFA_RX_PACKT ifi_ipackets
255 #  define IFA_TX_PACKT ifi_opackets
256 #  define IFA_RX_ERROR ifi_ierrors
257 #  define IFA_TX_ERROR ifi_oerrors
258 /* #endif HAVE_STRUCT_IF_DATA */
259
260 #elif HAVE_STRUCT_NET_DEVICE_STATS
261 #  define IFA_DATA net_device_stats
262 #  define IFA_RX_BYTES rx_bytes
263 #  define IFA_TX_BYTES tx_bytes
264 #  define IFA_RX_PACKT rx_packets
265 #  define IFA_TX_PACKT tx_packets
266 #  define IFA_RX_ERROR rx_errors
267 #  define IFA_TX_ERROR tx_errors
268 #else
269 #  error "No suitable type for `struct ifaddrs->ifa_data' found."
270 #endif
271
272         struct IFA_DATA *if_data;
273
274         if (getifaddrs (&if_list) != 0)
275                 return;
276
277         for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
278         {
279                 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
280                         continue;
281
282                 if_submit (if_ptr->ifa_name, "if_octets",
283                                 if_data->IFA_RX_BYTES,
284                                 if_data->IFA_TX_BYTES);
285                 if_submit (if_ptr->ifa_name, "if_packets",
286                                 if_data->IFA_RX_PACKT,
287                                 if_data->IFA_TX_PACKT);
288                 if_submit (if_ptr->ifa_name, "if_errors",
289                                 if_data->IFA_RX_ERROR,
290                                 if_data->IFA_TX_ERROR);
291         }
292
293         freeifaddrs (if_list);
294 /* #endif HAVE_GETIFADDRS */
295
296 #elif KERNEL_LINUX
297         FILE *fh;
298         char buffer[1024];
299         unsigned long long incoming, outgoing;
300         char *device;
301         
302         char *dummy;
303         char *fields[16];
304         int numfields;
305
306         if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
307         {
308                 syslog (LOG_WARNING, "traffic: fopen: %s", strerror (errno));
309                 return (-1);
310         }
311
312         while (fgets (buffer, 1024, fh) != NULL)
313         {
314                 if (!(dummy = strchr(buffer, ':')))
315                         continue;
316                 dummy[0] = '\0';
317                 dummy++;
318
319                 device = buffer;
320                 while (device[0] == ' ')
321                         device++;
322
323                 if (device[0] == '\0')
324                         continue;
325                 
326                 numfields = strsplit (dummy, fields, 16);
327
328                 if (numfields < 11)
329                         continue;
330
331                 incoming = atoll (fields[0]);
332                 outgoing = atoll (fields[8]);
333                 if_submit (device, "if_octets", incoming, outgoing);
334
335                 incoming = atoll (fields[1]);
336                 outgoing = atoll (fields[9]);
337                 if_submit (device, "if_packets", incoming, outgoing);
338
339                 incoming = atoll (fields[2]);
340                 outgoing = atoll (fields[10]);
341                 if_submit (device, "if_errors", incoming, outgoing);
342         }
343
344         fclose (fh);
345 /* #endif KERNEL_LINUX */
346
347 #elif HAVE_LIBKSTAT
348         int i;
349         unsigned long long rx;
350         unsigned long long tx;
351
352         if (kc == NULL)
353                 return;
354
355         for (i = 0; i < numif; i++)
356         {
357                 if (kstat_read (kc, ksp[i], NULL) == -1)
358                         continue;
359
360                 rx = get_kstat_value (ksp[i], "rbytes");
361                 tx = get_kstat_value (ksp[i], "obytes");
362                 if ((rx != -1LL) || (tx != -1LL))
363                         if_submit (ksp[i]->ks_name, "if_octets", rx, tx);
364
365                 rx = get_kstat_value (ksp[i], "ipackets");
366                 tx = get_kstat_value (ksp[i], "opackets");
367                 if ((rx != -1LL) || (tx != -1LL))
368                         if_submit (ksp[i]->ks_name, "if_packets", rx, tx);
369
370                 rx = get_kstat_value (ksp[i], "ierrors");
371                 tx = get_kstat_value (ksp[i], "oerrors");
372                 if ((rx != -1LL) || (tx != -1LL))
373                         if_submit (ksp[i]->ks_name, "if_errors", rx, tx);
374         }
375 /* #endif HAVE_LIBKSTAT */
376
377 #elif defined(HAVE_LIBSTATGRAB)
378         sg_network_io_stats *ios;
379         int i, num;
380
381         ios = sg_get_network_io_stats (&num);
382
383         for (i = 0; i < num; i++)
384                 if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
385 #endif /* HAVE_LIBSTATGRAB */
386
387         return (0);
388 } /* int traffic_read */
389 #endif /* TRAFFIC_HAVE_READ */
390
391 void module_register (void)
392 {
393         plugin_register_data_set (&octets_ds);
394         plugin_register_data_set (&packets_ds);
395         plugin_register_data_set (&errors_ds);
396
397         plugin_register_config ("interface", interface_config,
398                         config_keys, config_keys_num);
399
400 #if HAVE_LIBKSTAT
401         plugin_register_init ("interface", traffic_init);
402 #endif
403
404 #if TRAFFIC_HAVE_READ
405         plugin_register_read ("interface", traffic_read);
406 #endif
407 }