28 |
|
#include <stdlib.h> |
29 |
|
#include <string.h> |
30 |
|
#include "statgrab.h" |
31 |
+ |
#include "vector.h" |
32 |
+ |
#include "tools.h" |
33 |
|
#include <time.h> |
34 |
|
#ifdef SOLARIS |
35 |
|
#include <kstat.h> |
40 |
|
#include <net/if.h> |
41 |
|
#include <netinet/in.h> |
42 |
|
#include <sys/sockio.h> |
43 |
+ |
#include <unistd.h> |
44 |
|
#endif |
45 |
|
#ifdef LINUX |
46 |
|
#include <stdio.h> |
50 |
|
#include <sys/socket.h> |
51 |
|
#include <net/if.h> |
52 |
|
#include <ctype.h> |
50 |
– |
#include "tools.h" |
53 |
|
/* Stuff which could be defined by defining KERNEL, but |
54 |
|
* that would be a bad idea, so we'll just declare it here |
55 |
|
*/ |
68 |
|
#include <net/if.h> |
69 |
|
#include <net/if_media.h> |
70 |
|
#include <sys/ioctl.h> |
71 |
+ |
#include <unistd.h> |
72 |
|
#endif |
73 |
|
|
74 |
< |
static network_stat_t *network_stats=NULL; |
75 |
< |
static int interfaces=0; |
76 |
< |
|
77 |
< |
void network_stat_init(int start, int end, network_stat_t *net_stats){ |
78 |
< |
|
79 |
< |
for(net_stats+=start; start<end; start++){ |
80 |
< |
net_stats->interface_name=NULL; |
81 |
< |
net_stats->tx=0; |
82 |
< |
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 |
< |
} |
74 |
> |
static void network_stat_init(sg_network_io_stats *s) { |
75 |
> |
s->interface_name = NULL; |
76 |
> |
s->tx = 0; |
77 |
> |
s->rx = 0; |
78 |
> |
s->ipackets = 0; |
79 |
> |
s->opackets = 0; |
80 |
> |
s->ierrors = 0; |
81 |
> |
s->oerrors = 0; |
82 |
> |
s->collisions = 0; |
83 |
|
} |
84 |
|
|
85 |
< |
network_stat_t *network_stat_malloc(int needed_entries, int *cur_entries, network_stat_t *net_stats){ |
86 |
< |
|
91 |
< |
if(net_stats==NULL){ |
92 |
< |
|
93 |
< |
if((net_stats=malloc(needed_entries * sizeof(network_stat_t)))==NULL){ |
94 |
< |
return NULL; |
95 |
< |
} |
96 |
< |
network_stat_init(0, needed_entries, net_stats); |
97 |
< |
*cur_entries=needed_entries; |
98 |
< |
|
99 |
< |
return net_stats; |
100 |
< |
} |
101 |
< |
|
102 |
< |
|
103 |
< |
if(*cur_entries<needed_entries){ |
104 |
< |
net_stats=realloc(net_stats, (sizeof(network_stat_t)*needed_entries)); |
105 |
< |
if(net_stats==NULL){ |
106 |
< |
return NULL; |
107 |
< |
} |
108 |
< |
network_stat_init(*cur_entries, needed_entries, net_stats); |
109 |
< |
*cur_entries=needed_entries; |
110 |
< |
} |
111 |
< |
|
112 |
< |
return net_stats; |
85 |
> |
static void network_stat_destroy(sg_network_io_stats *s) { |
86 |
> |
free(s->interface_name); |
87 |
|
} |
88 |
|
|
89 |
+ |
VECTOR_DECLARE_STATIC(network_stats, sg_network_io_stats, 5, |
90 |
+ |
network_stat_init, network_stat_destroy); |
91 |
|
|
92 |
< |
network_stat_t *get_network_stats(int *entries){ |
92 |
> |
sg_network_io_stats *sg_get_network_io_stats(int *entries){ |
93 |
> |
int interfaces; |
94 |
> |
sg_network_io_stats *network_stat_ptr; |
95 |
|
|
118 |
– |
static int sizeof_network_stats=0; |
119 |
– |
network_stat_t *network_stat_ptr; |
120 |
– |
|
96 |
|
#ifdef SOLARIS |
97 |
|
kstat_ctl_t *kc; |
98 |
|
kstat_t *ksp; |
120 |
|
|
121 |
|
for(net_ptr=net;net_ptr!=NULL;net_ptr=net_ptr->ifa_next){ |
122 |
|
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
123 |
< |
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
124 |
< |
if(network_stats==NULL){ |
123 |
> |
|
124 |
> |
if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) { |
125 |
|
return NULL; |
126 |
|
} |
127 |
|
network_stat_ptr=network_stats+interfaces; |
128 |
|
|
129 |
< |
if(network_stat_ptr->interface_name!=NULL) free(network_stat_ptr->interface_name); |
130 |
< |
network_stat_ptr->interface_name=strdup(net_ptr->ifa_name); |
131 |
< |
if(network_stat_ptr->interface_name==NULL) return NULL; |
129 |
> |
if (sg_update_string(&network_stat_ptr->interface_name, |
130 |
> |
net_ptr->ifa_name) < 0) { |
131 |
> |
return NULL; |
132 |
> |
} |
133 |
|
net_data=(struct if_data *)net_ptr->ifa_data; |
134 |
|
network_stat_ptr->rx=net_data->ifi_ibytes; |
135 |
|
network_stat_ptr->tx=net_data->ifi_obytes; |
175 |
|
* have the rbytes/obytes values; for instance, |
176 |
|
* the loopback devices have this behaviour |
177 |
|
* (although they do track packets in/out). */ |
178 |
+ |
/* FIXME: Show packet counts when byte counts |
179 |
+ |
* not available. */ |
180 |
|
continue; |
181 |
|
} |
182 |
|
|
183 |
|
/* Create new network_stats */ |
184 |
< |
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
207 |
< |
if(network_stats==NULL){ |
184 |
> |
if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) { |
185 |
|
return NULL; |
186 |
|
} |
187 |
|
network_stat_ptr=network_stats+interfaces; |
226 |
|
network_stat_ptr->collisions=knp->value.ui32; |
227 |
|
|
228 |
|
/* Read interface name */ |
229 |
< |
if(network_stat_ptr->interface_name!=NULL){ |
230 |
< |
free(network_stat_ptr->interface_name); |
229 |
> |
if (sg_update_string(&network_stat_ptr->interface_name, |
230 |
> |
ksp->ks_name) < 0) { |
231 |
> |
return NULL; |
232 |
|
} |
255 |
– |
network_stat_ptr->interface_name=strdup(ksp->ks_name); |
233 |
|
|
234 |
|
/* Store systime */ |
235 |
|
network_stat_ptr->systime=time(NULL); |
260 |
|
if((regexec(®ex, line, 9, line_match, 0))!=0){ |
261 |
|
continue; |
262 |
|
} |
263 |
< |
network_stats=network_stat_malloc((interfaces+1), &sizeof_network_stats, network_stats); |
264 |
< |
if(network_stats==NULL){ |
265 |
< |
return NULL; |
263 |
> |
|
264 |
> |
if (VECTOR_RESIZE(network_stats, interfaces + 1) < 0) { |
265 |
> |
return NULL; |
266 |
|
} |
267 |
|
network_stat_ptr=network_stats+interfaces; |
268 |
|
|
270 |
|
free(network_stat_ptr->interface_name); |
271 |
|
} |
272 |
|
|
273 |
< |
network_stat_ptr->interface_name=get_string_match(line, &line_match[1]); |
274 |
< |
network_stat_ptr->rx=get_ll_match(line, &line_match[2]); |
275 |
< |
network_stat_ptr->tx=get_ll_match(line, &line_match[5]); |
276 |
< |
network_stat_ptr->ipackets=get_ll_match(line, &line_match[3]); |
277 |
< |
network_stat_ptr->opackets=get_ll_match(line, &line_match[6]); |
278 |
< |
network_stat_ptr->ierrors=get_ll_match(line, &line_match[4]); |
279 |
< |
network_stat_ptr->oerrors=get_ll_match(line, &line_match[7]); |
280 |
< |
network_stat_ptr->collisions=get_ll_match(line, &line_match[8]); |
273 |
> |
network_stat_ptr->interface_name=sg_get_string_match(line, &line_match[1]); |
274 |
> |
network_stat_ptr->rx=sg_get_ll_match(line, &line_match[2]); |
275 |
> |
network_stat_ptr->tx=sg_get_ll_match(line, &line_match[5]); |
276 |
> |
network_stat_ptr->ipackets=sg_get_ll_match(line, &line_match[3]); |
277 |
> |
network_stat_ptr->opackets=sg_get_ll_match(line, &line_match[6]); |
278 |
> |
network_stat_ptr->ierrors=sg_get_ll_match(line, &line_match[4]); |
279 |
> |
network_stat_ptr->oerrors=sg_get_ll_match(line, &line_match[7]); |
280 |
> |
network_stat_ptr->collisions=sg_get_ll_match(line, &line_match[8]); |
281 |
|
network_stat_ptr->systime=time(NULL); |
282 |
|
|
283 |
|
interfaces++; |
296 |
|
return network_stats; |
297 |
|
} |
298 |
|
|
299 |
< |
long long transfer_diff(long long new, long long old){ |
300 |
< |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) |
301 |
< |
#define MAXVAL 0xffffffffLL |
299 |
> |
static long long transfer_diff(long long new, long long old){ |
300 |
> |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) || defined(DFBSD) || defined(OPENBSD) |
301 |
> |
/* 32-bit quantities, so we must explicitly deal with wraparound. */ |
302 |
> |
#define MAXVAL 0x100000000LL |
303 |
> |
if (new >= old) { |
304 |
> |
return new - old; |
305 |
> |
} else { |
306 |
> |
return MAXVAL + new - old; |
307 |
> |
} |
308 |
|
#else |
309 |
< |
#define MAXVAL 0xffffffffffffffffLL |
309 |
> |
/* 64-bit quantities, so plain subtraction works. */ |
310 |
> |
return new - old; |
311 |
|
#endif |
328 |
– |
long long result; |
329 |
– |
if(new>=old){ |
330 |
– |
result = (new-old); |
331 |
– |
}else{ |
332 |
– |
result = (MAXVAL+(new-old)); |
333 |
– |
} |
334 |
– |
|
335 |
– |
return result; |
336 |
– |
|
312 |
|
} |
313 |
|
|
314 |
< |
network_stat_t *get_network_stats_diff(int *entries) { |
315 |
< |
static network_stat_t *diff = NULL; |
316 |
< |
static int diff_count = 0; |
317 |
< |
network_stat_t *src, *dest; |
318 |
< |
int i, j, new_count; |
314 |
> |
sg_network_io_stats *sg_get_network_io_stats_diff(int *entries) { |
315 |
> |
VECTOR_DECLARE_STATIC(diff, sg_network_io_stats, 1, |
316 |
> |
network_stat_init, network_stat_destroy); |
317 |
> |
sg_network_io_stats *src = NULL, *dest; |
318 |
> |
int i, j, diff_count, new_count; |
319 |
|
|
320 |
|
if (network_stats == NULL) { |
321 |
|
/* No previous stats, so we can't calculate a difference. */ |
322 |
< |
return get_network_stats(entries); |
322 |
> |
return sg_get_network_io_stats(entries); |
323 |
|
} |
324 |
|
|
325 |
|
/* Resize the results array to match the previous stats. */ |
326 |
< |
diff = network_stat_malloc(interfaces, &diff_count, diff); |
327 |
< |
if (diff == NULL) { |
326 |
> |
diff_count = VECTOR_SIZE(network_stats); |
327 |
> |
if (VECTOR_RESIZE(diff, diff_count) < 0) { |
328 |
|
return NULL; |
329 |
|
} |
330 |
|
|
333 |
|
src = &network_stats[i]; |
334 |
|
dest = &diff[i]; |
335 |
|
|
336 |
< |
if (dest->interface_name != NULL) { |
337 |
< |
free(dest->interface_name); |
336 |
> |
if (sg_update_string(&dest->interface_name, |
337 |
> |
src->interface_name) < 0) { |
338 |
> |
return NULL; |
339 |
|
} |
364 |
– |
dest->interface_name = strdup(src->interface_name); |
340 |
|
dest->rx = src->rx; |
341 |
|
dest->tx = src->tx; |
342 |
|
dest->ipackets = src->ipackets; |
348 |
|
} |
349 |
|
|
350 |
|
/* Get a new set of stats. */ |
351 |
< |
if (get_network_stats(&new_count) == NULL) { |
351 |
> |
if (sg_get_network_io_stats(&new_count) == NULL) { |
352 |
|
return NULL; |
353 |
|
} |
354 |
|
|
385 |
|
*entries = diff_count; |
386 |
|
return diff; |
387 |
|
} |
388 |
+ |
|
389 |
|
/* NETWORK INTERFACE STATS */ |
390 |
|
|
391 |
< |
void network_iface_stat_init(int start, int end, network_iface_stat_t *net_stats){ |
392 |
< |
|
393 |
< |
for(net_stats+=start; start<end; start++){ |
394 |
< |
net_stats->interface_name=NULL; |
419 |
< |
net_stats->speed=0; |
420 |
< |
net_stats->dup=UNKNOWN_DUPLEX; |
421 |
< |
net_stats++; |
422 |
< |
} |
391 |
> |
static void network_iface_stat_init(sg_network_iface_stats *s) { |
392 |
> |
s->interface_name = NULL; |
393 |
> |
s->speed = 0; |
394 |
> |
s->dup = SG_IFACE_DUPLEX_UNKNOWN; |
395 |
|
} |
396 |
|
|
397 |
< |
network_iface_stat_t *network_iface_stat_malloc(int needed_entries, int *cur_entries, network_iface_stat_t *net_stats){ |
398 |
< |
|
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; |
397 |
> |
static void network_iface_stat_destroy(sg_network_iface_stats *s) { |
398 |
> |
free(s->interface_name); |
399 |
|
} |
400 |
|
|
401 |
< |
network_iface_stat_t *get_network_iface_stats(int *entries){ |
402 |
< |
static network_iface_stat_t *network_iface_stats; |
403 |
< |
network_iface_stat_t *network_iface_stat_ptr; |
404 |
< |
static int sizeof_network_iface_stats=0; |
401 |
> |
sg_network_iface_stats *sg_get_network_iface_stats(int *entries){ |
402 |
> |
VECTOR_DECLARE_STATIC(network_iface_stats, sg_network_iface_stats, 5, |
403 |
> |
network_iface_stat_init, network_iface_stat_destroy); |
404 |
> |
sg_network_iface_stats *network_iface_stat_ptr; |
405 |
|
int ifaces = 0; |
406 |
|
|
407 |
|
#ifdef SOLARIS |
433 |
|
|
434 |
|
for(net_ptr=net; net_ptr!=NULL; net_ptr=net_ptr->ifa_next){ |
435 |
|
if(net_ptr->ifa_addr->sa_family != AF_LINK) continue; |
436 |
< |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
437 |
< |
if(network_iface_stats==NULL){ |
436 |
> |
|
437 |
> |
if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) { |
438 |
|
return NULL; |
439 |
|
} |
440 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
451 |
|
network_iface_stat_ptr->up = 0; |
452 |
|
} |
453 |
|
|
454 |
< |
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
455 |
< |
network_iface_stat_ptr->interface_name = strdup(net_ptr->ifa_name); |
456 |
< |
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
454 |
> |
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
455 |
> |
net_ptr->ifa_name) < 0) { |
456 |
> |
return NULL; |
457 |
> |
} |
458 |
|
|
459 |
|
network_iface_stat_ptr->speed = 0; |
460 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
460 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN; |
461 |
|
ifaces++; |
462 |
|
|
463 |
|
memset(&ifmed, 0, sizeof(struct ifmediareq)); |
464 |
< |
strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
464 |
> |
sg_strlcpy(ifmed.ifm_name, net_ptr->ifa_name, sizeof(ifmed.ifm_name)); |
465 |
|
if(ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmed) == -1){ |
466 |
|
/* Not all interfaces support the media ioctls. */ |
467 |
|
continue; |
514 |
|
} |
515 |
|
|
516 |
|
if( (ifmed.ifm_active | IFM_FDX) == ifmed.ifm_active ){ |
517 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
517 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_FULL; |
518 |
|
}else if( (ifmed.ifm_active | IFM_HDX) == ifmed.ifm_active ){ |
519 |
< |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
519 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_HALF; |
520 |
|
}else{ |
521 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
521 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN; |
522 |
|
} |
523 |
|
|
524 |
|
} |
547 |
|
continue; |
548 |
|
} |
549 |
|
|
550 |
< |
network_iface_stats = network_iface_stat_malloc(ifaces + 1, &sizeof_network_iface_stats, network_iface_stats); |
600 |
< |
if (network_iface_stats == NULL) { |
550 |
> |
if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) { |
551 |
|
return NULL; |
552 |
|
} |
553 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
554 |
|
ifaces++; |
555 |
|
|
556 |
< |
if (network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
557 |
< |
network_iface_stat_ptr->interface_name = strdup(ksp->ks_name); |
558 |
< |
if (network_iface_stat_ptr->interface_name == NULL) return NULL; |
556 |
> |
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
557 |
> |
ksp->ks_name) < 0) { |
558 |
> |
return NULL; |
559 |
> |
} |
560 |
|
|
561 |
|
if ((ifr.ifr_flags & IFF_UP) != 0) { |
562 |
|
network_iface_stat_ptr->up = 1; |
570 |
|
network_iface_stat_ptr->speed = 0; |
571 |
|
} |
572 |
|
|
573 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
573 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN; |
574 |
|
if ((knp = kstat_data_lookup(ksp, "link_duplex")) != NULL) { |
575 |
|
switch (knp->value.ui32) { |
576 |
|
case 1: |
577 |
< |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
577 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_HALF; |
578 |
|
break; |
579 |
|
case 2: |
580 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
580 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_FULL; |
581 |
|
break; |
582 |
|
} |
583 |
|
} |
625 |
|
} |
626 |
|
|
627 |
|
/* We have a good interface to add */ |
628 |
< |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
678 |
< |
if(network_iface_stats==NULL){ |
628 |
> |
if (VECTOR_RESIZE(network_iface_stats, ifaces + 1) < 0) { |
629 |
|
return NULL; |
630 |
|
} |
631 |
|
network_iface_stat_ptr = network_iface_stats + ifaces; |
632 |
< |
network_iface_stat_ptr->interface_name = strdup(name); |
632 |
> |
|
633 |
> |
if (sg_update_string(&network_iface_stat_ptr->interface_name, |
634 |
> |
name) < 0) { |
635 |
> |
return NULL; |
636 |
> |
} |
637 |
|
if ((ifr.ifr_flags & IFF_UP) != 0) { |
638 |
|
network_iface_stat_ptr->up = 1; |
639 |
|
} else { |
650 |
|
|
651 |
|
switch (ethcmd.duplex) { |
652 |
|
case 0x00: |
653 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
653 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_FULL; |
654 |
|
break; |
655 |
|
case 0x01: |
656 |
< |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
656 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_HALF; |
657 |
|
break; |
658 |
|
default: |
659 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
659 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN; |
660 |
|
} |
661 |
|
} else { |
662 |
|
/* Not all interfaces support the ethtool ioctl. */ |
663 |
|
network_iface_stat_ptr->speed = 0; |
664 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
664 |
> |
network_iface_stat_ptr->dup = SG_IFACE_DUPLEX_UNKNOWN; |
665 |
|
} |
666 |
|
|
667 |
|
ifaces++; |