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 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
27 #define MODULE_NAME "traffic"
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
35 #define BUFSIZE 512
37 static char *traffic_filename_template = "traffic-%s.rrd";
39 static char *ds_def[] =
40 {
41 "DS:incoming:COUNTER:25:0:U",
42 "DS:outgoing:COUNTER:25:0:U",
43 NULL
44 };
45 static int ds_num = 2;
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 */
54 static void traffic_init (void)
55 {
56 #ifdef HAVE_LIBKSTAT
57 kstat_t *ksp_chain;
58 unsigned long long val;
60 numif = 0;
62 if (kc == NULL)
63 return;
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 }
82 static void traffic_write (char *host, char *inst, char *val)
83 {
84 char file[BUFSIZE];
85 int status;
87 status = snprintf (file, BUFSIZE, traffic_filename_template, inst);
88 if (status < 1)
89 return;
90 else if (status >= BUFSIZE)
91 return;
93 rrd_update_file (host, file, val, ds_def, ds_num);
94 }
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];
103 if (snprintf (buf, BUFSIZE, "%u:%lld:%lld", (unsigned int) curtime, incoming, outgoing) >= BUFSIZE)
104 return;
106 plugin_submit (MODULE_NAME, device, buf);
107 }
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;
117 char *dummy;
118 char *fields[16];
119 int numfields;
121 if ((fh = fopen ("/proc/net/dev", "r")) == NULL)
122 {
123 syslog (LOG_WARNING, "traffic: fopen: %s", strerror (errno));
124 return;
125 }
127 while (fgets (buffer, 1024, fh) != NULL)
128 {
129 if (buffer[6] != ':')
130 continue;
131 buffer[6] = '\0';
133 device = buffer;
134 while (device[0] == ' ')
135 device++;
137 if (device[0] == '\0')
138 continue;
140 dummy = buffer + 7;
141 numfields = strsplit (dummy, fields, 16);
143 if (numfields < 9)
144 continue;
146 incoming = atoll (fields[0]);
147 outgoing = atoll (fields[8]);
149 traffic_submit (device, incoming, outgoing);
150 }
152 fclose (fh);
153 /* #endif KERNEL_LINUX */
155 #elif defined(HAVE_LIBKSTAT)
156 int i;
157 unsigned long long incoming, outgoing;
159 if (kc == NULL)
160 return;
162 for (i = 0; i < numif; i++)
163 {
164 if (kstat_read (kc, ksp[i], NULL) == -1)
165 continue;
167 if ((incoming = get_kstat_value (ksp[i], "rbytes")) == -1LL)
168 continue;
169 if ((outgoing = get_kstat_value (ksp[i], "obytes")) == -1LL)
170 continue;
172 traffic_submit (ksp[i]->ks_name, incoming, outgoing);
173 }
174 /* #endif HAVE_LIBKSTAT */
176 #elif defined(HAVE_LIBSTATGRAB)
177 sg_network_io_stats *ios;
178 int i, num;
180 ios = sg_get_network_io_stats (&num);
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 */
190 void module_register (void)
191 {
192 plugin_register (MODULE_NAME, traffic_init, traffic_read, traffic_write);
193 }
195 #undef BUFSIZE
196 #undef MODULE_NAME