14b7dc32b1c6ac9ca33caab29466f5b2a16fd3c2
1 /**
2 * collectd - src/traffic.c
3 * Copyright (C) 2005,2006 Florian octo Forster
4 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 * Sune Marcher <sm at flork.dk>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "configfile.h"
29 #if HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32 #if HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
34 #endif
36 /* One cannot include both. This sucks. */
37 #if HAVE_LINUX_IF_H
38 # include <linux/if.h>
39 #elif HAVE_NET_IF_H
40 # include <net/if.h>
41 #endif
43 #if HAVE_LINUX_NETDEVICE_H
44 # include <linux/netdevice.h>
45 #endif
46 #if HAVE_IFADDRS_H
47 # include <ifaddrs.h>
48 #endif
50 #define MODULE_NAME "traffic"
52 /*
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
57 */
58 #if KERNEL_LINUX
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
66 #else
67 # define TRAFFIC_HAVE_READ 0
68 #endif
70 #define BUFSIZE 512
72 /*
73 * (Module-)Global variables
74 */
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 = "interface-%s/if_packets.rrd";
78 static char *errors_file = "interface-%s/if_errors.rrd";
79 /* TODO: Maybe implement multicast and broadcast counters */
81 static char *config_keys[] =
82 {
83 "Interface",
84 "IgnoreSelected",
85 NULL
86 };
87 static int config_keys_num = 2;
89 static char *bytes_ds_def[] =
90 {
91 "DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U",
92 "DS:outgoing:COUNTER:"COLLECTD_HEARTBEAT":0:U",
93 NULL
94 };
95 static int bytes_ds_num = 2;
97 static char *packets_ds_def[] =
98 {
99 "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
100 "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
101 NULL
102 };
103 static int packets_ds_num = 2;
105 static char *errors_ds_def[] =
106 {
107 "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
108 "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
109 NULL
110 };
111 static int errors_ds_num = 2;
113 static char **if_list = NULL;
114 static int if_list_num = 0;
115 /*
116 * if_list_action:
117 * 0 => default is to collect selected interface
118 * 1 => ignore selcted interfaces
119 */
120 static int if_list_action = 0;
122 #ifdef HAVE_LIBKSTAT
123 #define MAX_NUMIF 256
124 extern kstat_ctl_t *kc;
125 static kstat_t *ksp[MAX_NUMIF];
126 static int numif = 0;
127 #endif /* HAVE_LIBKSTAT */
129 static int traffic_config (char *key, char *value)
130 {
131 char **temp;
133 if (strcasecmp (key, "Interface") == 0)
134 {
135 temp = (char **) realloc (if_list, (if_list_num + 1) * sizeof (char *));
136 if (temp == NULL)
137 {
138 syslog (LOG_EMERG, "Cannot allocate more memory.");
139 return (1);
140 }
141 if_list = temp;
143 if ((if_list[if_list_num] = strdup (value)) == NULL)
144 {
145 syslog (LOG_EMERG, "Cannot allocate memory.");
146 return (1);
147 }
148 if_list_num++;
149 }
150 else if (strcasecmp (key, "IgnoreSelected") == 0)
151 {
152 if ((strcasecmp (value, "True") == 0)
153 || (strcasecmp (value, "Yes") == 0)
154 || (strcasecmp (value, "On") == 0))
155 if_list_action = 1;
156 else
157 if_list_action = 0;
158 }
159 else
160 {
161 return (-1);
162 }
164 return (0);
165 }
167 static void traffic_init (void)
168 {
169 #if HAVE_GETIFADDRS
170 /* nothing */
171 /* #endif HAVE_GETIFADDRS */
173 #elif KERNEL_LINUX
174 /* nothing */
175 /* #endif KERNEL_LINUX */
177 #elif HAVE_LIBKSTAT
178 kstat_t *ksp_chain;
179 unsigned long long val;
181 numif = 0;
183 if (kc == NULL)
184 return;
186 for (numif = 0, ksp_chain = kc->kc_chain;
187 (numif < MAX_NUMIF) && (ksp_chain != NULL);
188 ksp_chain = ksp_chain->ks_next)
189 {
190 if (strncmp (ksp_chain->ks_class, "net", 3))
191 continue;
192 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
193 continue;
194 if (kstat_read (kc, ksp_chain, NULL) == -1)
195 continue;
196 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
197 continue;
198 ksp[numif++] = ksp_chain;
199 }
200 /* #endif HAVE_LIBKSTAT */
202 #elif HAVE_LIBSTATG
203 /* nothing */
204 #endif /* HAVE_LIBSTATG */
206 return;
207 }
209 /*
210 * Check if this interface/instance should be ignored. This is called from
211 * both, `submit' and `write' to give client and server the ability to
212 * ignore certain stuff..
213 */
214 static int check_ignore_if (const char *interface)
215 {
216 int i;
218 /* If no interfaces are given collect all interfaces. Mostly to be
219 * backwards compatible, but also because this is much easier. */
220 if (if_list_num < 1)
221 return (0);
223 for (i = 0; i < if_list_num; i++)
224 if (strcasecmp (interface, if_list[i]) == 0)
225 return (if_list_action);
226 return (1 - if_list_action);
227 }
229 static void generic_write (char *host, char *inst, char *val,
230 char *file_template,
231 char **ds_def, int ds_num)
232 {
233 char file[512];
234 int status;
236 if (check_ignore_if (inst))
237 return;
239 status = snprintf (file, BUFSIZE, file_template, inst);
240 if (status < 1)
241 return;
242 else if (status >= 512)
243 return;
245 rrd_update_file (host, file, val, ds_def, ds_num);
246 }
248 static void bytes_write (char *host, char *inst, char *val)
249 {
250 generic_write (host, inst, val, bytes_file, bytes_ds_def, bytes_ds_num);
251 }
253 static void packets_write (char *host, char *inst, char *val)
254 {
255 generic_write (host, inst, val, packets_file, packets_ds_def, packets_ds_num);
256 }
258 static void errors_write (char *host, char *inst, char *val)
259 {
260 generic_write (host, inst, val, errors_file, errors_ds_def, errors_ds_num);
261 }
263 #if TRAFFIC_HAVE_READ
264 static void bytes_submit (char *dev,
265 unsigned long long rx,
266 unsigned long long tx)
267 {
268 char buf[512];
269 int status;
271 if (check_ignore_if (dev))
272 return;
274 status = snprintf (buf, 512, "%u:%lld:%lld",
275 (unsigned int) curtime,
276 rx, tx);
277 if ((status >= 512) || (status < 1))
278 return;
280 plugin_submit (MODULE_NAME, dev, buf);
281 }
283 #if HAVE_GETIFADDRS || KERNEL_LINUX || HAVE_LIBKSTAT
284 static void packets_submit (char *dev,
285 unsigned long long rx,
286 unsigned long long tx)
287 {
288 char buf[512];
289 int status;
291 if (check_ignore_if (dev))
292 return;
294 status = snprintf (buf, 512, "%u:%lld:%lld",
295 (unsigned int) curtime,
296 rx, tx);
297 if ((status >= 512) || (status < 1))
298 return;
299 plugin_submit ("if_packets", dev, buf);
300 }
302 static void errors_submit (char *dev,
303 unsigned long long rx,
304 unsigned long long tx)
305 {
306 char buf[512];
307 int status;
309 if (check_ignore_if (dev))
310 return;
312 status = snprintf (buf, 512, "%u:%lld:%lld",
313 (unsigned int) curtime,
314 rx, tx);
315 if ((status >= 512) || (status < 1))
316 return;
317 plugin_submit ("if_errors", dev, buf);
318 }
319 #endif /* HAVE_GETIFADDRS || KERNEL_LINUX || HAVE_LIBKSTAT */
321 static void traffic_read (void)
322 {
323 #if HAVE_GETIFADDRS
324 struct ifaddrs *if_list;
325 struct ifaddrs *if_ptr;
327 /* Darin/Mac OS X and possible other *BSDs */
328 #if HAVE_STRUCT_IF_DATA
329 # define IFA_DATA if_data
330 # define IFA_RX_BYTES ifi_ibytes
331 # define IFA_TX_BYTES ifi_obytes
332 # define IFA_RX_PACKT ifi_ipackets
333 # define IFA_TX_PACKT ifi_opackets
334 # define IFA_RX_ERROR ifi_ierrors
335 # define IFA_TX_ERROR ifi_oerrors
336 /* #endif HAVE_STRUCT_IF_DATA */
338 #elif HAVE_STRUCT_NET_DEVICE_STATS
339 # define IFA_DATA net_device_stats
340 # define IFA_RX_BYTES rx_bytes
341 # define IFA_TX_BYTES tx_bytes
342 # define IFA_RX_PACKT rx_packets
343 # define IFA_TX_PACKT tx_packets
344 # define IFA_RX_ERROR rx_errors
345 # define IFA_TX_ERROR tx_errors
346 #else
347 # error "No suitable type for `struct ifaddrs->ifa_data' found."
348 #endif
350 struct IFA_DATA *if_data;
352 if (getifaddrs (&if_list) != 0)
353 return;
355 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
356 {
357 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
358 continue;
360 bytes_submit (if_ptr->ifa_name,
361 if_data->IFA_RX_BYTES,
362 if_data->IFA_TX_BYTES);
363 packets_submit (if_ptr->ifa_name,
364 if_data->IFA_RX_PACKT,
365 if_data->IFA_TX_PACKT);
366 errors_submit (if_ptr->ifa_name,
367 if_data->IFA_RX_ERROR,
368 if_data->IFA_TX_ERROR);
369 }
371 freeifaddrs (if_list);
372 /* #endif HAVE_GETIFADDRS */
374 #elif KERNEL_LINUX
375 FILE *fh;
376 char buffer[1024];
377 unsigned long long incoming, outgoing;
378 char *device;
380 char *dummy;
381 char *fields[16];
382 int numfields;
384 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
385 {
386 syslog (LOG_WARNING, "traffic: fopen: %s", strerror (errno));
387 return;
388 }
390 while (fgets (buffer, 1024, fh) != NULL)
391 {
392 if (!(dummy = strchr(buffer, ':')))
393 continue;
394 dummy[0] = '\0';
395 dummy++;
397 device = buffer;
398 while (device[0] == ' ')
399 device++;
401 if (device[0] == '\0')
402 continue;
404 numfields = strsplit (dummy, fields, 16);
406 if (numfields < 11)
407 continue;
409 incoming = atoll (fields[0]);
410 outgoing = atoll (fields[8]);
411 bytes_submit (device, incoming, outgoing);
413 incoming = atoll (fields[1]);
414 outgoing = atoll (fields[9]);
415 packets_submit (device, incoming, outgoing);
417 incoming = atoll (fields[2]);
418 outgoing = atoll (fields[10]);
419 errors_submit (device, incoming, outgoing);
420 }
422 fclose (fh);
423 /* #endif KERNEL_LINUX */
425 #elif HAVE_LIBKSTAT
426 int i;
427 unsigned long long rx;
428 unsigned long long tx;
430 if (kc == NULL)
431 return;
433 for (i = 0; i < numif; i++)
434 {
435 if (kstat_read (kc, ksp[i], NULL) == -1)
436 continue;
438 rx = get_kstat_value (ksp[i], "rbytes");
439 tx = get_kstat_value (ksp[i], "obytes");
440 if ((rx != -1LL) || (tx != -1LL))
441 bytes_submit (ksp[i]->ks_name, rx, tx);
443 rx = get_kstat_value (ksp[i], "ipackets");
444 tx = get_kstat_value (ksp[i], "opackets");
445 if ((rx != -1LL) || (tx != -1LL))
446 packets_submit (ksp[i]->ks_name, rx, tx);
448 rx = get_kstat_value (ksp[i], "ierrors");
449 tx = get_kstat_value (ksp[i], "oerrors");
450 if ((rx != -1LL) || (tx != -1LL))
451 errors_submit (ksp[i]->ks_name, rx, tx);
452 }
453 /* #endif HAVE_LIBKSTAT */
455 #elif defined(HAVE_LIBSTATGRAB)
456 sg_network_io_stats *ios;
457 int i, num;
459 ios = sg_get_network_io_stats (&num);
461 for (i = 0; i < num; i++)
462 bytes_submit (ios[i].interface_name, ios[i].rx, ios[i].tx);
463 #endif /* HAVE_LIBSTATGRAB */
464 }
465 #else
466 #define traffic_read NULL
467 #endif /* TRAFFIC_HAVE_READ */
469 void module_register (void)
470 {
471 plugin_register (MODULE_NAME, traffic_init, traffic_read, bytes_write);
472 plugin_register ("if_packets", NULL, NULL, packets_write);
473 plugin_register ("if_errors", NULL, NULL, errors_write);
474 cf_register (MODULE_NAME, traffic_config, config_keys, config_keys_num);
475 }
477 #undef BUFSIZE
478 #undef MODULE_NAME