version 1.12, 2008/06/12 22:26:01 |
version 1.13, 2009/04/03 20:29:21 |
|
|
#include <net/if.h> |
#include <net/if.h> |
#include <net/if_dl.h> |
#include <net/if_dl.h> |
#include <net/route.h> |
#include <net/route.h> |
|
#include <sys/sockio.h> |
|
|
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
|
|
|
struct ifstat { |
struct ifstat { |
char ifs_name[IFNAMSIZ]; /* interface name */ |
char ifs_name[IFNAMSIZ]; /* interface name */ |
|
char ifs_description[IFDESCRSIZE]; |
struct ifcount ifs_cur; |
struct ifcount ifs_cur; |
struct ifcount ifs_old; |
struct ifcount ifs_old; |
struct ifcount ifs_now; |
struct ifcount ifs_now; |
|
|
/* Define fields */ |
/* Define fields */ |
field_def fields_if[] = { |
field_def fields_if[] = { |
{"IFACE", 8, 16, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0}, |
{"IFACE", 8, 16, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0}, |
{"STATE", 10, 16, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0}, |
{"STATE", 4, 6, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0}, |
{"IPKTS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0}, |
{"IPKTS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0}, |
{"IBYTES", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0}, |
{"IBYTES", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0}, |
{"IERRS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0}, |
{"IERRS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0}, |
|
|
{"OBYTES", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0}, |
{"OBYTES", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0}, |
{"OERRS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0}, |
{"OERRS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0}, |
{"COLLS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0}, |
{"COLLS", 5, 8, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0}, |
|
{"DESC", 14, 64, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0}, |
}; |
}; |
|
|
|
|
|
|
#define FLD_IF_OBYTES FIELD_ADDR(6) |
#define FLD_IF_OBYTES FIELD_ADDR(6) |
#define FLD_IF_OERRS FIELD_ADDR(7) |
#define FLD_IF_OERRS FIELD_ADDR(7) |
#define FLD_IF_COLLS FIELD_ADDR(8) |
#define FLD_IF_COLLS FIELD_ADDR(8) |
|
#define FLD_IF_DESC FIELD_ADDR(9) |
|
|
|
|
/* Define views */ |
/* Define views */ |
field_def *view_if_0[] = { |
field_def *view_if_0[] = { |
FLD_IF_IFACE, FLD_IF_STATE, FLD_IF_IPKTS, FLD_IF_IBYTES, |
FLD_IF_IFACE, FLD_IF_STATE, FLD_IF_DESC, FLD_IF_IPKTS, |
FLD_IF_IERRS, FLD_IF_OPKTS, FLD_IF_OBYTES, FLD_IF_OERRS, |
FLD_IF_IBYTES, FLD_IF_IERRS, FLD_IF_OPKTS, FLD_IF_OBYTES, |
FLD_IF_COLLS, NULL |
FLD_IF_OERRS, FLD_IF_COLLS, NULL |
}; |
}; |
|
|
/* Define view managers */ |
/* Define view managers */ |
|
|
struct sockaddr *info[RTAX_MAX]; |
struct sockaddr *info[RTAX_MAX]; |
struct sockaddr_dl *sdl; |
struct sockaddr_dl *sdl; |
char *buf, *next, *lim; |
char *buf, *next, *lim; |
|
static int s = -1; |
int mib[6]; |
int mib[6]; |
size_t need; |
size_t need; |
|
|
|
|
rt_getaddrinfo( |
rt_getaddrinfo( |
(struct sockaddr *)((struct if_msghdr *)next + 1), |
(struct sockaddr *)((struct if_msghdr *)next + 1), |
ifm.ifm_addrs, info); |
ifm.ifm_addrs, info); |
if ((sdl = (struct sockaddr_dl *)info[RTAX_IFP])) { |
sdl = (struct sockaddr_dl *)info[RTAX_IFP]; |
if (sdl->sdl_family == AF_LINK && |
|
sdl->sdl_nlen > 0) { |
if (sdl && sdl->sdl_family == AF_LINK && |
bcopy(sdl->sdl_data, ifs->ifs_name, |
sdl->sdl_nlen > 0) { |
sdl->sdl_nlen); |
struct ifreq ifrdesc; |
ifs->ifs_name[sdl->sdl_nlen] = '\0'; |
char ifdescr[IFDESCRSIZE]; |
|
int s; |
|
|
|
bcopy(sdl->sdl_data, ifs->ifs_name, |
|
sdl->sdl_nlen); |
|
ifs->ifs_name[sdl->sdl_nlen] = '\0'; |
|
|
|
/* Get the interface description */ |
|
memset(&ifrdesc, 0, sizeof(ifrdesc)); |
|
strlcpy(ifrdesc.ifr_name, ifs->ifs_name, |
|
sizeof(ifrdesc.ifr_name)); |
|
ifrdesc.ifr_data = (caddr_t)&ifdescr; |
|
|
|
s = socket(AF_INET, SOCK_DGRAM, 0); |
|
if (s != -1) { |
|
if (ioctl(s, SIOCGIFDESCR, &ifrdesc) == 0) |
|
strlcpy(ifs->ifs_description, |
|
ifrdesc.ifr_data, |
|
sizeof(ifs->ifs_description)); |
|
close(s); |
} |
} |
} |
} |
if (ifs->ifs_name[0] == '\0') |
if (ifs->ifs_name[0] == '\0') |
|
|
} |
} |
|
|
print_fld_tb(FLD_IF_STATE); |
print_fld_tb(FLD_IF_STATE); |
|
|
|
print_fld_str(FLD_IF_DESC, ifs->ifs_description); |
|
|
print_fld_size(FLD_IF_IBYTES, ifs->ifs_cur.ifc_ib); |
print_fld_size(FLD_IF_IBYTES, ifs->ifs_cur.ifc_ib); |
print_fld_size(FLD_IF_IPKTS, ifs->ifs_cur.ifc_ip); |
print_fld_size(FLD_IF_IPKTS, ifs->ifs_cur.ifc_ip); |