svn merge -r523:547 branches/config-step trunk
[collectd.git] / src / traffic.c
1 /**
2  * collectd - src/traffic.c
3  * Copyright (C) 2005  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  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26
27 #define MODULE_NAME "traffic"
28
29 #if defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT) || defined(HAVE_LIBSTATGRAB)
30 # define TRAFFIC_HAVE_READ 1
31 #else
32 # define TRAFFIC_HAVE_READ 0
33 #endif
34
35 #define BUFSIZE 512
36
37 static char *traffic_filename_template = "traffic-%s.rrd";
38
39 static char *ds_def[] =
40 {
41         "DS:incoming:COUNTER:"COLLECTD_HEARTBEAT":0:U",
42         "DS:outgoing:COUNTER:"COLLECTD_HEARTBEAT":0:U",
43         NULL
44 };
45 static int ds_num = 2;
46
47 #ifdef HAVE_LIBKSTAT
48 #define MAX_NUMIF 256
49 extern kstat_ctl_t *kc;
50 static kstat_t *ksp[MAX_NUMIF];
51 static int numif = 0;
52 #endif /* HAVE_LIBKSTAT */
53
54 static void traffic_init (void)
55 {
56 #ifdef HAVE_LIBKSTAT
57         kstat_t *ksp_chain;
58         unsigned long long val;
59
60         numif = 0;
61
62         if (kc == NULL)
63                 return;
64
65         for (numif = 0, ksp_chain = kc->kc_chain;
66                         (numif < MAX_NUMIF) && (ksp_chain != NULL);
67                         ksp_chain = ksp_chain->ks_next)
68         {
69                 if (strncmp (ksp_chain->ks_class, "net", 3))
70                         continue;
71                 if (ksp_chain->ks_type != KSTAT_TYPE_NAMED)
72                         continue;
73                 if (kstat_read (kc, ksp_chain, NULL) == -1)
74                         continue;
75                 if ((val = get_kstat_value (ksp_chain, "obytes")) == -1LL)
76                         continue;
77                 ksp[numif++] = ksp_chain;
78         }
79 #endif /* HAVE_LIBKSTAT */
80 }
81
82 static void traffic_write (char *host, char *inst, char *val)
83 {
84         char file[BUFSIZE];
85         int status;
86
87         status = snprintf (file, BUFSIZE, traffic_filename_template, inst);
88         if (status < 1)
89                 return;
90         else if (status >= BUFSIZE)
91                 return;
92
93         rrd_update_file (host, file, val, ds_def, ds_num);
94 }
95
96 #if TRAFFIC_HAVE_READ
97 static void traffic_submit (char *device,
98                 unsigned long long incoming,
99                 unsigned long long outgoing)
100 {
101         char buf[BUFSIZE];
102
103         if (snprintf (buf, BUFSIZE, "%u:%lld:%lld", (unsigned int) curtime, incoming, outgoing) >= BUFSIZE)
104                 return;
105
106         plugin_submit (MODULE_NAME, device, buf);
107 }
108
109 static void traffic_read (void)
110 {
111 #ifdef KERNEL_LINUX
112         FILE *fh;
113         char buffer[1024];
114         unsigned long long incoming, outgoing;
115         char *device;
116         
117         char *dummy;
118         char *fields[16];
119         int numfields;
120
121         if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
122         {
123                 syslog (LOG_WARNING, "traffic: fopen: %s", strerror (errno));
124                 return;
125         }
126
127         while (fgets (buffer, 1024, fh) != NULL)
128         {
129                 if (buffer[6] != ':')
130                         continue;
131                 buffer[6] = '\0';
132
133                 device = buffer;
134                 while (device[0] == ' ')
135                         device++;
136
137                 if (device[0] == '\0')
138                         continue;
139                 
140                 dummy = buffer + 7;
141                 numfields = strsplit (dummy, fields, 16);
142
143                 if (numfields < 9)
144                         continue;
145
146                 incoming = atoll (fields[0]);
147                 outgoing = atoll (fields[8]);
148
149                 traffic_submit (device, incoming, outgoing);
150         }
151
152         fclose (fh);
153 /* #endif KERNEL_LINUX */
154
155 #elif defined(HAVE_LIBKSTAT)
156         int i;
157         unsigned long long incoming, outgoing;
158
159         if (kc == NULL)
160                 return;
161
162         for (i = 0; i < numif; i++)
163         {
164                 if (kstat_read (kc, ksp[i], NULL) == -1)
165                         continue;
166
167                 if ((incoming = get_kstat_value (ksp[i], "rbytes")) == -1LL)
168                         continue;
169                 if ((outgoing = get_kstat_value (ksp[i], "obytes")) == -1LL)
170                         continue;
171
172                 traffic_submit (ksp[i]->ks_name, incoming, outgoing);
173         }
174 /* #endif HAVE_LIBKSTAT */
175
176 #elif defined(HAVE_LIBSTATGRAB)
177         sg_network_io_stats *ios;
178         int i, num;
179
180         ios = sg_get_network_io_stats (&num);
181
182         for (i = 0; i < num; i++)
183                 traffic_submit (ios[i].interface_name, ios[i].rx, ios[i].tx);
184 #endif /* HAVE_LIBSTATGRAB */
185 }
186 #else
187 #define traffic_read NULL
188 #endif /* TRAFFIC_HAVE_READ */
189
190 void module_register (void)
191 {
192         plugin_register (MODULE_NAME, traffic_init, traffic_read, traffic_write);
193 }
194
195 #undef BUFSIZE
196 #undef MODULE_NAME