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 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
27 #if HAVE_SYS_TYPES_H
28 # include <sys/types.h>
29 #endif
30 #if HAVE_SYS_SOCKET_H
31 # include <sys/socket.h>
32 #endif
34 /* One cannot include both. This sucks. */
35 #if HAVE_LINUX_IF_H
36 # include <linux/if.h>
37 #elif HAVE_NET_IF_H
38 # include <net/if.h>
39 #endif
41 #if HAVE_LINUX_NETDEVICE_H
42 # include <linux/netdevice.h>
43 #endif
44 #if HAVE_IFADDRS_H
45 # include <ifaddrs.h>
46 #endif
48 #define MODULE_NAME "traffic"
50 #if HAVE_GETIFADDRS || KERNEL_LINUX || HAVE_LIBKSTAT || HAVE_LIBSTATGRAB
51 # define TRAFFIC_HAVE_READ 1
52 #else
53 # define TRAFFIC_HAVE_READ 0
54 #endif
56 #define BUFSIZE 512
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[] =
64 {
65 "DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U",
66 "DS:outgoing:COUNTER:"COLLECTD_HEARTBEAT":0:U",
67 NULL
68 };
69 static int bytes_ds_num = 2;
71 static char *packets_ds_def[] =
72 {
73 "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
74 "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
75 NULL
76 };
77 static int packets_ds_num = 2;
79 static char *errors_ds_def[] =
80 {
81 "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
82 "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
83 NULL
84 };
85 static int errors_ds_num = 2;
87 #ifdef HAVE_LIBKSTAT
88 #define MAX_NUMIF 256
89 extern kstat_ctl_t *kc;
90 static kstat_t *ksp[MAX_NUMIF];
91 static int numif = 0;
92 #endif /* HAVE_LIBKSTAT */
94 static void traffic_init (void)
95 {
96 #if HAVE_GETIFADDRS
97 /* nothing */
98 /* #endif HAVE_GETIFADDRS */
100 #elif KERNEL_LINUX
101 /* nothing */
102 /* #endif KERNEL_LINUX */
104 #elif HAVE_LIBKSTAT
105 kstat_t *ksp_chain;
106 unsigned long long val;
108 numif = 0;
110 if (kc == NULL)
111 return;
113 for (numif = 0, ksp_chain = kc->kc_chain;
114 (numif < MAX_NUMIF) && (ksp_chain != NULL);
115 ksp_chain = ksp_chain->ks_next)
116 {
117 if (strncmp (ksp_chain->ks_class, "net", 3))
118 continue;
119 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
120 continue;
121 if (kstat_read (kc, ksp_chain, NULL) == -1)
122 continue;
123 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
124 continue;
125 ksp[numif++] = ksp_chain;
126 }
127 /* #endif HAVE_LIBKSTAT */
129 #elif HAVE_LIBSTATG
130 /* nothing */
131 #endif /* HAVE_LIBSTATG */
133 return;
134 }
136 static void generic_write (char *host, char *inst, char *val,
137 char *file_template,
138 char **ds_def, int ds_num)
139 {
140 char file[512];
141 int status;
143 status = snprintf (file, BUFSIZE, file_template, inst);
144 if (status < 1)
145 return;
146 else if (status >= 512)
147 return;
149 rrd_update_file (host, file, val, ds_def, ds_num);
150 }
152 static void bytes_write (char *host, char *inst, char *val)
153 {
154 generic_write (host, inst, val, bytes_file, bytes_ds_def, bytes_ds_num);
155 }
157 static void packets_write (char *host, char *inst, char *val)
158 {
159 generic_write (host, inst, val, packets_file, packets_ds_def, packets_ds_num);
160 }
162 static void errors_write (char *host, char *inst, char *val)
163 {
164 generic_write (host, inst, val, errors_file, errors_ds_def, errors_ds_num);
165 }
167 #if TRAFFIC_HAVE_READ
168 static void bytes_submit (char *device,
169 unsigned long long incoming,
170 unsigned long long outgoing)
171 {
172 char buf[BUFSIZE];
174 if (snprintf (buf, BUFSIZE, "%u:%lld:%lld", (unsigned int) curtime, incoming, outgoing) >= BUFSIZE)
175 return;
177 plugin_submit (MODULE_NAME, device, buf);
178 }
180 #if HAVE_GETIFADDRS
181 static void packets_submit (char *dev,
182 unsigned long long rx,
183 unsigned long long tx)
184 {
185 char buf[512];
186 int status;
188 status = snprintf (buf, 512, "%u:%lld:%lld",
189 (unsigned int) curtime,
190 rx, tx);
191 if ((status >= 512) || (status < 1))
192 return;
193 plugin_submit ("if_packets", dev, buf);
194 }
196 static void errors_submit (char *dev,
197 unsigned long long rx,
198 unsigned long long tx)
199 {
200 char buf[512];
201 int status;
203 status = snprintf (buf, 512, "%u:%lld:%lld",
204 (unsigned int) curtime,
205 rx, tx);
206 if ((status >= 512) || (status < 1))
207 return;
208 plugin_submit ("if_errors", dev, buf);
209 }
210 #endif /* HAVE_GETIFADDRS */
212 static void traffic_read (void)
213 {
214 #if HAVE_GETIFADDRS
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
237 #else
238 # error "No suitable type for `struct ifaddrs->ifa_data' found."
239 #endif
241 struct IFA_DATA *if_data;
243 if (getifaddrs (&if_list) != 0)
244 return;
246 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
247 {
248 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
249 continue;
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);
260 }
262 freeifaddrs (if_list);
263 /* #endif HAVE_GETIFADDRS */
265 #elif KERNEL_LINUX
266 FILE *fh;
267 char buffer[1024];
268 unsigned long long incoming, outgoing;
269 char *device;
271 char *dummy;
272 char *fields[16];
273 int numfields;
275 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
276 {
277 syslog (LOG_WARNING, "traffic: fopen: %s", strerror (errno));
278 return;
279 }
281 while (fgets (buffer, 1024, fh) != NULL)
282 {
283 if (buffer[6] != ':')
284 continue;
285 buffer[6] = '\0';
287 device = buffer;
288 while (device[0] == ' ')
289 device++;
291 if (device[0] == '\0')
292 continue;
294 dummy = buffer + 7;
295 numfields = strsplit (dummy, fields, 16);
297 if (numfields < 9)
298 continue;
300 incoming = atoll (fields[0]);
301 outgoing = atoll (fields[8]);
303 bytes_submit (device, incoming, outgoing);
304 }
306 fclose (fh);
307 /* #endif KERNEL_LINUX */
309 #elif defined(HAVE_LIBKSTAT)
310 int i;
311 unsigned long long incoming, outgoing;
313 if (kc == NULL)
314 return;
316 for (i = 0; i < numif; i++)
317 {
318 if (kstat_read (kc, ksp[i], NULL) == -1)
319 continue;
321 if ((incoming = get_kstat_value (ksp[i], "rbytes")) == -1LL)
322 continue;
323 if ((outgoing = get_kstat_value (ksp[i], "obytes")) == -1LL)
324 continue;
326 bytes_submit (ksp[i]->ks_name, incoming, outgoing);
327 }
328 /* #endif HAVE_LIBKSTAT */
330 #elif defined(HAVE_LIBSTATGRAB)
331 sg_network_io_stats *ios;
332 int i, num;
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 */
339 }
340 #else
341 #define traffic_read NULL
342 #endif /* TRAFFIC_HAVE_READ */
344 void module_register (void)
345 {
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);
349 }
351 #undef BUFSIZE
352 #undef MODULE_NAME