inteface plugin: add formatting string
[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         "ActiveInterfaceOnly",
88         NULL
89 };
90 static int config_keys_num = 3;
91
92 static ignorelist_t *ignorelist = NULL;
93
94 static _Bool active_interface_only = 0;
95
96 #ifdef HAVE_LIBKSTAT
97 #define MAX_NUMIF 256
98 extern kstat_ctl_t *kc;
99 static kstat_t *ksp[MAX_NUMIF];
100 static int numif = 0;
101 static _Bool unique_name = 0;
102 #endif /* HAVE_LIBKSTAT */
103
104 static int interface_config (const char *key, const char *value)
105 {
106         if (ignorelist == NULL)
107                 ignorelist = ignorelist_create (/* invert = */ 1);
108
109         if (strcasecmp (key, "Interface") == 0)
110         {
111                 ignorelist_add (ignorelist, value);
112         }
113         else if (strcasecmp (key, "IgnoreSelected") == 0)
114         {
115                 int invert = 1;
116                 if (IS_TRUE (value))
117                         invert = 0;
118                 ignorelist_set_invert (ignorelist, invert);
119         }
120         else if (strcasecmp (key, "ActiveInterfaceOnly") == 0)
121         {
122                 if (IS_TRUE (value))
123                         active_interface_only = 1;
124                 else
125                         active_interface_only = 0;
126         }
127         else if (strcasecmp (key, "UniqueName") == 0)
128         {
129                 #ifdef HAVE_LIBKSTAT
130                 if (IS_TRUE (value))
131                         unique_name = 1;
132                 #else
133                         WARNING ("interface plugin: the \"UniqueName\" option is only valid on Solaris.");
134                 #endif /* HAVE_LIBKSTAT */
135         }
136         else
137         {
138                 return (-1);
139         }
140
141         return (0);
142 }
143
144 #if HAVE_LIBKSTAT
145 static int interface_init (void)
146 {
147         kstat_t *ksp_chain;
148         derive_t val;
149
150         numif = 0;
151
152         if (kc == NULL)
153                 return (-1);
154
155         for (numif = 0, ksp_chain = kc->kc_chain;
156                         (numif < MAX_NUMIF) && (ksp_chain != NULL);
157                         ksp_chain = ksp_chain->ks_next)
158         {
159                 if (strncmp (ksp_chain->ks_class, "net", 3))
160                         continue;
161                 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
162                         continue;
163                 if (kstat_read (kc, ksp_chain, NULL) == -1)
164                         continue;
165                 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
166                         continue;
167                 ksp[numif++] = ksp_chain;
168         }
169
170         return (0);
171 } /* int interface_init */
172 #endif /* HAVE_LIBKSTAT */
173
174 static void if_submit (const char *dev, const char *type,
175                 derive_t rx,
176                 derive_t tx)
177 {
178         value_t values[2];
179         value_list_t vl = VALUE_LIST_INIT;
180
181         if (ignorelist_match (ignorelist, dev) != 0)
182                 return;
183
184         values[0].derive = rx;
185         values[1].derive = tx;
186
187         vl.values = values;
188         vl.values_len = 2;
189         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
190         sstrncpy (vl.plugin, "interface", sizeof (vl.plugin));
191         sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
192         sstrncpy (vl.type, type, sizeof (vl.type));
193
194         plugin_dispatch_values (&vl);
195 } /* void if_submit */
196
197 static int interface_read (void)
198 {
199 #if HAVE_GETIFADDRS
200         struct ifaddrs *if_list;
201         struct ifaddrs *if_ptr;
202
203 /* Darwin/Mac OS X and possible other *BSDs */
204 #if HAVE_STRUCT_IF_DATA
205 #  define IFA_DATA if_data
206 #  define IFA_RX_BYTES ifi_ibytes
207 #  define IFA_TX_BYTES ifi_obytes
208 #  define IFA_RX_PACKT ifi_ipackets
209 #  define IFA_TX_PACKT ifi_opackets
210 #  define IFA_RX_ERROR ifi_ierrors
211 #  define IFA_TX_ERROR ifi_oerrors
212 /* #endif HAVE_STRUCT_IF_DATA */
213
214 #elif HAVE_STRUCT_NET_DEVICE_STATS
215 #  define IFA_DATA net_device_stats
216 #  define IFA_RX_BYTES rx_bytes
217 #  define IFA_TX_BYTES tx_bytes
218 #  define IFA_RX_PACKT rx_packets
219 #  define IFA_TX_PACKT tx_packets
220 #  define IFA_RX_ERROR rx_errors
221 #  define IFA_TX_ERROR tx_errors
222 #else
223 #  error "No suitable type for `struct ifaddrs->ifa_data' found."
224 #endif
225
226         struct IFA_DATA *if_data;
227
228         if (getifaddrs (&if_list) != 0)
229                 return (-1);
230
231         for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
232         {
233                 if (if_ptr->ifa_addr != NULL && if_ptr->ifa_addr->sa_family == AF_LINK) {
234                         if_data = (struct IFA_DATA *) if_ptr->ifa_data;
235
236                         if ( active_interface_only && if_data->IFA_RX_PACKT == 0 && if_data->IFA_TX_PACKT == 0 )
237                                 continue;
238
239                         if_submit (if_ptr->ifa_name, "if_octets",
240                                 if_data->IFA_RX_BYTES,
241                                 if_data->IFA_TX_BYTES);
242                         if_submit (if_ptr->ifa_name, "if_packets",
243                                 if_data->IFA_RX_PACKT,
244                                 if_data->IFA_TX_PACKT);
245                         if_submit (if_ptr->ifa_name, "if_errors",
246                                 if_data->IFA_RX_ERROR,
247                                 if_data->IFA_TX_ERROR);
248                 }
249         }
250
251         freeifaddrs (if_list);
252 /* #endif HAVE_GETIFADDRS */
253
254 #elif KERNEL_LINUX
255         FILE *fh;
256         char buffer[1024];
257         derive_t incoming, outgoing;
258         char *device;
259
260         char *dummy;
261         char *fields[16];
262         int numfields;
263
264         if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
265         {
266                 char errbuf[1024];
267                 WARNING ("interface plugin: fopen: %s",
268                                 sstrerror (errno, errbuf, sizeof (errbuf)));
269                 return (-1);
270         }
271
272         while (fgets (buffer, 1024, fh) != NULL)
273         {
274                 if (!(dummy = strchr(buffer, ':')))
275                         continue;
276                 dummy[0] = '\0';
277                 dummy++;
278
279                 device = buffer;
280                 while (device[0] == ' ')
281                         device++;
282
283                 if (device[0] == '\0')
284                         continue;
285
286                 numfields = strsplit (dummy, fields, 16);
287
288                 if (numfields < 11)
289                         continue;
290
291                 incoming = atoll (fields[1]);
292                 outgoing = atoll (fields[9]);
293                 if ( active_interface_only && incoming == 0 && outgoing == 0 )
294                         continue;
295
296                 if_submit (device, "if_packets", incoming, outgoing);
297
298                 incoming = atoll (fields[0]);
299                 outgoing = atoll (fields[8]);
300                 if_submit (device, "if_octets", incoming, outgoing);
301
302                 incoming = atoll (fields[2]);
303                 outgoing = atoll (fields[10]);
304                 if_submit (device, "if_errors", incoming, outgoing);
305
306                 incoming = atoll (fields[3]);
307                 outgoing = atoll (fields[11]);
308                 if_submit (device, "if_dropped", incoming, outgoing);
309         }
310
311         fclose (fh);
312 /* #endif KERNEL_LINUX */
313
314 #elif HAVE_LIBKSTAT
315         int i;
316         derive_t rx;
317         derive_t tx;
318         char iname[DATA_MAX_NAME_LEN];
319
320         if (kc == NULL)
321                 return (-1);
322
323         for (i = 0; i < numif; i++)
324         {
325                 if (kstat_read (kc, ksp[i], NULL) == -1)
326                         continue;
327
328                 if (unique_name)
329                         ssnprintf(iname, sizeof(iname), "%s_%d_%s", ksp[i]->ks_module, ksp[i]->ks_instance, ksp[i]->ks_name);
330                 else
331                         sstrncpy(iname, ksp[i]->ks_name, sizeof(iname));
332
333                 /* try to get 64bit counters */
334                 rx = get_kstat_value (ksp[i], "ipackets64");
335                 tx = get_kstat_value (ksp[i], "opackets64");
336                 /* or fallback to 32bit */
337                 if (rx == -1LL)
338                         rx = get_kstat_value (ksp[i], "ipackets");
339                 if (tx == -1LL)
340                         tx = get_kstat_value (ksp[i], "opackets");
341                 if ( active_interface_only && rx == 0 && tx == 0 )
342                         continue;
343                 if ((rx != -1LL) || (tx != -1LL))
344                         if_submit (iname, "if_packets", rx, tx);
345
346                 /* try to get 64bit counters */
347                 rx = get_kstat_value (ksp[i], "rbytes64");
348                 tx = get_kstat_value (ksp[i], "obytes64");
349                 /* or fallback to 32bit */
350                 if (rx == -1LL)
351                         rx = get_kstat_value (ksp[i], "rbytes");
352                 if (tx == -1LL)
353                         tx = get_kstat_value (ksp[i], "obytes");
354                 if ((rx != -1LL) || (tx != -1LL))
355                         if_submit (iname, "if_octets", rx, tx);
356
357                 /* no 64bit error counters yet */
358                 rx = get_kstat_value (ksp[i], "ierrors");
359                 tx = get_kstat_value (ksp[i], "oerrors");
360                 if ((rx != -1LL) || (tx != -1LL))
361                         if_submit (iname, "if_errors", rx, tx);
362         }
363 /* #endif HAVE_LIBKSTAT */
364
365 #elif defined(HAVE_LIBSTATGRAB)
366         sg_network_io_stats *ios;
367         int i, num;
368
369         ios = sg_get_network_io_stats (&num);
370
371         for (i = 0; i < num; i++) {
372                 if ( active_interface_only && ios[i].rx == 0 && ios[i].tx == 0 )
373                         continue;
374                 if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
375         }
376 /* #endif HAVE_LIBSTATGRAB */
377
378 #elif defined(HAVE_PERFSTAT)
379         perfstat_id_t id;
380         int i, ifs;
381
382         if ((nif =  perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t), 0)) < 0)
383         {
384                 char errbuf[1024];
385                 WARNING ("interface plugin: perfstat_netinterface: %s",
386                         sstrerror (errno, errbuf, sizeof (errbuf)));
387                 return (-1);
388         }
389
390         if (pnif != nif || ifstat == NULL)
391         {
392                 free(ifstat);
393                 ifstat = malloc(nif * sizeof (*ifstat));
394         }
395         pnif = nif;
396
397         id.name[0]='\0';
398         if ((ifs = perfstat_netinterface(&id, ifstat, sizeof(perfstat_netinterface_t), nif)) < 0)
399         {
400                 char errbuf[1024];
401                 WARNING ("interface plugin: perfstat_netinterface (interfaces=%d): %s",
402                         nif, sstrerror (errno, errbuf, sizeof (errbuf)));
403                 return (-1);
404         }
405
406         for (i = 0; i < ifs; i++)
407         {
408                 if ( active_interface_only && ifstat[i].ipackets == 0 && ifstat[i].opackets == 0 )
409                         continue;
410
411                 if_submit (ifstat[i].name, "if_octets", ifstat[i].ibytes, ifstat[i].obytes);
412                 if_submit (ifstat[i].name, "if_packets", ifstat[i].ipackets ,ifstat[i].opackets);
413                 if_submit (ifstat[i].name, "if_errors", ifstat[i].ierrors, ifstat[i].oerrors );
414         }
415 #endif /* HAVE_PERFSTAT */
416
417         return (0);
418 } /* int interface_read */
419
420 void module_register (void)
421 {
422         plugin_register_config ("interface", interface_config,
423                         config_keys, config_keys_num);
424 #if HAVE_LIBKSTAT
425         plugin_register_init ("interface", interface_init);
426 #endif
427         plugin_register_read ("interface", interface_read);
428 } /* void module_register */
429
430 /*
431  * Local variables:
432  *  c-file-style: "linux"
433  *  indent-tabs-mode: t
434  *  c-indent-level: 4
435  *  c-basic-offset: 4
436  *  tab-width: 4
437  * End:
438  */