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"
27 #include "utils_ignorelist.h"
29 #if HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32 #if HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
34 #endif
36 /* One cannot include both. This sucks. */
37 #if HAVE_LINUX_IF_H
38 # include <linux/if.h>
39 #elif HAVE_NET_IF_H
40 # include <net/if.h>
41 #endif
43 #if HAVE_LINUX_NETDEVICE_H
44 # include <linux/netdevice.h>
45 #endif
46 #if HAVE_IFADDRS_H
47 # include <ifaddrs.h>
48 #endif
50 #if HAVE_STATGRAB_H
51 # include <statgrab.h>
52 #endif
54 /*
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
59 */
60 #if KERNEL_LINUX
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."
68 #endif
70 /*
71 * (Module-)Global variables
72 */
73 static const char *config_keys[] =
74 {
75 "Interface",
76 "IgnoreSelected",
77 NULL
78 };
79 static int config_keys_num = 2;
81 static ignorelist_t *ignorelist = NULL;
83 #ifdef HAVE_LIBKSTAT
84 #define MAX_NUMIF 256
85 extern kstat_ctl_t *kc;
86 static kstat_t *ksp[MAX_NUMIF];
87 static int numif = 0;
88 #endif /* HAVE_LIBKSTAT */
90 static int interface_config (const char *key, const char *value)
91 {
92 if (ignorelist == NULL)
93 ignorelist = ignorelist_create (/* invert = */ 1);
95 if (strcasecmp (key, "Interface") == 0)
96 {
97 ignorelist_add (ignorelist, value);
98 }
99 else if (strcasecmp (key, "IgnoreSelected") == 0)
100 {
101 int invert = 1;
102 if ((strcasecmp (value, "True") == 0)
103 || (strcasecmp (value, "Yes") == 0)
104 || (strcasecmp (value, "On") == 0))
105 invert = 0;
106 ignorelist_set_invert (ignorelist, invert);
107 }
108 else
109 {
110 return (-1);
111 }
113 return (0);
114 }
116 #if HAVE_LIBKSTAT
117 static int interface_init (void)
118 {
119 kstat_t *ksp_chain;
120 unsigned long long val;
122 numif = 0;
124 if (kc == NULL)
125 return (-1);
127 for (numif = 0, ksp_chain = kc->kc_chain;
128 (numif < MAX_NUMIF) && (ksp_chain != NULL);
129 ksp_chain = ksp_chain->ks_next)
130 {
131 if (strncmp (ksp_chain->ks_class, "net", 3))
132 continue;
133 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
134 continue;
135 if (kstat_read (kc, ksp_chain, NULL) == -1)
136 continue;
137 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
138 continue;
139 ksp[numif++] = ksp_chain;
140 }
142 return (0);
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)
149 {
150 value_t values[2];
151 value_list_t vl = VALUE_LIST_INIT;
153 if (ignorelist_match (ignorelist, dev) != 0)
154 return;
156 values[0].counter = rx;
157 values[1].counter = tx;
159 vl.values = values;
160 vl.values_len = 2;
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)
170 {
171 #if HAVE_GETIFADDRS
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
194 #else
195 # error "No suitable type for `struct ifaddrs->ifa_data' found."
196 #endif
198 struct IFA_DATA *if_data;
200 if (getifaddrs (&if_list) != 0)
201 return (-1);
203 for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
204 {
205 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
206 continue;
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);
217 }
219 freeifaddrs (if_list);
220 /* #endif HAVE_GETIFADDRS */
222 #elif KERNEL_LINUX
223 FILE *fh;
224 char buffer[1024];
225 unsigned long long incoming, outgoing;
226 char *device;
228 char *dummy;
229 char *fields[16];
230 int numfields;
232 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
233 {
234 char errbuf[1024];
235 WARNING ("interface plugin: fopen: %s",
236 sstrerror (errno, errbuf, sizeof (errbuf)));
237 return (-1);
238 }
240 while (fgets (buffer, 1024, fh) != NULL)
241 {
242 if (!(dummy = strchr(buffer, ':')))
243 continue;
244 dummy[0] = '\0';
245 dummy++;
247 device = buffer;
248 while (device[0] == ' ')
249 device++;
251 if (device[0] == '\0')
252 continue;
254 numfields = strsplit (dummy, fields, 16);
256 if (numfields < 11)
257 continue;
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);
270 }
272 fclose (fh);
273 /* #endif KERNEL_LINUX */
275 #elif HAVE_LIBKSTAT
276 int i;
277 unsigned long long rx;
278 unsigned long long tx;
280 if (kc == NULL)
281 return (-1);
283 for (i = 0; i < numif; i++)
284 {
285 if (kstat_read (kc, ksp[i], NULL) == -1)
286 continue;
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);
302 }
303 /* #endif HAVE_LIBKSTAT */
305 #elif defined(HAVE_LIBSTATGRAB)
306 sg_network_io_stats *ios;
307 int i, num;
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 */
315 return (0);
316 } /* int interface_read */
318 void module_register (void)
319 {
320 plugin_register_config ("interface", interface_config,
321 config_keys, config_keys_num);
322 #if HAVE_LIBKSTAT
323 plugin_register_init ("interface", interface_init);
324 #endif
325 plugin_register_read ("interface", interface_read);
326 } /* void module_register */