linux c语言获取CPU,内存,网速,磁盘使用,IO

  1 #include <stdio.h>  
  2     #include <stdlib.h>  
  3     #include <unistd.h>
  4     #include <string.h>   
  5     #define MAXBUFSIZE 1024
  6     #define WAIT_SECOND 3   //暂停时间,单位为“秒”  
  7     typedef  struct occupy        
  8     {  
  9         char name[20];      
 10         unsigned int user;  
 11         unsigned int nice;  
 12         unsigned int system;
 13         unsigned int idle;  
 14     } CPU_OCCUPY ;  
 15     
 16     typedef struct PACKED         
 17     {
 18         char name[20]; 
 19         long total; 
 20         char name2[20];
 21         long free;            
 22     }MEM_OCCUPY;
 23 
 24     float g_cpu_used;           
 25     int cpu_num;                //定义一个全局的int类型cup_num  
 26     void cal_occupy(CPU_OCCUPY *, CPU_OCCUPY *); 
 27     void get_occupy(CPU_OCCUPY *); 
 28     void get_mem_occupy(MEM_OCCUPY *) ;
 29     float get_io_occupy();
 30     void get_disk_occupy(char ** reused);
 31     void getCurrentDownloadRates(long int * save_rate);
 32 
 33     int main(){  
 34       CPU_OCCUPY ocpu,ncpu;
 35       MEM_OCCUPY mem;
 36 
 37       //获取cpu核数
 38       cpu_num = sysconf(_SC_NPROCESSORS_ONLN); 
 39       printf("cpu mum:%d\n",cpu_num);
 40       
 41       //获取cpu使用率
 42       get_occupy(&ocpu);                     
 43       sleep(1);                                  
 44       get_occupy(&ncpu);                      
 45       cal_occupy(&ocpu, &ncpu); 
 46       printf("cpu used:%4.2f \n", g_cpu_used);  
 47       
 48       //获取内存使用率
 49       get_mem_occupy(&mem);
 50 
 51       double using = ((double)(mem.total - mem.free)/mem.total)*100;
 52       printf("mem used:%4.2f\n",using);
 53       //获取io使用率
 54       printf("io used:%4.2f\n",get_io_occupy());
 55       
 56       //获取当前磁盘的使用率
 57       char t[20]="";
 58       char *used = t;
 59        get_disk_occupy(&used);
 60        
 61       //char used[20]=" " ;
 62       //get_disk_occupy((char **)&used);
 63       printf("disk used:%s\n",used);
 64       
 65       //网络延迟
 66         long int start_download_rates;  //保存开始时的流量计数  
 67         long int end_download_rates;    //保存结果时的流量计数  
 68         getCurrentDownloadRates(&start_download_rates);//获取当前流量,并保存在start_download_rates里  
 69         sleep(WAIT_SECOND); //休眠多少秒,这个值根据宏定义中的WAIT_SECOND的值来确定  
 70         getCurrentDownloadRates(&end_download_rates);//获取当前流量,并保存在end_download_rates里  
 71         printf("download is : %4.2f byte/s \n",( (float)(end_download_rates-start_download_rates))/WAIT_SECOND );
 72 }
 73     void  cal_occupy (CPU_OCCUPY *o, CPU_OCCUPY *n){  
 74         double od, nd;  
 75         double id, sd;  
 76         double scale;    
 77         od = (double) (o->user + o->nice + o->system +o->idle);//第一次(用户+优先级+系统+空闲)的时间再赋给od  
 78         nd = (double) (n->user + n->nice + n->system +n->idle);//第二次(用户+优先级+系统+空闲)的时间再赋给od  
 79         scale = 100.0 / (float)(nd-od);       //100除强制转换(nd-od)之差为float类型再赋给scale这个变量  
 80         id = (double) (n->user - o->user);    //用户第一次和第二次的时间之差再赋给id  
 81         sd = (double) (n->system - o->system);//系统第一次和第二次的时间之差再赋给sd  
 82         g_cpu_used = ((sd+id)*100.0)/(nd-od); //((用户+系统)乖100)除(第一次和第二次的时间差)再赋给g_cpu_used  
 83     }  
 84     void  get_occupy (CPU_OCCUPY *o) {  
 85         FILE *fd;         
 86         int n;            
 87         char buff[MAXBUFSIZE];                                                                                               
 88         fd = fopen ("/proc/stat", "r"); //这里只读取stat文件的第一行及cpu总信息,如需获取每核cpu的使用情况,请分析stat文件的接下来几行。
 89         fgets (buff, sizeof(buff), fd); 
 90         sscanf (buff, "%s %u %u %u %u", o->name, &o->user, &o->nice,&o->system, &o->idle);  
 91        fclose(fd);     
 92     }  
 93     void get_mem_occupy(MEM_OCCUPY * mem){
 94         FILE * fd;
 95         char buff[MAXBUFSIZE];
 96         fd = fopen("/proc/meminfo","r");
 97         fgets (buff, sizeof(buff), fd); 
 98         sscanf (buff, "%s %ld", mem->name,&mem->total);  
 99         fgets (buff, sizeof(buff), fd); 
100         sscanf (buff, "%s %ld", mem->name2,&mem->free); 
101         }
102     float get_io_occupy(){
103             char cmd[] ="iostat -d -x";
104             char buffer[MAXBUFSIZE];  
105             char a[20];   
106             float arr[20];
107             FILE* pipe = popen(cmd, "r");    
108             if (!pipe)  return -1;    
109             fgets(buffer, sizeof(buffer), pipe);
110             fgets(buffer, sizeof(buffer), pipe);
111             fgets(buffer, sizeof(buffer), pipe);
112             fgets(buffer, sizeof(buffer), pipe);
113             sscanf(buffer,"%s %f %f %f %f %f %f %f %f %f %f %f %f %f ",a,&arr[0],&arr[1],&arr[2],&arr[3],&arr[4],&arr[5],&arr[6],&arr[7],&arr[8],&arr[9],&arr[10],&arr[11],&arr[12]);
114             //printf("%f\n",arr[12]);
115             return arr[12];
116             pclose(pipe);  
117         }
118     void get_disk_occupy(char ** reused){
119         char currentDirectoryPath[ MAXBUFSIZE ];
120         getcwd(currentDirectoryPath, MAXBUFSIZE);
121         //printf("当前目录:%s\n",currentDirectoryPath);
122         char cmd[50]="df ";
123         strcat(cmd,currentDirectoryPath);
124         //printf("%s\n",cmd);
125         
126         char buffer[MAXBUFSIZE];
127         FILE* pipe = popen(cmd, "r");    
128         char fileSys[20];
129         char blocks[20];
130         char used[20];
131         char free[20];
132         char percent[10];
133         char moment[20];
134         
135         if (!pipe)  return ;  
136         if(fgets(buffer, sizeof(buffer), pipe)!=NULL){
137             sscanf(buffer,"%s %s %s %s %s %s",fileSys,blocks,used,free,percent,moment);
138         }
139         if(fgets(buffer, sizeof(buffer), pipe)!=NULL){
140             sscanf(buffer,"%s %s %s %s %s %s",fileSys,blocks,used,free,percent,moment);
141         }
142         //printf("desk used:%s\n",percent);
143         strcpy(*reused,percent);
144         return ;
145     }
146     
147 void getCurrentDownloadRates(long int * save_rate)  
148 {  
149     char intface[] = "eth0:";  //这是网络接口名,根据主机配置
150     //char intface[] = "wlan0:";
151     FILE * net_dev_file;   
152     char buffer[1024]; 
153     size_t bytes_read; 
154     char * match;  
155     if ( (net_dev_file=fopen("/proc/net/dev", "r")) == NULL )
156     {  
157         printf("open file /proc/net/dev/ error!\n");  
158         exit(EXIT_FAILURE);  
159     }  
160 
161   int i = 0;
162     while(i++<20){
163         if(fgets(buffer, sizeof(buffer), net_dev_file)!=NULL){
164             if(strstr(buffer,intface)!=NULL){
165                 //printf("%d   %s\n",i,buffer);
166                 sscanf(buffer,"%s %ld",buffer,save_rate);
167                 break;
168             }
169         }
170         }
171         if(i==20) *save_rate=0.01;
172         fclose(net_dev_file); //关闭文件  
173     return ;  
174 }

打印结果

cpu mum:4
cpu used:1.25 
mem used:70.30
io used:0.10
disk used:4%
download is : 4963.33 byte/s 

猜你喜欢

转载自www.cnblogs.com/ip99/p/12127031.html