当前位置: 技术问答>linux和unix
怎么监测服务器的cup使用情况,有着方面的函数调用么?谢谢大家
来源: 互联网 发布时间:2015-06-18
本文导语: 怎么监测服务器的cup使用情况?还有内存的使用情况 | cat /proc/cpuinfo cat /proc/meminfo 可以看到cpu和内存的现在的状况 | struct CPU_USAGE { unsigned long cpu_user; ...
怎么监测服务器的cup使用情况?还有内存的使用情况
|
cat /proc/cpuinfo
cat /proc/meminfo
可以看到cpu和内存的现在的状况
cat /proc/meminfo
可以看到cpu和内存的现在的状况
|
struct CPU_USAGE
{
unsigned long cpu_user;
unsigned long cpu_sys;
unsigned long cpu_nice;
unsigned long cpu_idle;
};
int Get_CpuInfor_DynamicData_From_Proc_Stat( struct CPU_USAGE *usage )
{
FILE *fp= NULL;
char tmp[10];
//char buffer[128+1];
fp = fopen(CPU_FILE_PROC_STAT, "r");
if ( fp == NULL )
{
perror("fopen");
return (-1);
};
//printf("%s %dn", __FILE__, __LINE__);
fscanf( fp, "%s %lu %lu %lu %lu", tmp, &(usage->cpu_user), &(usage->cpu_sys), &(usage->cpu_nice), &(usage->cpu_idle) );
//printf("%s %dn", __FILE__, __LINE__);
fclose( fp );
//printf("%s %dn", __FILE__, __LINE__);
return ( 1 );
}
double Get_Cpu_Use_Rate( const struct CPU_USAGE *cur, const struct CPU_USAGE *old )
{
double user, sys, nice, idle, total;
double use_rate;
user = (double)(cur->cpu_user - old->cpu_user);
sys = (double)(cur->cpu_sys - old->cpu_sys);
nice = (double)(cur->cpu_nice - old->cpu_nice);
idle = (double)(cur->cpu_idle - old->cpu_idle);
total = user + sys + nice + idle;
use_rate = ( 1 - idle / total ) * 100;
return use_rate;
}
这是我自己写的,你试着用用,欢迎多提宝贵意见。
{
unsigned long cpu_user;
unsigned long cpu_sys;
unsigned long cpu_nice;
unsigned long cpu_idle;
};
int Get_CpuInfor_DynamicData_From_Proc_Stat( struct CPU_USAGE *usage )
{
FILE *fp= NULL;
char tmp[10];
//char buffer[128+1];
fp = fopen(CPU_FILE_PROC_STAT, "r");
if ( fp == NULL )
{
perror("fopen");
return (-1);
};
//printf("%s %dn", __FILE__, __LINE__);
fscanf( fp, "%s %lu %lu %lu %lu", tmp, &(usage->cpu_user), &(usage->cpu_sys), &(usage->cpu_nice), &(usage->cpu_idle) );
//printf("%s %dn", __FILE__, __LINE__);
fclose( fp );
//printf("%s %dn", __FILE__, __LINE__);
return ( 1 );
}
double Get_Cpu_Use_Rate( const struct CPU_USAGE *cur, const struct CPU_USAGE *old )
{
double user, sys, nice, idle, total;
double use_rate;
user = (double)(cur->cpu_user - old->cpu_user);
sys = (double)(cur->cpu_sys - old->cpu_sys);
nice = (double)(cur->cpu_nice - old->cpu_nice);
idle = (double)(cur->cpu_idle - old->cpu_idle);
total = user + sys + nice + idle;
use_rate = ( 1 - idle / total ) * 100;
return use_rate;
}
这是我自己写的,你试着用用,欢迎多提宝贵意见。