[Lcdproc] NIC statistics ported to NetBSD

Markus Dolze bsdfan@nurfuerspam.de
Tue May 9 22:26:02 2006


This is a multi-part message in MIME format.
--------------040509040309050806010008
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

I ported machine_get_iface_stats() to NetBSD. It should work on any 
version, but only version 3 is tested. The patch also removes some 
trailing whitespaces, although this was not on purpose.

It should also work on OpenBSD (i want to try that) and FreeBSD 
(although I already used the sysctl interface there).

Regards
Markus

--------------040509040309050806010008
Content-Type: text/plain;
 name="patch-client_lcdproc_machine_NetBSD.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="patch-client_lcdproc_machine_NetBSD.c"

--- clients/lcdproc/machine_NetBSD.c.orig	Sat Feb 18 19:48:02 2006
+++ clients/lcdproc/machine_NetBSD.c	Wed May 10 00:19:21 2006
@@ -15,7 +15,7 @@
  * 3. Neither the name of the author nor the names of its contributors
  *    may be used to endorse or promote products derived from this
  *    software without specific prior written permission.
- * 
+ *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
@@ -52,6 +52,9 @@
 #include <uvm/uvm_extern.h>
 #include <machine/apmvar.h>
 #include <errno.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <ifaddrs.h>
 
 #if (__NetBSD_Version__ >= 300000000)
 #include <sys/statvfs.h>
@@ -92,7 +95,7 @@
 {
 	int apmd;
 	struct apm_power_info apmi;
-   
+
 	if ((apmd = open("/dev/apm", O_RDONLY)) == -1)
 	{
 		*acstat   = LCDP_AC_ON;
@@ -100,14 +103,14 @@
 		*percent  = 100;
 		return(TRUE);
 	}
-   
+
 	memset(&apmi, 0, sizeof(apmi));
 	if (ioctl(apmd, APM_IOC_GETPOWER, &apmi) == -1)
-	{   
+	{
 		perror("APM_IOC_GETPOWER failed in get_batt_stat()");
 		return(FALSE);
 	}
-    
+
 	switch(apmi.ac_state)
 	{
 		case APM_AC_OFF:
@@ -137,15 +140,15 @@
 #if (__NetBSD_Version__ >= 300000000)
 	struct statvfs *mntbuf;
 	struct statvfs *pp;
-#else	
+#else
 	struct statfs *mntbuf;
 	struct statfs *pp;
 #endif
-	int statcnt, fscnt, i; 
+	int statcnt, fscnt, i;
 
 	fscnt = getmntinfo(&mntbuf, MNT_WAIT);
-	if (fscnt == 0) 
-	{ 
+	if (fscnt == 0)
+	{
 		perror("getmntinfo");
 		return(FALSE);
 	}
@@ -177,7 +180,7 @@
 			statcnt++;
 		}
 	}
-	
+
 	*cnt = statcnt;
 	return(TRUE);
 }
@@ -250,7 +253,7 @@
 		perror("sysctl vm.uvmexp2 failed");
 		return(FALSE);
 	}
-    
+
 	/* memory */
 	result[0].total		= pagetok(uvmexp.npages);
 	result[0].free		= pagetok(uvmexp.free);
@@ -367,7 +370,53 @@
 	else
 		*idle = 100.*curr_load.idle/curr_load.total;
 
-	return(TRUE); 
+	return(TRUE);
+}
+
+/* Get network statistics */
+int machine_get_iface_stats (IfaceInfo *interface)
+{
+	static int     first_time = 1;	/* is it first time we call this function? */
+	struct ifaddrs *ifa, *ifa_ptr;
+	struct if_data *ifd;
+
+	/* get first interface */
+	if (getifaddrs(&ifa) == -1)
+		perror("getifaddr failed");
+
+	/* loop through all interfaces */
+	for (ifa_ptr = ifa; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next) {
+		interface->status = down; /* set status down by default */
+
+		/* check if we got the right interface and if it is Link type */
+		if ((strcmp(ifa_ptr->ifa_name, interface->name) == 0) &&
+			(ifa_ptr->ifa_addr->sa_family == AF_LINK)) {
+
+			ifd = (struct if_data *)ifa_ptr->ifa_data;
+			interface->last_online = time(NULL);	/* save actual time */
+
+			if ((ifa_ptr->ifa_flags & IFF_UP) == IFF_UP)
+				interface->status = up;	/* is up */
+
+			interface->rc_byte = ifd->ifi_ibytes;
+			interface->tr_byte = ifd->ifi_obytes;
+			interface->rc_pkt = ifd->ifi_ipackets;
+			interface->tr_pkt = ifd->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;
+		}
+	}
+	freeifaddrs(ifa);
+
+	/* if we are here there is no interface with the given name */
+	return 0;
 }
 
 #endif /* __NetBSD__ */

--------------040509040309050806010008--