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 #if HAVE_STATGRAB_H
50 # include <statgrab.h>
51 #endif
53 /*
54 * Various people have reported problems with `getifaddrs' and varying versions
55 * of `glibc'. That's why it's disabled by default. Since more statistics are
56 * available this way one may enable it using the `--enable-getifaddrs' option
57 * of the configure script. -octo
58 */
59 #if KERNEL_LINUX
60 # if !COLLECT_GETIFADDRS
61 # undef HAVE_GETIFADDRS
62 # endif /* !COLLECT_GETIFADDRS */
63 #endif /* KERNEL_LINUX */
65 #if !HAVE_GETIFADDRS && !KERNEL_LINUX && !HAVE_LIBKSTAT && !HAVE_LIBSTATGRAB
66 # error "No applicable input method."
67 #endif
69 /*
70 * (Module-)Global variables
71 */
72 static const char *config_keys[] =
73 {
74 "Interface",
75 "IgnoreSelected",
76 NULL
77 };
78 static int config_keys_num = 2;
80 static char **if_list = NULL;
81 static int if_list_num = 0;
82 /*
83 * if_list_action:
84 * 0 => default is to collect selected interface
85 * 1 => ignore selcted interfaces
86 */
87 static int if_list_action = 0;
89 #ifdef HAVE_LIBKSTAT
90 #define MAX_NUMIF 256
91 extern kstat_ctl_t *kc;
92 static kstat_t *ksp[MAX_NUMIF];
93 static int numif = 0;
94 #endif /* HAVE_LIBKSTAT */
96 static int interface_config (const char *key, const char *value)
97 {
98 char **temp;
100 if (strcasecmp (key, "Interface") == 0)
101 {
102 temp = (char **) realloc (if_list, (if_list_num + 1) * sizeof (char *));
103 if (temp == NULL)
104 {
105 ERROR ("Cannot allocate more memory.");
106 return (1);
107 }
108 if_list = temp;
110 if ((if_list[if_list_num] = strdup (value)) == NULL)
111 {
112 ERROR ("Cannot allocate memory.");
113 return (1);
114 }
115 if_list_num++;
116 }
117 else if (strcasecmp (key, "IgnoreSelected") == 0)
118 {
119 if ((strcasecmp (value, "True") == 0)
120 || (strcasecmp (value, "Yes") == 0)
121 || (strcasecmp (value, "On") == 0))
122 if_list_action = 1;
123 else
124 if_list_action = 0;
125 }
126 else
127 {
128 return (-1);
129 }
131 return (0);
132 }
134 #if HAVE_LIBKSTAT
135 static int interface_init (void)
136 {
137 kstat_t *ksp_chain;
138 unsigned long long val;
140 numif = 0;
142 if (kc == NULL)
143 return (-1);
145 for (numif = 0, ksp_chain = kc->kc_chain;
146 (numif < MAX_NUMIF) && (ksp_chain != NULL);
147 ksp_chain = ksp_chain->ks_next)
148 {
149 if (strncmp (ksp_chain->ks_class, "net", 3))
150 continue;
151 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
152 continue;
153 if (kstat_read (kc, ksp_chain, NULL) == -1)
154 continue;
155 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
156 continue;
157 ksp[numif++] = ksp_chain;
158 }
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 static void if_submit (const char *dev, const char *type,
185 unsigned long long rx,
186 unsigned long long tx)
187 {
188 value_t values[2];
189 value_list_t vl = VALUE_LIST_INIT;
191 if (check_ignore_if (dev))
192 return;
194 values[0].counter = rx;
195 values[1].counter = tx;
197 vl.values = values;
198 vl.values_len = 2;
199 vl.time = time (NULL);
200 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
201 sstrncpy (vl.plugin, "interface", sizeof (vl.plugin));
202 strncpy (vl.type_instance, dev, sizeof (vl.type_instance));
204 plugin_dispatch_values (type, &vl);
205 } /* void if_submit */
207 static int interface_read (void)
208 {
209 #if HAVE_GETIFADDRS
210 struct ifaddrs *if_list;
211 struct ifaddrs *if_ptr;
213 /* Darin/Mac OS X and possible other *BSDs */
214 #if HAVE_STRUCT_IF_DATA
215 # define IFA_DATA if_data
216 # define IFA_RX_BYTES ifi_ibytes
217 # define IFA_TX_BYTES ifi_obytes
218 # define IFA_RX_PACKT ifi_ipackets
219 # define IFA_TX_PACKT ifi_opackets
220 # define IFA_RX_ERROR ifi_ierrors
221 # define IFA_TX_ERROR ifi_oerrors
222 /* #endif HAVE_STRUCT_IF_DATA */
224 #elif HAVE_STRUCT_NET_DEVICE_STATS
225 # define IFA_DATA net_device_stats
226 # define IFA_RX_BYTES rx_bytes
227 # define IFA_TX_BYTES tx_bytes
228 # define IFA_RX_PACKT rx_packets
229 # define IFA_TX_PACKT tx_packets
230 # define IFA_RX_ERROR rx_errors
231 # define IFA_TX_ERROR tx_errors
232 #else
233 # error "No suitable type for `struct ifaddrs->ifa_data' found."
234 #endif
236 struct IFA_DATA *if_data;
238 if (getifaddrs (&if_list) != 0)
239 return (-1);
241 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
242 {
243 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
244 continue;
246 if_submit (if_ptr->ifa_name, "if_octets",
247 if_data->IFA_RX_BYTES,
248 if_data->IFA_TX_BYTES);
249 if_submit (if_ptr->ifa_name, "if_packets",
250 if_data->IFA_RX_PACKT,
251 if_data->IFA_TX_PACKT);
252 if_submit (if_ptr->ifa_name, "if_errors",
253 if_data->IFA_RX_ERROR,
254 if_data->IFA_TX_ERROR);
255 }
257 freeifaddrs (if_list);
258 /* #endif HAVE_GETIFADDRS */
260 #elif KERNEL_LINUX
261 FILE *fh;
262 char buffer[1024];
263 unsigned long long incoming, outgoing;
264 char *device;
266 char *dummy;
267 char *fields[16];
268 int numfields;
270 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
271 {
272 char errbuf[1024];
273 WARNING ("interface plugin: fopen: %s",
274 sstrerror (errno, errbuf, sizeof (errbuf)));
275 return (-1);
276 }
278 while (fgets (buffer, 1024, fh) != NULL)
279 {
280 if (!(dummy = strchr(buffer, ':')))
281 continue;
282 dummy[0] = '\0';
283 dummy++;
285 device = buffer;
286 while (device[0] == ' ')
287 device++;
289 if (device[0] == '\0')
290 continue;
292 numfields = strsplit (dummy, fields, 16);
294 if (numfields < 11)
295 continue;
297 incoming = atoll (fields[0]);
298 outgoing = atoll (fields[8]);
299 if_submit (device, "if_octets", incoming, outgoing);
301 incoming = atoll (fields[1]);
302 outgoing = atoll (fields[9]);
303 if_submit (device, "if_packets", incoming, outgoing);
305 incoming = atoll (fields[2]);
306 outgoing = atoll (fields[10]);
307 if_submit (device, "if_errors", incoming, outgoing);
308 }
310 fclose (fh);
311 /* #endif KERNEL_LINUX */
313 #elif HAVE_LIBKSTAT
314 int i;
315 unsigned long long rx;
316 unsigned long long tx;
318 if (kc == NULL)
319 return (-1);
321 for (i = 0; i < numif; i++)
322 {
323 if (kstat_read (kc, ksp[i], NULL) == -1)
324 continue;
326 rx = get_kstat_value (ksp[i], "rbytes");
327 tx = get_kstat_value (ksp[i], "obytes");
328 if ((rx != -1LL) || (tx != -1LL))
329 if_submit (ksp[i]->ks_name, "if_octets", rx, tx);
331 rx = get_kstat_value (ksp[i], "ipackets");
332 tx = get_kstat_value (ksp[i], "opackets");
333 if ((rx != -1LL) || (tx != -1LL))
334 if_submit (ksp[i]->ks_name, "if_packets", rx, tx);
336 rx = get_kstat_value (ksp[i], "ierrors");
337 tx = get_kstat_value (ksp[i], "oerrors");
338 if ((rx != -1LL) || (tx != -1LL))
339 if_submit (ksp[i]->ks_name, "if_errors", rx, tx);
340 }
341 /* #endif HAVE_LIBKSTAT */
343 #elif defined(HAVE_LIBSTATGRAB)
344 sg_network_io_stats *ios;
345 int i, num;
347 ios = sg_get_network_io_stats (&num);
349 for (i = 0; i < num; i++)
350 if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
351 #endif /* HAVE_LIBSTATGRAB */
353 return (0);
354 } /* int interface_read */
356 void module_register (void)
357 {
358 plugin_register_config ("interface", interface_config,
359 config_keys, config_keys_num);
360 #if HAVE_LIBKSTAT
361 plugin_register_init ("interface", interface_init);
362 #endif
363 plugin_register_read ("interface", interface_read);
364 } /* void module_register */