2 * collectd - src/traffic.c
3 * Copyright (C) 2005,2006 Florian octo Forster
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; either version 2 of the License, or (at your
8 * option) any later version.
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.
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
20 * Florian octo Forster <octo at verplant.org>
21 * Sune Marcher <sm at flork.dk>
27 #include "configfile.h"
30 # include <sys/types.h>
33 # include <sys/socket.h>
36 /* One cannot include both. This sucks. */
38 # include <linux/if.h>
43 #if HAVE_LINUX_NETDEVICE_H
44 # include <linux/netdevice.h>
50 #define MODULE_NAME "traffic"
53 * Various people have reported problems with `getifaddrs' and varying versions
54 * of `glibc'. That's why it's disabled by default. Since more statistics are
55 * available this way one may enable it using the `--enable-getifaddrs' option
56 * of the configure script. -octo
59 # if !COLLECT_GETIFADDRS
60 # undef HAVE_GETIFADDRS
61 # endif /* !COLLECT_GETIFADDRS */
62 #endif /* KERNEL_LINUX */
64 #if HAVE_GETIFADDRS || KERNEL_LINUX || HAVE_LIBKSTAT || HAVE_LIBSTATGRAB
65 # define TRAFFIC_HAVE_READ 1
67 # define TRAFFIC_HAVE_READ 0
73 * (Module-)Global variables
75 /* TODO: Move this to `interface-%s/<blah>.rrd' in version 4. */
76 static char *bytes_file = "traffic-%s.rrd";
77 static char *packets_file = "interface-%s/if_packets.rrd";
78 static char *errors_file = "interface-%s/if_errors.rrd";
79 /* TODO: Maybe implement multicast and broadcast counters */
81 static char *config_keys[] =
87 static int config_keys_num = 2;
89 static char *bytes_ds_def[] =
91 "DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U",
92 "DS:outgoing:COUNTER:"COLLECTD_HEARTBEAT":0:U",
95 static int bytes_ds_num = 2;
97 static char *packets_ds_def[] =
99 "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
100 "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
103 static int packets_ds_num = 2;
105 static char *errors_ds_def[] =
107 "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
108 "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
111 static int errors_ds_num = 2;
113 static char **if_list = NULL;
114 static int if_list_num = 0;
117 * 0 => default is to collect selected interface
118 * 1 => ignore selcted interfaces
120 static int if_list_action = 0;
123 #define MAX_NUMIF 256
124 extern kstat_ctl_t *kc;
125 static kstat_t *ksp[MAX_NUMIF];
126 static int numif = 0;
127 #endif /* HAVE_LIBKSTAT */
129 static int traffic_config (char *key, char *value)
133 if (strcasecmp (key, "Interface") == 0)
135 temp = (char **) realloc (if_list, (if_list_num + 1) * sizeof (char *));
138 syslog (LOG_EMERG, "Cannot allocate more memory.");
143 if ((if_list[if_list_num] = strdup (value)) == NULL)
145 syslog (LOG_EMERG, "Cannot allocate memory.");
150 else if (strcasecmp (key, "IgnoreSelected") == 0)
152 if ((strcasecmp (value, "True") == 0)
153 || (strcasecmp (value, "Yes") == 0)
154 || (strcasecmp (value, "On") == 0))
167 static void traffic_init (void)
171 /* #endif HAVE_GETIFADDRS */
175 /* #endif KERNEL_LINUX */
179 unsigned long long val;
186 for (numif = 0, ksp_chain = kc->kc_chain;
187 (numif < MAX_NUMIF) && (ksp_chain != NULL);
188 ksp_chain = ksp_chain->ks_next)
190 if (strncmp (ksp_chain->ks_class, "net", 3))
192 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
194 if (kstat_read (kc, ksp_chain, NULL) == -1)
196 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
198 ksp[numif++] = ksp_chain;
200 /* #endif HAVE_LIBKSTAT */
204 #endif /* HAVE_LIBSTATG */
210 * Check if this interface/instance should be ignored. This is called from
211 * both, `submit' and `write' to give client and server the ability to
212 * ignore certain stuff..
214 static int check_ignore_if (const char *interface)
218 /* If no interfaces are given collect all interfaces. Mostly to be
219 * backwards compatible, but also because this is much easier. */
223 for (i = 0; i < if_list_num; i++)
224 if (strcasecmp (interface, if_list[i]) == 0)
225 return (if_list_action);
226 return (1 - if_list_action);
229 static void generic_write (char *host, char *inst, char *val,
231 char **ds_def, int ds_num)
236 if (check_ignore_if (inst))
239 status = snprintf (file, BUFSIZE, file_template, inst);
242 else if (status >= 512)
245 rrd_update_file (host, file, val, ds_def, ds_num);
248 static void bytes_write (char *host, char *inst, char *val)
250 generic_write (host, inst, val, bytes_file, bytes_ds_def, bytes_ds_num);
253 static void packets_write (char *host, char *inst, char *val)
255 generic_write (host, inst, val, packets_file, packets_ds_def, packets_ds_num);
258 static void errors_write (char *host, char *inst, char *val)
260 generic_write (host, inst, val, errors_file, errors_ds_def, errors_ds_num);
263 #if TRAFFIC_HAVE_READ
264 static void bytes_submit (char *dev,
265 unsigned long long rx,
266 unsigned long long tx)
271 if (check_ignore_if (dev))
274 status = snprintf (buf, 512, "%u:%lld:%lld",
275 (unsigned int) curtime,
277 if ((status >= 512) || (status < 1))
280 plugin_submit (MODULE_NAME, dev, buf);
283 #if HAVE_GETIFADDRS || KERNEL_LINUX || HAVE_LIBKSTAT
284 static void packets_submit (char *dev,
285 unsigned long long rx,
286 unsigned long long tx)
291 if (check_ignore_if (dev))
294 status = snprintf (buf, 512, "%u:%lld:%lld",
295 (unsigned int) curtime,
297 if ((status >= 512) || (status < 1))
299 plugin_submit ("if_packets", dev, buf);
302 static void errors_submit (char *dev,
303 unsigned long long rx,
304 unsigned long long tx)
309 if (check_ignore_if (dev))
312 status = snprintf (buf, 512, "%u:%lld:%lld",
313 (unsigned int) curtime,
315 if ((status >= 512) || (status < 1))
317 plugin_submit ("if_errors", dev, buf);
319 #endif /* HAVE_GETIFADDRS || KERNEL_LINUX || HAVE_LIBKSTAT */
321 static void traffic_read (void)
324 struct ifaddrs *if_list;
325 struct ifaddrs *if_ptr;
327 /* Darin/Mac OS X and possible other *BSDs */
328 #if HAVE_STRUCT_IF_DATA
329 # define IFA_DATA if_data
330 # define IFA_RX_BYTES ifi_ibytes
331 # define IFA_TX_BYTES ifi_obytes
332 # define IFA_RX_PACKT ifi_ipackets
333 # define IFA_TX_PACKT ifi_opackets
334 # define IFA_RX_ERROR ifi_ierrors
335 # define IFA_TX_ERROR ifi_oerrors
336 /* #endif HAVE_STRUCT_IF_DATA */
338 #elif HAVE_STRUCT_NET_DEVICE_STATS
339 # define IFA_DATA net_device_stats
340 # define IFA_RX_BYTES rx_bytes
341 # define IFA_TX_BYTES tx_bytes
342 # define IFA_RX_PACKT rx_packets
343 # define IFA_TX_PACKT tx_packets
344 # define IFA_RX_ERROR rx_errors
345 # define IFA_TX_ERROR tx_errors
347 # error "No suitable type for `struct ifaddrs->ifa_data' found."
350 struct IFA_DATA *if_data;
352 if (getifaddrs (&if_list) != 0)
355 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
357 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
360 bytes_submit (if_ptr->ifa_name,
361 if_data->IFA_RX_BYTES,
362 if_data->IFA_TX_BYTES);
363 packets_submit (if_ptr->ifa_name,
364 if_data->IFA_RX_PACKT,
365 if_data->IFA_TX_PACKT);
366 errors_submit (if_ptr->ifa_name,
367 if_data->IFA_RX_ERROR,
368 if_data->IFA_TX_ERROR);
371 freeifaddrs (if_list);
372 /* #endif HAVE_GETIFADDRS */
377 unsigned long long incoming, outgoing;
384 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
386 syslog (LOG_WARNING, "traffic: fopen: %s", strerror (errno));
390 while (fgets (buffer, 1024, fh) != NULL)
392 if (!(dummy = strchr(buffer, ':')))
398 while (device[0] == ' ')
401 if (device[0] == '\0')
404 numfields = strsplit (dummy, fields, 16);
409 incoming = atoll (fields[0]);
410 outgoing = atoll (fields[8]);
411 bytes_submit (device, incoming, outgoing);
413 incoming = atoll (fields[1]);
414 outgoing = atoll (fields[9]);
415 packets_submit (device, incoming, outgoing);
417 incoming = atoll (fields[2]);
418 outgoing = atoll (fields[10]);
419 errors_submit (device, incoming, outgoing);
423 /* #endif KERNEL_LINUX */
427 unsigned long long rx;
428 unsigned long long tx;
433 for (i = 0; i < numif; i++)
435 if (kstat_read (kc, ksp[i], NULL) == -1)
438 rx = get_kstat_value (ksp[i], "rbytes");
439 tx = get_kstat_value (ksp[i], "obytes");
440 if ((rx != -1LL) || (tx != -1LL))
441 bytes_submit (ksp[i]->ks_name, rx, tx);
443 rx = get_kstat_value (ksp[i], "ipackets");
444 tx = get_kstat_value (ksp[i], "opackets");
445 if ((rx != -1LL) || (tx != -1LL))
446 packets_submit (ksp[i]->ks_name, rx, tx);
448 rx = get_kstat_value (ksp[i], "ierrors");
449 tx = get_kstat_value (ksp[i], "oerrors");
450 if ((rx != -1LL) || (tx != -1LL))
451 errors_submit (ksp[i]->ks_name, rx, tx);
453 /* #endif HAVE_LIBKSTAT */
455 #elif defined(HAVE_LIBSTATGRAB)
456 sg_network_io_stats *ios;
459 ios = sg_get_network_io_stats (&num);
461 for (i = 0; i < num; i++)
462 bytes_submit (ios[i].interface_name, ios[i].rx, ios[i].tx);
463 #endif /* HAVE_LIBSTATGRAB */
466 #define traffic_read NULL
467 #endif /* TRAFFIC_HAVE_READ */
469 void module_register (void)
471 plugin_register (MODULE_NAME, traffic_init, traffic_read, bytes_write);
472 plugin_register ("if_packets", NULL, NULL, packets_write);
473 plugin_register ("if_errors", NULL, NULL, errors_write);
474 cf_register (MODULE_NAME, traffic_config, config_keys, config_keys_num);