1 /**
2 * collectd - src/interface.c
3 * Copyright (C) 2005-2010 Florian octo Forster
4 * Copyright (C) 2009 Manuel Sanmartin
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Florian octo Forster <octo at collectd.org>
21 * Sune Marcher <sm at flork.dk>
22 * Manuel Sanmartin
23 **/
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
29 #include "utils_ignorelist.h"
31 #if HAVE_SYS_TYPES_H
32 # include <sys/types.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 #if HAVE_PERFSTAT
54 # include <sys/protosw.h>
55 # include <libperfstat.h>
56 #endif
58 /*
59 * Various people have reported problems with `getifaddrs' and varying versions
60 * of `glibc'. That's why it's disabled by default. Since more statistics are
61 * available this way one may enable it using the `--enable-getifaddrs' option
62 * of the configure script. -octo
63 */
64 #if KERNEL_LINUX
65 # if !COLLECT_GETIFADDRS
66 # undef HAVE_GETIFADDRS
67 # endif /* !COLLECT_GETIFADDRS */
68 #endif /* KERNEL_LINUX */
70 #if HAVE_PERFSTAT
71 static perfstat_netinterface_t *ifstat;
72 static int nif;
73 static int pnif;
74 #endif /* HAVE_PERFSTAT */
76 #if !HAVE_GETIFADDRS && !KERNEL_LINUX && !HAVE_LIBKSTAT && !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
77 # error "No applicable input method."
78 #endif
80 /*
81 * (Module-)Global variables
82 */
83 static const char *config_keys[] =
84 {
85 "Interface",
86 "IgnoreSelected",
87 NULL
88 };
89 static int config_keys_num = 2;
91 static ignorelist_t *ignorelist = NULL;
92 static _Bool unique_name = 0;
94 #ifdef HAVE_LIBKSTAT
95 #define MAX_NUMIF 256
96 extern kstat_ctl_t *kc;
97 static kstat_t *ksp[MAX_NUMIF];
98 static int numif = 0;
99 #endif /* HAVE_LIBKSTAT */
101 static int interface_config (const char *key, const char *value)
102 {
103 if (ignorelist == NULL)
104 ignorelist = ignorelist_create (/* invert = */ 1);
106 if (strcasecmp (key, "Interface") == 0)
107 {
108 ignorelist_add (ignorelist, value);
109 }
110 else if (strcasecmp (key, "IgnoreSelected") == 0)
111 {
112 int invert = 1;
113 if (IS_TRUE (value))
114 invert = 0;
115 ignorelist_set_invert (ignorelist, invert);
116 }
117 else if (strcasecmp (key, "UniqueName") == 0)
118 {
119 if (IS_TRUE (value))
120 unique_name = 1;
121 }
122 else
123 {
124 return (-1);
125 }
127 return (0);
128 }
130 #if HAVE_LIBKSTAT
131 static int interface_init (void)
132 {
133 kstat_t *ksp_chain;
134 derive_t val;
136 numif = 0;
138 if (kc == NULL)
139 return (-1);
141 for (numif = 0, ksp_chain = kc->kc_chain;
142 (numif < MAX_NUMIF) && (ksp_chain != NULL);
143 ksp_chain = ksp_chain->ks_next)
144 {
145 if (strncmp (ksp_chain->ks_class, "net", 3))
146 continue;
147 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
148 continue;
149 if (kstat_read (kc, ksp_chain, NULL) == -1)
150 continue;
151 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
152 continue;
153 ksp[numif++] = ksp_chain;
154 }
156 return (0);
157 } /* int interface_init */
158 #endif /* HAVE_LIBKSTAT */
160 static void if_submit (const char *dev, const char *type,
161 derive_t rx,
162 derive_t tx)
163 {
164 value_t values[2];
165 value_list_t vl = VALUE_LIST_INIT;
167 if (ignorelist_match (ignorelist, dev) != 0)
168 return;
170 values[0].derive = rx;
171 values[1].derive = tx;
173 vl.values = values;
174 vl.values_len = 2;
175 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
176 sstrncpy (vl.plugin, "interface", sizeof (vl.plugin));
177 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
178 sstrncpy (vl.type, type, sizeof (vl.type));
180 plugin_dispatch_values (&vl);
181 } /* void if_submit */
183 static int interface_read (void)
184 {
185 #if HAVE_GETIFADDRS
186 struct ifaddrs *if_list;
187 struct ifaddrs *if_ptr;
189 /* Darin/Mac OS X and possible other *BSDs */
190 #if HAVE_STRUCT_IF_DATA
191 # define IFA_DATA if_data
192 # define IFA_RX_BYTES ifi_ibytes
193 # define IFA_TX_BYTES ifi_obytes
194 # define IFA_RX_PACKT ifi_ipackets
195 # define IFA_TX_PACKT ifi_opackets
196 # define IFA_RX_ERROR ifi_ierrors
197 # define IFA_TX_ERROR ifi_oerrors
198 /* #endif HAVE_STRUCT_IF_DATA */
200 #elif HAVE_STRUCT_NET_DEVICE_STATS
201 # define IFA_DATA net_device_stats
202 # define IFA_RX_BYTES rx_bytes
203 # define IFA_TX_BYTES tx_bytes
204 # define IFA_RX_PACKT rx_packets
205 # define IFA_TX_PACKT tx_packets
206 # define IFA_RX_ERROR rx_errors
207 # define IFA_TX_ERROR tx_errors
208 #else
209 # error "No suitable type for `struct ifaddrs->ifa_data' found."
210 #endif
212 struct IFA_DATA *if_data;
214 if (getifaddrs (&if_list) != 0)
215 return (-1);
217 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
218 {
219 if (if_ptr->ifa_addr != NULL && if_ptr->ifa_addr->sa_family == AF_LINK) {
220 if_data = (struct IFA_DATA *) if_ptr->ifa_data;
222 if_submit (if_ptr->ifa_name, "if_octets",
223 if_data->IFA_RX_BYTES,
224 if_data->IFA_TX_BYTES);
225 if_submit (if_ptr->ifa_name, "if_packets",
226 if_data->IFA_RX_PACKT,
227 if_data->IFA_TX_PACKT);
228 if_submit (if_ptr->ifa_name, "if_errors",
229 if_data->IFA_RX_ERROR,
230 if_data->IFA_TX_ERROR);
231 }
232 }
234 freeifaddrs (if_list);
235 /* #endif HAVE_GETIFADDRS */
237 #elif KERNEL_LINUX
238 FILE *fh;
239 char buffer[1024];
240 derive_t incoming, outgoing;
241 char *device;
243 char *dummy;
244 char *fields[16];
245 int numfields;
247 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
248 {
249 char errbuf[1024];
250 WARNING ("interface plugin: fopen: %s",
251 sstrerror (errno, errbuf, sizeof (errbuf)));
252 return (-1);
253 }
255 while (fgets (buffer, 1024, fh) != NULL)
256 {
257 if (!(dummy = strchr(buffer, ':')))
258 continue;
259 dummy[0] = '\0';
260 dummy++;
262 device = buffer;
263 while (device[0] == ' ')
264 device++;
266 if (device[0] == '\0')
267 continue;
269 numfields = strsplit (dummy, fields, 16);
271 if (numfields < 11)
272 continue;
274 incoming = atoll (fields[0]);
275 outgoing = atoll (fields[8]);
276 if_submit (device, "if_octets", incoming, outgoing);
278 incoming = atoll (fields[1]);
279 outgoing = atoll (fields[9]);
280 if_submit (device, "if_packets", incoming, outgoing);
282 incoming = atoll (fields[2]);
283 outgoing = atoll (fields[10]);
284 if_submit (device, "if_errors", incoming, outgoing);
285 }
287 fclose (fh);
288 /* #endif KERNEL_LINUX */
290 #elif HAVE_LIBKSTAT
291 int i;
292 derive_t rx;
293 derive_t tx;
294 char iname[DATA_MAX_NAME_LEN];
296 if (kc == NULL)
297 return (-1);
299 for (i = 0; i < numif; i++)
300 {
301 if (kstat_read (kc, ksp[i], NULL) == -1)
302 continue;
304 if (unique_name)
305 snprintf(iname, sizeof(iname), "%s_%d_%s", ksp[i]->ks_module, ksp[i]->ks_instance, ksp[i]->ks_name);
306 else
307 snprintf(iname, sizeof(iname), "%s", ksp[i]->ks_name);
309 /* try to get 64bit counters */
310 rx = get_kstat_value (ksp[i], "rbytes64");
311 tx = get_kstat_value (ksp[i], "obytes64");
312 /* or fallback to 32bit */
313 if (rx == -1LL)
314 rx = get_kstat_value (ksp[i], "rbytes");
315 if (tx == -1LL)
316 tx = get_kstat_value (ksp[i], "obytes");
317 if ((rx != -1LL) || (tx != -1LL))
318 if_submit (iname, "if_octets", rx, tx);
320 /* try to get 64bit counters */
321 rx = get_kstat_value (ksp[i], "ipackets64");
322 tx = get_kstat_value (ksp[i], "opackets64");
323 /* or fallback to 32bit */
324 if (rx == -1LL)
325 rx = get_kstat_value (ksp[i], "ipackets");
326 if (tx == -1LL)
327 tx = get_kstat_value (ksp[i], "opackets");
328 if ((rx != -1LL) || (tx != -1LL))
329 if_submit (iname, "if_packets", rx, tx);
331 /* no 64bit error counters yet */
332 rx = get_kstat_value (ksp[i], "ierrors");
333 tx = get_kstat_value (ksp[i], "oerrors");
334 if ((rx != -1LL) || (tx != -1LL))
335 if_submit (iname, "if_errors", rx, tx);
336 }
337 /* #endif HAVE_LIBKSTAT */
339 #elif defined(HAVE_LIBSTATGRAB)
340 sg_network_io_stats *ios;
341 int i, num;
343 ios = sg_get_network_io_stats (&num);
345 for (i = 0; i < num; i++)
346 if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
347 /* #endif HAVE_LIBSTATGRAB */
349 #elif defined(HAVE_PERFSTAT)
350 perfstat_id_t id;
351 int i, ifs;
353 if ((nif = perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t), 0)) < 0)
354 {
355 char errbuf[1024];
356 WARNING ("interface plugin: perfstat_netinterface: %s",
357 sstrerror (errno, errbuf, sizeof (errbuf)));
358 return (-1);
359 }
361 if (pnif != nif || ifstat == NULL)
362 {
363 if (ifstat != NULL)
364 free(ifstat);
365 ifstat = malloc(nif * sizeof(perfstat_netinterface_t));
366 }
367 pnif = nif;
369 id.name[0]='\0';
370 if ((ifs = perfstat_netinterface(&id, ifstat, sizeof(perfstat_netinterface_t), nif)) < 0)
371 {
372 char errbuf[1024];
373 WARNING ("interface plugin: perfstat_netinterface (interfaces=%d): %s",
374 nif, sstrerror (errno, errbuf, sizeof (errbuf)));
375 return (-1);
376 }
378 for (i = 0; i < ifs; i++)
379 {
380 if_submit (ifstat[i].name, "if_octets", ifstat[i].ibytes, ifstat[i].obytes);
381 if_submit (ifstat[i].name, "if_packets", ifstat[i].ipackets ,ifstat[i].opackets);
382 if_submit (ifstat[i].name, "if_errors", ifstat[i].ierrors, ifstat[i].oerrors );
383 }
384 #endif /* HAVE_PERFSTAT */
386 return (0);
387 } /* int interface_read */
389 void module_register (void)
390 {
391 plugin_register_config ("interface", interface_config,
392 config_keys, config_keys_num);
393 #if HAVE_LIBKSTAT
394 plugin_register_init ("interface", interface_init);
395 #endif
396 plugin_register_read ("interface", interface_read);
397 } /* void module_register */