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.39 |
* $Id: network_stats.c,v 1.38 2004/02/14 00:27:58 ats 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 |
pajs |
1.27 |
#include <sys/ioctl.h> |
41 |
|
|
#include <sys/socket.h> |
42 |
|
|
#include <net/if.h> |
43 |
|
|
#include <ctype.h> |
44 |
pajs |
1.6 |
#include "tools.h" |
45 |
pajs |
1.27 |
/* Stuff which could be defined by defining KERNEL, but |
46 |
|
|
* that would be a bad idea, so we'll just declare it here |
47 |
|
|
*/ |
48 |
|
|
typedef __uint8_t u8; |
49 |
|
|
typedef __uint16_t u16; |
50 |
|
|
typedef __uint32_t u32; |
51 |
|
|
#include <linux/ethtool.h> |
52 |
|
|
#include <linux/sockios.h> |
53 |
ats |
1.32 |
#include <unistd.h> |
54 |
pajs |
1.1 |
#endif |
55 |
ats |
1.18 |
#ifdef ALLBSD |
56 |
pajs |
1.14 |
#include <sys/types.h> |
57 |
|
|
#include <sys/socket.h> |
58 |
|
|
#include <ifaddrs.h> |
59 |
|
|
#include <net/if.h> |
60 |
pajs |
1.26 |
#include <net/if_media.h> |
61 |
|
|
#include <sys/ioctl.h> |
62 |
pajs |
1.14 |
#endif |
63 |
pajs |
1.1 |
|
64 |
pajs |
1.3 |
static network_stat_t *network_stats=NULL; |
65 |
|
|
static int interfaces=0; |
66 |
pajs |
1.1 |
|
67 |
pajs |
1.3 |
void network_stat_init(int start, int end, network_stat_t *net_stats){ |
68 |
|
|
|
69 |
|
|
for(net_stats+=start; start<end; start++){ |
70 |
|
|
net_stats->interface_name=NULL; |
71 |
|
|
net_stats->tx=0; |
72 |
|
|
net_stats->rx=0; |
73 |
|
|
net_stats++; |
74 |
|
|
} |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
network_stat_t *network_stat_malloc(int needed_entries, int *cur_entries, network_stat_t *net_stats){ |
78 |
|
|
|
79 |
|
|
if(net_stats==NULL){ |
80 |
|
|
|
81 |
|
|
if((net_stats=malloc(needed_entries * sizeof(network_stat_t)))==NULL){ |
82 |
pajs |
1.1 |
return NULL; |
83 |
|
|
} |
84 |
pajs |
1.3 |
network_stat_init(0, needed_entries, net_stats); |
85 |
|
|
*cur_entries=needed_entries; |
86 |
|
|
|
87 |
pajs |
1.1 |
return net_stats; |
88 |
|
|
} |
89 |
pajs |
1.3 |
|
90 |
|
|
|
91 |
|
|
if(*cur_entries<needed_entries){ |
92 |
|
|
net_stats=realloc(net_stats, (sizeof(network_stat_t)*needed_entries)); |
93 |
|
|
if(net_stats==NULL){ |
94 |
pajs |
1.1 |
return NULL; |
95 |
|
|
} |
96 |
pajs |
1.3 |
network_stat_init(*cur_entries, needed_entries, net_stats); |
97 |
|
|
*cur_entries=needed_entries; |
98 |
pajs |
1.1 |
} |
99 |
|
|
|
100 |
|
|
return net_stats; |
101 |
|
|
} |
102 |
|
|
|
103 |
pajs |
1.3 |
|
104 |
pajs |
1.1 |
network_stat_t *get_network_stats(int *entries){ |
105 |
pajs |
1.6 |
|
106 |
|
|
static int sizeof_network_stats=0; |
107 |
|
|
network_stat_t *network_stat_ptr; |
108 |
|
|
|
109 |
|
|
#ifdef SOLARIS |
110 |
pajs |
1.1 |
kstat_ctl_t *kc; |
111 |
|
|
kstat_t *ksp; |
112 |
|
|
kstat_named_t *knp; |
113 |
pajs |
1.6 |
#endif |
114 |
pajs |
1.3 |
|
115 |
pajs |
1.6 |
#ifdef LINUX |
116 |
|
|
FILE *f; |
117 |
pajs |
1.12 |
/* Horrible big enough, but it should be easily big enough */ |
118 |
pajs |
1.6 |
char line[8096]; |
119 |
|
|
regex_t regex; |
120 |
|
|
regmatch_t line_match[4]; |
121 |
pajs |
1.14 |
#endif |
122 |
ats |
1.18 |
#ifdef ALLBSD |
123 |
pajs |
1.14 |
struct ifaddrs *net, *net_ptr; |
124 |
|
|
struct if_data *net_data; |
125 |
|
|
#endif |
126 |
|
|
|
127 |
ats |
1.18 |
#ifdef ALLBSD |
128 |
pajs |
1.14 |
if(getifaddrs(&net) != 0){ |
129 |
|
|
return NULL; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
interfaces=0; |
133 |
|
|
|
134 |
|
|
for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){ |
135 |
|
|
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
136 |
|
|
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
137 |
|
|
if(network_stats==NULL){ |
138 |
|
|
return NULL; |
139 |
|
|
} |
140 |
|
|
network_stat_ptr=network_stats+interfaces; |
141 |
|
|
|
142 |
|
|
if(network_stat_ptr->interface_name!=NULL) free(network_stat_ptr->interface_name); |
143 |
|
|
network_stat_ptr->interface_name=strdup(net_ptr->ifa_name); |
144 |
|
|
if(network_stat_ptr->interface_name==NULL) return NULL; |
145 |
|
|
net_data=(struct if_data *)net_ptr->ifa_data; |
146 |
|
|
network_stat_ptr->rx=net_data->ifi_ibytes; |
147 |
|
|
network_stat_ptr->tx=net_data->ifi_obytes; |
148 |
|
|
network_stat_ptr->systime=time(NULL); |
149 |
|
|
interfaces++; |
150 |
|
|
} |
151 |
|
|
freeifaddrs(net); |
152 |
pajs |
1.6 |
#endif |
153 |
pajs |
1.1 |
|
154 |
pajs |
1.6 |
#ifdef SOLARIS |
155 |
pajs |
1.1 |
if ((kc = kstat_open()) == NULL) { |
156 |
|
|
return NULL; |
157 |
|
|
} |
158 |
|
|
|
159 |
pajs |
1.6 |
interfaces=0; |
160 |
pajs |
1.3 |
|
161 |
pajs |
1.1 |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
162 |
|
|
if (!strcmp(ksp->ks_class, "net")) { |
163 |
|
|
kstat_read(kc, ksp, NULL); |
164 |
|
|
|
165 |
pajs |
1.7 |
#ifdef SOL7 |
166 |
|
|
#define RLOOKUP "rbytes" |
167 |
|
|
#define WLOOKUP "obytes" |
168 |
|
|
#define VALTYPE value.ui32 |
169 |
|
|
#else |
170 |
|
|
#define RLOOKUP "rbytes64" |
171 |
|
|
#define WLOOKUP "obytes64" |
172 |
|
|
#define VALTYPE value.ui64 |
173 |
|
|
#endif |
174 |
|
|
|
175 |
|
|
if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){ |
176 |
pajs |
1.1 |
/* Not a network interface, so skip to the next entry */ |
177 |
|
|
continue; |
178 |
|
|
} |
179 |
pajs |
1.3 |
|
180 |
|
|
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
181 |
pajs |
1.1 |
if(network_stats==NULL){ |
182 |
|
|
return NULL; |
183 |
|
|
} |
184 |
|
|
network_stat_ptr=network_stats+interfaces; |
185 |
pajs |
1.7 |
network_stat_ptr->rx=knp->VALTYPE; |
186 |
pajs |
1.1 |
|
187 |
pajs |
1.7 |
if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){ |
188 |
pajs |
1.1 |
/* Not a network interface, so skip to the next entry */ |
189 |
|
|
continue; |
190 |
|
|
} |
191 |
pajs |
1.7 |
network_stat_ptr->tx=knp->VALTYPE; |
192 |
pajs |
1.1 |
if(network_stat_ptr->interface_name!=NULL){ |
193 |
|
|
free(network_stat_ptr->interface_name); |
194 |
|
|
} |
195 |
|
|
network_stat_ptr->interface_name=strdup(ksp->ks_name); |
196 |
pajs |
1.3 |
|
197 |
|
|
network_stat_ptr->systime=time(NULL); |
198 |
pajs |
1.1 |
interfaces++; |
199 |
|
|
} |
200 |
|
|
} |
201 |
pajs |
1.2 |
|
202 |
|
|
kstat_close(kc); |
203 |
pajs |
1.6 |
#endif |
204 |
|
|
#ifdef LINUX |
205 |
|
|
f=fopen("/proc/net/dev", "r"); |
206 |
|
|
if(f==NULL){ |
207 |
|
|
return NULL; |
208 |
|
|
} |
209 |
|
|
/* read the 2 lines.. Its the title, so we dont care :) */ |
210 |
|
|
fgets(line, sizeof(line), f); |
211 |
|
|
fgets(line, sizeof(line), f); |
212 |
|
|
|
213 |
|
|
|
214 |
|
|
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){ |
215 |
|
|
return NULL; |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
interfaces=0; |
219 |
pajs |
1.1 |
|
220 |
pajs |
1.6 |
while((fgets(line, sizeof(line), f)) != NULL){ |
221 |
|
|
if((regexec(®ex, line, 4, line_match, 0))!=0){ |
222 |
|
|
continue; |
223 |
|
|
} |
224 |
|
|
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
225 |
|
|
if(network_stats==NULL){ |
226 |
|
|
return NULL; |
227 |
|
|
} |
228 |
|
|
network_stat_ptr=network_stats+interfaces; |
229 |
|
|
|
230 |
|
|
if(network_stat_ptr->interface_name!=NULL){ |
231 |
|
|
free(network_stat_ptr->interface_name); |
232 |
|
|
} |
233 |
|
|
|
234 |
|
|
network_stat_ptr->interface_name=get_string_match(line, &line_match[1]); |
235 |
|
|
network_stat_ptr->rx=get_ll_match(line, &line_match[2]); |
236 |
|
|
network_stat_ptr->tx=get_ll_match(line, &line_match[3]); |
237 |
|
|
network_stat_ptr->systime=time(NULL); |
238 |
|
|
|
239 |
|
|
interfaces++; |
240 |
|
|
} |
241 |
pajs |
1.12 |
fclose(f); |
242 |
pajs |
1.13 |
regfree(®ex); |
243 |
pajs |
1.6 |
|
244 |
|
|
#endif |
245 |
ats |
1.20 |
|
246 |
|
|
#ifdef CYGWIN |
247 |
|
|
return NULL; |
248 |
|
|
#endif |
249 |
|
|
|
250 |
pajs |
1.1 |
*entries=interfaces; |
251 |
|
|
|
252 |
|
|
return network_stats; |
253 |
pajs |
1.3 |
} |
254 |
|
|
|
255 |
pajs |
1.8 |
long long transfer_diff(long long new, long long old){ |
256 |
pajs |
1.17 |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) |
257 |
ats |
1.19 |
#define MAXVAL 4294967296LL |
258 |
pajs |
1.8 |
#else |
259 |
ats |
1.19 |
#define MAXVAL 18446744073709551616LL |
260 |
pajs |
1.8 |
#endif |
261 |
|
|
long long result; |
262 |
pajs |
1.10 |
if(new>=old){ |
263 |
pajs |
1.8 |
result = (new-old); |
264 |
|
|
}else{ |
265 |
|
|
result = (MAXVAL+(new-old)); |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
return result; |
269 |
|
|
|
270 |
|
|
} |
271 |
|
|
|
272 |
ats |
1.24 |
network_stat_t *get_network_stats_diff(int *entries) { |
273 |
|
|
static network_stat_t *diff = NULL; |
274 |
|
|
static int diff_count = 0; |
275 |
|
|
network_stat_t *src, *dest; |
276 |
|
|
int i, j, new_count; |
277 |
|
|
|
278 |
|
|
if (network_stats == NULL) { |
279 |
|
|
/* No previous stats, so we can't calculate a difference. */ |
280 |
|
|
return get_network_stats(entries); |
281 |
pajs |
1.3 |
} |
282 |
|
|
|
283 |
ats |
1.24 |
/* Resize the results array to match the previous stats. */ |
284 |
|
|
diff = network_stat_malloc(interfaces, &diff_count, diff); |
285 |
|
|
if (diff == NULL) { |
286 |
pajs |
1.3 |
return NULL; |
287 |
|
|
} |
288 |
|
|
|
289 |
ats |
1.24 |
/* Copy the previous stats into the result. */ |
290 |
|
|
for (i = 0; i < diff_count; i++) { |
291 |
|
|
src = &network_stats[i]; |
292 |
|
|
dest = &diff[i]; |
293 |
pajs |
1.3 |
|
294 |
ats |
1.24 |
if (dest->interface_name != NULL) { |
295 |
|
|
free(dest->interface_name); |
296 |
pajs |
1.3 |
} |
297 |
ats |
1.24 |
dest->interface_name = strdup(src->interface_name); |
298 |
|
|
dest->rx = src->rx; |
299 |
|
|
dest->tx = src->tx; |
300 |
|
|
dest->systime = src->systime; |
301 |
|
|
} |
302 |
pajs |
1.3 |
|
303 |
ats |
1.24 |
/* Get a new set of stats. */ |
304 |
|
|
if (get_network_stats(&new_count) == NULL) { |
305 |
ats |
1.21 |
return NULL; |
306 |
|
|
} |
307 |
pajs |
1.3 |
|
308 |
ats |
1.24 |
/* For each previous stat... */ |
309 |
|
|
for (i = 0; i < diff_count; i++) { |
310 |
|
|
dest = &diff[i]; |
311 |
|
|
|
312 |
|
|
/* ... find the corresponding new stat ... */ |
313 |
|
|
for (j = 0; j < new_count; j++) { |
314 |
|
|
/* Try the new stat in the same position first, |
315 |
|
|
since that's most likely to be it. */ |
316 |
|
|
src = &network_stats[(i + j) % new_count]; |
317 |
|
|
if (strcmp(src->interface_name, dest->interface_name) == 0) { |
318 |
|
|
break; |
319 |
|
|
} |
320 |
|
|
} |
321 |
|
|
if (j == new_count) { |
322 |
|
|
/* No match found. */ |
323 |
|
|
continue; |
324 |
pajs |
1.3 |
} |
325 |
|
|
|
326 |
ats |
1.24 |
/* ... and subtract the previous stat from it to get the |
327 |
|
|
difference. */ |
328 |
|
|
dest->rx = transfer_diff(src->rx, dest->rx); |
329 |
|
|
dest->tx = transfer_diff(src->tx, dest->tx); |
330 |
|
|
dest->systime = src->systime - dest->systime; |
331 |
pajs |
1.3 |
} |
332 |
|
|
|
333 |
ats |
1.24 |
*entries = diff_count; |
334 |
|
|
return diff; |
335 |
|
|
} |
336 |
pajs |
1.25 |
/* NETWORK INTERFACE STATS */ |
337 |
|
|
|
338 |
|
|
void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){ |
339 |
|
|
|
340 |
|
|
for(net_stats+=start; start<end; start++){ |
341 |
|
|
net_stats->interface_name=NULL; |
342 |
|
|
net_stats->speed=0; |
343 |
ats |
1.31 |
net_stats->dup=UNKNOWN_DUPLEX; |
344 |
pajs |
1.25 |
net_stats++; |
345 |
|
|
} |
346 |
|
|
} |
347 |
|
|
|
348 |
|
|
network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){ |
349 |
|
|
|
350 |
|
|
if(net_stats==NULL){ |
351 |
|
|
|
352 |
|
|
if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){ |
353 |
|
|
return NULL; |
354 |
|
|
} |
355 |
|
|
network_iface_stat_init(0, needed_entries, net_stats); |
356 |
|
|
*cur_entries=needed_entries; |
357 |
|
|
|
358 |
|
|
return net_stats; |
359 |
|
|
} |
360 |
|
|
|
361 |
|
|
|
362 |
|
|
if(*cur_entries<needed_entries){ |
363 |
|
|
net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries)); |
364 |
|
|
if(net_stats==NULL){ |
365 |
|
|
return NULL; |
366 |
|
|
} |
367 |
|
|
network_iface_stat_init(*cur_entries, needed_entries, net_stats); |
368 |
|
|
*cur_entries=needed_entries; |
369 |
|
|
} |
370 |
|
|
|
371 |
|
|
return net_stats; |
372 |
|
|
} |
373 |
|
|
|
374 |
|
|
network_iface_stat_t *get_network_iface_stats(int *entries){ |
375 |
|
|
static network_iface_stat_t *network_iface_stats; |
376 |
|
|
network_iface_stat_t *network_iface_stat_ptr; |
377 |
|
|
static int sizeof_network_iface_stats=0; |
378 |
pajs |
1.26 |
static int ifaces; |
379 |
pajs |
1.25 |
|
380 |
|
|
#ifdef SOLARIS |
381 |
|
|
kstat_ctl_t *kc; |
382 |
|
|
kstat_t *ksp; |
383 |
|
|
kstat_named_t *knp; |
384 |
|
|
#endif |
385 |
pajs |
1.26 |
#ifdef ALLBSD |
386 |
|
|
struct ifaddrs *net, *net_ptr; |
387 |
|
|
struct ifmediareq ifmed; |
388 |
pajs |
1.36 |
struct ifreq ifr; |
389 |
ats |
1.37 |
int sock; |
390 |
pajs |
1.26 |
int x; |
391 |
|
|
#endif |
392 |
pajs |
1.27 |
#ifdef LINUX |
393 |
|
|
FILE *f; |
394 |
|
|
/* Horrible big enough, but it should be easily big enough */ |
395 |
|
|
char line[8096]; |
396 |
|
|
int sock; |
397 |
|
|
#endif |
398 |
pajs |
1.26 |
ifaces = 0; |
399 |
|
|
#ifdef ALLBSD |
400 |
|
|
if(getifaddrs(&net) != 0){ |
401 |
|
|
return NULL; |
402 |
|
|
} |
403 |
|
|
|
404 |
ats |
1.37 |
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL; |
405 |
pajs |
1.26 |
|
406 |
|
|
for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){ |
407 |
|
|
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
408 |
|
|
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
409 |
|
|
if(network_iface_stats==NULL){ |
410 |
|
|
return NULL; |
411 |
|
|
} |
412 |
|
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
413 |
|
|
|
414 |
|
|
memset(&ifmed, 0, sizeof(struct ifmediareq)); |
415 |
|
|
strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
416 |
ats |
1.37 |
if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
417 |
pajs |
1.26 |
continue; |
418 |
|
|
} |
419 |
|
|
|
420 |
|
|
/* We may need to change this if we start doing wireless devices too */ |
421 |
|
|
if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){ |
422 |
|
|
/* Not a ETHER device */ |
423 |
|
|
continue; |
424 |
|
|
} |
425 |
|
|
|
426 |
|
|
if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
427 |
|
|
network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name); |
428 |
|
|
if(network_iface_stat_ptr->interface_name == NULL) return NULL; |
429 |
|
|
|
430 |
|
|
/* Only intrested in the first 4 bits) - Assuming only ETHER devices */ |
431 |
|
|
x = ifmed.ifm_active & 0x0f; |
432 |
|
|
switch(x){ |
433 |
|
|
/* 10 Mbit connections. Speedy :) */ |
434 |
|
|
case(IFM_10_T): |
435 |
|
|
case(IFM_10_2): |
436 |
|
|
case(IFM_10_5): |
437 |
|
|
case(IFM_10_STP): |
438 |
|
|
case(IFM_10_FL): |
439 |
|
|
network_iface_stat_ptr->speed = 10; |
440 |
|
|
break; |
441 |
|
|
/* 100 Mbit conneections */ |
442 |
|
|
case(IFM_100_TX): |
443 |
|
|
case(IFM_100_FX): |
444 |
|
|
case(IFM_100_T4): |
445 |
|
|
case(IFM_100_VG): |
446 |
|
|
case(IFM_100_T2): |
447 |
|
|
network_iface_stat_ptr->speed = 100; |
448 |
|
|
break; |
449 |
|
|
/* 1000 Mbit connections */ |
450 |
|
|
case(IFM_1000_SX): |
451 |
|
|
case(IFM_1000_LX): |
452 |
|
|
case(IFM_1000_CX): |
453 |
ats |
1.31 |
#if defined(FREEBSD) && !defined(FREEBSD5) |
454 |
pajs |
1.26 |
case(IFM_1000_TX): |
455 |
tdb |
1.28 |
case(IFM_1000_FX): |
456 |
ats |
1.31 |
#else |
457 |
|
|
case(IFM_1000_T): |
458 |
tdb |
1.28 |
#endif |
459 |
pajs |
1.26 |
network_iface_stat_ptr->speed = 1000; |
460 |
|
|
break; |
461 |
|
|
/* We don't know what it is */ |
462 |
|
|
default: |
463 |
|
|
network_iface_stat_ptr->speed = 0; |
464 |
|
|
break; |
465 |
|
|
} |
466 |
|
|
|
467 |
|
|
if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){ |
468 |
|
|
network_iface_stat_ptr->dup = FULL_DUPLEX; |
469 |
|
|
}else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){ |
470 |
|
|
network_iface_stat_ptr->dup = HALF_DUPLEX; |
471 |
|
|
}else{ |
472 |
ats |
1.31 |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
473 |
pajs |
1.26 |
} |
474 |
pajs |
1.36 |
|
475 |
ats |
1.37 |
memset(&ifr, 0, sizeof(ifr)); |
476 |
|
|
strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name)); |
477 |
|
|
|
478 |
pajs |
1.36 |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){ |
479 |
|
|
continue; |
480 |
|
|
} |
481 |
|
|
if((ifr.ifr_flags & IFF_UP) != 0){ |
482 |
|
|
network_iface_stat_ptr->up = 1; |
483 |
|
|
}else{ |
484 |
|
|
network_iface_stat_ptr->up = 0; |
485 |
|
|
} |
486 |
|
|
|
487 |
pajs |
1.26 |
ifaces++; |
488 |
|
|
} |
489 |
|
|
freeifaddrs(net); |
490 |
ats |
1.37 |
close(sock); |
491 |
pajs |
1.26 |
#endif |
492 |
pajs |
1.25 |
|
493 |
|
|
#ifdef SOLARIS |
494 |
|
|
if ((kc = kstat_open()) == NULL) { |
495 |
|
|
return NULL; |
496 |
|
|
} |
497 |
|
|
|
498 |
|
|
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
499 |
|
|
if (!strcmp(ksp->ks_class, "net")) { |
500 |
|
|
kstat_read(kc, ksp, NULL); |
501 |
|
|
if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){ |
502 |
|
|
/* Not a network interface, so skip to the next entry */ |
503 |
|
|
continue; |
504 |
|
|
} |
505 |
|
|
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
506 |
|
|
if(network_iface_stats==NULL){ |
507 |
|
|
return NULL; |
508 |
|
|
} |
509 |
pajs |
1.26 |
network_iface_stat_ptr = network_iface_stats + ifaces; |
510 |
pajs |
1.25 |
network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000); |
511 |
|
|
|
512 |
pajs |
1.35 |
if((knp=kstat_data_lookup(ksp, "link_up"))==NULL){ |
513 |
|
|
/* Not a network interface, so skip to the next entry */ |
514 |
|
|
continue; |
515 |
|
|
} |
516 |
|
|
/* Solaris has 1 for up, and 0 for not. As we do too */ |
517 |
|
|
network_iface_stat_ptr->up = value.ui32; |
518 |
|
|
|
519 |
pajs |
1.25 |
if((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){ |
520 |
|
|
/* Not a network interface, so skip to the next entry */ |
521 |
|
|
continue; |
522 |
|
|
} |
523 |
|
|
|
524 |
pajs |
1.33 |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
525 |
tdb |
1.34 |
if(knp->value.ui32 == 2){ |
526 |
pajs |
1.25 |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
527 |
pajs |
1.33 |
} |
528 |
tdb |
1.34 |
if(knp->value.ui32 == 1){ |
529 |
pajs |
1.25 |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
530 |
|
|
} |
531 |
|
|
|
532 |
pajs |
1.26 |
if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
533 |
pajs |
1.25 |
network_iface_stat_ptr->interface_name = strdup(ksp->ks_name); |
534 |
pajs |
1.26 |
if(network_iface_stat_ptr->interface_name == NULL) return NULL; |
535 |
|
|
ifaces++; |
536 |
pajs |
1.25 |
} |
537 |
|
|
} |
538 |
|
|
kstat_close(kc); |
539 |
|
|
|
540 |
|
|
#endif |
541 |
pajs |
1.27 |
#ifdef LINUX |
542 |
|
|
f = fopen("/proc/net/dev", "r"); |
543 |
|
|
if(f == NULL){ |
544 |
|
|
return NULL; |
545 |
|
|
} |
546 |
|
|
|
547 |
|
|
/* Setup stuff so we can do the ioctl to get the info */ |
548 |
|
|
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){ |
549 |
|
|
return NULL; |
550 |
|
|
} |
551 |
|
|
|
552 |
|
|
/* Ignore first 2 lines.. Just headings */ |
553 |
pajs |
1.29 |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
554 |
|
|
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
555 |
pajs |
1.27 |
|
556 |
|
|
while((fgets(line, sizeof(line), f)) != NULL){ |
557 |
|
|
char *name, *ptr; |
558 |
|
|
struct ifreq ifr; |
559 |
ats |
1.38 |
struct ethtool_cmd ethcmd; |
560 |
pajs |
1.27 |
int err; |
561 |
|
|
|
562 |
|
|
/* Get the interface name */ |
563 |
|
|
ptr = strchr(line, ':'); |
564 |
|
|
if (ptr == NULL) continue; |
565 |
|
|
*ptr='\0'; |
566 |
|
|
name = line; |
567 |
|
|
while(isspace(*(name))){ |
568 |
|
|
name++; |
569 |
|
|
} |
570 |
|
|
|
571 |
ats |
1.38 |
memset(&ifr, 0, sizeof ifr); |
572 |
|
|
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
573 |
pajs |
1.27 |
|
574 |
ats |
1.38 |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
575 |
|
|
continue; |
576 |
|
|
} |
577 |
pajs |
1.27 |
|
578 |
|
|
/* We have a good interface to add */ |
579 |
|
|
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
580 |
|
|
if(network_iface_stats==NULL){ |
581 |
|
|
return NULL; |
582 |
|
|
} |
583 |
|
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
584 |
|
|
network_iface_stat_ptr->interface_name = strdup(name); |
585 |
ats |
1.38 |
if ((ifr.ifr_flags & IFF_UP) != 0) { |
586 |
pajs |
1.35 |
network_iface_stat_ptr->up = 1; |
587 |
ats |
1.38 |
} else { |
588 |
pajs |
1.35 |
network_iface_stat_ptr->up = 0; |
589 |
|
|
} |
590 |
|
|
|
591 |
ats |
1.38 |
memset(ðcmd, 0, sizeof ethcmd); |
592 |
|
|
ethcmd.cmd = ETHTOOL_GSET; |
593 |
|
|
ifr.ifr_data = (caddr_t) ðcmd; |
594 |
|
|
|
595 |
|
|
err = ioctl(sock, SIOCETHTOOL, &ifr); |
596 |
|
|
if (err == 0) { |
597 |
|
|
network_iface_stat_ptr->speed = ethcmd.speed; |
598 |
|
|
|
599 |
|
|
switch (ethcmd.duplex) { |
600 |
|
|
case 0x00: |
601 |
|
|
network_iface_stat_ptr->dup = FULL_DUPLEX; |
602 |
|
|
break; |
603 |
|
|
case 0x01: |
604 |
|
|
network_iface_stat_ptr->dup = HALF_DUPLEX; |
605 |
|
|
break; |
606 |
|
|
default: |
607 |
|
|
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
608 |
|
|
} |
609 |
|
|
} else { |
610 |
|
|
/* Not all interfaces support the ethtool ioctl. */ |
611 |
ats |
1.39 |
network_iface_stat_ptr->speed = 0; |
612 |
ats |
1.38 |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
613 |
pajs |
1.27 |
} |
614 |
ats |
1.38 |
|
615 |
pajs |
1.27 |
ifaces++; |
616 |
|
|
} |
617 |
pajs |
1.30 |
close(sock); |
618 |
ats |
1.38 |
fclose(f); |
619 |
pajs |
1.27 |
#endif |
620 |
pajs |
1.26 |
*entries = ifaces; |
621 |
pajs |
1.25 |
return network_iface_stats; |
622 |
|
|
} |
623 |
|
|
|