1 |
tdb |
1.22 |
/* |
2 |
pajs |
1.1 |
* i-scream central monitoring system |
3 |
tdb |
1.15 |
* http://www.i-scream.org |
4 |
tdb |
1.22 |
* Copyright (C) 2000-2004 i-scream |
5 |
pajs |
1.1 |
* |
6 |
tdb |
1.22 |
* This library is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU Lesser General Public |
8 |
|
|
* License as published by the Free Software Foundation; either |
9 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
10 |
pajs |
1.1 |
* |
11 |
tdb |
1.22 |
* This library is distributed in the hope that it will be useful, |
12 |
pajs |
1.1 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
tdb |
1.22 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
|
|
* Lesser General Public License for more details. |
15 |
pajs |
1.1 |
* |
16 |
tdb |
1.22 |
* You should have received a copy of the GNU Lesser General Public |
17 |
|
|
* License along with this library; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
19 |
|
|
* 02111-1307 USA |
20 |
tdb |
1.23 |
* |
21 |
ats |
1.24 |
* $Id: network_stats.c,v 1.23 2004/01/19 16:49:21 tdb Exp $ |
22 |
pajs |
1.1 |
*/ |
23 |
|
|
|
24 |
|
|
#ifdef HAVE_CONFIG_H |
25 |
|
|
#include "config.h" |
26 |
|
|
#endif |
27 |
|
|
|
28 |
tdb |
1.4 |
#include <stdlib.h> |
29 |
pajs |
1.6 |
#include <string.h> |
30 |
pajs |
1.1 |
#include "statgrab.h" |
31 |
tdb |
1.11 |
#include <time.h> |
32 |
pajs |
1.1 |
#ifdef SOLARIS |
33 |
|
|
#include <kstat.h> |
34 |
|
|
#include <sys/sysinfo.h> |
35 |
pajs |
1.6 |
#endif |
36 |
|
|
#ifdef LINUX |
37 |
|
|
#include <stdio.h> |
38 |
|
|
#include <sys/types.h> |
39 |
|
|
#include <regex.h> |
40 |
|
|
#include "tools.h" |
41 |
pajs |
1.1 |
#endif |
42 |
ats |
1.18 |
#ifdef ALLBSD |
43 |
pajs |
1.14 |
#include <sys/types.h> |
44 |
|
|
#include <sys/socket.h> |
45 |
|
|
#include <ifaddrs.h> |
46 |
|
|
#include <net/if.h> |
47 |
|
|
#endif |
48 |
pajs |
1.1 |
|
49 |
pajs |
1.3 |
static network_stat_t *network_stats=NULL; |
50 |
|
|
static int interfaces=0; |
51 |
pajs |
1.1 |
|
52 |
pajs |
1.3 |
void network_stat_init(int start, int end, network_stat_t *net_stats){ |
53 |
|
|
|
54 |
|
|
for(net_stats+=start; start<end; start++){ |
55 |
|
|
net_stats->interface_name=NULL; |
56 |
|
|
net_stats->tx=0; |
57 |
|
|
net_stats->rx=0; |
58 |
|
|
net_stats++; |
59 |
|
|
} |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
network_stat_t *network_stat_malloc(int needed_entries, int *cur_entries, network_stat_t *net_stats){ |
63 |
|
|
|
64 |
|
|
if(net_stats==NULL){ |
65 |
|
|
|
66 |
|
|
if((net_stats=malloc(needed_entries * sizeof(network_stat_t)))==NULL){ |
67 |
pajs |
1.1 |
return NULL; |
68 |
|
|
} |
69 |
pajs |
1.3 |
network_stat_init(0, needed_entries, net_stats); |
70 |
|
|
*cur_entries=needed_entries; |
71 |
|
|
|
72 |
pajs |
1.1 |
return net_stats; |
73 |
|
|
} |
74 |
pajs |
1.3 |
|
75 |
|
|
|
76 |
|
|
if(*cur_entries<needed_entries){ |
77 |
|
|
net_stats=realloc(net_stats, (sizeof(network_stat_t)*needed_entries)); |
78 |
|
|
if(net_stats==NULL){ |
79 |
pajs |
1.1 |
return NULL; |
80 |
|
|
} |
81 |
pajs |
1.3 |
network_stat_init(*cur_entries, needed_entries, net_stats); |
82 |
|
|
*cur_entries=needed_entries; |
83 |
pajs |
1.1 |
} |
84 |
|
|
|
85 |
|
|
return net_stats; |
86 |
|
|
} |
87 |
|
|
|
88 |
pajs |
1.3 |
|
89 |
pajs |
1.1 |
network_stat_t *get_network_stats(int *entries){ |
90 |
pajs |
1.6 |
|
91 |
|
|
static int sizeof_network_stats=0; |
92 |
|
|
network_stat_t *network_stat_ptr; |
93 |
|
|
|
94 |
|
|
#ifdef SOLARIS |
95 |
pajs |
1.1 |
kstat_ctl_t *kc; |
96 |
|
|
kstat_t *ksp; |
97 |
|
|
kstat_named_t *knp; |
98 |
pajs |
1.6 |
#endif |
99 |
pajs |
1.3 |
|
100 |
pajs |
1.6 |
#ifdef LINUX |
101 |
|
|
FILE *f; |
102 |
pajs |
1.12 |
/* Horrible big enough, but it should be easily big enough */ |
103 |
pajs |
1.6 |
char line[8096]; |
104 |
|
|
regex_t regex; |
105 |
|
|
regmatch_t line_match[4]; |
106 |
pajs |
1.14 |
#endif |
107 |
ats |
1.18 |
#ifdef ALLBSD |
108 |
pajs |
1.14 |
struct ifaddrs *net, *net_ptr; |
109 |
|
|
struct if_data *net_data; |
110 |
|
|
#endif |
111 |
|
|
|
112 |
ats |
1.18 |
#ifdef ALLBSD |
113 |
pajs |
1.14 |
if(getifaddrs(&net) != 0){ |
114 |
|
|
return NULL; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
interfaces=0; |
118 |
|
|
|
119 |
|
|
for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){ |
120 |
|
|
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
121 |
|
|
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
122 |
|
|
if(network_stats==NULL){ |
123 |
|
|
return NULL; |
124 |
|
|
} |
125 |
|
|
network_stat_ptr=network_stats+interfaces; |
126 |
|
|
|
127 |
|
|
if(network_stat_ptr->interface_name!=NULL) free(network_stat_ptr->interface_name); |
128 |
|
|
network_stat_ptr->interface_name=strdup(net_ptr->ifa_name); |
129 |
|
|
if(network_stat_ptr->interface_name==NULL) return NULL; |
130 |
|
|
net_data=(struct if_data *)net_ptr->ifa_data; |
131 |
|
|
network_stat_ptr->rx=net_data->ifi_ibytes; |
132 |
|
|
network_stat_ptr->tx=net_data->ifi_obytes; |
133 |
|
|
network_stat_ptr->systime=time(NULL); |
134 |
|
|
interfaces++; |
135 |
|
|
} |
136 |
|
|
freeifaddrs(net); |
137 |
pajs |
1.6 |
#endif |
138 |
pajs |
1.1 |
|
139 |
pajs |
1.6 |
#ifdef SOLARIS |
140 |
pajs |
1.1 |
if ((kc = kstat_open()) == NULL) { |
141 |
|
|
return NULL; |
142 |
|
|
} |
143 |
|
|
|
144 |
pajs |
1.6 |
interfaces=0; |
145 |
pajs |
1.3 |
|
146 |
pajs |
1.1 |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
147 |
|
|
if (!strcmp(ksp->ks_class, "net")) { |
148 |
|
|
kstat_read(kc, ksp, NULL); |
149 |
|
|
|
150 |
pajs |
1.7 |
#ifdef SOL7 |
151 |
|
|
#define RLOOKUP "rbytes" |
152 |
|
|
#define WLOOKUP "obytes" |
153 |
|
|
#define VALTYPE value.ui32 |
154 |
|
|
#else |
155 |
|
|
#define RLOOKUP "rbytes64" |
156 |
|
|
#define WLOOKUP "obytes64" |
157 |
|
|
#define VALTYPE value.ui64 |
158 |
|
|
#endif |
159 |
|
|
|
160 |
|
|
if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){ |
161 |
pajs |
1.1 |
/* Not a network interface, so skip to the next entry */ |
162 |
|
|
continue; |
163 |
|
|
} |
164 |
pajs |
1.3 |
|
165 |
|
|
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
166 |
pajs |
1.1 |
if(network_stats==NULL){ |
167 |
|
|
return NULL; |
168 |
|
|
} |
169 |
|
|
network_stat_ptr=network_stats+interfaces; |
170 |
pajs |
1.7 |
network_stat_ptr->rx=knp->VALTYPE; |
171 |
pajs |
1.1 |
|
172 |
pajs |
1.7 |
if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){ |
173 |
pajs |
1.1 |
/* Not a network interface, so skip to the next entry */ |
174 |
|
|
continue; |
175 |
|
|
} |
176 |
pajs |
1.7 |
network_stat_ptr->tx=knp->VALTYPE; |
177 |
pajs |
1.1 |
if(network_stat_ptr->interface_name!=NULL){ |
178 |
|
|
free(network_stat_ptr->interface_name); |
179 |
|
|
} |
180 |
|
|
network_stat_ptr->interface_name=strdup(ksp->ks_name); |
181 |
pajs |
1.3 |
|
182 |
|
|
network_stat_ptr->systime=time(NULL); |
183 |
pajs |
1.1 |
interfaces++; |
184 |
|
|
} |
185 |
|
|
} |
186 |
pajs |
1.2 |
|
187 |
|
|
kstat_close(kc); |
188 |
pajs |
1.6 |
#endif |
189 |
|
|
#ifdef LINUX |
190 |
|
|
f=fopen("/proc/net/dev", "r"); |
191 |
|
|
if(f==NULL){ |
192 |
|
|
return NULL; |
193 |
|
|
} |
194 |
|
|
/* read the 2 lines.. Its the title, so we dont care :) */ |
195 |
|
|
fgets(line, sizeof(line), f); |
196 |
|
|
fgets(line, sizeof(line), f); |
197 |
|
|
|
198 |
|
|
|
199 |
|
|
if((regcomp(®ex, "^[[:space:]]*([^:]+):[[:space:]]*([[:digit:]]+)[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+[[:space:]]+([[:digit:]]+)", REG_EXTENDED))!=0){ |
200 |
|
|
return NULL; |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
interfaces=0; |
204 |
pajs |
1.1 |
|
205 |
pajs |
1.6 |
while((fgets(line, sizeof(line), f)) != NULL){ |
206 |
|
|
if((regexec(®ex, line, 4, line_match, 0))!=0){ |
207 |
|
|
continue; |
208 |
|
|
} |
209 |
|
|
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
210 |
|
|
if(network_stats==NULL){ |
211 |
|
|
return NULL; |
212 |
|
|
} |
213 |
|
|
network_stat_ptr=network_stats+interfaces; |
214 |
|
|
|
215 |
|
|
if(network_stat_ptr->interface_name!=NULL){ |
216 |
|
|
free(network_stat_ptr->interface_name); |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
network_stat_ptr->interface_name=get_string_match(line, &line_match[1]); |
220 |
|
|
network_stat_ptr->rx=get_ll_match(line, &line_match[2]); |
221 |
|
|
network_stat_ptr->tx=get_ll_match(line, &line_match[3]); |
222 |
|
|
network_stat_ptr->systime=time(NULL); |
223 |
|
|
|
224 |
|
|
interfaces++; |
225 |
|
|
} |
226 |
pajs |
1.12 |
fclose(f); |
227 |
pajs |
1.13 |
regfree(®ex); |
228 |
pajs |
1.6 |
|
229 |
|
|
#endif |
230 |
ats |
1.20 |
|
231 |
|
|
#ifdef CYGWIN |
232 |
|
|
return NULL; |
233 |
|
|
#endif |
234 |
|
|
|
235 |
pajs |
1.1 |
*entries=interfaces; |
236 |
|
|
|
237 |
|
|
return network_stats; |
238 |
pajs |
1.3 |
} |
239 |
|
|
|
240 |
pajs |
1.8 |
long long transfer_diff(long long new, long long old){ |
241 |
pajs |
1.17 |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) |
242 |
ats |
1.19 |
#define MAXVAL 4294967296LL |
243 |
pajs |
1.8 |
#else |
244 |
ats |
1.19 |
#define MAXVAL 18446744073709551616LL |
245 |
pajs |
1.8 |
#endif |
246 |
|
|
long long result; |
247 |
pajs |
1.10 |
if(new>=old){ |
248 |
pajs |
1.8 |
result = (new-old); |
249 |
|
|
}else{ |
250 |
|
|
result = (MAXVAL+(new-old)); |
251 |
|
|
} |
252 |
|
|
|
253 |
|
|
return result; |
254 |
|
|
|
255 |
|
|
} |
256 |
|
|
|
257 |
ats |
1.24 |
network_stat_t *get_network_stats_diff(int *entries) { |
258 |
|
|
static network_stat_t *diff = NULL; |
259 |
|
|
static int diff_count = 0; |
260 |
|
|
network_stat_t *src, *dest; |
261 |
|
|
int i, j, new_count; |
262 |
|
|
|
263 |
|
|
if (network_stats == NULL) { |
264 |
|
|
/* No previous stats, so we can't calculate a difference. */ |
265 |
|
|
return get_network_stats(entries); |
266 |
pajs |
1.3 |
} |
267 |
|
|
|
268 |
ats |
1.24 |
/* Resize the results array to match the previous stats. */ |
269 |
|
|
diff = network_stat_malloc(interfaces, &diff_count, diff); |
270 |
|
|
if (diff == NULL) { |
271 |
pajs |
1.3 |
return NULL; |
272 |
|
|
} |
273 |
|
|
|
274 |
ats |
1.24 |
/* Copy the previous stats into the result. */ |
275 |
|
|
for (i = 0; i < diff_count; i++) { |
276 |
|
|
src = &network_stats[i]; |
277 |
|
|
dest = &diff[i]; |
278 |
pajs |
1.3 |
|
279 |
ats |
1.24 |
if (dest->interface_name != NULL) { |
280 |
|
|
free(dest->interface_name); |
281 |
pajs |
1.3 |
} |
282 |
ats |
1.24 |
dest->interface_name = strdup(src->interface_name); |
283 |
|
|
dest->rx = src->rx; |
284 |
|
|
dest->tx = src->tx; |
285 |
|
|
dest->systime = src->systime; |
286 |
|
|
} |
287 |
pajs |
1.3 |
|
288 |
ats |
1.24 |
/* Get a new set of stats. */ |
289 |
|
|
if (get_network_stats(&new_count) == NULL) { |
290 |
ats |
1.21 |
return NULL; |
291 |
|
|
} |
292 |
pajs |
1.3 |
|
293 |
ats |
1.24 |
/* For each previous stat... */ |
294 |
|
|
for (i = 0; i < diff_count; i++) { |
295 |
|
|
dest = &diff[i]; |
296 |
|
|
|
297 |
|
|
/* ... find the corresponding new stat ... */ |
298 |
|
|
for (j = 0; j < new_count; j++) { |
299 |
|
|
/* Try the new stat in the same position first, |
300 |
|
|
since that's most likely to be it. */ |
301 |
|
|
src = &network_stats[(i + j) % new_count]; |
302 |
|
|
if (strcmp(src->interface_name, dest->interface_name) == 0) { |
303 |
|
|
break; |
304 |
|
|
} |
305 |
|
|
} |
306 |
|
|
if (j == new_count) { |
307 |
|
|
/* No match found. */ |
308 |
|
|
continue; |
309 |
pajs |
1.3 |
} |
310 |
|
|
|
311 |
ats |
1.24 |
/* ... and subtract the previous stat from it to get the |
312 |
|
|
difference. */ |
313 |
|
|
dest->rx = transfer_diff(src->rx, dest->rx); |
314 |
|
|
dest->tx = transfer_diff(src->tx, dest->tx); |
315 |
|
|
dest->systime = src->systime - dest->systime; |
316 |
pajs |
1.3 |
} |
317 |
|
|
|
318 |
ats |
1.24 |
*entries = diff_count; |
319 |
|
|
return diff; |
320 |
|
|
} |