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"
52 #if HAVE_GETIFADDRS || KERNEL_LINUX || HAVE_LIBKSTAT || HAVE_LIBSTATGRAB
53 # define TRAFFIC_HAVE_READ 1
55 # define TRAFFIC_HAVE_READ 0
61 * (Module-)Global variables
63 /* TODO: Move this to `interface-%s/<blah>.rrd' in version 4. */
64 static char *bytes_file = "traffic-%s.rrd";
65 static char *packets_file = "if_packets-%s.rrd";
66 static char *errors_file = "if_errors-%s.rrd";
67 /* TODO: Maybe implement multicast and broadcast counters */
69 static char *config_keys[] =
74 static int config_keys_num = 1;
76 static char *bytes_ds_def[] =
78 "DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U",
79 "DS:outgoing:COUNTER:"COLLECTD_HEARTBEAT":0:U",
82 static int bytes_ds_num = 2;
84 static char *packets_ds_def[] =
86 "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
87 "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
90 static int packets_ds_num = 2;
92 static char *errors_ds_def[] =
94 "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
95 "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
98 static int errors_ds_num = 2;
100 static char **if_ignore_list = NULL;
101 static int if_ignore_list_num = 0;
104 #define MAX_NUMIF 256
105 extern kstat_ctl_t *kc;
106 static kstat_t *ksp[MAX_NUMIF];
107 static int numif = 0;
108 #endif /* HAVE_LIBKSTAT */
110 static int traffic_config (char *key, char *value)
114 if (strcasecmp (key, "Ignore") != 0)
117 temp = (char **) realloc (if_ignore_list, (if_ignore_list_num + 1) * sizeof (char *));
120 syslog (LOG_EMERG, "Cannot allocate more memory.");
123 if_ignore_list = temp;
125 if ((if_ignore_list[if_ignore_list_num] = strdup (value)) == NULL)
127 syslog (LOG_EMERG, "Cannot allocate memory.");
130 if_ignore_list_num++;
132 syslog (LOG_NOTICE, "traffic: Ignoring interface `%s'", value);
137 static void traffic_init (void)
141 /* #endif HAVE_GETIFADDRS */
145 /* #endif KERNEL_LINUX */
149 unsigned long long val;
156 for (numif = 0, ksp_chain = kc->kc_chain;
157 (numif < MAX_NUMIF) && (ksp_chain != NULL);
158 ksp_chain = ksp_chain->ks_next)
160 if (strncmp (ksp_chain->ks_class, "net", 3))
162 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
164 if (kstat_read (kc, ksp_chain, NULL) == -1)
166 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
168 ksp[numif++] = ksp_chain;
170 /* #endif HAVE_LIBKSTAT */
174 #endif /* HAVE_LIBSTATG */
180 * Check if this interface/instance should be ignored. This is called from
181 * both, `submit' and `write' to give client and server the ability to ignore
184 static int check_ignore_if (const char *interface)
188 for (i = 0; i < if_ignore_list_num; i++)
189 if (strcasecmp (interface, if_ignore_list[i]) == 0)
194 static void generic_write (char *host, char *inst, char *val,
196 char **ds_def, int ds_num)
201 if (check_ignore_if (inst))
204 status = snprintf (file, BUFSIZE, file_template, inst);
207 else if (status >= 512)
210 rrd_update_file (host, file, val, ds_def, ds_num);
213 static void bytes_write (char *host, char *inst, char *val)
215 generic_write (host, inst, val, bytes_file, bytes_ds_def, bytes_ds_num);
218 static void packets_write (char *host, char *inst, char *val)
220 generic_write (host, inst, val, packets_file, packets_ds_def, packets_ds_num);
223 static void errors_write (char *host, char *inst, char *val)
225 generic_write (host, inst, val, errors_file, errors_ds_def, errors_ds_num);
228 #if TRAFFIC_HAVE_READ
229 static void bytes_submit (char *dev,
230 unsigned long long rx,
231 unsigned long long tx)
236 if (check_ignore_if (dev))
239 status = snprintf (buf, 512, "%u:%lld:%lld",
240 (unsigned int) curtime,
242 if ((status >= 512) || (status < 1))
245 plugin_submit (MODULE_NAME, dev, buf);
248 #if HAVE_GETIFADDRS || HAVE_LIBKSTAT
249 static void packets_submit (char *dev,
250 unsigned long long rx,
251 unsigned long long tx)
256 if (check_ignore_if (dev))
259 status = snprintf (buf, 512, "%u:%lld:%lld",
260 (unsigned int) curtime,
262 if ((status >= 512) || (status < 1))
264 plugin_submit ("if_packets", dev, buf);
267 static void errors_submit (char *dev,
268 unsigned long long rx,
269 unsigned long long tx)
274 if (check_ignore_if (dev))
277 status = snprintf (buf, 512, "%u:%lld:%lld",
278 (unsigned int) curtime,
280 if ((status >= 512) || (status < 1))
282 plugin_submit ("if_errors", dev, buf);
284 #endif /* HAVE_GETIFADDRS || HAVE_LIBKSTAT */
286 static void traffic_read (void)
289 struct ifaddrs *if_list;
290 struct ifaddrs *if_ptr;
292 /* Darin/Mac OS X and possible other *BSDs */
293 #if HAVE_STRUCT_IF_DATA
294 # define IFA_DATA if_data
295 # define IFA_RX_BYTES ifi_ibytes
296 # define IFA_TX_BYTES ifi_obytes
297 # define IFA_RX_PACKT ifi_ipackets
298 # define IFA_TX_PACKT ifi_opackets
299 # define IFA_RX_ERROR ifi_ierrors
300 # define IFA_TX_ERROR ifi_oerrors
301 /* #endif HAVE_STRUCT_IF_DATA */
303 #elif HAVE_STRUCT_NET_DEVICE_STATS
304 # define IFA_DATA net_device_stats
305 # define IFA_RX_BYTES rx_bytes
306 # define IFA_TX_BYTES tx_bytes
307 # define IFA_RX_PACKT rx_packets
308 # define IFA_TX_PACKT tx_packets
309 # define IFA_RX_ERROR rx_errors
310 # define IFA_TX_ERROR tx_errors
312 # error "No suitable type for `struct ifaddrs->ifa_data' found."
315 struct IFA_DATA *if_data;
317 if (getifaddrs (&if_list) != 0)
320 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
322 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
325 bytes_submit (if_ptr->ifa_name,
326 if_data->IFA_RX_BYTES,
327 if_data->IFA_TX_BYTES);
328 packets_submit (if_ptr->ifa_name,
329 if_data->IFA_RX_PACKT,
330 if_data->IFA_TX_PACKT);
331 errors_submit (if_ptr->ifa_name,
332 if_data->IFA_RX_ERROR,
333 if_data->IFA_TX_ERROR);
336 freeifaddrs (if_list);
337 /* #endif HAVE_GETIFADDRS */
342 unsigned long long incoming, outgoing;
349 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
351 syslog (LOG_WARNING, "traffic: fopen: %s", strerror (errno));
355 while (fgets (buffer, 1024, fh) != NULL)
357 if (!(dummy = strchr(buffer, ':')))
363 while (device[0] == ' ')
366 if (device[0] == '\0')
369 numfields = strsplit (dummy, fields, 16);
374 incoming = atoll (fields[0]);
375 outgoing = atoll (fields[8]);
377 bytes_submit (device, incoming, outgoing);
381 /* #endif KERNEL_LINUX */
385 unsigned long long rx;
386 unsigned long long tx;
391 for (i = 0; i < numif; i++)
393 if (kstat_read (kc, ksp[i], NULL) == -1)
396 rx = get_kstat_value (ksp[i], "rbytes");
397 tx = get_kstat_value (ksp[i], "obytes");
398 if ((rx != -1LL) || (tx != -1LL))
399 bytes_submit (ksp[i]->ks_name, rx, tx);
401 rx = get_kstat_value (ksp[i], "ipackets");
402 tx = get_kstat_value (ksp[i], "opackets");
403 if ((rx != -1LL) || (tx != -1LL))
404 packets_submit (ksp[i]->ks_name, rx, tx);
406 rx = get_kstat_value (ksp[i], "ierrors");
407 tx = get_kstat_value (ksp[i], "oerrors");
408 if ((rx != -1LL) || (tx != -1LL))
409 errors_submit (ksp[i]->ks_name, rx, tx);
411 /* #endif HAVE_LIBKSTAT */
413 #elif defined(HAVE_LIBSTATGRAB)
414 sg_network_io_stats *ios;
417 ios = sg_get_network_io_stats (&num);
419 for (i = 0; i < num; i++)
420 bytes_submit (ios[i].interface_name, ios[i].rx, ios[i].tx);
421 #endif /* HAVE_LIBSTATGRAB */
424 #define traffic_read NULL
425 #endif /* TRAFFIC_HAVE_READ */
427 void module_register (void)
429 plugin_register (MODULE_NAME, traffic_init, traffic_read, bytes_write);
430 plugin_register ("if_packets", NULL, NULL, packets_write);
431 plugin_register ("if_errors", NULL, NULL, errors_write);
432 cf_register (MODULE_NAME, traffic_config, config_keys, config_keys_num);