2 * collectd - src/interface.c
3 * Copyright (C) 2005-2007 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; only version 2 of the License is applicable.
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.
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
19 * Florian octo Forster <octo at verplant.org>
20 * Sune Marcher <sm at flork.dk>
26 #include "configfile.h"
27 #include "utils_ignorelist.h"
30 # include <sys/types.h>
33 # include <sys/socket.h>
36 /* One cannot include both. This sucks. */
38 # include <linux/if.h>
43 #if HAVE_LINUX_NETDEVICE_H
44 # include <linux/netdevice.h>
51 # include <statgrab.h>
55 * Various people have reported problems with `getifaddrs' and varying versions
56 * of `glibc'. That's why it's disabled by default. Since more statistics are
57 * available this way one may enable it using the `--enable-getifaddrs' option
58 * of the configure script. -octo
61 # if !COLLECT_GETIFADDRS
62 # undef HAVE_GETIFADDRS
63 # endif /* !COLLECT_GETIFADDRS */
64 #endif /* KERNEL_LINUX */
66 #if !HAVE_GETIFADDRS && !KERNEL_LINUX && !HAVE_LIBKSTAT && !HAVE_LIBSTATGRAB
67 # error "No applicable input method."
71 * (Module-)Global variables
73 static const char *config_keys[] =
79 static int config_keys_num = 2;
81 static ignorelist_t *ignorelist = NULL;
85 extern kstat_ctl_t *kc;
86 static kstat_t *ksp[MAX_NUMIF];
88 #endif /* HAVE_LIBKSTAT */
90 static int interface_config (const char *key, const char *value)
92 if (ignorelist == NULL)
93 ignorelist = ignorelist_create (/* invert = */ 1);
95 if (strcasecmp (key, "Interface") == 0)
97 ignorelist_add (ignorelist, value);
99 else if (strcasecmp (key, "IgnoreSelected") == 0)
102 if ((strcasecmp (value, "True") == 0)
103 || (strcasecmp (value, "Yes") == 0)
104 || (strcasecmp (value, "On") == 0))
106 ignorelist_set_invert (ignorelist, invert);
117 static int interface_init (void)
120 unsigned long long val;
127 for (numif = 0, ksp_chain = kc->kc_chain;
128 (numif < MAX_NUMIF) && (ksp_chain != NULL);
129 ksp_chain = ksp_chain->ks_next)
131 if (strncmp (ksp_chain->ks_class, "net", 3))
133 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
135 if (kstat_read (kc, ksp_chain, NULL) == -1)
137 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
139 ksp[numif++] = ksp_chain;
143 } /* int interface_init */
144 #endif /* HAVE_LIBKSTAT */
146 static void if_submit (const char *dev, const char *type,
147 unsigned long long rx,
148 unsigned long long tx)
151 value_list_t vl = VALUE_LIST_INIT;
153 if (ignorelist_match (ignorelist, dev) != 0)
156 values[0].counter = rx;
157 values[1].counter = tx;
161 vl.time = time (NULL);
162 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
163 sstrncpy (vl.plugin, "interface", sizeof (vl.plugin));
164 sstrncpy (vl.type, type, sizeof (vl.type));
165 sstrncpy (vl.type_instance, dev, sizeof (vl.type_instance));
167 plugin_dispatch_values (&vl);
168 } /* void if_submit */
170 static int interface_read (void)
173 struct ifaddrs *if_list;
174 struct ifaddrs *if_ptr;
176 /* Darin/Mac OS X and possible other *BSDs */
177 #if HAVE_STRUCT_IF_DATA
178 # define IFA_DATA if_data
179 # define IFA_RX_BYTES ifi_ibytes
180 # define IFA_TX_BYTES ifi_obytes
181 # define IFA_RX_PACKT ifi_ipackets
182 # define IFA_TX_PACKT ifi_opackets
183 # define IFA_RX_ERROR ifi_ierrors
184 # define IFA_TX_ERROR ifi_oerrors
185 /* #endif HAVE_STRUCT_IF_DATA */
187 #elif HAVE_STRUCT_NET_DEVICE_STATS
188 # define IFA_DATA net_device_stats
189 # define IFA_RX_BYTES rx_bytes
190 # define IFA_TX_BYTES tx_bytes
191 # define IFA_RX_PACKT rx_packets
192 # define IFA_TX_PACKT tx_packets
193 # define IFA_RX_ERROR rx_errors
194 # define IFA_TX_ERROR tx_errors
196 # error "No suitable type for `struct ifaddrs->ifa_data' found."
199 struct IFA_DATA *if_data;
201 if (getifaddrs (&if_list) != 0)
204 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
206 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
209 if_submit (if_ptr->ifa_name, "if_octets",
210 if_data->IFA_RX_BYTES,
211 if_data->IFA_TX_BYTES);
212 if_submit (if_ptr->ifa_name, "if_packets",
213 if_data->IFA_RX_PACKT,
214 if_data->IFA_TX_PACKT);
215 if_submit (if_ptr->ifa_name, "if_errors",
216 if_data->IFA_RX_ERROR,
217 if_data->IFA_TX_ERROR);
220 freeifaddrs (if_list);
221 /* #endif HAVE_GETIFADDRS */
226 unsigned long long incoming, outgoing;
233 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
236 WARNING ("interface plugin: fopen: %s",
237 sstrerror (errno, errbuf, sizeof (errbuf)));
241 while (fgets (buffer, 1024, fh) != NULL)
243 if (!(dummy = strchr(buffer, ':')))
249 while (device[0] == ' ')
252 if (device[0] == '\0')
255 numfields = strsplit (dummy, fields, 16);
260 incoming = atoll (fields[0]);
261 outgoing = atoll (fields[8]);
262 if_submit (device, "if_octets", incoming, outgoing);
264 incoming = atoll (fields[1]);
265 outgoing = atoll (fields[9]);
266 if_submit (device, "if_packets", incoming, outgoing);
268 incoming = atoll (fields[2]);
269 outgoing = atoll (fields[10]);
270 if_submit (device, "if_errors", incoming, outgoing);
274 /* #endif KERNEL_LINUX */
278 unsigned long long rx;
279 unsigned long long tx;
284 for (i = 0; i < numif; i++)
286 if (kstat_read (kc, ksp[i], NULL) == -1)
289 rx = get_kstat_value (ksp[i], "rbytes");
290 tx = get_kstat_value (ksp[i], "obytes");
291 if ((rx != -1LL) || (tx != -1LL))
292 if_submit (ksp[i]->ks_name, "if_octets", rx, tx);
294 rx = get_kstat_value (ksp[i], "ipackets");
295 tx = get_kstat_value (ksp[i], "opackets");
296 if ((rx != -1LL) || (tx != -1LL))
297 if_submit (ksp[i]->ks_name, "if_packets", rx, tx);
299 rx = get_kstat_value (ksp[i], "ierrors");
300 tx = get_kstat_value (ksp[i], "oerrors");
301 if ((rx != -1LL) || (tx != -1LL))
302 if_submit (ksp[i]->ks_name, "if_errors", rx, tx);
304 /* #endif HAVE_LIBKSTAT */
306 #elif defined(HAVE_LIBSTATGRAB)
307 sg_network_io_stats *ios;
310 ios = sg_get_network_io_stats (&num);
312 for (i = 0; i < num; i++)
313 if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
314 #endif /* HAVE_LIBSTATGRAB */
317 } /* int interface_read */
319 void module_register (void)
321 plugin_register_config ("interface", interface_config,
322 config_keys, config_keys_num);
324 plugin_register_init ("interface", interface_init);
326 plugin_register_read ("interface", interface_read);
327 } /* void module_register */