1 /**
2 * collectd - src/interface.c
3 * Copyright (C) 2005-2007 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; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 * Sune Marcher <sm at flork.dk>
21 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
28 #if HAVE_SYS_TYPES_H
29 # include <sys/types.h>
30 #endif
31 #if HAVE_SYS_SOCKET_H
32 # include <sys/socket.h>
33 #endif
35 /* One cannot include both. This sucks. */
36 #if HAVE_LINUX_IF_H
37 # include <linux/if.h>
38 #elif HAVE_NET_IF_H
39 # include <net/if.h>
40 #endif
42 #if HAVE_LINUX_NETDEVICE_H
43 # include <linux/netdevice.h>
44 #endif
45 #if HAVE_IFADDRS_H
46 # include <ifaddrs.h>
47 #endif
49 /*
50 * Various people have reported problems with `getifaddrs' and varying versions
51 * of `glibc'. That's why it's disabled by default. Since more statistics are
52 * available this way one may enable it using the `--enable-getifaddrs' option
53 * of the configure script. -octo
54 */
55 #if KERNEL_LINUX
56 # if !COLLECT_GETIFADDRS
57 # undef HAVE_GETIFADDRS
58 # endif /* !COLLECT_GETIFADDRS */
59 #endif /* KERNEL_LINUX */
61 #if HAVE_GETIFADDRS || KERNEL_LINUX || HAVE_LIBKSTAT || HAVE_LIBSTATGRAB
62 # define INTERFACE_HAVE_READ 1
63 #else
64 # define INTERFACE_HAVE_READ 0
65 #endif
67 /*
68 * (Module-)Global variables
69 */
70 static const char *config_keys[] =
71 {
72 "Interface",
73 "IgnoreSelected",
74 NULL
75 };
76 static int config_keys_num = 2;
78 static char **if_list = NULL;
79 static int if_list_num = 0;
80 /*
81 * if_list_action:
82 * 0 => default is to collect selected interface
83 * 1 => ignore selcted interfaces
84 */
85 static int if_list_action = 0;
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 int interface_config (const char *key, const char *value)
95 {
96 char **temp;
98 if (strcasecmp (key, "Interface") == 0)
99 {
100 temp = (char **) realloc (if_list, (if_list_num + 1) * sizeof (char *));
101 if (temp == NULL)
102 {
103 ERROR ("Cannot allocate more memory.");
104 return (1);
105 }
106 if_list = temp;
108 if ((if_list[if_list_num] = strdup (value)) == NULL)
109 {
110 ERROR ("Cannot allocate memory.");
111 return (1);
112 }
113 if_list_num++;
114 }
115 else if (strcasecmp (key, "IgnoreSelected") == 0)
116 {
117 if ((strcasecmp (value, "True") == 0)
118 || (strcasecmp (value, "Yes") == 0)
119 || (strcasecmp (value, "On") == 0))
120 if_list_action = 1;
121 else
122 if_list_action = 0;
123 }
124 else
125 {
126 return (-1);
127 }
129 return (0);
130 }
132 #if HAVE_LIBKSTAT
133 static int interface_init (void)
134 {
135 #if HAVE_LIBKSTAT
136 kstat_t *ksp_chain;
137 unsigned long long val;
139 numif = 0;
141 if (kc == NULL)
142 return (-1);
144 for (numif = 0, ksp_chain = kc->kc_chain;
145 (numif < MAX_NUMIF) && (ksp_chain != NULL);
146 ksp_chain = ksp_chain->ks_next)
147 {
148 if (strncmp (ksp_chain->ks_class, "net", 3))
149 continue;
150 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
151 continue;
152 if (kstat_read (kc, ksp_chain, NULL) == -1)
153 continue;
154 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
155 continue;
156 ksp[numif++] = ksp_chain;
157 }
158 #endif /* HAVE_LIBKSTAT */
160 return (0);
161 } /* int interface_init */
162 #endif /* HAVE_LIBKSTAT */
164 /*
165 * Check if this interface/instance should be ignored. This is called from
166 * both, `submit' and `write' to give client and server the ability to
167 * ignore certain stuff..
168 */
169 static int check_ignore_if (const char *interface)
170 {
171 int i;
173 /* If no interfaces are given collect all interfaces. Mostly to be
174 * backwards compatible, but also because this is much easier. */
175 if (if_list_num < 1)
176 return (0);
178 for (i = 0; i < if_list_num; i++)
179 if (strcasecmp (interface, if_list[i]) == 0)
180 return (if_list_action);
181 return (1 - if_list_action);
182 } /* int check_ignore_if */
184 #if INTERFACE_HAVE_READ
185 static void if_submit (const char *dev, const char *type,
186 unsigned long long rx,
187 unsigned long long tx)
188 {
189 value_t values[2];
190 value_list_t vl = VALUE_LIST_INIT;
192 if (check_ignore_if (dev))
193 return;
195 values[0].counter = rx;
196 values[1].counter = tx;
198 vl.values = values;
199 vl.values_len = 2;
200 vl.time = time (NULL);
201 strcpy (vl.host, hostname_g);
202 strcpy (vl.plugin, "interface");
203 strncpy (vl.type_instance, dev, sizeof (vl.type_instance));
205 plugin_dispatch_values (type, &vl);
206 } /* void if_submit */
208 static int interface_read (void)
209 {
210 #if HAVE_GETIFADDRS
211 struct ifaddrs *if_list;
212 struct ifaddrs *if_ptr;
214 /* Darin/Mac OS X and possible other *BSDs */
215 #if HAVE_STRUCT_IF_DATA
216 # define IFA_DATA if_data
217 # define IFA_RX_BYTES ifi_ibytes
218 # define IFA_TX_BYTES ifi_obytes
219 # define IFA_RX_PACKT ifi_ipackets
220 # define IFA_TX_PACKT ifi_opackets
221 # define IFA_RX_ERROR ifi_ierrors
222 # define IFA_TX_ERROR ifi_oerrors
223 /* #endif HAVE_STRUCT_IF_DATA */
225 #elif HAVE_STRUCT_NET_DEVICE_STATS
226 # define IFA_DATA net_device_stats
227 # define IFA_RX_BYTES rx_bytes
228 # define IFA_TX_BYTES tx_bytes
229 # define IFA_RX_PACKT rx_packets
230 # define IFA_TX_PACKT tx_packets
231 # define IFA_RX_ERROR rx_errors
232 # define IFA_TX_ERROR tx_errors
233 #else
234 # error "No suitable type for `struct ifaddrs->ifa_data' found."
235 #endif
237 struct IFA_DATA *if_data;
239 if (getifaddrs (&if_list) != 0)
240 return (-1);
242 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
243 {
244 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
245 continue;
247 if_submit (if_ptr->ifa_name, "if_octets",
248 if_data->IFA_RX_BYTES,
249 if_data->IFA_TX_BYTES);
250 if_submit (if_ptr->ifa_name, "if_packets",
251 if_data->IFA_RX_PACKT,
252 if_data->IFA_TX_PACKT);
253 if_submit (if_ptr->ifa_name, "if_errors",
254 if_data->IFA_RX_ERROR,
255 if_data->IFA_TX_ERROR);
256 }
258 freeifaddrs (if_list);
259 /* #endif HAVE_GETIFADDRS */
261 #elif KERNEL_LINUX
262 FILE *fh;
263 char buffer[1024];
264 unsigned long long incoming, outgoing;
265 char *device;
267 char *dummy;
268 char *fields[16];
269 int numfields;
271 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
272 {
273 char errbuf[1024];
274 WARNING ("interface plugin: fopen: %s",
275 sstrerror (errno, errbuf, sizeof (errbuf)));
276 return (-1);
277 }
279 while (fgets (buffer, 1024, fh) != NULL)
280 {
281 if (!(dummy = strchr(buffer, ':')))
282 continue;
283 dummy[0] = '\0';
284 dummy++;
286 device = buffer;
287 while (device[0] == ' ')
288 device++;
290 if (device[0] == '\0')
291 continue;
293 numfields = strsplit (dummy, fields, 16);
295 if (numfields < 11)
296 continue;
298 incoming = atoll (fields[0]);
299 outgoing = atoll (fields[8]);
300 if_submit (device, "if_octets", incoming, outgoing);
302 incoming = atoll (fields[1]);
303 outgoing = atoll (fields[9]);
304 if_submit (device, "if_packets", incoming, outgoing);
306 incoming = atoll (fields[2]);
307 outgoing = atoll (fields[10]);
308 if_submit (device, "if_errors", incoming, outgoing);
309 }
311 fclose (fh);
312 /* #endif KERNEL_LINUX */
314 #elif HAVE_LIBKSTAT
315 int i;
316 unsigned long long rx;
317 unsigned long long tx;
319 if (kc == NULL)
320 return;
322 for (i = 0; i < numif; i++)
323 {
324 if (kstat_read (kc, ksp[i], NULL) == -1)
325 continue;
327 rx = get_kstat_value (ksp[i], "rbytes");
328 tx = get_kstat_value (ksp[i], "obytes");
329 if ((rx != -1LL) || (tx != -1LL))
330 if_submit (ksp[i]->ks_name, "if_octets", rx, tx);
332 rx = get_kstat_value (ksp[i], "ipackets");
333 tx = get_kstat_value (ksp[i], "opackets");
334 if ((rx != -1LL) || (tx != -1LL))
335 if_submit (ksp[i]->ks_name, "if_packets", rx, tx);
337 rx = get_kstat_value (ksp[i], "ierrors");
338 tx = get_kstat_value (ksp[i], "oerrors");
339 if ((rx != -1LL) || (tx != -1LL))
340 if_submit (ksp[i]->ks_name, "if_errors", rx, tx);
341 }
342 /* #endif HAVE_LIBKSTAT */
344 #elif defined(HAVE_LIBSTATGRAB)
345 sg_network_io_stats *ios;
346 int i, num;
348 ios = sg_get_network_io_stats (&num);
350 for (i = 0; i < num; i++)
351 if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
352 #endif /* HAVE_LIBSTATGRAB */
354 return (0);
355 } /* int interface_read */
356 #endif /* INTERFACE_HAVE_READ */
358 void module_register (void)
359 {
360 plugin_register_config ("interface", interface_config,
361 config_keys, config_keys_num);
362 #if HAVE_LIBKSTAT
363 plugin_register_init ("interface", interface_init);
364 #endif
365 #if INTERFACE_HAVE_READ
366 plugin_register_read ("interface", interface_read);
367 #endif
368 } /* void module_register */