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>
28 # include <sys/types.h>
31 # include <sys/socket.h>
34 /* One cannot include both. This sucks. */
36 # include <linux/if.h>
41 #if HAVE_LINUX_NETDEVICE_H
42 # include <linux/netdevice.h>
48 #define MODULE_NAME "traffic"
50 #if HAVE_GETIFADDRS || KERNEL_LINUX || HAVE_LIBKSTAT || HAVE_LIBSTATGRAB
51 # define TRAFFIC_HAVE_READ 1
53 # define TRAFFIC_HAVE_READ 0
58 /* TODO: Move this to `interface-%s/<blah>.rrd' in version 4. */
59 static char *bytes_file = "traffic-%s.rrd";
60 static char *packets_file = "if_packets-%s.rrd";
61 static char *errors_file = "if_errors-%s.rrd";
63 static char *bytes_ds_def[] =
65 "DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U",
66 "DS:outgoing:COUNTER:"COLLECTD_HEARTBEAT":0:U",
69 static int bytes_ds_num = 2;
71 static char *packets_ds_def[] =
73 "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
74 "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
77 static int packets_ds_num = 2;
79 static char *errors_ds_def[] =
81 "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
82 "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
85 static int errors_ds_num = 2;
89 extern kstat_ctl_t *kc;
90 static kstat_t *ksp[MAX_NUMIF];
92 #endif /* HAVE_LIBKSTAT */
94 static void traffic_init (void)
98 /* #endif HAVE_GETIFADDRS */
102 /* #endif KERNEL_LINUX */
106 unsigned long long val;
113 for (numif = 0, ksp_chain = kc->kc_chain;
114 (numif < MAX_NUMIF) && (ksp_chain != NULL);
115 ksp_chain = ksp_chain->ks_next)
117 if (strncmp (ksp_chain->ks_class, "net", 3))
119 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
121 if (kstat_read (kc, ksp_chain, NULL) == -1)
123 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
125 ksp[numif++] = ksp_chain;
127 /* #endif HAVE_LIBKSTAT */
131 #endif /* HAVE_LIBSTATG */
136 static void generic_write (char *host, char *inst, char *val,
138 char **ds_def, int ds_num)
143 status = snprintf (file, BUFSIZE, file_template, inst);
146 else if (status >= 512)
149 rrd_update_file (host, file, val, ds_def, ds_num);
152 static void bytes_write (char *host, char *inst, char *val)
154 generic_write (host, inst, val, bytes_file, bytes_ds_def, bytes_ds_num);
157 static void packets_write (char *host, char *inst, char *val)
159 generic_write (host, inst, val, packets_file, packets_ds_def, packets_ds_num);
162 static void errors_write (char *host, char *inst, char *val)
164 generic_write (host, inst, val, errors_file, errors_ds_def, errors_ds_num);
167 #if TRAFFIC_HAVE_READ
168 static void bytes_submit (char *device,
169 unsigned long long incoming,
170 unsigned long long outgoing)
174 if (snprintf (buf, BUFSIZE, "%u:%lld:%lld", (unsigned int) curtime, incoming, outgoing) >= BUFSIZE)
177 plugin_submit (MODULE_NAME, device, buf);
181 static void packets_submit (char *dev,
182 unsigned long long rx,
183 unsigned long long tx)
188 status = snprintf (buf, 512, "%u:%lld:%lld",
189 (unsigned int) curtime,
191 if ((status >= 512) || (status < 1))
193 plugin_submit ("if_packets", dev, buf);
196 static void errors_submit (char *dev,
197 unsigned long long rx,
198 unsigned long long tx)
203 status = snprintf (buf, 512, "%u:%lld:%lld",
204 (unsigned int) curtime,
206 if ((status >= 512) || (status < 1))
208 plugin_submit ("if_errors", dev, buf);
210 #endif /* HAVE_GETIFADDRS */
212 static void traffic_read (void)
215 struct ifaddrs *if_list;
216 struct ifaddrs *if_ptr;
218 /* Darin/Mac OS X and possible other *BSDs */
219 #if HAVE_STRUCT_IF_DATA
220 # define IFA_DATA if_data
221 # define IFA_RX_BYTES ifi_ibytes
222 # define IFA_TX_BYTES ifi_obytes
223 # define IFA_RX_PACKT ifi_ipackets
224 # define IFA_TX_PACKT ifi_opackets
225 # define IFA_RX_ERROR ifi_ierrors
226 # define IFA_TX_ERROR ifi_oerrors
227 /* #endif HAVE_STRUCT_IF_DATA */
229 #elif HAVE_STRUCT_NET_DEVICE_STATS
230 # define IFA_DATA net_device_stats
231 # define IFA_RX_BYTES rx_bytes
232 # define IFA_TX_BYTES tx_bytes
233 # define IFA_RX_PACKT rx_packets
234 # define IFA_TX_PACKT tx_packets
235 # define IFA_RX_ERROR rx_errors
236 # define IFA_TX_ERROR tx_errors
238 # error "No suitable type for `struct ifaddrs->ifa_data' found."
241 struct IFA_DATA *if_data;
243 if (getifaddrs (&if_list) != 0)
246 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
248 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
251 bytes_submit (if_ptr->ifa_name,
252 if_data->IFA_RX_BYTES,
253 if_data->IFA_TX_BYTES);
254 packets_submit (if_ptr->ifa_name,
255 if_data->IFA_RX_PACKT,
256 if_data->IFA_TX_PACKT);
257 errors_submit (if_ptr->ifa_name,
258 if_data->IFA_RX_ERROR,
259 if_data->IFA_TX_ERROR);
262 freeifaddrs (if_list);
263 /* #endif HAVE_GETIFADDRS */
268 unsigned long long incoming, outgoing;
275 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
277 syslog (LOG_WARNING, "traffic: fopen: %s", strerror (errno));
281 while (fgets (buffer, 1024, fh) != NULL)
283 if (buffer[6] != ':')
288 while (device[0] == ' ')
291 if (device[0] == '\0')
295 numfields = strsplit (dummy, fields, 16);
300 incoming = atoll (fields[0]);
301 outgoing = atoll (fields[8]);
303 bytes_submit (device, incoming, outgoing);
307 /* #endif KERNEL_LINUX */
309 #elif defined(HAVE_LIBKSTAT)
311 unsigned long long incoming, outgoing;
316 for (i = 0; i < numif; i++)
318 if (kstat_read (kc, ksp[i], NULL) == -1)
321 if ((incoming = get_kstat_value (ksp[i], "rbytes")) == -1LL)
323 if ((outgoing = get_kstat_value (ksp[i], "obytes")) == -1LL)
326 bytes_submit (ksp[i]->ks_name, incoming, outgoing);
328 /* #endif HAVE_LIBKSTAT */
330 #elif defined(HAVE_LIBSTATGRAB)
331 sg_network_io_stats *ios;
334 ios = sg_get_network_io_stats (&num);
336 for (i = 0; i < num; i++)
337 bytes_submit (ios[i].interface_name, ios[i].rx, ios[i].tx);
338 #endif /* HAVE_LIBSTATGRAB */
341 #define traffic_read NULL
342 #endif /* TRAFFIC_HAVE_READ */
344 void module_register (void)
346 plugin_register (MODULE_NAME, traffic_init, traffic_read, bytes_write);
347 plugin_register ("if_packets", NULL, NULL, packets_write);
348 plugin_register ("if_errors", NULL, NULL, errors_write);