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 = "if_packets-%s.rrd";
78 static char *errors_file = "if_errors-%s.rrd";
79 /* TODO: Maybe implement multicast and broadcast counters */
81 static char *config_keys[] =
86 static int config_keys_num = 1;
88 static char *bytes_ds_def[] =
90 "DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U",
91 "DS:outgoing:COUNTER:"COLLECTD_HEARTBEAT":0:U",
94 static int bytes_ds_num = 2;
96 static char *packets_ds_def[] =
98 "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
99 "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
102 static int packets_ds_num = 2;
104 static char *errors_ds_def[] =
106 "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
107 "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
110 static int errors_ds_num = 2;
112 static char **if_ignore_list = NULL;
113 static int if_ignore_list_num = 0;
116 #define MAX_NUMIF 256
117 extern kstat_ctl_t *kc;
118 static kstat_t *ksp[MAX_NUMIF];
119 static int numif = 0;
120 #endif /* HAVE_LIBKSTAT */
122 static int traffic_config (char *key, char *value)
126 if (strcasecmp (key, "Ignore") != 0)
129 temp = (char **) realloc (if_ignore_list, (if_ignore_list_num + 1) * sizeof (char *));
132 syslog (LOG_EMERG, "Cannot allocate more memory.");
135 if_ignore_list = temp;
137 if ((if_ignore_list[if_ignore_list_num] = strdup (value)) == NULL)
139 syslog (LOG_EMERG, "Cannot allocate memory.");
142 if_ignore_list_num++;
144 syslog (LOG_NOTICE, "traffic: Ignoring interface `%s'", value);
149 static void traffic_init (void)
153 /* #endif HAVE_GETIFADDRS */
157 /* #endif KERNEL_LINUX */
161 unsigned long long val;
168 for (numif = 0, ksp_chain = kc->kc_chain;
169 (numif < MAX_NUMIF) && (ksp_chain != NULL);
170 ksp_chain = ksp_chain->ks_next)
172 if (strncmp (ksp_chain->ks_class, "net", 3))
174 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
176 if (kstat_read (kc, ksp_chain, NULL) == -1)
178 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
180 ksp[numif++] = ksp_chain;
182 /* #endif HAVE_LIBKSTAT */
186 #endif /* HAVE_LIBSTATG */
192 * Check if this interface/instance should be ignored. This is called from
193 * both, `submit' and `write' to give client and server the ability to ignore
196 static int check_ignore_if (const char *interface)
200 for (i = 0; i < if_ignore_list_num; i++)
201 if (strcasecmp (interface, if_ignore_list[i]) == 0)
206 static void generic_write (char *host, char *inst, char *val,
208 char **ds_def, int ds_num)
213 if (check_ignore_if (inst))
216 status = snprintf (file, BUFSIZE, file_template, inst);
219 else if (status >= 512)
222 rrd_update_file (host, file, val, ds_def, ds_num);
225 static void bytes_write (char *host, char *inst, char *val)
227 generic_write (host, inst, val, bytes_file, bytes_ds_def, bytes_ds_num);
230 static void packets_write (char *host, char *inst, char *val)
232 generic_write (host, inst, val, packets_file, packets_ds_def, packets_ds_num);
235 static void errors_write (char *host, char *inst, char *val)
237 generic_write (host, inst, val, errors_file, errors_ds_def, errors_ds_num);
240 #if TRAFFIC_HAVE_READ
241 static void bytes_submit (char *dev,
242 unsigned long long rx,
243 unsigned long long tx)
248 if (check_ignore_if (dev))
251 status = snprintf (buf, 512, "%u:%lld:%lld",
252 (unsigned int) curtime,
254 if ((status >= 512) || (status < 1))
257 plugin_submit (MODULE_NAME, dev, buf);
260 #if HAVE_GETIFADDRS || KERNEL_LINUX || HAVE_LIBKSTAT
261 static void packets_submit (char *dev,
262 unsigned long long rx,
263 unsigned long long tx)
268 if (check_ignore_if (dev))
271 status = snprintf (buf, 512, "%u:%lld:%lld",
272 (unsigned int) curtime,
274 if ((status >= 512) || (status < 1))
276 plugin_submit ("if_packets", dev, buf);
279 static void errors_submit (char *dev,
280 unsigned long long rx,
281 unsigned long long tx)
286 if (check_ignore_if (dev))
289 status = snprintf (buf, 512, "%u:%lld:%lld",
290 (unsigned int) curtime,
292 if ((status >= 512) || (status < 1))
294 plugin_submit ("if_errors", dev, buf);
296 #endif /* HAVE_GETIFADDRS || KERNEL_LINUX || HAVE_LIBKSTAT */
298 static void traffic_read (void)
301 struct ifaddrs *if_list;
302 struct ifaddrs *if_ptr;
304 /* Darin/Mac OS X and possible other *BSDs */
305 #if HAVE_STRUCT_IF_DATA
306 # define IFA_DATA if_data
307 # define IFA_RX_BYTES ifi_ibytes
308 # define IFA_TX_BYTES ifi_obytes
309 # define IFA_RX_PACKT ifi_ipackets
310 # define IFA_TX_PACKT ifi_opackets
311 # define IFA_RX_ERROR ifi_ierrors
312 # define IFA_TX_ERROR ifi_oerrors
313 /* #endif HAVE_STRUCT_IF_DATA */
315 #elif HAVE_STRUCT_NET_DEVICE_STATS
316 # define IFA_DATA net_device_stats
317 # define IFA_RX_BYTES rx_bytes
318 # define IFA_TX_BYTES tx_bytes
319 # define IFA_RX_PACKT rx_packets
320 # define IFA_TX_PACKT tx_packets
321 # define IFA_RX_ERROR rx_errors
322 # define IFA_TX_ERROR tx_errors
324 # error "No suitable type for `struct ifaddrs->ifa_data' found."
327 struct IFA_DATA *if_data;
329 if (getifaddrs (&if_list) != 0)
332 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
334 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
337 bytes_submit (if_ptr->ifa_name,
338 if_data->IFA_RX_BYTES,
339 if_data->IFA_TX_BYTES);
340 packets_submit (if_ptr->ifa_name,
341 if_data->IFA_RX_PACKT,
342 if_data->IFA_TX_PACKT);
343 errors_submit (if_ptr->ifa_name,
344 if_data->IFA_RX_ERROR,
345 if_data->IFA_TX_ERROR);
348 freeifaddrs (if_list);
349 /* #endif HAVE_GETIFADDRS */
354 unsigned long long incoming, outgoing;
361 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
363 syslog (LOG_WARNING, "traffic: fopen: %s", strerror (errno));
367 while (fgets (buffer, 1024, fh) != NULL)
369 if (!(dummy = strchr(buffer, ':')))
375 while (device[0] == ' ')
378 if (device[0] == '\0')
381 numfields = strsplit (dummy, fields, 16);
386 incoming = atoll (fields[0]);
387 outgoing = atoll (fields[8]);
388 bytes_submit (device, incoming, outgoing);
390 incoming = atoll (fields[1]);
391 outgoing = atoll (fields[9]);
392 packets_submit (device, incoming, outgoing);
394 incoming = atoll (fields[2]);
395 outgoing = atoll (fields[10]);
396 errors_submit (device, incoming, outgoing);
400 /* #endif KERNEL_LINUX */
404 unsigned long long rx;
405 unsigned long long tx;
410 for (i = 0; i < numif; i++)
412 if (kstat_read (kc, ksp[i], NULL) == -1)
415 rx = get_kstat_value (ksp[i], "rbytes");
416 tx = get_kstat_value (ksp[i], "obytes");
417 if ((rx != -1LL) || (tx != -1LL))
418 bytes_submit (ksp[i]->ks_name, rx, tx);
420 rx = get_kstat_value (ksp[i], "ipackets");
421 tx = get_kstat_value (ksp[i], "opackets");
422 if ((rx != -1LL) || (tx != -1LL))
423 packets_submit (ksp[i]->ks_name, rx, tx);
425 rx = get_kstat_value (ksp[i], "ierrors");
426 tx = get_kstat_value (ksp[i], "oerrors");
427 if ((rx != -1LL) || (tx != -1LL))
428 errors_submit (ksp[i]->ks_name, rx, tx);
430 /* #endif HAVE_LIBKSTAT */
432 #elif defined(HAVE_LIBSTATGRAB)
433 sg_network_io_stats *ios;
436 ios = sg_get_network_io_stats (&num);
438 for (i = 0; i < num; i++)
439 bytes_submit (ios[i].interface_name, ios[i].rx, ios[i].tx);
440 #endif /* HAVE_LIBSTATGRAB */
443 #define traffic_read NULL
444 #endif /* TRAFFIC_HAVE_READ */
446 void module_register (void)
448 plugin_register (MODULE_NAME, traffic_init, traffic_read, bytes_write);
449 plugin_register ("if_packets", NULL, NULL, packets_write);
450 plugin_register ("if_errors", NULL, NULL, errors_write);
451 cf_register (MODULE_NAME, traffic_config, config_keys, config_keys_num);