perfstat_netinterface Interface

The perfstat_netinterface interface returns a set of structures of type perfstat_netinterface_t, which is defined in the libperfstat.h file.

Selected fields from the perfstat_netinterface_t structure include:
name Interface name (from ODM)
description Interface description (from ODM)
ipackets Total number of input packets received on this network interface
opackets Total number of output packets sent on this network interface
ierror Total number of input errors on this network interface
oerror Total number of output errors on this network interface
Several other network-interface related metrics (such as number of bytes sent and received, type, and bitrate) are also returned. For a complete list of other network-interfaced related metrics, see the perfstat_netinterface_t section in the libperfstat.h header file in Files Reference.
The following code shows an example of how perfstat_netinterface is used:
#include <stdio.h> 
#include <stdlib.h>
#include <libperfstat.h>
#include <net/if_types.h>

char *
decode(uchar type) {

    switch(type) {

    case IFT_LOOP:
        return("loopback");

    case IFT_ISO88025:
        return("token-ring");

    case IFT_ETHER:
        return("ethernet");
    }

    return("other");
}

int main(int argc, char* argv[]) {
   int i, ret, tot;
   perfstat_netinterface_t *statp;
   perfstat_id_t first;

   /* check how many perfstat_netinterface_t structures are available */
   tot = perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t), 0);

   /* check for error */
   if (tot < 0)
   {

   /* check for error */
   if (tot == 0)
   {
	printf("No network interfaces found\n");
	exit(-1);
   }   

	perror("perfstat_netinterface");
	exit(-1);
   }   

   /* allocate enough memory for all the structures */
   statp = calloc(tot, sizeof(perfstat_netinterface_t));
   
   /* set name to first interface */
   strcpy(first.name, FIRST_NETINTERFACE);
   
   /* ask to get all the structures available in one call */
   /* return code is number of structures returned */
   ret = perfstat_netinterface(&first, statp, sizeof(perfstat_netinterface_t), tot);

   /* check for error */
   if (ret <= 0)
   {

	perror("perfstat_netinterface");
	exit(-1);
   }   

   /* print statistics for each of the interfaces */
   for (i = 0; i < ret; i++) {
       printf("\nStatistics for interface : %s\n", statp[i].name);
       printf("------------------------\n");
       printf("type : %s\n", decode(statp[i].type));
       printf("\ninput statistics:\n");
       printf("number of packets : %llu\n", statp[i].ipackets);
       printf("number of errors  : %llu\n", statp[i].ierrors);
       printf("number of bytes   : %llu\n", statp[i].ibytes);
       printf("\noutput statistics:\n");
       printf("number of packets : %llu\n", statp[i].opackets);
       printf("number of bytes   : %llu\n", statp[i].obytes);
       printf("number of errors  : %llu\n", statp[i].oerrors);
       }
}
The preceding program produces the following output:
Statistics for interface : tr0
------------------------
type : token-ring

input statistics:
number of packets : 306352
number of errors  : 0
number of bytes   : 24831776

output statistics:
number of packets : 62669
number of bytes   : 11497679
number of errors  : 0

Statistics for interface : lo0
------------------------
type : loopback

input statistics:
number of packets : 336
number of errors  : 0
number of bytes   : 20912

output statistics:
number of packets : 336
number of bytes   : 20912
number of errors  : 0

The preceding program emulates diskadapterstat behavior and also shows how perfstat_netinterface is used.