[Lcdproc] Mac OS X / Darwin iface

Eric Pooch epooch@cox.net
Wed Nov 29 06:45:03 2006


--Apple-Mail-7--356150287
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	delsp=yes;
	format=flowed

This patch enables the lcdproc network interface (iface) stats screen  
for Mac OS X and Darwin


--Apple-Mail-7--356150287
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream;
	x-unix-mode=0644;
	name=machine_Darwin_iface.patch
Content-Disposition: attachment;
	filename=machine_Darwin_iface.patch

--- machine_Darwin.c.old	2006-11-27 23:49:23.000000000 -0800
+++ machine_Darwin.c	2006-11-27 23:49:33.000000000 -0800
@@ -47,7 +47,9 @@
 #include <sys/user.h>
 #include <kvm.h>
 #include <errno.h>
-
+#include <sys/socket.h>
+#include <net/if.h>
+#include <net/if_mib.h>
 #include <mach/mach.h>
 
 #include "main.h"
@@ -431,8 +433,60 @@
 /* Get network statistics */
 int machine_get_iface_stats (IfaceInfo *interface)
 {
-	/* Implementation missing */
-	return 0;
-}
+	static int      first_time = 1;	/* is it first time we call this function? */
+	int             rows;
+	int             name[6] = {CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_SYSTEM, IFMIB_IFCOUNT};
+	size_t          len;
+	struct ifmibdata ifmd; /* ifmibdata contains the network statistics */
+	
+	len = sizeof(rows);
+	/* get number of interfaces */
+	//if (sysctlbyname("net.link.generic.system.ifcount", &rows, &len, NULL, 0) == 0) {
+	if (sysctl(name, 5, &rows, &len, 0, 0) == 0) {
+		interface->status = down; /* set status down by default */
+		
+		name[3] = IFMIB_IFDATA;
+		name[4] = 0;
+		name[5] = IFDATA_GENERAL;
+		
+		len = sizeof(ifmd);
+		/* walk through all interfaces in the ifmib table from last to first */
+		for ( ; rows > 0; rows--) {
+			name[4] = rows; /* set the interface index */
+			/* retrive the ifmibdata for the current index */
+			if (sysctl(name, 6, &ifmd, &len, NULL, 0) == -1) {
+				perror("read sysctl");
+				break;
+			}
+			/* check if its interface name matches */
+			if (strcmp(ifmd.ifmd_name, interface->name) == 0) {
+				interface->last_online = time(NULL);	/* save actual time */
+				
+				if ((ifmd.ifmd_flags & IFF_UP) == IFF_UP)
+					interface->status = up;	/* is up */
+
+				interface->rc_byte = ifmd.ifmd_data.ifi_ibytes;
+				interface->tr_byte = ifmd.ifmd_data.ifi_obytes;
+				interface->rc_pkt = ifmd.ifmd_data.ifi_ipackets;
+				interface->tr_pkt = ifmd.ifmd_data.ifi_opackets;
+
+				if (first_time) {
+					interface->rc_byte_old = interface->rc_byte;
+					interface->tr_byte_old = interface->tr_byte;
+					interface->rc_pkt_old = interface->rc_pkt;
+					interface->tr_pkt_old = interface->tr_pkt;
+					first_time = 0;	/* now it isn't first time */
+				}
+				return 1;
+			}
+		}
+		/* if we are here there is no interface with the given name */
+		return 0;
+	} else {
+		perror("read sysctl IFMIB_IFCOUNT");
+		return 0;
+	}
+} /* get_iface_stats() */
+
 
 #endif /* __APPLE__ */

--Apple-Mail-7--356150287
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed


--Eric

--Apple-Mail-7--356150287--