07c9ecfbeb1cb990f3c7a9b1f15e8e70b5e9789d
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";
62 /* TODO: Maybe implement multicast and broadcast counters */
64 static char *bytes_ds_def[] =
65 {
66 "DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U",
67 "DS:outgoing:COUNTER:"COLLECTD_HEARTBEAT":0:U",
68 NULL
69 };
70 static int bytes_ds_num = 2;
72 static char *packets_ds_def[] =
73 {
74 "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
75 "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
76 NULL
77 };
78 static int packets_ds_num = 2;
80 static char *errors_ds_def[] =
81 {
82 "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
83 "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
84 NULL
85 };
86 static int errors_ds_num = 2;
88 #ifdef HAVE_LIBKSTAT
89 #define MAX_NUMIF 256
90 extern kstat_ctl_t *kc;
91 static kstat_t *ksp[MAX_NUMIF];
92 static int numif = 0;
93 #endif /* HAVE_LIBKSTAT */
95 static void traffic_init (void)
96 {
97 #if HAVE_GETIFADDRS
98 /* nothing */
99 /* #endif HAVE_GETIFADDRS */
101 #elif KERNEL_LINUX
102 /* nothing */
103 /* #endif KERNEL_LINUX */
105 #elif HAVE_LIBKSTAT
106 kstat_t *ksp_chain;
107 unsigned long long val;
109 numif = 0;
111 if (kc == NULL)
112 return;
114 for (numif = 0, ksp_chain = kc->kc_chain;
115 (numif < MAX_NUMIF) && (ksp_chain != NULL);
116 ksp_chain = ksp_chain->ks_next)
117 {
118 if (strncmp (ksp_chain->ks_class, "net", 3))
119 continue;
120 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
121 continue;
122 if (kstat_read (kc, ksp_chain, NULL) == -1)
123 continue;
124 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
125 continue;
126 ksp[numif++] = ksp_chain;
127 }
128 /* #endif HAVE_LIBKSTAT */
130 #elif HAVE_LIBSTATG
131 /* nothing */
132 #endif /* HAVE_LIBSTATG */
134 return;
135 }
137 static void generic_write (char *host, char *inst, char *val,
138 char *file_template,
139 char **ds_def, int ds_num)
140 {
141 char file[512];
142 int status;
144 status = snprintf (file, BUFSIZE, file_template, inst);
145 if (status < 1)
146 return;
147 else if (status >= 512)
148 return;
150 rrd_update_file (host, file, val, ds_def, ds_num);
151 }
153 static void bytes_write (char *host, char *inst, char *val)
154 {
155 generic_write (host, inst, val, bytes_file, bytes_ds_def, bytes_ds_num);
156 }
158 static void packets_write (char *host, char *inst, char *val)
159 {
160 generic_write (host, inst, val, packets_file, packets_ds_def, packets_ds_num);
161 }
163 static void errors_write (char *host, char *inst, char *val)
164 {
165 generic_write (host, inst, val, errors_file, errors_ds_def, errors_ds_num);
166 }
168 #if TRAFFIC_HAVE_READ
169 static void bytes_submit (char *device,
170 unsigned long long incoming,
171 unsigned long long outgoing)
172 {
173 char buf[BUFSIZE];
175 if (snprintf (buf, BUFSIZE, "%u:%lld:%lld", (unsigned int) curtime, incoming, outgoing) >= BUFSIZE)
176 return;
178 plugin_submit (MODULE_NAME, device, buf);
179 }
181 #if HAVE_GETIFADDRS || HAVE_LIBKSTAT
182 static void packets_submit (char *dev,
183 unsigned long long rx,
184 unsigned long long tx)
185 {
186 char buf[512];
187 int status;
189 status = snprintf (buf, 512, "%u:%lld:%lld",
190 (unsigned int) curtime,
191 rx, tx);
192 if ((status >= 512) || (status < 1))
193 return;
194 plugin_submit ("if_packets", dev, buf);
195 }
197 static void errors_submit (char *dev,
198 unsigned long long rx,
199 unsigned long long tx)
200 {
201 char buf[512];
202 int status;
204 status = snprintf (buf, 512, "%u:%lld:%lld",
205 (unsigned int) curtime,
206 rx, tx);
207 if ((status >= 512) || (status < 1))
208 return;
209 plugin_submit ("if_errors", dev, buf);
210 }
211 #endif /* HAVE_GETIFADDRS || HAVE_LIBKSTAT */
213 static void traffic_read (void)
214 {
215 #if HAVE_GETIFADDRS
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
238 #else
239 # error "No suitable type for `struct ifaddrs->ifa_data' found."
240 #endif
242 struct IFA_DATA *if_data;
244 if (getifaddrs (&if_list) != 0)
245 return;
247 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
248 {
249 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
250 continue;
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);
261 }
263 freeifaddrs (if_list);
264 /* #endif HAVE_GETIFADDRS */
266 #elif KERNEL_LINUX
267 FILE *fh;
268 char buffer[1024];
269 unsigned long long incoming, outgoing;
270 char *device;
272 char *dummy;
273 char *fields[16];
274 int numfields;
276 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
277 {
278 syslog (LOG_WARNING, "traffic: fopen: %s", strerror (errno));
279 return;
280 }
282 while (fgets (buffer, 1024, fh) != NULL)
283 {
284 if (!(dummy = strchr(buffer, ':')))
285 continue;
286 dummy[0] = '\0';
287 dummy++;
289 device = buffer;
290 while (device[0] == ' ')
291 device++;
293 if (device[0] == '\0')
294 continue;
296 numfields = strsplit (dummy, fields, 16);
298 if (numfields < 9)
299 continue;
301 incoming = atoll (fields[0]);
302 outgoing = atoll (fields[8]);
304 bytes_submit (device, incoming, outgoing);
305 }
307 fclose (fh);
308 /* #endif KERNEL_LINUX */
310 #elif HAVE_LIBKSTAT
311 int i;
312 unsigned long long rx;
313 unsigned long long tx;
315 if (kc == NULL)
316 return;
318 for (i = 0; i < numif; i++)
319 {
320 if (kstat_read (kc, ksp[i], NULL) == -1)
321 continue;
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);
337 }
338 /* #endif HAVE_LIBKSTAT */
340 #elif defined(HAVE_LIBSTATGRAB)
341 sg_network_io_stats *ios;
342 int i, num;
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 */
349 }
350 #else
351 #define traffic_read NULL
352 #endif /* TRAFFIC_HAVE_READ */
354 void module_register (void)
355 {
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);
359 }
361 #undef BUFSIZE
362 #undef MODULE_NAME