C++ 獲取Linux 伺服器CPU佔用率+記憶體空閒率(親測絕對可以執行)

墨尔基阿德斯發表於2024-08-19

轉自:C++ 獲取Linux 伺服器CPU佔用率+記憶體空閒率(親測絕對可以執行) - 遠征i - 部落格園 (cnblogs.com)

程式碼來自網路,部分修改,親測絕對可用

C++:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <iostream>   
#include <unistd.h>

using namespace std;  

typedef struct MEMPACKED         //定義一個mem occupy的結構體  
{  
    char name1[20];      //定義一個char型別的陣列名name有20個元素  
    unsigned long MemTotal;  
    char name2[20];  
    unsigned long MemFree;  
    char name3[20];  
    unsigned long Buffers;  
    char name4[20];  
    unsigned long Cached;  
    char name5[20];  
    unsigned long SwapCached;  
}MEM_OCCUPY;  
  
//proc/stat檔案結構  
//cpu  633666 46912 249878 176813696 782884 2859 19625 0  
//cpu0 633666 46912 249878 176813696 782884 2859 19625 0  
//intr 5812844  
//ctxt 265816063  
//btime 1455203832  
//processes 596625  
//procs_running 1  
//procs_blocked 0  
  
typedef struct CPUPACKED         //定義一個cpu occupy的結構體  
{  
    char name[20];      //定義一個char型別的陣列名name有20個元素  
    unsigned int user; //定義一個無符號的int型別的user  
    unsigned int nice; //定義一個無符號的int型別的nice  
    unsigned int system;//定義一個無符號的int型別的system  
    unsigned int idle; //定義一個無符號的int型別的idle  
    unsigned int lowait;  
    unsigned int irq;  
    unsigned int softirq;  
}CPU_OCCUPY;  
  
  
void get_memoccupy(MEM_OCCUPY *mem) //對無型別get函式含有一個形參結構體類弄的指標O  
{  
    FILE *fd;  
    char buff[256];  
    MEM_OCCUPY *m;  
    m = mem;  
      
    fd = fopen("/proc/meminfo", "r");  
    //MemTotal: 515164 kB  
    //MemFree: 7348 kB  
    //Buffers: 7892 kB  
    //Cached: 241852  kB  
    //SwapCached: 0 kB  
    //從fd檔案中讀取長度為buff的字串再存到起始地址為buff這個空間裡   
    fgets(buff, sizeof(buff), fd);  
    sscanf(buff, "%s %lu ", m->name1, &m->MemTotal);  
    fgets(buff, sizeof(buff), fd);  
    sscanf(buff, "%s %lu ", m->name2, &m->MemFree);  
    fgets(buff, sizeof(buff), fd);  
    sscanf(buff, "%s %lu ", m->name3, &m->Buffers);  
    fgets(buff, sizeof(buff), fd);  
    sscanf(buff, "%s %lu ", m->name4, &m->Cached);  
    fgets(buff, sizeof(buff), fd);   
    sscanf(buff, "%s %lu", m->name5, &m->SwapCached);  
      
    fclose(fd);     //關閉檔案fd  
}  
  
  
int get_cpuoccupy(CPU_OCCUPY *cpust) //對無型別get函式含有一個形參結構體類弄的指標O  
{  
    FILE *fd;  
    char buff[256];  
    CPU_OCCUPY *cpu_occupy;  
    cpu_occupy = cpust;  
      
    fd = fopen("/proc/stat", "r");  
    fgets(buff, sizeof(buff), fd);  
      
    sscanf(buff, "%s %u %u %u %u %u %u %u", cpu_occupy->name, &cpu_occupy->user, &cpu_occupy->nice, &cpu_occupy->system, &cpu_occupy->idle, &cpu_occupy->lowait, &cpu_occupy->irq, &cpu_occupy->softirq);  
      
      
    fclose(fd);  
      
    return 0;  
}  
  
  
void cal_cpuoccupy(CPU_OCCUPY *o, CPU_OCCUPY *n)  
{  
    unsigned long od, nd;  
    double cpu_use = 0;  
      
    od = (unsigned long)(o->user + o->nice + o->system + o->idle + o->lowait + o->irq + o->softirq);//第一次(使用者+優先順序+系統+空閒)的時間再賦給od  
    nd = (unsigned long)(n->user + n->nice + n->system + n->idle + n->lowait + n->irq + n->softirq);//第二次(使用者+優先順序+系統+空閒)的時間再賦給od  
    double sum = nd - od;  
    double idle = n->idle - o->idle;  
    cpu_use = idle / sum;  
    idle = n->user + n->system + n->nice - o->user - o->system - o->nice;  
    cpu_use = idle / sum;  
    printf("%.3f\n",cpu_use);  
}  
  
int main( int argc, char **argv )  
{  
    MEM_OCCUPY mem_stat;  
    CPU_OCCUPY cpu_stat1;  
    CPU_OCCUPY cpu_stat2;

    //獲取記憶體
    //(MemTotal - MemFree)/ MemTotal
    get_memoccupy((MEM_OCCUPY *)&mem_stat);  
    //printf(" [MemTotal] = %lu \n [MemFree] = %lu \n [Buffers] = %lu \n [Cached] = %lu \n [SwapCached] = %lu \n", mem_stat.MemTotal, mem_stat.MemFree, mem_stat.Buffers, mem_stat.Cached, mem_stat.SwapCached);  
    printf("%.3f\n", mem_stat.MemFree * 1.0 / ( mem_stat.MemTotal * 1.0  ) );
    //第一次獲取cpu使用情況  
    get_cpuoccupy((CPU_OCCUPY *)&cpu_stat1);  
    
    usleep(100000);
    
    //第二次獲取cpu使用情況  
    get_cpuoccupy((CPU_OCCUPY *)&cpu_stat2);  
    //計算cpu使用率  
    cal_cpuoccupy((CPU_OCCUPY *)&cpu_stat1, (CPU_OCCUPY *)&cpu_stat2);  
    return 0;
}

編譯

g++ get.cpp -o get

執行

[root@xxxxxxx ]# ./get

0.117
1.000
[root@xxxxxxx ]# ./get
0.119
0.000

上方的數值代表 記憶體空閒 / 記憶體總數

下方的數值代表 CPU佔用率

詳細解釋:

https://blog.csdn.net/nineday/article/details/1928847

計劃搭建一個簡易版的分散式叢集...

一臺主伺服器,一臺高配的資料庫伺服器,n 臺計算伺服器
(1)在每臺計算伺服器中,寫一個定時任務,每隔 1 s 觸發一個PHP程式,該PHP將觸發上方的CPP程式,獲得伺服器狀態,更新DATABASE中的伺服器狀態表
(2)主伺服器將獲得客戶端的請求,插入DATABASE中的訊息佇列表
(3)主伺服器中將寫一個servlet 定時函式,每隔 0.5 s 輪詢訊息佇列表,一旦發現有新的訊息請求集合或個體,則將該訊息集合轉換成所封裝物件的佇列
(4)從前往後掃描佇列,每次選定 DATABASE中空閒資源最大的 一臺計算伺服器,傳送一個包含JSON格式物件的HTTP請求
(5)計算伺服器接收到HTTP請求後,將展開計算,計算完成後,更新DATABASE中的資料欄位
(6)客戶端AJAX 0.5s 輪詢DATABASE,直到完成計算伺服器完成計算

【上方叢集中,採用 jsp + servlet 實現 java 後臺。。。。。。得抓緊學習spring大家族啊,被技術限制住了......雖然servlet本身是單例項多執行緒的

主伺服器 servlet多執行緒 --- 任務佇列 --- 分散式平行計算 --- 計算伺服器 servlet多執行緒

不知道併發咋樣。。。第一次碰分散式和併發這塊...

如果有大佬可以指點下,會非常感謝的!!!!!!

相關文章