auxstats: get network stats in a portable manner on FreeBSD
as the old grody way doesn't work any more on FreeBSD-10 and later.
This commit is contained in:
parent
019be4481f
commit
da8a485fc1
1 changed files with 14 additions and 33 deletions
|
|
@ -18,10 +18,9 @@
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <libc.h>
|
#include <libc.h>
|
||||||
#include <bio.h>
|
#include <bio.h>
|
||||||
|
#include <ifaddrs.h>
|
||||||
#include "dat.h"
|
#include "dat.h"
|
||||||
|
|
||||||
/* XXX: #if __FreeBSD_version */
|
|
||||||
|
|
||||||
void xapm(int);
|
void xapm(int);
|
||||||
void xloadavg(int);
|
void xloadavg(int);
|
||||||
void xcpu(int);
|
void xcpu(int);
|
||||||
|
|
@ -45,7 +44,6 @@ void (*statfn[])(int) =
|
||||||
static kvm_t *kvm;
|
static kvm_t *kvm;
|
||||||
|
|
||||||
static struct nlist nl[] = {
|
static struct nlist nl[] = {
|
||||||
{ "_ifnet" },
|
|
||||||
{ "_cp_time" },
|
{ "_cp_time" },
|
||||||
{ "" }
|
{ "" }
|
||||||
};
|
};
|
||||||
|
|
@ -86,45 +84,28 @@ kread(ulong addr, char *buf, int size)
|
||||||
void
|
void
|
||||||
xnet(int first)
|
xnet(int first)
|
||||||
{
|
{
|
||||||
|
struct ifaddrs *ifap, *ifa;
|
||||||
ulong out, in, outb, inb, err;
|
ulong out, in, outb, inb, err;
|
||||||
static ulong ifnetaddr;
|
|
||||||
ulong addr;
|
|
||||||
struct ifnet ifnet;
|
|
||||||
struct ifnethead ifnethead;
|
|
||||||
char name[16];
|
|
||||||
|
|
||||||
if(first)
|
if(first)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if(ifnetaddr == 0){
|
if (getifaddrs(&ifap) != 0)
|
||||||
ifnetaddr = nl[0].n_value;
|
|
||||||
if(ifnetaddr == 0)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(kread(ifnetaddr, (char*)&ifnethead, sizeof ifnethead) < 0)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
out = in = outb = inb = err = 0;
|
out = in = outb = inb = err = 0;
|
||||||
addr = (ulong)TAILQ_FIRST(&ifnethead);
|
#define IFA_STAT(s) (((struct if_data *)ifa->ifa_data)->ifi_ ## s)
|
||||||
while(addr){
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
||||||
#if __FreeBSD_version < 500000
|
if (ifa->ifa_addr->sa_family != AF_LINK)
|
||||||
if(kread(addr, (char*)&ifnet, sizeof ifnet) < 0
|
continue;
|
||||||
|| kread((ulong)ifnet.if_name, name, 16) < 0)
|
out += IFA_STAT(opackets);
|
||||||
return;
|
in += IFA_STAT(ipackets);
|
||||||
#else
|
outb += IFA_STAT(obytes);
|
||||||
if(kread(addr, (char*)&ifnet, sizeof ifnet) < 0
|
inb += IFA_STAT(ibytes);
|
||||||
|| kread((ulong)ifnet.if_dname, name, 16) < 0)
|
err += IFA_STAT(oerrors) + IFA_STAT(ierrors);
|
||||||
return;
|
|
||||||
#endif
|
|
||||||
name[15] = 0;
|
|
||||||
addr = (ulong)TAILQ_NEXT(&ifnet, if_link);
|
|
||||||
out += ifnet.if_opackets;
|
|
||||||
in += ifnet.if_ipackets;
|
|
||||||
outb += ifnet.if_obytes;
|
|
||||||
inb += ifnet.if_ibytes;
|
|
||||||
err += ifnet.if_oerrors+ifnet.if_ierrors;
|
|
||||||
}
|
}
|
||||||
|
freeifaddrs(ifap);
|
||||||
|
|
||||||
Bprint(&bout, "etherin %lud 1000\n", in);
|
Bprint(&bout, "etherin %lud 1000\n", in);
|
||||||
Bprint(&bout, "etherout %lud 1000\n", out);
|
Bprint(&bout, "etherout %lud 1000\n", out);
|
||||||
Bprint(&bout, "etherinb %lud 1000000\n", inb);
|
Bprint(&bout, "etherinb %lud 1000000\n", inb);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue