获取CPU,内存使用率,磁盘相关信息

时间:2020-12-03 05:53:52
 
 CPU信息
bool get_cpuoccupy(CPU_OCCUPY *cpust) //对无类型get函数含有一个形参结构体类弄的指针O{	FILE *fd;	char buff[256];	CPU_OCCUPY *cpu_occupy;	cpu_occupy = cpust;	if ((fd = fopen("/proc/stat", "r")) <= 0)		return false;	memset(buff, 0, 256);	fgets(buff, sizeof(buff), fd);	sscanf(buff, "%s %u %u %u %u", cpu_occupy->name, &cpu_occupy->user,			&cpu_occupy->nice, &cpu_occupy->system, &cpu_occupy->idle);	fclose(fd);	return true;}//获取CPU频率 MHZbool get_cpu_info(unsigned int *total){	FILE *fd;	char buff[256];	char name[128], Hz[128], tmp[128];	char *p;	*total = 0;	if ((fd = fopen("/proc/cpuinfo", "r")) <= 0)		return false;	memset(buff, 0, 256);	for (int i = 0; i < 7; i++)	{		fgets(buff, sizeof(buff), fd);	}	sscanf(buff, "%s %s %s %s", name, Hz, tmp, tmp);	p = strtok(tmp, ".");	*total = atoi(p);	fclose(fd);	return true;}

计算CPU使用率

int cal_cpuoccupy(CPU_OCCUPY *o, CPU_OCCUPY *n)
{
unsigned long oldtatal, newtotal;
unsigned long id, sd, nd;
int cpu_use = 0;

oldtatal = (unsigned long) (o->user + o->nice + o->system + o->idle);//第一次(用户+优先级+系统+空闲)的时间再赋给od
newtotal = (unsigned long) (n->user + n->nice + n->system + n->idle);//第二次(用户+优先级+系统+空闲)的时间再赋给od

id = (unsigned long) (n->user - o->user); //用户第一次和第二次的时间之差再赋给id
nd = (unsigned long) (n->nice - o->nice);
sd = (unsigned long) (n->system - o->system);//系统第一次和第二次的时间之差再赋给sd
if ((newtotal - oldtatal) != 0)
cpu_use = (int) ((sd + id + nd) * 10000) / (newtotal - oldtatal); //((用户+系统)乖100)除(第一次和第二次的时间差)再赋给g_cpu_used
else
cpu_use = 0;
return cpu_use;
}


内存信息

 

bool get_memoccupy(MEM_OCCUPY *mem) //对无类型get函数含有一个形参结构体类弄的指针O
{
FILE *fd;
char buff[256], tmp[20];
MEM_OCCUPY *m;
m = mem;

if ((fd = fopen("/proc/meminfo", "r")) <= 0)
{
return false;
}

memset(buff, 0, 256);
fgets(buff, sizeof(buff), fd);
sscanf(buff, "%s %lu %s", m->name, &m->total, tmp);

memset(buff, 0, 256);
fgets(buff, sizeof(buff), fd); //从fd文件中读取长度为buff的字符串再存到起始地址为buff这个空间里
sscanf(buff, "%s %lu %s", m->name2, &m->free, tmp);

fclose(fd); //关闭文件fd
return true;
}


 调用函数

bool qury_sys_resource(char *buf)
{
CPU_OCCUPY cpu_stat1;
CPU_OCCUPY cpu_stat2;
MEM_OCCUPY mem_stat;
unsigned int cpu_MHZ;
char xml[1024];
int cpu_used, memused;
float cpu_use_per = 0;

memset(&cpu_stat1, 0, sizeof(cpu_stat1));
memset(&cpu_stat2, 0, sizeof(cpu_stat2));
memset(&mem_stat, 0, sizeof(mem_stat));
memset(xml, 0, 1024);
//第一次获取cpu使用情况
if (!get_cpuoccupy((CPU_OCCUPY *) &cpu_stat1))
{
if (debug_level)
{
cout << "get cpu stat err!" << endl;
}
sprintf(
buf,
"HTTP/1.1 500 Server err \nServer:Brainaire \nConnection: close \nContent-Type:text/html; charset=UTF-8 \n \n");
return false;
}

//获取内存
if (!get_memoccupy((MEM_OCCUPY *) &mem_stat))
{
if (debug_level)
{
cout << "get memery stat err!" << endl;
}
sprintf(
buf,
"HTTP/1.1 500 Server err \nServer:Brainaire \nConnection: close \nContent-Type:text/html; charset=UTF-8 \n \n");
return false;
}

if (!get_cpu_info(&cpu_MHZ))
{
if (debug_level)
{
cout << "get cpu info err!" << endl;
}
sprintf(
buf,
"HTTP/1.1 500 Server err \nServer:Brainaire \nConnection: close \nContent-Type:text/html; charset=UTF-8 \n \n");
return false;
}
sleep(1);

//第二次获取cpu使用情况
if (!get_cpuoccupy((CPU_OCCUPY *) &cpu_stat2))
{
if (debug_level)
{
cout << "get cpu stat err!" << endl;
}
sprintf(
buf,
"HTTP/1.1 500 Server err \nServer:Brainaire \nConnection: close \nContent-Type:text/html; charset=UTF-8 \n \n");
return false;
}

//计算cpu使用率
cpu_used = cal_cpuoccupy((CPU_OCCUPY *) &cpu_stat1,
(CPU_OCCUPY *) &cpu_stat2);
cpu_use_per = (float) (cpu_used / 100.0);
//memery used
if ((mem_stat.total - mem_stat.free) == 0)
{
memused = 100;
}
else
{
memused = (int) ((mem_stat.total - mem_stat.free) * 100
/ mem_stat.total);
}
sprintf(
xml,
"<Message>\n <Cpu Total=”%d” Used=”%3.1f” /> \n <Memory Total=”%ld” Used=”%d” />\n</Message>",
cpu_MHZ, cpu_use_per, mem_stat.total / 1024, memused);
sprintf(
buf,
"HTTP/1.1 200 OK \nServer:Brainaire \nConnection: close \nContent-type: xml/text; \nContent-Length:%d \n \n%s",
strlen(xml), xml);
if (debug_level)
{
cout << "memery total: " << mem_stat.total / 1024 << ", used: "
<< memused << endl;
cout << "cpu used = " << cpu_use_per << "%" << endl;
cout << buf;
}
return true;
}


 

 获取磁盘分区情况

int get_disk_rw(const char *disk_name)
{
FILE *fd;
char tmp[256], name[256], a[256], flag[256];
if ((fd = fopen("/etc/mtab", "r")) <= 0)
{
if (debug_level)
{
perror("/etc/mtab");
}
return 0;
}
while (fgets(tmp, sizeof(tmp), fd))
{
memset(flag, 0, sizeof(flag));
sscanf(tmp, "%s %s %s %s", name, a, a, flag);
if (strcmp(name, disk_name) == 0)
{
// cout<<flag<<endl;
for (int i = 0; i < 256; i++)
{
if ((flag[i] == 'r' && ((flag[i + 1] == 'o') || flag[i + 1]
== ',' || flag[i + 1] == '\0')))
{
return 1;
}
if (flag[i + 1] == '\0')
{
return 0;
}
}
}
}
return 0;
}

void qury_single_disk_info(const char *disk_path, char *buf)
{
FILE *fd;
char tmp[256], name[256];
unsigned long long total, used, avail;
float ftotal, favail;
if (!(fd = popen("df", "r")))
{
sprintf(
buf,
"HTTP/1.1 500 Server err \nServer:Brainaire \nConnection: close \nContent-Type:text/html; charset=UTF-8 \n \n");
return;
}
while (fgets(tmp, sizeof(tmp), fd))
{
sscanf(tmp, "%s %llu %llu %llu", name, &total, &used, &avail);
if (strcmp(disk_path, name) == 0)
{
ftotal = (float) (total / 1000000.0);
favail = (float) (avail / 1000000.0);
sprintf(
tmp,
"<Message>\n <DiskList>\n <Disk Path=\"%s\" Status=\"%d\" TotalSpace=\"%0.2f\" FreeSpace=\"%0.2f\"/>\n </DiskList>\n</Message>",
disk_path, get_disk_rw(disk_path), ftotal, favail);
sprintf(
buf,
"HTTP/1.1 200 OK\nServer:Brainaire\nConnection: close\nContent-type: xml/text; \nContent-Length:%d\n\n%s",
strlen(tmp), tmp);
if (debug_level)
cout << buf << endl;
return;
}
}
sprintf(tmp, "<Message>\n <DiskList>\n </DiskList>\n</Message>");
sprintf(
buf,
"HTTP/1.1 200 OK \nServer:Brainaire \nConnection: close \nContent-type: xml/text; \nContent-Length:%d \n \n%s",
strlen(tmp), tmp);
return;
}

void qury_all_disk_info(char *buf)
{
FILE *fd;
char tmp[256], name[256], res_buf[MAX_BUF_SIZE];
unsigned long long total, used, avail;
float ftotal, favail;
memset(res_buf, 0, MAX_BUF_SIZE);
if (!(fd = popen("df", "r")))
{
sprintf(
buf,
"HTTP/1.1 500 Server err \nServer:Brainaire \nConnection: close \nContent-Type:text/html; charset=UTF-8 \n \n");
return;
}
sprintf(res_buf, "<Message>\n <DiskList>\n");
fgets(tmp, sizeof(tmp), fd);
while (fgets(tmp, sizeof(tmp), fd))
{
sscanf(tmp, "%s %llu %llu %llu", name, &total, &used, &avail);
ftotal = (float) (total / 1000000.0);
favail = (float) (avail / 1000000.0);
sprintf(
res_buf + strlen(res_buf),
" <Disk Path=\"%s\" Status=\"%d\" TotalSpace=\"%0.2f\" FreeSpace=\"%0.2f\"/>\n",
name, get_disk_rw(name), ftotal, favail);
}

sprintf(res_buf + strlen(res_buf), " </DiskList>\n</Message>");

sprintf(
buf,
"HTTP/1.1 200 OK\nServer:Brainaire\nConnection: close\nContent-type: xml/text; \nContent-Length:%d\n\n%s",
strlen(res_buf), res_buf);
if (debug_level)
cout << buf << endl;
return;
}