linux c語言獲取CPU,內存,網速,磁盤使用,IO

#include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #define MAXBUFSIZE 1024
    #define WAIT_SECOND 3   //暫停時間,單位爲“秒”
    typedef  struct occupy
    {
        char name[20];
        unsigned int user;
        unsigned int nice;
        unsigned int system;
        unsigned int idle;
    } CPU_OCCUPY ;

    typedef struct PACKED
    {
        char name[20];
        long total;
        char name2[20];
        long free;
    }MEM_OCCUPY;

    float g_cpu_used;
    int cpu_num;                //定義一個全局的int類型cup_num
    void cal_occupy(CPU_OCCUPY *, CPU_OCCUPY *);
    void get_occupy(CPU_OCCUPY *);
    void get_mem_occupy(MEM_OCCUPY *) ;
    float get_io_occupy();
    void get_disk_occupy(char ** reused);
    void getCurrentDownloadRates(long int * save_rate);

    int main(){
      CPU_OCCUPY ocpu,ncpu;
      MEM_OCCUPY mem;

      //獲取cpu核數
      cpu_num = sysconf(_SC_NPROCESSORS_ONLN);
      printf("cpu mum:%d\n",cpu_num);

      //獲取cpu使用率
      get_occupy(&ocpu);
      sleep(1);
      get_occupy(&ncpu);
      cal_occupy(&ocpu, &ncpu);
      printf("cpu used:%4.2f \n", g_cpu_used);

      //獲取內存使用率
      get_mem_occupy(&mem);

      double using = ((double)(mem.total - mem.free)/mem.total)*100;
      printf("mem used:%4.2f\n",using);
      //獲取io使用率
      printf("io used:%4.2f\n",get_io_occupy());

      //獲取當前磁盤的使用率
      char t[20]="";
      char *used = t;
       get_disk_occupy(&used);

      //char used[20]=" " ;
      //get_disk_occupy((char **)&used);
      printf("disk used:%s\n",used);

      //網絡延遲
        long int start_download_rates;  //保存開始時的流量計數
        long int end_download_rates;    //保存結果時的流量計數
        getCurrentDownloadRates(&start_download_rates);//獲取當前流量,並保存在start_download_rates裏
        sleep(WAIT_SECOND); //休眠多少秒,這個值根據宏定義中的WAIT_SECOND的值來確定
        getCurrentDownloadRates(&end_download_rates);//獲取當前流量,並保存在end_download_rates裏
        printf("download is : %4.2f byte/s \n",( (float)(end_download_rates-start_download_rates))/WAIT_SECOND );
}
    void  cal_occupy (CPU_OCCUPY *o, CPU_OCCUPY *n){
        double od, nd;
        double id, sd;
        double scale;
        od = (double) (o->user + o->nice + o->system +o->idle);//第一次(用戶+優先級+系統+空閒)的時間再賦給od
        nd = (double) (n->user + n->nice + n->system +n->idle);//第二次(用戶+優先級+系統+空閒)的時間再賦給od
        scale = 100.0 / (float)(nd-od);       //100除強制轉換(nd-od)之差爲float類型再賦給scale這個變量
        id = (double) (n->user - o->user);    //用戶第一次和第二次的時間之差再賦給id
        sd = (double) (n->system - o->system);//系統第一次和第二次的時間之差再賦給sd
        g_cpu_used = ((sd+id)*100.0)/(nd-od); //((用戶+系統)乖100)除(第一次和第二次的時間差)再賦給g_cpu_used
    }
    void  get_occupy (CPU_OCCUPY *o) {
        FILE *fd;
        int n;
        char buff[MAXBUFSIZE];
        fd = fopen ("/proc/stat", "r"); //這裏只讀取stat文件的第一行及cpu總信息,如需獲取每核cpu的使用情況,請分析stat文件的接下來幾行。
        fgets (buff, sizeof(buff), fd);
        sscanf (buff, "%s %u %u %u %u", o->name, &o->user, &o->nice,&o->system, &o->idle);
       fclose(fd);
    }
    void get_mem_occupy(MEM_OCCUPY * mem){
        FILE * fd;
        char buff[MAXBUFSIZE];
        fd = fopen("/proc/meminfo","r");
        fgets (buff, sizeof(buff), fd);
        sscanf (buff, "%s %ld", mem->name,&mem->total);
        fgets (buff, sizeof(buff), fd);
        sscanf (buff, "%s %ld", mem->name2,&mem->free);
        }
    float get_io_occupy(){
            char cmd[] ="iostat -d -x";
            char buffer[MAXBUFSIZE];
            char a[20];
            float arr[20];
            FILE* pipe = popen(cmd, "r");
            if (!pipe)  return -1;
            fgets(buffer, sizeof(buffer), pipe);
            fgets(buffer, sizeof(buffer), pipe);
            fgets(buffer, sizeof(buffer), pipe);
            fgets(buffer, sizeof(buffer), pipe);
            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]);
            //printf("%f\n",arr[12]);
            return arr[12];
            pclose(pipe);
        }
    void get_disk_occupy(char ** reused){
        char currentDirectoryPath[ MAXBUFSIZE ];
        getcwd(currentDirectoryPath, MAXBUFSIZE);
        //printf("當前目錄:%s\n",currentDirectoryPath);
        char cmd[50]="df ";
        strcat(cmd,currentDirectoryPath);
        //printf("%s\n",cmd);

        char buffer[MAXBUFSIZE];
        FILE* pipe = popen(cmd, "r");
        char fileSys[20];
        char blocks[20];
        char used[20];
        char free[20];
        char percent[10];
        char moment[20];

        if (!pipe)  return ;
        if(fgets(buffer, sizeof(buffer), pipe)!=NULL){
            sscanf(buffer,"%s %s %s %s %s %s",fileSys,blocks,used,free,percent,moment);
        }
        if(fgets(buffer, sizeof(buffer), pipe)!=NULL){
            sscanf(buffer,"%s %s %s %s %s %s",fileSys,blocks,used,free,percent,moment);
        }
        //printf("desk used:%s\n",percent);
        strcpy(*reused,percent);
        return ;
    }

void getCurrentDownloadRates(long int * save_rate)
{
    char intface[] = "eth0:";  //這是網絡接口名,根據主機配置
    //char intface[] = "wlan0:";
    FILE * net_dev_file;
    char buffer[1024];
    size_t bytes_read;
    char * match;
    if ( (net_dev_file=fopen("/proc/net/dev", "r")) == NULL )
    {
        printf("open file /proc/net/dev/ error!\n");
        exit(EXIT_FAILURE);
    }

  int i = 0;
    while(i++<20){
        if(fgets(buffer, sizeof(buffer), net_dev_file)!=NULL){
            if(strstr(buffer,intface)!=NULL){
                //printf("%d   %s\n",i,buffer);
                sscanf(buffer,"%s %ld",buffer,save_rate);
                break;
            }
        }
        }
        if(i==20) *save_rate=0.01;
        fclose(net_dev_file); //關閉文件
    return ;
}

轉自:https://www.cnblogs.com/ip99/p/12127031.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章