Linux 下监测网速的测试接口 --已经测试单位为byte=8bit

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <mntent.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <time.h>
#include<string.h>

 

typedef  enum
{
 Eth0 = 0,
 Eth1,
 Eth2,
 Eth3, 
}rate_flow;


#define Durning 10
int get_net_data(char *putout, char *data,int data_len ,char *type)
{
 if(putout==NULL || data==NULL ||type==NULL)
 {
  return -1;
 }
 char *tmp = NULL;
 int i;
 if(strstr(putout,type))
 { 
  tmp = strstr(putout,type) ;
  tmp += strlen(type);
  //printf("tmp=%s  ,type=%s strlen(type) =%d \n",tmp,type,strlen(type));
  memset(data,0,sizeof(data));
  for(i =0;i< data_len;i++)
  {
   if(*tmp >='0' && *tmp <='9')
   {
    *data++= *tmp++;
    //tmp++;
   }
   else
   {
    break;
   }
  }
 }
 
 if(i == data_len)
 {
  return -1;
 }

 return 0;
 
}
int net_rate_flow_display(rate_flow card)
{
 char putout[2048] = {}; 
 char cmd[96] = {};
 char RxData[32];
 char TxData[32];
 char RxData1[32];
 char TxData1[32];
 char DataRecv[32];
 char DataSend[32];
 int i;
 char *tmp = NULL;
 FILE* fp;
 switch(card)
 {
  case Eth0:
   printf("eth0 ::  \n");
   snprintf(cmd,sizeof(cmd), "ifconfig eth0 | grep \"RX bytes:\"");
   break;
  case Eth1:
   printf("eth1 ::   \n");
   snprintf(cmd,sizeof(cmd), "ifconfig eth1 | grep \"RX bytes:\"");
   break;
  case Eth2:
   printf("eth2 ::   \n");
   snprintf(cmd,sizeof(cmd), "ifconfig eth2 | grep \"RX bytes:\"");
   break;
  case Eth3:
   printf("eth3 ::   \n");
   snprintf(cmd,sizeof(cmd), "ifconfig eth3 | grep \"RX bytes:\"");
   break;
  default :
   fprintf(stderr,"card in put error !\n");
   return -1;
 }
 
 fp = popen(cmd, "r");
 
 if(fgets(putout, sizeof(putout), fp) == NULL)
 {
  printf("fgets == NULL  !\n");
  pclose(fp);
  return -1;
 }
 
 //printf("fisrt   %s\n",putout);
// fisrt get Rxdata  & get Txdata
 if(get_net_data(putout, RxData,sizeof(RxData) ,"RX bytes:") == -1
  || get_net_data(putout, TxData,sizeof(RxData) ,"TX bytes:") == -1)
 {
  return -1;
 }
  
 printf("plz wait %d seconds \n",Durning);
 //  wait ,util get the net flow
 sleep(Durning);

 
// then get Rxdata1  & get Txdata1
 memset(putout,0,sizeof(putout));
 fp = popen(cmd, "r");  
 if(fgets(putout, sizeof(putout), fp) == NULL)
 {
  printf("fgets == NULL  !\n");
  pclose(fp);
  return -1;
 } 
 //printf("second   %s\n",putout);
 if(get_net_data(putout, RxData1,sizeof(RxData) ,"RX bytes:") == -1
  || get_net_data(putout, TxData1,sizeof(RxData) ,"TX bytes:") == -1)
 {
  return -1;
 }

 
 printf("Net flow:  (B=8b,B-byte,b-bit)\n");
 long long unit1 = atoll(RxData1);
 long long unit = atoll(RxData);
 long long recvdata_1s = (unit1 - unit)/Durning;
 
 //printf(" RxData1 = %s unit1 == %lld recvdata_1s  = %lld  \n" ,RxData1,unit1,recvdata_1s);
 float Data;
 if(recvdata_1s < 1)
 {
  printf("no data recv  in !!!! \n");
 }
 else
 {
  if(recvdata_1s /(1024*1024))
  {
   Data= ((float)recvdata_1s) / (1024*1024);   
   printf("RX %lldbyte/s(%.2fMB/s)      ",recvdata_1s,Data);
  }
  else if(recvdata_1s /1024)
  {
   Data= ((float)recvdata_1s) /1024;   
   printf("RX %lldbyte/s(%.2fKB/s)       ",recvdata_1s,Data);
  }
  else
  {   
   printf("RX %lldbyte/s                             ",recvdata_1s);
  }
 }

 
 
 unit1 = atoll(TxData1);
 unit = atoll(TxData);
 recvdata_1s = (unit1 - unit)/Durning;
 
 if(recvdata_1s < 1)
 {
  printf("no data send  out !!!! \n");
 }
 else
 {
  if(recvdata_1s /(1024*1024))
  {
   Data= ((float)recvdata_1s) / (1024*1024);   
   printf("TX %lldbyte/s(%.2fMB/s)    \n",recvdata_1s,Data);
  }
  else if(recvdata_1s /1024)
  {
   Data= ((float)recvdata_1s) /1024;   
   printf("TX %lldbyte/s(%.2fKB/s)    \n",recvdata_1s,Data);
  }
  else
  {   
   printf("TX %lldbyte/s       \n",recvdata_1s);
  }
 }
 printf("-----------------------------------------------------------------\n\n");  
}
int main(int argc ,char *argv[])
{
 
 if(argc <=1)
 {
  fprintf(stderr,"argc <= 1! \n");
  printf("paramers like : ./netflow eth0  \n");
  printf("paramers like : ./netflow eth1  \n");
  printf("paramers like : ./netflow eth2  \n");
  printf("paramers like : ./netflow eth3  \n");
  return -1;
 }
 char *param1 = argv[1];
 rate_flow network_card = Eth0;
 if(!param1)
 {
  fprintf(stderr,"the paramer1 is NULL!\n"); 
  return -1;
 }

 if(!strcmp(param1,"eth0"))
 {
  network_card = Eth0;
 }
 else if(!strcmp(param1,"eth1"))
 {
  network_card = Eth1;
 }
 else if(!strcmp(param1,"eth2"))
 {
  network_card = Eth2;
 }
 else if(!strcmp(param1,"eth3"))
 {
  network_card = Eth3;
 }
 else
 {
  fprintf(stderr,"the paramer1 is error!\n"); 
  return -1;
 }
 
 net_rate_flow_display(network_card);
  

 return 0;

}

 

思路为:使用ifconfig 命令获取网卡收到数据,每隔10s钟进行统计;另外也可以通过/proc/net/dev 中查看对应网卡的数据,然后进行统计计算 道理一样。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值