今天忙活了半天,在Linux平台下,总算可以获取到一些性能指标了,结果,Linux上面的数据发送到Windows上面会出现发送为空的现象,可能是Socket套接字存在问题,不搞了。
#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;
// 得到CPU利用率百分比
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
}
// 计算CPU差值
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);
}
// 获取CPU利用率并返回字典
char * GetCpuInfo()
{
cpu_info ptr;
CPU_OCCUPY ocpu, ncpu;
char buffer[1024] = {0};
char ref_buffer[4096] = {0};
// 获取cpu使用率
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);
// std::cout << "Used = " << ptr.Used << " Avg1 = " << ptr.Avg1 << " Avg5 = " << ptr.Avg5 << " Avg15 = " << ptr.Avg15 << std::endl;
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);
}
}
// std::cout << "Total = " << ptr.Total << " Free = " << ptr.Free << " Available = " << ptr.Available << std::endl;
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);
// std::cout << "Used = " << ptr.used << " mount = " << ptr.mount << std::endl;
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' }";
}