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";
62 /* TODO: Maybe implement multicast and broadcast counters */
64 static char *bytes_ds_def[] =
66 "DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U",
67 "DS:outgoing:COUNTER:"COLLECTD_HEARTBEAT":0:U",
70 static int bytes_ds_num = 2;
72 static char *packets_ds_def[] =
74 "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
75 "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
78 static int packets_ds_num = 2;
80 static char *errors_ds_def[] =
82 "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
83 "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
86 static int errors_ds_num = 2;
90 extern kstat_ctl_t *kc;
91 static kstat_t *ksp[MAX_NUMIF];
93 #endif /* HAVE_LIBKSTAT */
95 static void traffic_init (void)
99 /* #endif HAVE_GETIFADDRS */
103 /* #endif KERNEL_LINUX */
107 unsigned long long val;
114 for (numif = 0, ksp_chain = kc->kc_chain;
115 (numif < MAX_NUMIF) && (ksp_chain != NULL);
116 ksp_chain = ksp_chain->ks_next)
118 if (strncmp (ksp_chain->ks_class, "net", 3))
120 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
122 if (kstat_read (kc, ksp_chain, NULL) == -1)
124 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
126 ksp[numif++] = ksp_chain;
128 /* #endif HAVE_LIBKSTAT */
132 #endif /* HAVE_LIBSTATG */
137 static void generic_write (char *host, char *inst, char *val,
139 char **ds_def, int ds_num)
144 status = snprintf (file, BUFSIZE, file_template, inst);
147 else if (status >= 512)
150 rrd_update_file (host, file, val, ds_def, ds_num);
153 static void bytes_write (char *host, char *inst, char *val)
155 generic_write (host, inst, val, bytes_file, bytes_ds_def, bytes_ds_num);
158 static void packets_write (char *host, char *inst, char *val)
160 generic_write (host, inst, val, packets_file, packets_ds_def, packets_ds_num);
163 static void errors_write (char *host, char *inst, char *val)
165 generic_write (host, inst, val, errors_file, errors_ds_def, errors_ds_num);
168 #if TRAFFIC_HAVE_READ
169 static void bytes_submit (char *device,
170 unsigned long long incoming,
171 unsigned long long outgoing)
175 if (snprintf (buf, BUFSIZE, "%u:%lld:%lld", (unsigned int) curtime, incoming, outgoing) >= BUFSIZE)
178 plugin_submit (MODULE_NAME, device, buf);
181 #if HAVE_GETIFADDRS || HAVE_LIBKSTAT
182 static void packets_submit (char *dev,
183 unsigned long long rx,
184 unsigned long long tx)
189 status = snprintf (buf, 512, "%u:%lld:%lld",
190 (unsigned int) curtime,
192 if ((status >= 512) || (status < 1))
194 plugin_submit ("if_packets", dev, buf);
197 static void errors_submit (char *dev,
198 unsigned long long rx,
199 unsigned long long tx)
204 status = snprintf (buf, 512, "%u:%lld:%lld",
205 (unsigned int) curtime,
207 if ((status >= 512) || (status < 1))
209 plugin_submit ("if_errors", dev, buf);
211 #endif /* HAVE_GETIFADDRS || HAVE_LIBKSTAT */
213 static void traffic_read (void)
216 struct ifaddrs *if_list;
217 struct ifaddrs *if_ptr;
219 /* Darin/Mac OS X and possible other *BSDs */
220 #if HAVE_STRUCT_IF_DATA
221 # define IFA_DATA if_data
222 # define IFA_RX_BYTES ifi_ibytes
223 # define IFA_TX_BYTES ifi_obytes
224 # define IFA_RX_PACKT ifi_ipackets
225 # define IFA_TX_PACKT ifi_opackets
226 # define IFA_RX_ERROR ifi_ierrors
227 # define IFA_TX_ERROR ifi_oerrors
228 /* #endif HAVE_STRUCT_IF_DATA */
230 #elif HAVE_STRUCT_NET_DEVICE_STATS
231 # define IFA_DATA net_device_stats
232 # define IFA_RX_BYTES rx_bytes
233 # define IFA_TX_BYTES tx_bytes
234 # define IFA_RX_PACKT rx_packets
235 # define IFA_TX_PACKT tx_packets
236 # define IFA_RX_ERROR rx_errors
237 # define IFA_TX_ERROR tx_errors
239 # error "No suitable type for `struct ifaddrs->ifa_data' found."
242 struct IFA_DATA *if_data;
244 if (getifaddrs (&if_list) != 0)
247 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
249 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
252 bytes_submit (if_ptr->ifa_name,
253 if_data->IFA_RX_BYTES,
254 if_data->IFA_TX_BYTES);
255 packets_submit (if_ptr->ifa_name,
256 if_data->IFA_RX_PACKT,
257 if_data->IFA_TX_PACKT);
258 errors_submit (if_ptr->ifa_name,
259 if_data->IFA_RX_ERROR,
260 if_data->IFA_TX_ERROR);
263 freeifaddrs (if_list);
264 /* #endif HAVE_GETIFADDRS */
269 unsigned long long incoming, outgoing;
276 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
278 syslog (LOG_WARNING, "traffic: fopen: %s", strerror (errno));
282 while (fgets (buffer, 1024, fh) != NULL)
284 if (buffer[6] != ':')
289 while (device[0] == ' ')
292 if (device[0] == '\0')
296 numfields = strsplit (dummy, fields, 16);
301 incoming = atoll (fields[0]);
302 outgoing = atoll (fields[8]);
304 bytes_submit (device, incoming, outgoing);
308 /* #endif KERNEL_LINUX */
312 unsigned long long rx;
313 unsigned long long tx;
318 for (i = 0; i < numif; i++)
320 if (kstat_read (kc, ksp[i], NULL) == -1)
323 rx = get_kstat_value (ksp[i], "rbytes");
324 tx = get_kstat_value (ksp[i], "obytes");
325 if ((rx != -1LL) || (tx != -1LL))
326 bytes_submit (ksp[i]->ks_name, rx, tx);
328 rx = get_kstat_value (ksp[i], "ipackets");
329 tx = get_kstat_value (ksp[i], "opackets");
330 if ((rx != -1LL) || (tx != -1LL))
331 packets_submit (ksp[i]->ks_name, rx, tx);
333 rx = get_kstat_value (ksp[i], "ierrors");
334 tx = get_kstat_value (ksp[i], "oerrors");
335 if ((rx != -1LL) || (tx != -1LL))
336 errors_submit (ksp[i]->ks_name, rx, tx);
338 /* #endif HAVE_LIBKSTAT */
340 #elif defined(HAVE_LIBSTATGRAB)
341 sg_network_io_stats *ios;
344 ios = sg_get_network_io_stats (&num);
346 for (i = 0; i < num; i++)
347 bytes_submit (ios[i].interface_name, ios[i].rx, ios[i].tx);
348 #endif /* HAVE_LIBSTATGRAB */
351 #define traffic_read NULL
352 #endif /* TRAFFIC_HAVE_READ */
354 void module_register (void)
356 plugin_register (MODULE_NAME, traffic_init, traffic_read, bytes_write);
357 plugin_register ("if_packets", NULL, NULL, packets_write);
358 plugin_register ("if_errors", NULL, NULL, errors_write);