Setting a max and min for gpsd timeout.
[collectd.git] / src / interface.c
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  **/
24
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
29 #include "utils_ignorelist.h"
30
31 #if HAVE_SYS_TYPES_H
32 #  include <sys/types.h>
33 #endif
34
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
41
42 #if HAVE_LINUX_NETDEVICE_H
43 #  include <linux/netdevice.h>
44 #endif
45 #if HAVE_IFADDRS_H
46 #  include <ifaddrs.h>
47 #endif
48
49 #if HAVE_STATGRAB_H
50 # include <statgrab.h>
51 #endif
52
53 #if HAVE_PERFSTAT
54 # include <sys/protosw.h>
55 # include <libperfstat.h>
56 #endif
57
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 */
69
70 #if HAVE_PERFSTAT
71 static perfstat_netinterface_t *ifstat;
72 static int nif;
73 static int pnif;
74 #endif /* HAVE_PERFSTAT */
75
76 #if !HAVE_GETIFADDRS && !KERNEL_LINUX && !HAVE_LIBKSTAT && !HAVE_LIBSTATGRAB && !HAVE_PERFSTAT
77 # error "No applicable input method."
78 #endif
79
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;
90
91 static ignorelist_t *ignorelist = NULL;
92
93 #ifdef HAVE_LIBKSTAT
94 #define MAX_NUMIF 256
95 extern kstat_ctl_t *kc;
96 static kstat_t *ksp[MAX_NUMIF];
97 static int numif = 0;
98 #endif /* HAVE_LIBKSTAT */
99
100 static int interface_config (const char *key, const char *value)
101 {
102         if (ignorelist == NULL)
103                 ignorelist = ignorelist_create (/* invert = */ 1);
104
105         if (strcasecmp (key, "Interface") == 0)
106         {
107                 ignorelist_add (ignorelist, value);
108         }
109         else if (strcasecmp (key, "IgnoreSelected") == 0)
110         {
111                 int invert = 1;
112                 if (IS_TRUE (value))
113                         invert = 0;
114                 ignorelist_set_invert (ignorelist, invert);
115         }
116         else
117         {
118                 return (-1);
119         }
120
121         return (0);
122 }
123
124 #if HAVE_LIBKSTAT
125 static int interface_init (void)
126 {
127         kstat_t *ksp_chain;
128         derive_t val;
129
130         numif = 0;
131
132         if (kc == NULL)
133                 return (-1);
134
135         for (numif = 0, ksp_chain = kc->kc_chain;
136                         (numif < MAX_NUMIF) && (ksp_chain != NULL);
137                         ksp_chain = ksp_chain->ks_next)
138         {
139                 if (strncmp (ksp_chain->ks_class, "net", 3))
140                         continue;
141                 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
142                         continue;
143                 if (kstat_read (kc, ksp_chain, NULL) == -1)
144                         continue;
145                 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
146                         continue;
147                 ksp[numif++] = ksp_chain;
148         }
149
150         return (0);
151 } /* int interface_init */
152 #endif /* HAVE_LIBKSTAT */
153
154 static void if_submit (const char *dev, const char *type,
155                 derive_t rx,
156                 derive_t tx)
157 {
158         value_t values[2];
159         value_list_t vl = VALUE_LIST_INIT;
160
161         if (ignorelist_match (ignorelist, dev) != 0)
162                 return;
163
164         values[0].derive = rx;
165         values[1].derive = tx;
166
167         vl.values = values;
168         vl.values_len = 2;
169         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
170         sstrncpy (vl.plugin, "interface", sizeof (vl.plugin));
171         sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
172         sstrncpy (vl.type, type, sizeof (vl.type));
173
174         plugin_dispatch_values (&vl);
175 } /* void if_submit */
176
177 static int interface_read (void)
178 {
179 #if HAVE_GETIFADDRS
180         struct ifaddrs *if_list;
181         struct ifaddrs *if_ptr;
182
183 /* Darin/Mac OS X and possible other *BSDs */
184 #if HAVE_STRUCT_IF_DATA
185 #  define IFA_DATA if_data
186 #  define IFA_RX_BYTES ifi_ibytes
187 #  define IFA_TX_BYTES ifi_obytes
188 #  define IFA_RX_PACKT ifi_ipackets
189 #  define IFA_TX_PACKT ifi_opackets
190 #  define IFA_RX_ERROR ifi_ierrors
191 #  define IFA_TX_ERROR ifi_oerrors
192 /* #endif HAVE_STRUCT_IF_DATA */
193
194 #elif HAVE_STRUCT_NET_DEVICE_STATS
195 #  define IFA_DATA net_device_stats
196 #  define IFA_RX_BYTES rx_bytes
197 #  define IFA_TX_BYTES tx_bytes
198 #  define IFA_RX_PACKT rx_packets
199 #  define IFA_TX_PACKT tx_packets
200 #  define IFA_RX_ERROR rx_errors
201 #  define IFA_TX_ERROR tx_errors
202 #else
203 #  error "No suitable type for `struct ifaddrs->ifa_data' found."
204 #endif
205
206         struct IFA_DATA *if_data;
207
208         if (getifaddrs (&if_list) != 0)
209                 return (-1);
210
211         for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
212         {
213                 if (if_ptr->ifa_addr != NULL && if_ptr->ifa_addr->sa_family == AF_LINK) {
214                         if_data = (struct IFA_DATA *) if_ptr->ifa_data;
215
216                         if_submit (if_ptr->ifa_name, "if_octets",
217                                 if_data->IFA_RX_BYTES,
218                                 if_data->IFA_TX_BYTES);
219                         if_submit (if_ptr->ifa_name, "if_packets",
220                                 if_data->IFA_RX_PACKT,
221                                 if_data->IFA_TX_PACKT);
222                         if_submit (if_ptr->ifa_name, "if_errors",
223                                 if_data->IFA_RX_ERROR,
224                                 if_data->IFA_TX_ERROR);
225                 }
226         }
227
228         freeifaddrs (if_list);
229 /* #endif HAVE_GETIFADDRS */
230
231 #elif KERNEL_LINUX
232         FILE *fh;
233         char buffer[1024];
234         derive_t incoming, outgoing;
235         char *device;
236
237         char *dummy;
238         char *fields[16];
239         int numfields;
240
241         if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
242         {
243                 char errbuf[1024];
244                 WARNING ("interface plugin: fopen: %s",
245                                 sstrerror (errno, errbuf, sizeof (errbuf)));
246                 return (-1);
247         }
248
249         while (fgets (buffer, 1024, fh) != NULL)
250         {
251                 if (!(dummy = strchr(buffer, ':')))
252                         continue;
253                 dummy[0] = '\0';
254                 dummy++;
255
256                 device = buffer;
257                 while (device[0] == ' ')
258                         device++;
259
260                 if (device[0] == '\0')
261                         continue;
262
263                 numfields = strsplit (dummy, fields, 16);
264
265                 if (numfields < 11)
266                         continue;
267
268                 incoming = atoll (fields[0]);
269                 outgoing = atoll (fields[8]);
270                 if_submit (device, "if_octets", incoming, outgoing);
271
272                 incoming = atoll (fields[1]);
273                 outgoing = atoll (fields[9]);
274                 if_submit (device, "if_packets", incoming, outgoing);
275
276                 incoming = atoll (fields[2]);
277                 outgoing = atoll (fields[10]);
278                 if_submit (device, "if_errors", incoming, outgoing);
279         }
280
281         fclose (fh);
282 /* #endif KERNEL_LINUX */
283
284 #elif HAVE_LIBKSTAT
285         int i;
286         derive_t rx;
287         derive_t tx;
288
289         if (kc == NULL)
290                 return (-1);
291
292         for (i = 0; i < numif; i++)
293         {
294                 if (kstat_read (kc, ksp[i], NULL) == -1)
295                         continue;
296
297                 /* try to get 64bit counters */
298                 rx = get_kstat_value (ksp[i], "rbytes64");
299                 tx = get_kstat_value (ksp[i], "obytes64");
300                 /* or fallback to 32bit */
301                 if (rx == -1LL)
302                         rx = get_kstat_value (ksp[i], "rbytes");
303                 if (tx == -1LL)
304                         tx = get_kstat_value (ksp[i], "obytes");
305                 if ((rx != -1LL) || (tx != -1LL))
306                         if_submit (ksp[i]->ks_name, "if_octets", rx, tx);
307
308                 /* try to get 64bit counters */
309                 rx = get_kstat_value (ksp[i], "ipackets64");
310                 tx = get_kstat_value (ksp[i], "opackets64");
311                 /* or fallback to 32bit */
312                 if (rx == -1LL)
313                         rx = get_kstat_value (ksp[i], "ipackets");
314                 if (tx == -1LL)
315                         tx = get_kstat_value (ksp[i], "opackets");
316                 if ((rx != -1LL) || (tx != -1LL))
317                         if_submit (ksp[i]->ks_name, "if_packets", rx, tx);
318
319                 /* no 64bit error counters yet */
320                 rx = get_kstat_value (ksp[i], "ierrors");
321                 tx = get_kstat_value (ksp[i], "oerrors");
322                 if ((rx != -1LL) || (tx != -1LL))
323                         if_submit (ksp[i]->ks_name, "if_errors", rx, tx);
324         }
325 /* #endif HAVE_LIBKSTAT */
326
327 #elif defined(HAVE_LIBSTATGRAB)
328         sg_network_io_stats *ios;
329         int i, num;
330
331         ios = sg_get_network_io_stats (&num);
332
333         for (i = 0; i < num; i++)
334                 if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
335 /* #endif HAVE_LIBSTATGRAB */
336
337 #elif defined(HAVE_PERFSTAT)
338         perfstat_id_t id;
339         int i, ifs;
340
341         if ((nif =  perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t), 0)) < 0)
342         {
343                 char errbuf[1024];
344                 WARNING ("interface plugin: perfstat_netinterface: %s",
345                         sstrerror (errno, errbuf, sizeof (errbuf)));
346                 return (-1);
347         }
348
349         if (pnif != nif || ifstat == NULL)
350         {
351                 if (ifstat != NULL)
352                         free(ifstat);
353                 ifstat = malloc(nif * sizeof(perfstat_netinterface_t));
354         }
355         pnif = nif;
356
357         id.name[0]='\0';
358         if ((ifs = perfstat_netinterface(&id, ifstat, sizeof(perfstat_netinterface_t), nif)) < 0)
359         {
360                 char errbuf[1024];
361                 WARNING ("interface plugin: perfstat_netinterface (interfaces=%d): %s",
362                         nif, sstrerror (errno, errbuf, sizeof (errbuf)));
363                 return (-1);
364         }
365
366         for (i = 0; i < ifs; i++)
367         {
368                 if_submit (ifstat[i].name, "if_octets", ifstat[i].ibytes, ifstat[i].obytes);
369                 if_submit (ifstat[i].name, "if_packets", ifstat[i].ipackets ,ifstat[i].opackets);
370                 if_submit (ifstat[i].name, "if_errors", ifstat[i].ierrors, ifstat[i].oerrors );
371         }
372 #endif /* HAVE_PERFSTAT */
373
374         return (0);
375 } /* int interface_read */
376
377 void module_register (void)
378 {
379         plugin_register_config ("interface", interface_config,
380                         config_keys, config_keys_num);
381 #if HAVE_LIBKSTAT
382         plugin_register_init ("interface", interface_init);
383 #endif
384         plugin_register_read ("interface", interface_read);
385 } /* void module_register */