diff options
author | sem <sem@FreeBSD.org> | 2004-07-31 17:46:36 +0800 |
---|---|---|
committer | sem <sem@FreeBSD.org> | 2004-07-31 17:46:36 +0800 |
commit | 781411d60b941dc403030c87fd19b5260e3f5d18 (patch) | |
tree | 8fa37df982baf659a711d5f56c87e261ae15b9f5 /sysutils | |
parent | a9084662b88b3a3dda76d81366c0d455f4d2a689 (diff) | |
download | freebsd-ports-gnome-781411d60b941dc403030c87fd19b5260e3f5d18.tar.gz freebsd-ports-gnome-781411d60b941dc403030c87fd19b5260e3f5d18.tar.zst freebsd-ports-gnome-781411d60b941dc403030c87fd19b5260e3f5d18.zip |
add network interfaces monitoring support
PR: ports/69839
Submitted by: maintainer
Diffstat (limited to 'sysutils')
-rw-r--r-- | sysutils/torsmo/files/freebsd.c | 143 |
1 files changed, 47 insertions, 96 deletions
diff --git a/sysutils/torsmo/files/freebsd.c b/sysutils/torsmo/files/freebsd.c index fed04d33cb53..d8d65ef73f81 100644 --- a/sysutils/torsmo/files/freebsd.c +++ b/sysutils/torsmo/files/freebsd.c @@ -22,6 +22,8 @@ #include <sys/socket.h> #include <net/if.h> #include <net/if_mib.h> +#include <sys/socket.h> +#include <ifaddrs.h> void net_init(); @@ -138,112 +140,61 @@ void update_meminfo() { } } -void net_init() -{ -/* XXX */ -#if 0 - struct ifmibdata tempndata; - int numifaces; - size_t len2; - int mib[5], datamib[6]; - int i; - - mib[0] = CTL_NET; - mib[1] = PF_LINK; - mib[2] = NETLINK_GENERIC; - mib[3] = IFMIB_SYSTEM; - mib[4] = IFMIB_IFCOUNT; - - datamib[0] = CTL_NET; - datamib[1] = PF_LINK; - datamib[2] = NETLINK_GENERIC; - datamib[3] = IFMIB_IFDATA; - datamib[4] = 1; - datamib[5] = IFDATA_GENERAL; - - len = sizeof(struct ifmibdata); - len2 = sizeof(numifaces); - - if(sysctl(mib, 5, &numifaces, &len2, NULL, 0) < 0) - fprintf( stderr, "wmnet: failed to perform sysctl" ); - - for(i = 1; i <= numifaces; i++) - { - datamib[4] = i; - if(sysctl(datamib, 6, &tempndata, &len, NULL, 0) < 0) - { - fprintf( stderr, "wmnet: failed to get device(%d) data", i ); - break; - } - - } - - - /* calculate and allocate mem for ifmibdata containing the if stats */ - data = malloc(len); - -#endif -} - void update_net_stats() { -#if 0 - static int rep; - unsigned int i; - char buf[256]; + struct net_stat *ns; double delta; + long long r, t, last_recv, last_trans; /* get delta */ delta = current_update_time - last_update_time; if (delta <= 0.0001) return; - net_init(); - - /* read each interface */ - for (i=0; i<16; i++) { - struct net_stat *ns; - char *s, *p; - long long r, t, last_recv, last_trans; - int datamib[6]; - - ns = get_net_stat(s); - last_recv = ns->recv; - last_trans = ns->trans; - - datamib[0] = CTL_NET; - datamib[1] = PF_LINK; - datamib[2] = NETLINK_GENERIC; - datamib[3] = IFMIB_IFDATA; - datamib[4] = i; - datamib[5] = IFDATA_GENERAL; - - if (sysctl(datamib, 6, data, &len, NULL, 0) < 0 ) { - perror("sysctl"); - } - - r = data->ifmd_data.ifi_ipackets; - t = data->ifmd_data.ifi_opackets; - - if (r < ns->last_read_recv) - ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r; - else - ns->recv += (r - ns->last_read_recv); - ns->last_read_recv = r; - - if (t < ns->last_read_trans) - ns->trans += ((long long) 4294967295U - ns->last_read_trans) + t; - else - ns->trans += (t - ns->last_read_trans); - ns->last_read_trans = t; - - /* calculate speeds */ - ns->recv_speed = (ns->recv - last_recv) / delta; - ns->trans_speed = (ns->trans - last_trans) / delta; - } - -#endif + struct ifaddrs *ifap, *ifa; + struct if_data *ifd; + + if (getifaddrs(&ifap) < 0) + return; + + for (ifa = ifap; ifa; ifa = ifa->ifa_next) { + ns = get_net_stat((const char *)ifa->ifa_name); + + if (ifa->ifa_flags & IFF_UP) { + last_recv = ns->recv; + last_trans = ns->trans; + + if (ifa->ifa_addr->sa_family != AF_LINK) + continue; + + ifd = (struct if_data *)ifa->ifa_data; + r = ifd->ifi_ibytes; + t = ifd->ifi_obytes; + + if (r < ns->last_read_recv) + ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r; + else + ns->recv += (r - ns->last_read_recv); + + ns->last_read_recv = r; + + if (t < ns->last_read_trans) + ns->trans += ((long long) 4294967295U - ns->last_read_trans) + t; + else + ns->trans += (t - ns->last_read_trans); + + ns->last_read_trans = t; + + + /* calculate speeds */ + ns->recv_speed = (ns->recv - last_recv) / delta; + ns->trans_speed = (ns->trans - last_trans) / delta; + } + } + + freeifaddrs(ifap); } + int get_total_processes() { /* It's easier to use kvm here than sysctl */ |