traffic plugin: Store the packet- and error-counter for the proc interface, too.
[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 /* TODO: Move this to `interface-%s/<blah>.rrd' in version 4. */
76 static char *bytes_file   = "traffic-%s.rrd";
77 static char *packets_file = "if_packets-%s.rrd";
78 static char *errors_file  = "if_errors-%s.rrd";
79 /* TODO: Maybe implement multicast and broadcast counters */
80
81 static char *config_keys[] =
82 {
83         "Ignore",
84         NULL
85 };
86 static int config_keys_num = 1;
87
88 static char *bytes_ds_def[] =
89 {
90         "DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U",
91         "DS:outgoing:COUNTER:"COLLECTD_HEARTBEAT":0:U",
92         NULL
93 };
94 static int bytes_ds_num = 2;
95
96 static char *packets_ds_def[] =
97 {
98         "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
99         "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
100         NULL
101 };
102 static int packets_ds_num = 2;
103
104 static char *errors_ds_def[] =
105 {
106         "DS:rx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
107         "DS:tx:COUNTER:"COLLECTD_HEARTBEAT":0:U",
108         NULL
109 };
110 static int errors_ds_num = 2;
111
112 static char **if_ignore_list = NULL;
113 static int    if_ignore_list_num = 0;
114
115 #ifdef HAVE_LIBKSTAT
116 #define MAX_NUMIF 256
117 extern kstat_ctl_t *kc;
118 static kstat_t *ksp[MAX_NUMIF];
119 static int numif = 0;
120 #endif /* HAVE_LIBKSTAT */
121
122 static int traffic_config (char *key, char *value)
123 {
124         char **temp;
125
126         if (strcasecmp (key, "Ignore") != 0)
127                 return (-1);
128
129         temp = (char **) realloc (if_ignore_list, (if_ignore_list_num + 1) * sizeof (char *));
130         if (temp == NULL)
131         {
132                 syslog (LOG_EMERG, "Cannot allocate more memory.");
133                 return (1);
134         }
135         if_ignore_list = temp;
136
137         if ((if_ignore_list[if_ignore_list_num] = strdup (value)) == NULL)
138         {
139                 syslog (LOG_EMERG, "Cannot allocate memory.");
140                 return (1);
141         }
142         if_ignore_list_num++;
143
144         syslog (LOG_NOTICE, "traffic: Ignoring interface `%s'", value);
145
146         return (0);
147 }
148
149 static void traffic_init (void)
150 {
151 #if HAVE_GETIFADDRS
152         /* nothing */
153 /* #endif HAVE_GETIFADDRS */
154
155 #elif KERNEL_LINUX
156         /* nothing */
157 /* #endif KERNEL_LINUX */
158
159 #elif HAVE_LIBKSTAT
160         kstat_t *ksp_chain;
161         unsigned long long val;
162
163         numif = 0;
164
165         if (kc == NULL)
166                 return;
167
168         for (numif = 0, ksp_chain = kc->kc_chain;
169                         (numif < MAX_NUMIF) && (ksp_chain != NULL);
170                         ksp_chain = ksp_chain->ks_next)
171         {
172                 if (strncmp (ksp_chain->ks_class, "net", 3))
173                         continue;
174                 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
175                         continue;
176                 if (kstat_read (kc, ksp_chain, NULL) == -1)
177                         continue;
178                 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
179                         continue;
180                 ksp[numif++] = ksp_chain;
181         }
182 /* #endif HAVE_LIBKSTAT */
183
184 #elif HAVE_LIBSTATG
185         /* nothing */
186 #endif /* HAVE_LIBSTATG */
187
188         return;
189 }
190
191 /*
192  * Check if this interface/instance should be ignored. This is called from
193  * both, `submit' and `write' to give client and server the ability to ignore
194  * certain stuff..
195  */
196 static int check_ignore_if (const char *interface)
197 {
198         int i;
199
200         for (i = 0; i < if_ignore_list_num; i++)
201                 if (strcasecmp (interface, if_ignore_list[i]) == 0)
202                         return (1);
203         return (0);
204 }
205
206 static void generic_write (char *host, char *inst, char *val,
207                 char *file_template,
208                 char **ds_def, int ds_num)
209 {
210         char file[512];
211         int status;
212
213         if (check_ignore_if (inst))
214                 return;
215
216         status = snprintf (file, BUFSIZE, file_template, inst);
217         if (status < 1)
218                 return;
219         else if (status >= 512)
220                 return;
221
222         rrd_update_file (host, file, val, ds_def, ds_num);
223 }
224
225 static void bytes_write (char *host, char *inst, char *val)
226 {
227         generic_write (host, inst, val, bytes_file, bytes_ds_def, bytes_ds_num);
228 }
229
230 static void packets_write (char *host, char *inst, char *val)
231 {
232         generic_write (host, inst, val, packets_file, packets_ds_def, packets_ds_num);
233 }
234
235 static void errors_write (char *host, char *inst, char *val)
236 {
237         generic_write (host, inst, val, errors_file, errors_ds_def, errors_ds_num);
238 }
239
240 #if TRAFFIC_HAVE_READ
241 static void bytes_submit (char *dev,
242                 unsigned long long rx,
243                 unsigned long long tx)
244 {
245         char buf[512];
246         int  status;
247
248         if (check_ignore_if (dev))
249                 return;
250
251         status = snprintf (buf, 512, "%u:%lld:%lld",
252                                 (unsigned int) curtime,
253                                 rx, tx);
254         if ((status >= 512) || (status < 1))
255                 return;
256
257         plugin_submit (MODULE_NAME, dev, buf);
258 }
259
260 #if HAVE_GETIFADDRS || HAVE_LIBKSTAT
261 static void packets_submit (char *dev,
262                 unsigned long long rx,
263                 unsigned long long tx)
264 {
265         char buf[512];
266         int  status;
267
268         if (check_ignore_if (dev))
269                 return;
270
271         status = snprintf (buf, 512, "%u:%lld:%lld",
272                         (unsigned int) curtime,
273                         rx, tx);
274         if ((status >= 512) || (status < 1))
275                 return;
276         plugin_submit ("if_packets", dev, buf);
277 }
278
279 static void errors_submit (char *dev,
280                 unsigned long long rx,
281                 unsigned long long tx)
282 {
283         char buf[512];
284         int  status;
285
286         if (check_ignore_if (dev))
287                 return;
288
289         status = snprintf (buf, 512, "%u:%lld:%lld",
290                         (unsigned int) curtime,
291                         rx, tx);
292         if ((status >= 512) || (status < 1))
293                 return;
294         plugin_submit ("if_errors", dev, buf);
295 }
296 #endif /* HAVE_GETIFADDRS || HAVE_LIBKSTAT */
297
298 static void traffic_read (void)
299 {
300 #if HAVE_GETIFADDRS
301         struct ifaddrs *if_list;
302         struct ifaddrs *if_ptr;
303
304 /* Darin/Mac OS X and possible other *BSDs */
305 #if HAVE_STRUCT_IF_DATA
306 #  define IFA_DATA if_data
307 #  define IFA_RX_BYTES ifi_ibytes
308 #  define IFA_TX_BYTES ifi_obytes
309 #  define IFA_RX_PACKT ifi_ipackets
310 #  define IFA_TX_PACKT ifi_opackets
311 #  define IFA_RX_ERROR ifi_ierrors
312 #  define IFA_TX_ERROR ifi_oerrors
313 /* #endif HAVE_STRUCT_IF_DATA */
314
315 #elif HAVE_STRUCT_NET_DEVICE_STATS
316 #  define IFA_DATA net_device_stats
317 #  define IFA_RX_BYTES rx_bytes
318 #  define IFA_TX_BYTES tx_bytes
319 #  define IFA_RX_PACKT rx_packets
320 #  define IFA_TX_PACKT tx_packets
321 #  define IFA_RX_ERROR rx_errors
322 #  define IFA_TX_ERROR tx_errors
323 #else
324 #  error "No suitable type for `struct ifaddrs->ifa_data' found."
325 #endif
326
327         struct IFA_DATA *if_data;
328
329         if (getifaddrs (&if_list) != 0)
330                 return;
331
332         for (if_ptr = if_list; if_ptr != NULL; if_ptr = if_ptr->ifa_next)
333         {
334                 if ((if_data = (struct IFA_DATA *) if_ptr->ifa_data) == NULL)
335                         continue;
336
337                 bytes_submit (if_ptr->ifa_name,
338                                 if_data->IFA_RX_BYTES,
339                                 if_data->IFA_TX_BYTES);
340                 packets_submit (if_ptr->ifa_name,
341                                 if_data->IFA_RX_PACKT,
342                                 if_data->IFA_TX_PACKT);
343                 errors_submit (if_ptr->ifa_name,
344                                 if_data->IFA_RX_ERROR,
345                                 if_data->IFA_TX_ERROR);
346         }
347
348         freeifaddrs (if_list);
349 /* #endif HAVE_GETIFADDRS */
350
351 #elif KERNEL_LINUX
352         FILE *fh;
353         char buffer[1024];
354         unsigned long long incoming, outgoing;
355         char *device;
356         
357         char *dummy;
358         char *fields[16];
359         int numfields;
360
361         if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
362         {
363                 syslog (LOG_WARNING, "traffic: fopen: %s", strerror (errno));
364                 return;
365         }
366
367         while (fgets (buffer, 1024, fh) != NULL)
368         {
369                 if (!(dummy = strchr(buffer, ':')))
370                         continue;
371                 dummy[0] = '\0';
372                 dummy++;
373
374                 device = buffer;
375                 while (device[0] == ' ')
376                         device++;
377
378                 if (device[0] == '\0')
379                         continue;
380                 
381                 numfields = strsplit (dummy, fields, 16);
382
383                 if (numfields < 11)
384                         continue;
385
386                 incoming = atoll (fields[0]);
387                 outgoing = atoll (fields[8]);
388                 bytes_submit (device, incoming, outgoing);
389
390                 incoming = atoll (fields[1]);
391                 incoming = atoll (fields[9]);
392                 packets_submit (device, incoming, outgoing);
393
394                 incoming = atoll (fields[2]);
395                 incoming = atoll (fields[10]);
396                 errors_submit (device, incoming, outgoing);
397         }
398
399         fclose (fh);
400 /* #endif KERNEL_LINUX */
401
402 #elif HAVE_LIBKSTAT
403         int i;
404         unsigned long long rx;
405         unsigned long long tx;
406
407         if (kc == NULL)
408                 return;
409
410         for (i = 0; i < numif; i++)
411         {
412                 if (kstat_read (kc, ksp[i], NULL) == -1)
413                         continue;
414
415                 rx = get_kstat_value (ksp[i], "rbytes");
416                 tx = get_kstat_value (ksp[i], "obytes");
417                 if ((rx != -1LL) || (tx != -1LL))
418                         bytes_submit (ksp[i]->ks_name, rx, tx);
419
420                 rx = get_kstat_value (ksp[i], "ipackets");
421                 tx = get_kstat_value (ksp[i], "opackets");
422                 if ((rx != -1LL) || (tx != -1LL))
423                         packets_submit (ksp[i]->ks_name, rx, tx);
424
425                 rx = get_kstat_value (ksp[i], "ierrors");
426                 tx = get_kstat_value (ksp[i], "oerrors");
427                 if ((rx != -1LL) || (tx != -1LL))
428                         errors_submit (ksp[i]->ks_name, rx, tx);
429         }
430 /* #endif HAVE_LIBKSTAT */
431
432 #elif defined(HAVE_LIBSTATGRAB)
433         sg_network_io_stats *ios;
434         int i, num;
435
436         ios = sg_get_network_io_stats (&num);
437
438         for (i = 0; i < num; i++)
439                 bytes_submit (ios[i].interface_name, ios[i].rx, ios[i].tx);
440 #endif /* HAVE_LIBSTATGRAB */
441 }
442 #else
443 #define traffic_read NULL
444 #endif /* TRAFFIC_HAVE_READ */
445
446 void module_register (void)
447 {
448         plugin_register (MODULE_NAME, traffic_init, traffic_read, bytes_write);
449         plugin_register ("if_packets", NULL, NULL, packets_write);
450         plugin_register ("if_errors",  NULL, NULL, errors_write);
451         cf_register (MODULE_NAME, traffic_config, config_keys, config_keys_num);
452 }
453
454 #undef BUFSIZE
455 #undef MODULE_NAME