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)
104 ignorelist_set_invert (ignorelist, invert);
115 static int interface_init (void)
118 unsigned long long val;
125 for (numif = 0, ksp_chain = kc->kc_chain;
126 (numif < MAX_NUMIF) && (ksp_chain != NULL);
127 ksp_chain = ksp_chain->ks_next)
129 if (strncmp (ksp_chain->ks_class, "net", 3))
131 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
133 if (kstat_read (kc, ksp_chain, NULL) == -1)
135 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
137 ksp[numif++] = ksp_chain;
141 } /* int interface_init */
142 #endif /* HAVE_LIBKSTAT */
144 static void if_submit (const char *dev, const char *type,
145 unsigned long long rx,
146 unsigned long long tx)
149 value_list_t vl = VALUE_LIST_INIT;
151 if (ignorelist_match (ignorelist, dev) != 0)
154 values[0].counter = rx;
155 values[1].counter = tx;
159 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
160 sstrncpy (vl.plugin, "interface", sizeof (vl.plugin));
161 sstrncpy (vl.type, type, sizeof (vl.type));
162 sstrncpy (vl.type_instance, dev, sizeof (vl.type_instance));
164 plugin_dispatch_values (&vl);
165 } /* void if_submit */
167 static int interface_read (void)
170 struct ifaddrs *if_list;
171 struct ifaddrs *if_ptr;
173 /* Darin/Mac OS X and possible other *BSDs */
174 #if HAVE_STRUCT_IF_DATA
175 # define IFA_DATA if_data
176 # define IFA_RX_BYTES ifi_ibytes
177 # define IFA_TX_BYTES ifi_obytes
178 # define IFA_RX_PACKT ifi_ipackets
179 # define IFA_TX_PACKT ifi_opackets
180 # define IFA_RX_ERROR ifi_ierrors
181 # define IFA_TX_ERROR ifi_oerrors
182 /* #endif HAVE_STRUCT_IF_DATA */
184 #elif HAVE_STRUCT_NET_DEVICE_STATS
185 # define IFA_DATA net_device_stats
186 # define IFA_RX_BYTES rx_bytes
187 # define IFA_TX_BYTES tx_bytes
188 # define IFA_RX_PACKT rx_packets
189 # define IFA_TX_PACKT tx_packets
190 # define IFA_RX_ERROR rx_errors
191 # define IFA_TX_ERROR tx_errors
193 # error "No suitable type for `struct ifaddrs->ifa_data' found."
196 struct IFA_DATA *if_data;
198 if (getifaddrs (&if_list) != 0)
201 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
203 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
206 if_submit (if_ptr->ifa_name, "if_octets",
207 if_data->IFA_RX_BYTES,
208 if_data->IFA_TX_BYTES);
209 if_submit (if_ptr->ifa_name, "if_packets",
210 if_data->IFA_RX_PACKT,
211 if_data->IFA_TX_PACKT);
212 if_submit (if_ptr->ifa_name, "if_errors",
213 if_data->IFA_RX_ERROR,
214 if_data->IFA_TX_ERROR);
217 freeifaddrs (if_list);
218 /* #endif HAVE_GETIFADDRS */
223 unsigned long long incoming, outgoing;
230 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
233 WARNING ("interface plugin: fopen: %s",
234 sstrerror (errno, errbuf, sizeof (errbuf)));
238 while (fgets (buffer, 1024, fh) != NULL)
240 if (!(dummy = strchr(buffer, ':')))
246 while (device[0] == ' ')
249 if (device[0] == '\0')
252 numfields = strsplit (dummy, fields, 16);
257 incoming = atoll (fields[0]);
258 outgoing = atoll (fields[8]);
259 if_submit (device, "if_octets", incoming, outgoing);
261 incoming = atoll (fields[1]);
262 outgoing = atoll (fields[9]);
263 if_submit (device, "if_packets", incoming, outgoing);
265 incoming = atoll (fields[2]);
266 outgoing = atoll (fields[10]);
267 if_submit (device, "if_errors", incoming, outgoing);
271 /* #endif KERNEL_LINUX */
275 unsigned long long rx;
276 unsigned long long tx;
281 for (i = 0; i < numif; i++)
283 if (kstat_read (kc, ksp[i], NULL) == -1)
286 rx = get_kstat_value (ksp[i], "rbytes");
287 tx = get_kstat_value (ksp[i], "obytes");
288 if ((rx != -1LL) || (tx != -1LL))
289 if_submit (ksp[i]->ks_name, "if_octets", rx, tx);
291 rx = get_kstat_value (ksp[i], "ipackets");
292 tx = get_kstat_value (ksp[i], "opackets");
293 if ((rx != -1LL) || (tx != -1LL))
294 if_submit (ksp[i]->ks_name, "if_packets", rx, tx);
296 rx = get_kstat_value (ksp[i], "ierrors");
297 tx = get_kstat_value (ksp[i], "oerrors");
298 if ((rx != -1LL) || (tx != -1LL))
299 if_submit (ksp[i]->ks_name, "if_errors", rx, tx);
301 /* #endif HAVE_LIBKSTAT */
303 #elif defined(HAVE_LIBSTATGRAB)
304 sg_network_io_stats *ios;
307 ios = sg_get_network_io_stats (&num);
309 for (i = 0; i < num; i++)
310 if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
311 #endif /* HAVE_LIBSTATGRAB */
314 } /* int interface_read */
316 void module_register (void)
318 plugin_register_config ("interface", interface_config,
319 config_keys, config_keys_num);
321 plugin_register_init ("interface", interface_init);
323 plugin_register_read ("interface", interface_read);
324 } /* void module_register */