#include <iostream> #include <unistd.h> #include <stdlib.h> #include <sys/socket.h> #include <arpa/inet.h> #include <string.h> #include <errno.h> #define SERV_PORT 8888 #define SERV_IP "0.0.0.0"
using namespace std;
float g_cpu_used;
typedef struct { char name[20]; unsigned int user; unsigned int nice; unsigned int system; unsigned int idle; } CPU_OCCUPY;
typedef struct { char Used[32]; char Avg1[32]; char Avg5[32]; char Avg15[32]; } cpu_info;
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); nd = (double)(n->user + n->nice + n->system + n->idle); scale = 100.0 / (float)(nd - od); id = (double)(n->user - o->user); sd = (double)(n->system - o->system); g_cpu_used = ((sd + id)*100.0) / (nd - od); }
void get_occupy(CPU_OCCUPY *o) { FILE *fd; int n; char buff[1024] = {0}; fd = fopen("/proc/stat", "r"); fgets(buff, sizeof(buff), fd); sscanf(buff, "%s %u %u %u %u", o->name, &o->user, &o->nice, &o->system, &o->idle); fclose(fd); }
char * GetCpuInfo() { cpu_info ptr; CPU_OCCUPY ocpu, ncpu; char buffer[1024] = {0}; char ref_buffer[4096] = {0};
get_occupy(&ocpu); sleep(1); get_occupy(&ncpu); cal_occupy(&ocpu, &ncpu); sprintf(ptr.Used,"%.2f",g_cpu_used); FILE *pipe = popen("cat /proc/loadavg","r"); fgets(buffer,sizeof(buffer),pipe); sscanf(buffer,"%s %s %s", ptr.Avg1, ptr.Avg5, ptr.Avg15);
sprintf(ref_buffer, "{ 'platform': 'Centos', 'Used': %s, 'Avg1': %s, 'Avg5': %s, 'Avg15': %s }",ptr.Used, ptr.Avg1, ptr.Avg5, ptr.Avg15); pclose(pipe); return ref_buffer; }
typedef struct { char Total[32]; char Free[32]; char Available[32]; }mem_info;
char * GetMemoryInfo() { mem_info ptr; char buffer[1024] = {0}; char ref_buffer[4096] = {0}; FILE *pipe = popen("cat /proc/meminfo","r"); for(int x=0; x<3; x++) { if(x == 0) { fgets(buffer,sizeof(buffer),pipe); sscanf(buffer,"%s %s", ptr.Total, ptr.Total); } else if(x == 1) { fgets(buffer,sizeof(buffer),pipe); sscanf(buffer,"%s %s", ptr.Free, ptr.Free); } else if(x == 2) { fgets(buffer,sizeof(buffer),pipe); sscanf(buffer,"%s %s", ptr.Available, ptr.Available); } } sprintf(ref_buffer,"{ 'platform': 'Centos' , 'Total': %s , 'Free': %s , 'Available': %s }",ptr.Total , ptr.Free, ptr.Available); pclose(pipe); return ref_buffer; }
typedef struct { char none[32]; char mount[128]; char used[32]; }disk_info;
char * GetDiskInfo() { disk_info ptr; char buffer[1024] = {0}; char ref_buffer[4096]={0}; FILE *pipe = popen("df","r"); int x=0 , y=0; strcat(ref_buffer,"{ 'platform': 'Centos', 'value': [ "); for(; fgets(buffer,sizeof(buffer),pipe)!=NULL; y++) { char tulp[1024] = {0}; if(y<1) { continue; } sscanf(buffer,"%s %s %s %s %s %s",ptr.none, ptr.none, ptr.none, ptr.none, ptr.used, ptr.mount); sprintf(tulp,"('%s','%s'),",ptr.mount, ptr.used); strcat(ref_buffer,tulp); x++; } strcat(ref_buffer," ]}"); std::cout << ref_buffer << std::endl; pclose(pipe); return ref_buffer; }
char * GetProcessInfo(char *szProcessName) { char buffer[1024] = {0}; char ref_buffer[4096]={0}; sprintf(buffer,"ps aux | grep '%s' | grep -v 'grep' | awk {'print $2'}",szProcessName); FILE *pipe = popen(buffer, "r"); char tmp[32]={0}; fgets(tmp,sizeof(tmp),pipe); if(strlen(tmp)==0) { return (char *)"{ 'platform': 'Centos', 'flag':'false' }"; } else { return (char *)"{ 'platform': 'Centos', 'flag':'true' }"; } }
char *GetPing() { return "{ 'platform': 'Centos', 'flag':'true' }"; }
|