#include #include #include #include #define DELTA(member) (net2[i].member - net1[i].member) #ifndef FIRST_NETINTERFACE #define FIRST_NETINTERFACE "" #endif 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 * net1; perfstat_netinterface_t * net2; int seconds = 1; int times = 1; char str[512]; char perfdata[1024]; char * if_name; double in_packets; double in_errors; double in_Kbytes; double out_packets; double out_errors; double out_Kbytes; /* check how many perfstat_netinterface_t structures are available */ tot = perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t), 0); /* allocate enough memory for all the structures */ net1 = malloc(sizeof(perfstat_netinterface_t) * tot); net2 = malloc(sizeof(perfstat_netinterface_t) * tot); /* ask to get all the structures available in one call */ /* return code is number of structures returned */ ret = perfstat_netinterface((perfstat_id_t *)FIRST_NETINTERFACE, net1, sizeof(perfstat_netinterface_t), tot); printf("Checked %d Interfaces\n",ret); strcpy(perfdata, " | "); for(int k = 0; k < times; k++) { sleep(seconds); ret = perfstat_netinterface((perfstat_id_t *)"", net2, sizeof(perfstat_netinterface_t), tot); if(ret <= 0) { printf("tot=%d ret=%d errno=%d\n",tot,ret,errno); perror("x"); exit(42); } /* print statistics for each of the interfaces */ if (ret > 0) { for (i = 0; i < ret; i++) { if_name = net2[i].name; in_packets = (double)DELTA(ipackets)/(double)seconds; in_errors = (double)DELTA(ierrors)/(double)seconds; in_Kbytes = (double)DELTA(ibytes)/1024.0/(double)seconds; out_packets = (double)DELTA(opackets)/(double)seconds; out_errors = (double)DELTA(oerrors)/(double)seconds; out_Kbytes = (double)DELTA(obytes)/1024.0/(double)seconds; sprintf(str, "%s_inKbytes=%.2fKB ", if_name, in_Kbytes); strcat(perfdata, str); sprintf(str, "%s_inPackets=%.2f ", if_name, in_packets); strcat(perfdata, str); sprintf(str, "%s_inErrors=%.2f ", if_name, in_errors); strcat(perfdata, str); sprintf(str, "%s_outKbytes=%.2fKB ", if_name, out_Kbytes); strcat(perfdata, str); sprintf(str, "%s_outPackets=%.2f ", if_name, out_packets); strcat(perfdata, str); sprintf(str, "%s_outErrors=%.2f ", if_name, out_errors); strcat(perfdata, str); /* printf("\nStatistics for interface : %s\n", net2[i].name); printf("------------------------\n"); printf("type : %s\n", decode(net2[i].type)); printf("\ninput statistics:\n"); printf("number of packets/sec : %.2f\n", (double)DELTA(ipackets)/(double)seconds); printf("number of errors/sec : %.2f\n", (double)DELTA(ierrors)/(double)seconds); printf("number of bytes/sec : %.2f\n", (double)DELTA(ibytes)/1024.0/(double)seconds); printf("\noutput statistics:\n"); printf("number of packets/sec : %.2f\n", (double)DELTA(opackets)/(double)seconds); printf("number of errors/sec : %.2f\n", (double)DELTA(oerrors)/(double)seconds); printf("number of bytes/sec : %.2f\n", (double)DELTA(obytes)/1024.0/(double)seconds); */ } } memcpy(net1, net2, sizeof(perfstat_netinterface_t) * tot ); } printf("%s", perfdata); printf("\n"); return 0; }