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 /* 2^32 = 4294967296 = ~4.2GByte/s = ~34GBit/s */
76 static data_source_t octets_dsrc[2] =
78 {"rx", DS_TYPE_COUNTER, 0, 4294967295.0},
79 {"tx", DS_TYPE_COUNTER, 0, 4294967295.0}
82 static data_set_t octets_ds =
84 "if_octets", 2, octets_dsrc
87 static data_source_t packets_dsrc[2] =
89 {"rx", DS_TYPE_COUNTER, 0, 4294967295.0},
90 {"tx", DS_TYPE_COUNTER, 0, 4294967295.0}
93 static data_set_t packets_ds =
95 "if_packets", 2, packets_dsrc
98 static data_source_t errors_dsrc[2] =
100 {"rx", DS_TYPE_COUNTER, 0, 4294967295.0},
101 {"tx", DS_TYPE_COUNTER, 0, 4294967295.0}
104 static data_set_t errors_ds =
106 "if_errors", 2, errors_dsrc
109 static const char *config_keys[] =
115 static int config_keys_num = 2;
117 static char **if_list = NULL;
118 static int if_list_num = 0;
121 * 0 => default is to collect selected interface
122 * 1 => ignore selcted interfaces
124 static int if_list_action = 0;
127 #define MAX_NUMIF 256
128 extern kstat_ctl_t *kc;
129 static kstat_t *ksp[MAX_NUMIF];
130 static int numif = 0;
131 #endif /* HAVE_LIBKSTAT */
133 static int interface_config (const char *key, const char *value)
137 if (strcasecmp (key, "Interface") == 0)
139 temp = (char **) realloc (if_list, (if_list_num + 1) * sizeof (char *));
142 syslog (LOG_EMERG, "Cannot allocate more memory.");
147 if ((if_list[if_list_num] = strdup (value)) == NULL)
149 syslog (LOG_EMERG, "Cannot allocate memory.");
154 else if (strcasecmp (key, "IgnoreSelected") == 0)
156 if ((strcasecmp (value, "True") == 0)
157 || (strcasecmp (value, "Yes") == 0)
158 || (strcasecmp (value, "On") == 0))
172 static int traffic_init (void)
176 unsigned long long val;
183 for (numif = 0, ksp_chain = kc->kc_chain;
184 (numif < MAX_NUMIF) && (ksp_chain != NULL);
185 ksp_chain = ksp_chain->ks_next)
187 if (strncmp (ksp_chain->ks_class, "net", 3))
189 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
191 if (kstat_read (kc, ksp_chain, NULL) == -1)
193 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
195 ksp[numif++] = ksp_chain;
197 #endif /* HAVE_LIBKSTAT */
200 } /* int traffic_init */
201 #endif /* HAVE_LIBKSTAT */
204 * Check if this interface/instance should be ignored. This is called from
205 * both, `submit' and `write' to give client and server the ability to
206 * ignore certain stuff..
208 static int check_ignore_if (const char *interface)
212 /* If no interfaces are given collect all interfaces. Mostly to be
213 * backwards compatible, but also because this is much easier. */
217 for (i = 0; i < if_list_num; i++)
218 if (strcasecmp (interface, if_list[i]) == 0)
219 return (if_list_action);
220 return (1 - if_list_action);
221 } /* int check_ignore_if */
223 #if TRAFFIC_HAVE_READ
224 static void if_submit (const char *dev, const char *type,
225 unsigned long long rx,
226 unsigned long long tx)
229 value_list_t vl = VALUE_LIST_INIT;
231 if (check_ignore_if (dev))
234 values[0].counter = rx;
235 values[1].counter = tx;
239 vl.time = time (NULL);
241 /* FIXME: do this globally */
242 if (gethostname (vl.host, sizeof (vl.host)) != 0)
244 syslog (LOG_ERR, "load plugin: gethostname failed: %s",
248 strcpy (vl.plugin, "interface");
249 strcpy (vl.plugin_instance, "");
250 strncpy (vl.type_instance, dev, sizeof (vl.type_instance));
252 plugin_dispatch_values (type, &vl);
253 } /* void if_submit */
255 static int traffic_read (void)
258 struct ifaddrs *if_list;
259 struct ifaddrs *if_ptr;
261 /* Darin/Mac OS X and possible other *BSDs */
262 #if HAVE_STRUCT_IF_DATA
263 # define IFA_DATA if_data
264 # define IFA_RX_BYTES ifi_ibytes
265 # define IFA_TX_BYTES ifi_obytes
266 # define IFA_RX_PACKT ifi_ipackets
267 # define IFA_TX_PACKT ifi_opackets
268 # define IFA_RX_ERROR ifi_ierrors
269 # define IFA_TX_ERROR ifi_oerrors
270 /* #endif HAVE_STRUCT_IF_DATA */
272 #elif HAVE_STRUCT_NET_DEVICE_STATS
273 # define IFA_DATA net_device_stats
274 # define IFA_RX_BYTES rx_bytes
275 # define IFA_TX_BYTES tx_bytes
276 # define IFA_RX_PACKT rx_packets
277 # define IFA_TX_PACKT tx_packets
278 # define IFA_RX_ERROR rx_errors
279 # define IFA_TX_ERROR tx_errors
281 # error "No suitable type for `struct ifaddrs->ifa_data' found."
284 struct IFA_DATA *if_data;
286 if (getifaddrs (&if_list) != 0)
289 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
291 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
294 if_submit (if_ptr->ifa_name, "if_octets",
295 if_data->IFA_RX_BYTES,
296 if_data->IFA_TX_BYTES);
297 if_submit (if_ptr->ifa_name, "if_packets",
298 if_data->IFA_RX_PACKT,
299 if_data->IFA_TX_PACKT);
300 if_submit (if_ptr->ifa_name, "if_errors",
301 if_data->IFA_RX_ERROR,
302 if_data->IFA_TX_ERROR);
305 freeifaddrs (if_list);
306 /* #endif HAVE_GETIFADDRS */
311 unsigned long long incoming, outgoing;
318 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
320 syslog (LOG_WARNING, "traffic: fopen: %s", strerror (errno));
324 while (fgets (buffer, 1024, fh) != NULL)
326 if (!(dummy = strchr(buffer, ':')))
332 while (device[0] == ' ')
335 if (device[0] == '\0')
338 numfields = strsplit (dummy, fields, 16);
343 incoming = atoll (fields[0]);
344 outgoing = atoll (fields[8]);
345 if_submit (device, "if_octets", incoming, outgoing);
347 incoming = atoll (fields[1]);
348 outgoing = atoll (fields[9]);
349 if_submit (device, "if_packets", incoming, outgoing);
351 incoming = atoll (fields[2]);
352 outgoing = atoll (fields[10]);
353 if_submit (device, "if_errors", incoming, outgoing);
357 /* #endif KERNEL_LINUX */
361 unsigned long long rx;
362 unsigned long long tx;
367 for (i = 0; i < numif; i++)
369 if (kstat_read (kc, ksp[i], NULL) == -1)
372 rx = get_kstat_value (ksp[i], "rbytes");
373 tx = get_kstat_value (ksp[i], "obytes");
374 if ((rx != -1LL) || (tx != -1LL))
375 if_submit (ksp[i]->ks_name, "if_octets", rx, tx);
377 rx = get_kstat_value (ksp[i], "ipackets");
378 tx = get_kstat_value (ksp[i], "opackets");
379 if ((rx != -1LL) || (tx != -1LL))
380 if_submit (ksp[i]->ks_name, "if_packets", rx, tx);
382 rx = get_kstat_value (ksp[i], "ierrors");
383 tx = get_kstat_value (ksp[i], "oerrors");
384 if ((rx != -1LL) || (tx != -1LL))
385 if_submit (ksp[i]->ks_name, "if_errors", rx, tx);
387 /* #endif HAVE_LIBKSTAT */
389 #elif defined(HAVE_LIBSTATGRAB)
390 sg_network_io_stats *ios;
393 ios = sg_get_network_io_stats (&num);
395 for (i = 0; i < num; i++)
396 if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
397 #endif /* HAVE_LIBSTATGRAB */
400 } /* int traffic_read */
401 #endif /* TRAFFIC_HAVE_READ */
403 void module_register (void)
405 plugin_register_data_set (&octets_ds);
406 plugin_register_data_set (&packets_ds);
407 plugin_register_data_set (&errors_ds);
409 plugin_register_config ("interface", interface_config,
410 config_keys, config_keys_num);
413 plugin_register_init ("interface", traffic_init);
416 #if TRAFFIC_HAVE_READ
417 plugin_register_read ("interface", traffic_read);