src/plugin.h: Added `VALUE_LIST_INIT', a static initializer for value_list_t.
[collectd.git] / src / traffic.c
1 /**
2  * collectd - src/traffic.c
3  * Copyright (C) 2005,2006  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; either version 2 of the License, or (at your
8  * option) any later version.
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 verplant.org>
21  *   Sune Marcher <sm at flork.dk>
22  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "configfile.h"
28
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
35
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
42
43 #if HAVE_LINUX_NETDEVICE_H
44 #  include <linux/netdevice.h>
45 #endif
46 #if HAVE_IFADDRS_H
47 #  include <ifaddrs.h>
48 #endif
49
50 #define MODULE_NAME "traffic"
51
52 /*
53  * Various people have reported problems with `getifaddrs' and varying versions
54  * of `glibc'. That's why it's disabled by default. Since more statistics are
55  * available this way one may enable it using the `--enable-getifaddrs' option
56  * of the configure script. -octo
57  */
58 #if KERNEL_LINUX
59 # if !COLLECT_GETIFADDRS
60 #  undef HAVE_GETIFADDRS
61 # endif /* !COLLECT_GETIFADDRS */
62 #endif /* KERNEL_LINUX */
63
64 #if HAVE_GETIFADDRS || KERNEL_LINUX || HAVE_LIBKSTAT || HAVE_LIBSTATGRAB
65 # define TRAFFIC_HAVE_READ 1
66 #else
67 # define TRAFFIC_HAVE_READ 0
68 #endif
69
70 #define BUFSIZE 512
71
72 /*
73  * (Module-)Global variables
74  */
75 /* 2^32 = 4294967296 = ~4.2GByte/s = ~34GBit/s */
76 static data_source_t octets_dsrc[2] =
77 {
78         {"rx", DS_TYPE_COUNTER, 0, 4294967295.0},
79         {"tx", DS_TYPE_COUNTER, 0, 4294967295.0}
80 };
81
82 static data_set_t octets_ds =
83 {
84         "if_octets", 2, octets_dsrc
85 };
86
87 static data_source_t packets_dsrc[2] =
88 {
89         {"rx", DS_TYPE_COUNTER, 0, 4294967295.0},
90         {"tx", DS_TYPE_COUNTER, 0, 4294967295.0}
91 };
92
93 static data_set_t packets_ds =
94 {
95         "if_packets", 2, packets_dsrc
96 };
97
98 static data_source_t errors_dsrc[2] =
99 {
100         {"rx", DS_TYPE_COUNTER, 0, 4294967295.0},
101         {"tx", DS_TYPE_COUNTER, 0, 4294967295.0}
102 };
103
104 static data_set_t errors_ds =
105 {
106         "if_errors", 2, errors_dsrc
107 };
108
109 static const char *config_keys[] =
110 {
111         "Interface",
112         "IgnoreSelected",
113         NULL
114 };
115 static int config_keys_num = 2;
116
117 static char **if_list = NULL;
118 static int    if_list_num = 0;
119 /* 
120  * if_list_action:
121  * 0 => default is to collect selected interface
122  * 1 => ignore selcted interfaces
123  */
124 static int    if_list_action = 0;
125
126 #ifdef HAVE_LIBKSTAT
127 #define MAX_NUMIF 256
128 extern kstat_ctl_t *kc;
129 static kstat_t *ksp[MAX_NUMIF];
130 static int numif = 0;
131 #endif /* HAVE_LIBKSTAT */
132
133 static int interface_config (const char *key, const char *value)
134 {
135         char **temp;
136
137         if (strcasecmp (key, "Interface") == 0)
138         {
139                 temp = (char **) realloc (if_list, (if_list_num + 1) * sizeof (char *));
140                 if (temp == NULL)
141                 {
142                         syslog (LOG_EMERG, "Cannot allocate more memory.");
143                         return (1);
144                 }
145                 if_list = temp;
146
147                 if ((if_list[if_list_num] = strdup (value)) == NULL)
148                 {
149                         syslog (LOG_EMERG, "Cannot allocate memory.");
150                         return (1);
151                 }
152                 if_list_num++;
153         }
154         else if (strcasecmp (key, "IgnoreSelected") == 0)
155         {
156                 if ((strcasecmp (value, "True") == 0)
157                                 || (strcasecmp (value, "Yes") == 0)
158                                 || (strcasecmp (value, "On") == 0))
159                         if_list_action = 1;
160                 else
161                         if_list_action = 0;
162         }
163         else
164         {
165                 return (-1);
166         }
167
168         return (0);
169 }
170
171 #if HAVE_LIBKSTAT
172 static int traffic_init (void)
173 {
174 #if HAVE_LIBKSTAT
175         kstat_t *ksp_chain;
176         unsigned long long val;
177
178         numif = 0;
179
180         if (kc == NULL)
181                 return (-1);
182
183         for (numif = 0, ksp_chain = kc->kc_chain;
184                         (numif < MAX_NUMIF) && (ksp_chain != NULL);
185                         ksp_chain = ksp_chain->ks_next)
186         {
187                 if (strncmp (ksp_chain->ks_class, "net", 3))
188                         continue;
189                 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
190                         continue;
191                 if (kstat_read (kc, ksp_chain, NULL) == -1)
192                         continue;
193                 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
194                         continue;
195                 ksp[numif++] = ksp_chain;
196         }
197 #endif /* HAVE_LIBKSTAT */
198
199         return (0);
200 } /* int traffic_init */
201 #endif /* HAVE_LIBKSTAT */
202
203 /*
204  * Check if this interface/instance should be ignored. This is called from
205  * both, `submit' and `write' to give client and server the ability to
206  * ignore certain stuff..
207  */
208 static int check_ignore_if (const char *interface)
209 {
210         int i;
211
212         /* If no interfaces are given collect all interfaces. Mostly to be
213          * backwards compatible, but also because this is much easier. */
214         if (if_list_num < 1)
215                 return (0);
216
217         for (i = 0; i < if_list_num; i++)
218                 if (strcasecmp (interface, if_list[i]) == 0)
219                         return (if_list_action);
220         return (1 - if_list_action);
221 } /* int check_ignore_if */
222
223 #if TRAFFIC_HAVE_READ
224 static void if_submit (const char *dev, const char *type,
225                 unsigned long long rx,
226                 unsigned long long tx)
227 {
228         value_t values[2];
229         value_list_t vl = VALUE_LIST_INIT;
230
231         if (check_ignore_if (dev))
232                 return;
233
234         values[0].counter = rx;
235         values[1].counter = tx;
236
237         vl.values = values;
238         vl.values_len = 2;
239         vl.time = time (NULL);
240
241         /* FIXME: do this globally */
242         if (gethostname (vl.host, sizeof (vl.host)) != 0)
243         {
244                 syslog (LOG_ERR, "load plugin: gethostname failed: %s",
245                                 strerror (errno));
246                 return;
247         }
248         strcpy (vl.plugin, "interface");
249         strcpy (vl.plugin_instance, "");
250         strncpy (vl.type_instance, dev, sizeof (vl.type_instance));
251
252         plugin_dispatch_values (type, &vl);
253 } /* void if_submit */
254
255 static int traffic_read (void)
256 {
257 #if HAVE_GETIFADDRS
258         struct ifaddrs *if_list;
259         struct ifaddrs *if_ptr;
260
261 /* Darin/Mac OS X and possible other *BSDs */
262 #if HAVE_STRUCT_IF_DATA
263 #  define IFA_DATA if_data
264 #  define IFA_RX_BYTES ifi_ibytes
265 #  define IFA_TX_BYTES ifi_obytes
266 #  define IFA_RX_PACKT ifi_ipackets
267 #  define IFA_TX_PACKT ifi_opackets
268 #  define IFA_RX_ERROR ifi_ierrors
269 #  define IFA_TX_ERROR ifi_oerrors
270 /* #endif HAVE_STRUCT_IF_DATA */
271
272 #elif HAVE_STRUCT_NET_DEVICE_STATS
273 #  define IFA_DATA net_device_stats
274 #  define IFA_RX_BYTES rx_bytes
275 #  define IFA_TX_BYTES tx_bytes
276 #  define IFA_RX_PACKT rx_packets
277 #  define IFA_TX_PACKT tx_packets
278 #  define IFA_RX_ERROR rx_errors
279 #  define IFA_TX_ERROR tx_errors
280 #else
281 #  error "No suitable type for `struct ifaddrs->ifa_data' found."
282 #endif
283
284         struct IFA_DATA *if_data;
285
286         if (getifaddrs (&if_list) != 0)
287                 return;
288
289         for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
290         {
291                 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
292                         continue;
293
294                 if_submit (if_ptr->ifa_name, "if_octets",
295                                 if_data->IFA_RX_BYTES,
296                                 if_data->IFA_TX_BYTES);
297                 if_submit (if_ptr->ifa_name, "if_packets",
298                                 if_data->IFA_RX_PACKT,
299                                 if_data->IFA_TX_PACKT);
300                 if_submit (if_ptr->ifa_name, "if_errors",
301                                 if_data->IFA_RX_ERROR,
302                                 if_data->IFA_TX_ERROR);
303         }
304
305         freeifaddrs (if_list);
306 /* #endif HAVE_GETIFADDRS */
307
308 #elif KERNEL_LINUX
309         FILE *fh;
310         char buffer[1024];
311         unsigned long long incoming, outgoing;
312         char *device;
313         
314         char *dummy;
315         char *fields[16];
316         int numfields;
317
318         if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
319         {
320                 syslog (LOG_WARNING, "traffic: fopen: %s", strerror (errno));
321                 return (-1);
322         }
323
324         while (fgets (buffer, 1024, fh) != NULL)
325         {
326                 if (!(dummy = strchr(buffer, ':')))
327                         continue;
328                 dummy[0] = '\0';
329                 dummy++;
330
331                 device = buffer;
332                 while (device[0] == ' ')
333                         device++;
334
335                 if (device[0] == '\0')
336                         continue;
337                 
338                 numfields = strsplit (dummy, fields, 16);
339
340                 if (numfields < 11)
341                         continue;
342
343                 incoming = atoll (fields[0]);
344                 outgoing = atoll (fields[8]);
345                 if_submit (device, "if_octets", incoming, outgoing);
346
347                 incoming = atoll (fields[1]);
348                 outgoing = atoll (fields[9]);
349                 if_submit (device, "if_packets", incoming, outgoing);
350
351                 incoming = atoll (fields[2]);
352                 outgoing = atoll (fields[10]);
353                 if_submit (device, "if_errors", incoming, outgoing);
354         }
355
356         fclose (fh);
357 /* #endif KERNEL_LINUX */
358
359 #elif HAVE_LIBKSTAT
360         int i;
361         unsigned long long rx;
362         unsigned long long tx;
363
364         if (kc == NULL)
365                 return;
366
367         for (i = 0; i < numif; i++)
368         {
369                 if (kstat_read (kc, ksp[i], NULL) == -1)
370                         continue;
371
372                 rx = get_kstat_value (ksp[i], "rbytes");
373                 tx = get_kstat_value (ksp[i], "obytes");
374                 if ((rx != -1LL) || (tx != -1LL))
375                         if_submit (ksp[i]->ks_name, "if_octets", rx, tx);
376
377                 rx = get_kstat_value (ksp[i], "ipackets");
378                 tx = get_kstat_value (ksp[i], "opackets");
379                 if ((rx != -1LL) || (tx != -1LL))
380                         if_submit (ksp[i]->ks_name, "if_packets", rx, tx);
381
382                 rx = get_kstat_value (ksp[i], "ierrors");
383                 tx = get_kstat_value (ksp[i], "oerrors");
384                 if ((rx != -1LL) || (tx != -1LL))
385                         if_submit (ksp[i]->ks_name, "if_errors", rx, tx);
386         }
387 /* #endif HAVE_LIBKSTAT */
388
389 #elif defined(HAVE_LIBSTATGRAB)
390         sg_network_io_stats *ios;
391         int i, num;
392
393         ios = sg_get_network_io_stats (&num);
394
395         for (i = 0; i < num; i++)
396                 if_submit (ios[i].interface_name, "if_octets", ios[i].rx, ios[i].tx);
397 #endif /* HAVE_LIBSTATGRAB */
398
399         return (0);
400 } /* int traffic_read */
401 #endif /* TRAFFIC_HAVE_READ */
402
403 void module_register (void)
404 {
405         plugin_register_data_set (&octets_ds);
406         plugin_register_data_set (&packets_ds);
407         plugin_register_data_set (&errors_ds);
408
409         plugin_register_config ("interface", interface_config,
410                         config_keys, config_keys_num);
411
412 #if HAVE_LIBKSTAT
413         plugin_register_init ("interface", traffic_init);
414 #endif
415
416 #if TRAFFIC_HAVE_READ
417         plugin_register_read ("interface", traffic_read);
418 #endif
419 }
420
421 #undef BUFSIZE
422 #undef MODULE_NAME