1 |
< |
/* |
1 |
> |
/* |
2 |
|
* i-scream central monitoring system |
3 |
< |
* http://www.i-scream.org.uk |
4 |
< |
* Copyright (C) 2000-2002 i-scream |
3 |
> |
* http://www.i-scream.org |
4 |
> |
* Copyright (C) 2000-2004 i-scream |
5 |
|
* |
6 |
< |
* This program is free software; you can redistribute it and/or |
7 |
< |
* modify it under the terms of the GNU General Public License |
8 |
< |
* as published by the Free Software Foundation; either version 2 |
9 |
< |
* of the License, or (at your option) any later version. |
6 |
> |
* 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 |
|
* |
11 |
< |
* This program is distributed in the hope that it will be useful, |
11 |
> |
* This library is distributed in the hope that it will be useful, |
12 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
< |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
< |
* GNU General Public License for more details. |
13 |
> |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
> |
* Lesser General Public License for more details. |
15 |
|
* |
16 |
< |
* You should have received a copy of the GNU General Public License |
17 |
< |
* along with this program; if not, write to the Free Software |
18 |
< |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
16 |
> |
* 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 |
> |
* |
21 |
> |
* $Id$ |
22 |
|
*/ |
23 |
|
|
24 |
|
#ifdef HAVE_CONFIG_H |
32 |
|
#ifdef SOLARIS |
33 |
|
#include <kstat.h> |
34 |
|
#include <sys/sysinfo.h> |
35 |
+ |
#include <sys/types.h> |
36 |
+ |
#include <sys/socket.h> |
37 |
+ |
#include <sys/ioctl.h> |
38 |
+ |
#include <net/if.h> |
39 |
+ |
#include <netinet/in.h> |
40 |
+ |
#include <sys/sockio.h> |
41 |
|
#endif |
42 |
|
#ifdef LINUX |
43 |
|
#include <stdio.h> |
44 |
|
#include <sys/types.h> |
45 |
|
#include <regex.h> |
46 |
+ |
#include <sys/ioctl.h> |
47 |
+ |
#include <sys/socket.h> |
48 |
+ |
#include <net/if.h> |
49 |
+ |
#include <ctype.h> |
50 |
|
#include "tools.h" |
51 |
+ |
/* Stuff which could be defined by defining KERNEL, but |
52 |
+ |
* that would be a bad idea, so we'll just declare it here |
53 |
+ |
*/ |
54 |
+ |
typedef __uint8_t u8; |
55 |
+ |
typedef __uint16_t u16; |
56 |
+ |
typedef __uint32_t u32; |
57 |
+ |
typedef __uint64_t u64; |
58 |
+ |
#include <linux/ethtool.h> |
59 |
+ |
#include <linux/sockios.h> |
60 |
+ |
#include <unistd.h> |
61 |
|
#endif |
62 |
< |
#ifdef FREEBSD |
62 |
> |
#ifdef ALLBSD |
63 |
|
#include <sys/types.h> |
64 |
|
#include <sys/socket.h> |
65 |
|
#include <ifaddrs.h> |
66 |
|
#include <net/if.h> |
67 |
+ |
#include <net/if_media.h> |
68 |
+ |
#include <sys/ioctl.h> |
69 |
|
#endif |
70 |
|
|
71 |
|
static network_stat_t *network_stats=NULL; |
77 |
|
net_stats->interface_name=NULL; |
78 |
|
net_stats->tx=0; |
79 |
|
net_stats->rx=0; |
80 |
+ |
net_stats->ipackets=0; |
81 |
+ |
net_stats->opackets=0; |
82 |
+ |
net_stats->ierrors=0; |
83 |
+ |
net_stats->oerrors=0; |
84 |
+ |
net_stats->collisions=0; |
85 |
|
net_stats++; |
86 |
|
} |
87 |
|
} |
129 |
|
/* Horrible big enough, but it should be easily big enough */ |
130 |
|
char line[8096]; |
131 |
|
regex_t regex; |
132 |
< |
regmatch_t line_match[4]; |
132 |
> |
regmatch_t line_match[9]; |
133 |
|
#endif |
134 |
< |
#ifdef FREEBSD |
134 |
> |
#ifdef ALLBSD |
135 |
|
struct ifaddrs *net, *net_ptr; |
136 |
|
struct if_data *net_data; |
137 |
|
#endif |
138 |
|
|
139 |
< |
#ifdef FREEBSD |
139 |
> |
#ifdef ALLBSD |
140 |
|
if(getifaddrs(&net) != 0){ |
141 |
|
return NULL; |
142 |
|
} |
156 |
|
if(network_stat_ptr->interface_name==NULL) return NULL; |
157 |
|
net_data=(struct if_data *)net_ptr->ifa_data; |
158 |
|
network_stat_ptr->rx=net_data->ifi_ibytes; |
159 |
< |
network_stat_ptr->tx=net_data->ifi_obytes; |
159 |
> |
network_stat_ptr->tx=net_data->ifi_obytes; |
160 |
> |
network_stat_ptr->ipackets=net_data->ifi_ipackets; |
161 |
> |
network_stat_ptr->opackets=net_data->ifi_opackets; |
162 |
> |
network_stat_ptr->ierrors=net_data->ifi_ierrors; |
163 |
> |
network_stat_ptr->oerrors=net_data->ifi_oerrors; |
164 |
> |
network_stat_ptr->collisions=net_data->ifi_collisions; |
165 |
|
network_stat_ptr->systime=time(NULL); |
166 |
|
interfaces++; |
167 |
|
} |
180 |
|
kstat_read(kc, ksp, NULL); |
181 |
|
|
182 |
|
#ifdef SOL7 |
183 |
< |
#define RLOOKUP "rbytes" |
184 |
< |
#define WLOOKUP "obytes" |
183 |
> |
#define LRX "rbytes" |
184 |
> |
#define LTX "obytes" |
185 |
> |
#define LIPACKETS "ipackets" |
186 |
> |
#define LOPACKETS "opackets" |
187 |
|
#define VALTYPE value.ui32 |
188 |
|
#else |
189 |
< |
#define RLOOKUP "rbytes64" |
190 |
< |
#define WLOOKUP "obytes64" |
189 |
> |
#define LRX "rbytes64" |
190 |
> |
#define LTX "obytes64" |
191 |
> |
#define LIPACKETS "ipackets64" |
192 |
> |
#define LOPACKETS "opackets64" |
193 |
|
#define VALTYPE value.ui64 |
194 |
|
#endif |
195 |
|
|
196 |
< |
if((knp=kstat_data_lookup(ksp, RLOOKUP))==NULL){ |
197 |
< |
/* Not a network interface, so skip to the next entry */ |
196 |
> |
/* Read rx */ |
197 |
> |
if((knp=kstat_data_lookup(ksp, LRX))==NULL){ |
198 |
> |
/* This is a network interface, but it doesn't |
199 |
> |
* have the rbytes/obytes values; for instance, |
200 |
> |
* the loopback devices have this behaviour |
201 |
> |
* (although they do track packets in/out). */ |
202 |
> |
/* FIXME: Show packet counts when byte counts |
203 |
> |
* not available. */ |
204 |
|
continue; |
205 |
|
} |
206 |
|
|
207 |
+ |
/* Create new network_stats */ |
208 |
|
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
209 |
|
if(network_stats==NULL){ |
210 |
|
return NULL; |
211 |
|
} |
212 |
|
network_stat_ptr=network_stats+interfaces; |
213 |
+ |
|
214 |
+ |
/* Finish reading rx */ |
215 |
|
network_stat_ptr->rx=knp->VALTYPE; |
216 |
|
|
217 |
< |
if((knp=kstat_data_lookup(ksp, WLOOKUP))==NULL){ |
218 |
< |
/* Not a network interface, so skip to the next entry */ |
217 |
> |
/* Read tx */ |
218 |
> |
if((knp=kstat_data_lookup(ksp, LTX))==NULL){ |
219 |
|
continue; |
220 |
|
} |
221 |
|
network_stat_ptr->tx=knp->VALTYPE; |
222 |
+ |
|
223 |
+ |
/* Read ipackets */ |
224 |
+ |
if((knp=kstat_data_lookup(ksp, LIPACKETS))==NULL){ |
225 |
+ |
continue; |
226 |
+ |
} |
227 |
+ |
network_stat_ptr->ipackets=knp->VALTYPE; |
228 |
+ |
|
229 |
+ |
/* Read opackets */ |
230 |
+ |
if((knp=kstat_data_lookup(ksp, LOPACKETS))==NULL){ |
231 |
+ |
continue; |
232 |
+ |
} |
233 |
+ |
network_stat_ptr->opackets=knp->VALTYPE; |
234 |
+ |
|
235 |
+ |
/* Read ierrors */ |
236 |
+ |
if((knp=kstat_data_lookup(ksp, "ierrors"))==NULL){ |
237 |
+ |
continue; |
238 |
+ |
} |
239 |
+ |
network_stat_ptr->ierrors=knp->value.ui32; |
240 |
+ |
|
241 |
+ |
/* Read oerrors */ |
242 |
+ |
if((knp=kstat_data_lookup(ksp, "oerrors"))==NULL){ |
243 |
+ |
continue; |
244 |
+ |
} |
245 |
+ |
network_stat_ptr->oerrors=knp->value.ui32; |
246 |
+ |
|
247 |
+ |
/* Read collisions */ |
248 |
+ |
if((knp=kstat_data_lookup(ksp, "collisions"))==NULL){ |
249 |
+ |
continue; |
250 |
+ |
} |
251 |
+ |
network_stat_ptr->collisions=knp->value.ui32; |
252 |
+ |
|
253 |
+ |
/* Read interface name */ |
254 |
|
if(network_stat_ptr->interface_name!=NULL){ |
255 |
|
free(network_stat_ptr->interface_name); |
256 |
|
} |
257 |
|
network_stat_ptr->interface_name=strdup(ksp->ks_name); |
258 |
|
|
259 |
+ |
/* Store systime */ |
260 |
|
network_stat_ptr->systime=time(NULL); |
261 |
+ |
|
262 |
|
interfaces++; |
263 |
|
} |
264 |
|
} |
275 |
|
fgets(line, sizeof(line), f); |
276 |
|
|
277 |
|
|
278 |
< |
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){ |
278 |
> |
if((regcomp(®ex, "^ *([^:]+): *([0-9]+) +([0-9]+) +([0-9]+) +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +([0-9]+) +([0-9]+) +([0-9]+) +[0-9]+ +[0-9]+ +([0-9]+)", REG_EXTENDED))!=0){ |
279 |
|
return NULL; |
280 |
|
} |
281 |
|
|
282 |
|
interfaces=0; |
283 |
|
|
284 |
|
while((fgets(line, sizeof(line), f)) != NULL){ |
285 |
< |
if((regexec(®ex, line, 4, line_match, 0))!=0){ |
285 |
> |
if((regexec(®ex, line, 9, line_match, 0))!=0){ |
286 |
|
continue; |
287 |
|
} |
288 |
|
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
297 |
|
|
298 |
|
network_stat_ptr->interface_name=get_string_match(line, &line_match[1]); |
299 |
|
network_stat_ptr->rx=get_ll_match(line, &line_match[2]); |
300 |
< |
network_stat_ptr->tx=get_ll_match(line, &line_match[3]); |
300 |
> |
network_stat_ptr->tx=get_ll_match(line, &line_match[5]); |
301 |
> |
network_stat_ptr->ipackets=get_ll_match(line, &line_match[3]); |
302 |
> |
network_stat_ptr->opackets=get_ll_match(line, &line_match[6]); |
303 |
> |
network_stat_ptr->ierrors=get_ll_match(line, &line_match[4]); |
304 |
> |
network_stat_ptr->oerrors=get_ll_match(line, &line_match[7]); |
305 |
> |
network_stat_ptr->collisions=get_ll_match(line, &line_match[8]); |
306 |
|
network_stat_ptr->systime=time(NULL); |
307 |
|
|
308 |
|
interfaces++; |
311 |
|
regfree(®ex); |
312 |
|
|
313 |
|
#endif |
314 |
+ |
|
315 |
+ |
#ifdef CYGWIN |
316 |
+ |
return NULL; |
317 |
+ |
#endif |
318 |
+ |
|
319 |
|
*entries=interfaces; |
320 |
|
|
321 |
|
return network_stats; |
322 |
|
} |
323 |
|
|
324 |
|
long long transfer_diff(long long new, long long old){ |
325 |
< |
#ifdef SOL7 |
326 |
< |
#define MAXVAL 4294967296 |
325 |
> |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) || defined(OPENBSD) |
326 |
> |
/* 32-bit quantities, so we must explicitly deal with wraparound. */ |
327 |
> |
#define MAXVAL 0x100000000LL |
328 |
> |
if (new >= old) { |
329 |
> |
return new - old; |
330 |
> |
} else { |
331 |
> |
return MAXVAL + new - old; |
332 |
> |
} |
333 |
|
#else |
334 |
< |
#define MAXVAL 18446744073709551616 |
334 |
> |
/* 64-bit quantities, so plain subtraction works. */ |
335 |
> |
return new - old; |
336 |
|
#endif |
337 |
< |
long long result; |
239 |
< |
if(new>=old){ |
240 |
< |
result = (new-old); |
241 |
< |
}else{ |
242 |
< |
result = (MAXVAL+(new-old)); |
243 |
< |
} |
337 |
> |
} |
338 |
|
|
339 |
< |
return result; |
339 |
> |
network_stat_t *get_network_stats_diff(int *entries) { |
340 |
> |
static network_stat_t *diff = NULL; |
341 |
> |
static int diff_count = 0; |
342 |
> |
network_stat_t *src, *dest; |
343 |
> |
int i, j, new_count; |
344 |
|
|
345 |
< |
} |
345 |
> |
if (network_stats == NULL) { |
346 |
> |
/* No previous stats, so we can't calculate a difference. */ |
347 |
> |
return get_network_stats(entries); |
348 |
> |
} |
349 |
|
|
350 |
< |
network_stat_t *get_network_stats_diff(int *entries){ |
351 |
< |
static network_stat_t *network_stats_diff=NULL; |
352 |
< |
static int sizeof_net_stats_diff=0; |
353 |
< |
network_stat_t *network_stats_ptr, *network_stats_diff_ptr; |
354 |
< |
int ifaces, x, y; |
350 |
> |
/* Resize the results array to match the previous stats. */ |
351 |
> |
diff = network_stat_malloc(interfaces, &diff_count, diff); |
352 |
> |
if (diff == NULL) { |
353 |
> |
return NULL; |
354 |
> |
} |
355 |
|
|
356 |
< |
if(network_stats==NULL){ |
357 |
< |
network_stats_ptr=get_network_stats(&ifaces); |
358 |
< |
*entries=ifaces; |
359 |
< |
return network_stats_ptr; |
356 |
> |
/* Copy the previous stats into the result. */ |
357 |
> |
for (i = 0; i < diff_count; i++) { |
358 |
> |
src = &network_stats[i]; |
359 |
> |
dest = &diff[i]; |
360 |
> |
|
361 |
> |
if (dest->interface_name != NULL) { |
362 |
> |
free(dest->interface_name); |
363 |
> |
} |
364 |
> |
dest->interface_name = strdup(src->interface_name); |
365 |
> |
dest->rx = src->rx; |
366 |
> |
dest->tx = src->tx; |
367 |
> |
dest->ipackets = src->ipackets; |
368 |
> |
dest->opackets = src->opackets; |
369 |
> |
dest->ierrors = src->ierrors; |
370 |
> |
dest->oerrors = src->oerrors; |
371 |
> |
dest->collisions = src->collisions; |
372 |
> |
dest->systime = src->systime; |
373 |
|
} |
374 |
|
|
375 |
< |
network_stats_diff=network_stat_malloc(interfaces, &sizeof_net_stats_diff, network_stats_diff); |
376 |
< |
if(network_stats_diff==NULL){ |
375 |
> |
/* Get a new set of stats. */ |
376 |
> |
if (get_network_stats(&new_count) == NULL) { |
377 |
|
return NULL; |
378 |
|
} |
379 |
|
|
380 |
< |
network_stats_ptr=network_stats; |
381 |
< |
network_stats_diff_ptr=network_stats_diff; |
380 |
> |
/* For each previous stat... */ |
381 |
> |
for (i = 0; i < diff_count; i++) { |
382 |
> |
dest = &diff[i]; |
383 |
|
|
384 |
< |
for(ifaces=0;ifaces<interfaces;ifaces++){ |
385 |
< |
if(network_stats_diff_ptr->interface_name!=NULL){ |
386 |
< |
free(network_stats_diff_ptr->interface_name); |
384 |
> |
/* ... find the corresponding new stat ... */ |
385 |
> |
for (j = 0; j < new_count; j++) { |
386 |
> |
/* Try the new stat in the same position first, |
387 |
> |
since that's most likely to be it. */ |
388 |
> |
src = &network_stats[(i + j) % new_count]; |
389 |
> |
if (strcmp(src->interface_name, dest->interface_name) == 0) { |
390 |
> |
break; |
391 |
> |
} |
392 |
|
} |
393 |
< |
network_stats_diff_ptr->interface_name=strdup(network_stats_ptr->interface_name); |
394 |
< |
network_stats_diff_ptr->tx=network_stats_ptr->tx; |
395 |
< |
network_stats_diff_ptr->rx=network_stats_ptr->rx; |
396 |
< |
network_stats_diff_ptr->systime=network_stats->systime; |
393 |
> |
if (j == new_count) { |
394 |
> |
/* No match found. */ |
395 |
> |
continue; |
396 |
> |
} |
397 |
|
|
398 |
< |
network_stats_ptr++; |
399 |
< |
network_stats_diff_ptr++; |
398 |
> |
/* ... and subtract the previous stat from it to get the |
399 |
> |
difference. */ |
400 |
> |
dest->rx = transfer_diff(src->rx, dest->rx); |
401 |
> |
dest->tx = transfer_diff(src->tx, dest->tx); |
402 |
> |
dest->ipackets = transfer_diff(src->ipackets, dest->ipackets); |
403 |
> |
dest->opackets = transfer_diff(src->opackets, dest->opackets); |
404 |
> |
dest->ierrors = transfer_diff(src->ierrors, dest->ierrors); |
405 |
> |
dest->oerrors = transfer_diff(src->oerrors, dest->oerrors); |
406 |
> |
dest->collisions = transfer_diff(src->collisions, dest->collisions); |
407 |
> |
dest->systime = src->systime - dest->systime; |
408 |
|
} |
281 |
– |
network_stats_ptr=get_network_stats(&ifaces); |
282 |
– |
network_stats_diff_ptr=network_stats_diff; |
409 |
|
|
410 |
< |
for(x=0;x<sizeof_net_stats_diff;x++){ |
410 |
> |
*entries = diff_count; |
411 |
> |
return diff; |
412 |
> |
} |
413 |
> |
/* NETWORK INTERFACE STATS */ |
414 |
|
|
415 |
< |
if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){ |
416 |
< |
network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx); |
417 |
< |
network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx); |
418 |
< |
network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime; |
415 |
> |
void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){ |
416 |
> |
|
417 |
> |
for(net_stats+=start; start<end; start++){ |
418 |
> |
net_stats->interface_name=NULL; |
419 |
> |
net_stats->speed=0; |
420 |
> |
net_stats->dup=UNKNOWN_DUPLEX; |
421 |
> |
net_stats++; |
422 |
> |
} |
423 |
> |
} |
424 |
> |
|
425 |
> |
network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){ |
426 |
> |
|
427 |
> |
if(net_stats==NULL){ |
428 |
> |
|
429 |
> |
if((net_stats=malloc(needed_entries * sizeof(network_iface_stat_t)))==NULL){ |
430 |
> |
return NULL; |
431 |
> |
} |
432 |
> |
network_iface_stat_init(0, needed_entries, net_stats); |
433 |
> |
*cur_entries=needed_entries; |
434 |
> |
|
435 |
> |
return net_stats; |
436 |
> |
} |
437 |
> |
|
438 |
> |
|
439 |
> |
if(*cur_entries<needed_entries){ |
440 |
> |
net_stats=realloc(net_stats, (sizeof(network_iface_stat_t)*needed_entries)); |
441 |
> |
if(net_stats==NULL){ |
442 |
> |
return NULL; |
443 |
> |
} |
444 |
> |
network_iface_stat_init(*cur_entries, needed_entries, net_stats); |
445 |
> |
*cur_entries=needed_entries; |
446 |
> |
} |
447 |
> |
|
448 |
> |
return net_stats; |
449 |
> |
} |
450 |
> |
|
451 |
> |
network_iface_stat_t *get_network_iface_stats(int *entries){ |
452 |
> |
static network_iface_stat_t *network_iface_stats; |
453 |
> |
network_iface_stat_t *network_iface_stat_ptr; |
454 |
> |
static int sizeof_network_iface_stats=0; |
455 |
> |
int ifaces = 0; |
456 |
> |
|
457 |
> |
#ifdef SOLARIS |
458 |
> |
kstat_ctl_t *kc; |
459 |
> |
kstat_t *ksp; |
460 |
> |
kstat_named_t *knp; |
461 |
> |
int sock; |
462 |
> |
#endif |
463 |
> |
#ifdef ALLBSD |
464 |
> |
struct ifaddrs *net, *net_ptr; |
465 |
> |
struct ifmediareq ifmed; |
466 |
> |
struct ifreq ifr; |
467 |
> |
int sock; |
468 |
> |
int x; |
469 |
> |
#endif |
470 |
> |
#ifdef LINUX |
471 |
> |
FILE *f; |
472 |
> |
/* Horrible big enough, but it should be easily big enough */ |
473 |
> |
char line[8096]; |
474 |
> |
int sock; |
475 |
> |
#endif |
476 |
> |
|
477 |
> |
#ifdef ALLBSD |
478 |
> |
if(getifaddrs(&net) != 0){ |
479 |
> |
return NULL; |
480 |
> |
} |
481 |
> |
|
482 |
> |
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == 0) return NULL; |
483 |
> |
|
484 |
> |
for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){ |
485 |
> |
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
486 |
> |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
487 |
> |
if(network_iface_stats==NULL){ |
488 |
> |
return NULL; |
489 |
> |
} |
490 |
> |
network_iface_stat_ptr = network_iface_stats + ifaces; |
491 |
> |
|
492 |
> |
memset(&ifr, 0, sizeof(ifr)); |
493 |
> |
strncpy(ifr.ifr_name, net_ptr->ifa_name, sizeof(ifr.ifr_name)); |
494 |
> |
|
495 |
> |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0){ |
496 |
> |
continue; |
497 |
> |
} |
498 |
> |
if((ifr.ifr_flags & IFF_UP) != 0){ |
499 |
> |
network_iface_stat_ptr->up = 1; |
500 |
|
}else{ |
501 |
< |
|
502 |
< |
network_stats_ptr=network_stats; |
503 |
< |
for(y=0;y<ifaces;y++){ |
504 |
< |
if((strcmp(network_stats_diff_ptr->interface_name, network_stats_ptr->interface_name))==0){ |
505 |
< |
network_stats_diff_ptr->tx = transfer_diff(network_stats_ptr->tx, network_stats_diff_ptr->tx); |
506 |
< |
network_stats_diff_ptr->rx = transfer_diff(network_stats_ptr->rx, network_stats_diff_ptr->rx); |
507 |
< |
network_stats_diff_ptr->systime = network_stats_ptr->systime - network_stats_diff_ptr->systime; |
501 |
> |
network_iface_stat_ptr->up = 0; |
502 |
> |
} |
503 |
> |
|
504 |
> |
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
505 |
> |
network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name); |
506 |
> |
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
507 |
> |
|
508 |
> |
network_iface_stat_ptr->speed = 0; |
509 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
510 |
> |
ifaces++; |
511 |
> |
|
512 |
> |
memset(&ifmed, 0, sizeof(struct ifmediareq)); |
513 |
> |
strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
514 |
> |
if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
515 |
> |
/* Not all interfaces support the media ioctls. */ |
516 |
> |
continue; |
517 |
> |
} |
518 |
> |
|
519 |
> |
/* We may need to change this if we start doing wireless devices too */ |
520 |
> |
if( (ifmed.ifm_active | IFM_ETHER) != ifmed.ifm_active ){ |
521 |
> |
/* Not a ETHER device */ |
522 |
> |
continue; |
523 |
> |
} |
524 |
> |
|
525 |
> |
/* Only intrested in the first 4 bits) - Assuming only ETHER devices */ |
526 |
> |
x = ifmed.ifm_active & 0x0f; |
527 |
> |
switch(x){ |
528 |
> |
/* 10 Mbit connections. Speedy :) */ |
529 |
> |
case(IFM_10_T): |
530 |
> |
case(IFM_10_2): |
531 |
> |
case(IFM_10_5): |
532 |
> |
case(IFM_10_STP): |
533 |
> |
case(IFM_10_FL): |
534 |
> |
network_iface_stat_ptr->speed = 10; |
535 |
> |
break; |
536 |
> |
/* 100 Mbit conneections */ |
537 |
> |
case(IFM_100_TX): |
538 |
> |
case(IFM_100_FX): |
539 |
> |
case(IFM_100_T4): |
540 |
> |
case(IFM_100_VG): |
541 |
> |
case(IFM_100_T2): |
542 |
> |
network_iface_stat_ptr->speed = 100; |
543 |
> |
break; |
544 |
> |
/* 1000 Mbit connections */ |
545 |
> |
case(IFM_1000_SX): |
546 |
> |
case(IFM_1000_LX): |
547 |
> |
case(IFM_1000_CX): |
548 |
> |
#if defined(IFM_1000_TX) && !defined(OPENBSD) |
549 |
> |
case(IFM_1000_TX): /* FreeBSD 4 and others (but NOT OpenBSD)? */ |
550 |
> |
#endif |
551 |
> |
#ifdef IFM_1000_FX |
552 |
> |
case(IFM_1000_FX): /* FreeBSD 4 */ |
553 |
> |
#endif |
554 |
> |
#ifdef IFM_1000_T |
555 |
> |
case(IFM_1000_T): /* FreeBSD 5 */ |
556 |
> |
#endif |
557 |
> |
network_iface_stat_ptr->speed = 1000; |
558 |
> |
break; |
559 |
> |
/* We don't know what it is */ |
560 |
> |
default: |
561 |
> |
network_iface_stat_ptr->speed = 0; |
562 |
> |
break; |
563 |
> |
} |
564 |
> |
|
565 |
> |
if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){ |
566 |
> |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
567 |
> |
}else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){ |
568 |
> |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
569 |
> |
}else{ |
570 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
571 |
> |
} |
572 |
> |
|
573 |
> |
} |
574 |
> |
freeifaddrs(net); |
575 |
> |
close(sock); |
576 |
> |
#endif |
577 |
> |
|
578 |
> |
#ifdef SOLARIS |
579 |
> |
if ((kc = kstat_open()) == NULL) { |
580 |
> |
return NULL; |
581 |
> |
} |
582 |
> |
|
583 |
> |
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) { |
584 |
> |
return NULL; |
585 |
> |
} |
586 |
> |
|
587 |
> |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
588 |
> |
if (!strcmp(ksp->ks_class, "net")) { |
589 |
> |
struct ifreq ifr; |
590 |
> |
|
591 |
> |
kstat_read(kc, ksp, NULL); |
592 |
> |
|
593 |
> |
strncpy(ifr.ifr_name, ksp->ks_name, sizeof ifr.ifr_name); |
594 |
> |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
595 |
> |
/* Not a network interface. */ |
596 |
> |
continue; |
597 |
> |
} |
598 |
> |
|
599 |
> |
network_iface_stats = network_iface_stat_malloc(ifaces + 1, &sizeof_network_iface_stats, network_iface_stats); |
600 |
> |
if (network_iface_stats == NULL) { |
601 |
> |
return NULL; |
602 |
> |
} |
603 |
> |
network_iface_stat_ptr = network_iface_stats + ifaces; |
604 |
> |
ifaces++; |
605 |
> |
|
606 |
> |
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
607 |
> |
network_iface_stat_ptr->interface_name = strdup(ksp->ks_name); |
608 |
> |
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
609 |
> |
|
610 |
> |
if ((ifr.ifr_flags & IFF_UP) != 0) { |
611 |
> |
network_iface_stat_ptr->up = 1; |
612 |
> |
} else { |
613 |
> |
network_iface_stat_ptr->up = 1; |
614 |
> |
} |
615 |
> |
|
616 |
> |
if ((knp = kstat_data_lookup(ksp, "ifspeed")) != NULL) { |
617 |
> |
network_iface_stat_ptr->speed = knp->value.ui64 / (1000 * 1000); |
618 |
> |
} else { |
619 |
> |
network_iface_stat_ptr->speed = 0; |
620 |
> |
} |
621 |
> |
|
622 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
623 |
> |
if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) { |
624 |
> |
switch (knp->value.ui32) { |
625 |
> |
case 1: |
626 |
> |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
627 |
|
break; |
628 |
+ |
case 2: |
629 |
+ |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
630 |
+ |
break; |
631 |
|
} |
632 |
< |
|
301 |
< |
network_stats_ptr++; |
302 |
< |
} |
632 |
> |
} |
633 |
|
} |
634 |
+ |
} |
635 |
|
|
636 |
< |
network_stats_ptr++; |
637 |
< |
network_stats_diff_ptr++; |
636 |
> |
close(sock); |
637 |
> |
kstat_close(kc); |
638 |
> |
#endif |
639 |
> |
#ifdef LINUX |
640 |
> |
f = fopen("/proc/net/dev", "r"); |
641 |
> |
if(f == NULL){ |
642 |
> |
return NULL; |
643 |
> |
} |
644 |
> |
|
645 |
> |
/* Setup stuff so we can do the ioctl to get the info */ |
646 |
> |
if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0){ |
647 |
> |
return NULL; |
648 |
|
} |
649 |
|
|
650 |
< |
*entries=sizeof_net_stats_diff; |
651 |
< |
return network_stats_diff; |
652 |
< |
} |
650 |
> |
/* Ignore first 2 lines.. Just headings */ |
651 |
> |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
652 |
> |
if((fgets(line, sizeof(line), f)) == NULL) return NULL; |
653 |
> |
|
654 |
> |
while((fgets(line, sizeof(line), f)) != NULL){ |
655 |
> |
char *name, *ptr; |
656 |
> |
struct ifreq ifr; |
657 |
> |
struct ethtool_cmd ethcmd; |
658 |
> |
int err; |
659 |
> |
|
660 |
> |
/* Get the interface name */ |
661 |
> |
ptr = strchr(line, ':'); |
662 |
> |
if (ptr == NULL) continue; |
663 |
> |
*ptr='\0'; |
664 |
> |
name = line; |
665 |
> |
while(isspace(*(name))){ |
666 |
> |
name++; |
667 |
> |
} |
668 |
> |
|
669 |
> |
memset(&ifr, 0, sizeof ifr); |
670 |
> |
strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
671 |
> |
|
672 |
> |
if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
673 |
> |
continue; |
674 |
> |
} |
675 |
> |
|
676 |
> |
/* We have a good interface to add */ |
677 |
> |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
678 |
> |
if(network_iface_stats==NULL){ |
679 |
> |
return NULL; |
680 |
> |
} |
681 |
> |
network_iface_stat_ptr = network_iface_stats + ifaces; |
682 |
> |
network_iface_stat_ptr->interface_name = strdup(name); |
683 |
> |
if ((ifr.ifr_flags & IFF_UP) != 0) { |
684 |
> |
network_iface_stat_ptr->up = 1; |
685 |
> |
} else { |
686 |
> |
network_iface_stat_ptr->up = 0; |
687 |
> |
} |
688 |
> |
|
689 |
> |
memset(ðcmd, 0, sizeof ethcmd); |
690 |
> |
ethcmd.cmd = ETHTOOL_GSET; |
691 |
> |
ifr.ifr_data = (caddr_t) ðcmd; |
692 |
> |
|
693 |
> |
err = ioctl(sock, SIOCETHTOOL, &ifr); |
694 |
> |
if (err == 0) { |
695 |
> |
network_iface_stat_ptr->speed = ethcmd.speed; |
696 |
> |
|
697 |
> |
switch (ethcmd.duplex) { |
698 |
> |
case 0x00: |
699 |
> |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
700 |
> |
break; |
701 |
> |
case 0x01: |
702 |
> |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
703 |
> |
break; |
704 |
> |
default: |
705 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
706 |
> |
} |
707 |
> |
} else { |
708 |
> |
/* Not all interfaces support the ethtool ioctl. */ |
709 |
> |
network_iface_stat_ptr->speed = 0; |
710 |
> |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
711 |
> |
} |
712 |
> |
|
713 |
> |
ifaces++; |
714 |
> |
} |
715 |
> |
close(sock); |
716 |
> |
fclose(f); |
717 |
> |
#endif |
718 |
> |
*entries = ifaces; |
719 |
> |
return network_iface_stats; |
720 |
> |
} |
721 |
|
|