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> |
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 ALLBSD |
135 |
|
struct ifaddrs *net, *net_ptr; |
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++; |
322 |
|
} |
323 |
|
|
324 |
|
long long transfer_diff(long long new, long long old){ |
325 |
< |
#if defined(SOL7) || defined(LINUX) || defined(FREEBSD) |
326 |
< |
#define MAXVAL 4294967296LL |
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 18446744073709551616LL |
334 |
> |
/* 64-bit quantities, so plain subtraction works. */ |
335 |
> |
return new - old; |
336 |
|
#endif |
262 |
– |
long long result; |
263 |
– |
if(new>=old){ |
264 |
– |
result = (new-old); |
265 |
– |
}else{ |
266 |
– |
result = (MAXVAL+(new-old)); |
267 |
– |
} |
268 |
– |
|
269 |
– |
return result; |
270 |
– |
|
337 |
|
} |
338 |
|
|
339 |
|
network_stat_t *get_network_stats_diff(int *entries) { |
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 |
|
|
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 |
|
} |
409 |
|
|
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 |
< |
static int ifaces; |
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; |
473 |
|
char line[8096]; |
474 |
|
int sock; |
475 |
|
#endif |
476 |
< |
ifaces = 0; |
476 |
> |
|
477 |
|
#ifdef ALLBSD |
478 |
|
if(getifaddrs(&net) != 0){ |
479 |
|
return NULL; |
545 |
|
case(IFM_1000_SX): |
546 |
|
case(IFM_1000_LX): |
547 |
|
case(IFM_1000_CX): |
548 |
< |
#if defined(FREEBSD) && !defined(FREEBSD5) |
549 |
< |
case(IFM_1000_TX): |
473 |
< |
case(IFM_1000_FX): |
474 |
< |
#else |
475 |
< |
case(IFM_1000_T): |
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 */ |
576 |
|
#endif |
577 |
|
|
578 |
|
#ifdef SOLARIS |
579 |
< |
if ((kc = kstat_open()) == NULL) { |
580 |
< |
return NULL; |
581 |
< |
} |
579 |
> |
if ((kc = kstat_open()) == NULL) { |
580 |
> |
return NULL; |
581 |
> |
} |
582 |
|
|
583 |
< |
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
584 |
< |
if (!strcmp(ksp->ks_class, "net")) { |
585 |
< |
kstat_read(kc, ksp, NULL); |
586 |
< |
if((knp=kstat_data_lookup(ksp, "ifspeed"))==NULL){ |
587 |
< |
/* Not a network interface, so skip to the next entry */ |
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 |
< |
network_iface_stats=network_iface_stat_malloc((ifaces+1), &sizeof_network_iface_stats, network_iface_stats); |
599 |
< |
if(network_iface_stats==NULL){ |
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 |
< |
network_iface_stat_ptr->speed = knp->value.ui64 / (1000*1000); |
604 |
> |
ifaces++; |
605 |
|
|
606 |
< |
if((knp=kstat_data_lookup(ksp, "link_up"))==NULL){ |
607 |
< |
/* Not a network interface, so skip to the next entry */ |
608 |
< |
continue; |
520 |
< |
} |
521 |
< |
/* Solaris has 1 for up, and 0 for not. As we do too */ |
522 |
< |
network_iface_stat_ptr->up = knp->value.ui32; |
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((knp=kstat_data_lookup(ksp, "link_duplex"))==NULL){ |
611 |
< |
/* Not a network interface, so skip to the next entry */ |
612 |
< |
continue; |
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 |
< |
network_iface_stat_ptr->dup = UNKNOWN_DUPLEX; |
617 |
< |
if(knp->value.ui32 == 2){ |
618 |
< |
network_iface_stat_ptr->dup = FULL_DUPLEX; |
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 |
|
} |
533 |
– |
if(knp->value.ui32 == 1){ |
534 |
– |
network_iface_stat_ptr->dup = HALF_DUPLEX; |
535 |
– |
} |
621 |
|
|
622 |
< |
if(network_iface_stat_ptr->interface_name != NULL) free(network_iface_stat_ptr->interface_name); |
623 |
< |
network_iface_stat_ptr->interface_name = strdup(ksp->ks_name); |
624 |
< |
if(network_iface_stat_ptr->interface_name == NULL) return NULL; |
625 |
< |
ifaces++; |
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 |
> |
} |
633 |
|
} |
634 |
|
} |
635 |
+ |
|
636 |
+ |
close(sock); |
637 |
|
kstat_close(kc); |
544 |
– |
|
638 |
|
#endif |
639 |
|
#ifdef LINUX |
640 |
|
f = fopen("/proc/net/dev", "r"); |