2 * collectd - src/traffic.c
3 * Copyright (C) 2005 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>
27 #define MODULE_NAME "traffic"
29 #if defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) || defined(HAVE_LIBSTATGRAB)
30 # define TRAFFIC_HAVE_READ 1
32 # define TRAFFIC_HAVE_READ 0
37 static char *traffic_filename_template = "traffic-%s.rrd";
39 static char *ds_def[] =
41 "DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U",
42 "DS:outgoing:COUNTER:"COLLECTD_HEARTBEAT":0:U",
45 static int ds_num = 2;
49 extern kstat_ctl_t *kc;
50 static kstat_t *ksp[MAX_NUMIF];
52 #endif /* HAVE_LIBKSTAT */
54 static void traffic_init (void)
58 unsigned long long val;
65 for (numif = 0, ksp_chain = kc->kc_chain;
66 (numif < MAX_NUMIF) && (ksp_chain != NULL);
67 ksp_chain = ksp_chain->ks_next)
69 if (strncmp (ksp_chain->ks_class, "net", 3))
71 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
73 if (kstat_read (kc, ksp_chain, NULL) == -1)
75 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
77 ksp[numif++] = ksp_chain;
79 #endif /* HAVE_LIBKSTAT */
82 static void traffic_write (char *host, char *inst, char *val)
87 status = snprintf (file, BUFSIZE, traffic_filename_template, inst);
90 else if (status >= BUFSIZE)
93 rrd_update_file (host, file, val, ds_def, ds_num);
97 static void traffic_submit (char *device,
98 unsigned long long incoming,
99 unsigned long long outgoing)
103 if (snprintf (buf, BUFSIZE, "%u:%lld:%lld", (unsigned int) curtime, incoming, outgoing) >= BUFSIZE)
106 plugin_submit (MODULE_NAME, device, buf);
109 static void traffic_read (void)
114 unsigned long long incoming, outgoing;
121 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
123 syslog (LOG_WARNING, "traffic: fopen: %s", strerror (errno));
127 while (fgets (buffer, 1024, fh) != NULL)
129 if (buffer[6] != ':')
134 while (device[0] == ' ')
137 if (device[0] == '\0')
141 numfields = strsplit (dummy, fields, 16);
146 incoming = atoll (fields[0]);
147 outgoing = atoll (fields[8]);
149 traffic_submit (device, incoming, outgoing);
153 /* #endif KERNEL_LINUX */
155 #elif defined(HAVE_LIBKSTAT)
157 unsigned long long incoming, outgoing;
162 for (i = 0; i < numif; i++)
164 if (kstat_read (kc, ksp[i], NULL) == -1)
167 if ((incoming = get_kstat_value (ksp[i], "rbytes")) == -1LL)
169 if ((outgoing = get_kstat_value (ksp[i], "obytes")) == -1LL)
172 traffic_submit (ksp[i]->ks_name, incoming, outgoing);
174 /* #endif HAVE_LIBKSTAT */
176 #elif defined(HAVE_LIBSTATGRAB)
177 sg_network_io_stats *ios;
180 ios = sg_get_network_io_stats (&num);
182 for (i = 0; i < num; i++)
183 traffic_submit (ios[i].interface_name, ios[i].rx, ios[i].tx);
184 #endif /* HAVE_LIBSTATGRAB */
187 #define traffic_read NULL
188 #endif /* TRAFFIC_HAVE_READ */
190 void module_register (void)
192 plugin_register (MODULE_NAME, traffic_init, traffic_read, traffic_write);