2 * collectd - src/interface.c
3 * Copyright (C) 2005-2008 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 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
162 sstrncpy (vl.plugin, "interface", sizeof (vl.plugin));
163 sstrncpy (vl.type, type, sizeof (vl.type));
164 sstrncpy (vl.type_instance, dev, sizeof (vl.type_instance));
166 plugin_dispatch_values (&vl);
167 } /* void if_submit */
169 static int interface_read (void)
172 struct ifaddrs *if_list;
173 struct ifaddrs *if_ptr;
175 /* Darin/Mac OS X and possible other *BSDs */
176 #if HAVE_STRUCT_IF_DATA
177 # define IFA_DATA if_data
178 # define IFA_RX_BYTES ifi_ibytes
179 # define IFA_TX_BYTES ifi_obytes
180 # define IFA_RX_PACKT ifi_ipackets
181 # define IFA_TX_PACKT ifi_opackets
182 # define IFA_RX_ERROR ifi_ierrors
183 # define IFA_TX_ERROR ifi_oerrors
184 /* #endif HAVE_STRUCT_IF_DATA */
186 #elif HAVE_STRUCT_NET_DEVICE_STATS
187 # define IFA_DATA net_device_stats
188 # define IFA_RX_BYTES rx_bytes
189 # define IFA_TX_BYTES tx_bytes
190 # define IFA_RX_PACKT rx_packets
191 # define IFA_TX_PACKT tx_packets
192 # define IFA_RX_ERROR rx_errors
193 # define IFA_TX_ERROR tx_errors
195 # error "No suitable type for `struct ifaddrs->ifa_data' found."
198 struct IFA_DATA *if_data;
200 if (getifaddrs (&if_list) != 0)
203 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
205 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
208 if_submit (if_ptr->ifa_name, "if_octets",
209 if_data->IFA_RX_BYTES,
210 if_data->IFA_TX_BYTES);
211 if_submit (if_ptr->ifa_name, "if_packets",
212 if_data->IFA_RX_PACKT,
213 if_data->IFA_TX_PACKT);
214 if_submit (if_ptr->ifa_name, "if_errors",
215 if_data->IFA_RX_ERROR,
216 if_data->IFA_TX_ERROR);
219 freeifaddrs (if_list);
220 /* #endif HAVE_GETIFADDRS */
225 unsigned long long incoming, outgoing;
232 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
235 WARNING ("interface plugin: fopen: %s",
236 sstrerror (errno, errbuf, sizeof (errbuf)));
240 while (fgets (buffer, 1024, fh) != NULL)
242 if (!(dummy = strchr(buffer, ':')))
248 while (device[0] == ' ')
251 if (device[0] == '\0')
254 numfields = strsplit (dummy, fields, 16);
259 incoming = atoll (fields[0]);
260 outgoing = atoll (fields[8]);
261 if_submit (device, "if_octets", incoming, outgoing);
263 incoming = atoll (fields[1]);
264 outgoing = atoll (fields[9]);
265 if_submit (device, "if_packets", incoming, outgoing);
267 incoming = atoll (fields[2]);
268 outgoing = atoll (fields[10]);
269 if_submit (device, "if_errors", incoming, outgoing);
273 /* #endif KERNEL_LINUX */
277 unsigned long long rx;
278 unsigned long long tx;
283 for (i = 0; i < numif; i++)
285 if (kstat_read (kc, ksp[i], NULL) == -1)
288 rx = get_kstat_value (ksp[i], "rbytes");
289 tx = get_kstat_value (ksp[i], "obytes");
290 if ((rx != -1LL) || (tx != -1LL))
291 if_submit (ksp[i]->ks_name, "if_octets", rx, tx);
293 rx = get_kstat_value (ksp[i], "ipackets");
294 tx = get_kstat_value (ksp[i], "opackets");
295 if ((rx != -1LL) || (tx != -1LL))
296 if_submit (ksp[i]->ks_name, "if_packets", rx, tx);
298 rx = get_kstat_value (ksp[i], "ierrors");
299 tx = get_kstat_value (ksp[i], "oerrors");
300 if ((rx != -1LL) || (tx != -1LL))
301 if_submit (ksp[i]->ks_name, "if_errors", rx, tx);
303 /* #endif HAVE_LIBKSTAT */
305 #elif defined(HAVE_LIBSTATGRAB)
306 sg_network_io_stats *ios;
309 ios = sg_get_network_io_stats (&num);
311 for (i = 0; i < num; i++)
312 if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
313 #endif /* HAVE_LIBSTATGRAB */
316 } /* int interface_read */
318 void module_register (void)
320 plugin_register_config ("interface", interface_config,
321 config_keys, config_keys_num);
323 plugin_register_init ("interface", interface_init);
325 plugin_register_read ("interface", interface_read);
326 } /* void module_register */