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