/*
*
* get cpu and memory info
* wangzeng 2009.05.04
*/
#include < stdio.h >
#include < unistd.h >
#include < time.h >
#include < sys / types.h >
// cpu
struct cpustatus
{
long total;
float user;
float nice;
float system;
float idle;
};
// memory
struct meminfo
{
char total[ 20 ];
char free[ 20 ];
};
void errmsg( char * msg);
void get_cpu_status( struct cpustatus * );
void get_mem_info( struct meminfo * );
void write_log( struct cpustatus * , struct meminfo * );
int main( int ac, char * av[])
{
struct cpustatus cpu_stat;
struct meminfo mem_info;
get_cpu_status( & cpu_stat);
printf( " user\t nice\t system\t idle\n " );
printf( " %4.2f\t %4.2f\t %4.2f\t %4.2f\n " ,cpu_stat.user,cpu_stat.nice,cpu_stat.system,cpu_stat.idle);
get_mem_info( & mem_info);
printf( " total:\t%s\n " ,mem_info.total);
printf( " free:\t%s\n " ,mem_info.free);
write_log( & cpu_stat, & mem_info);
return 0 ;
}
// deal error
void errmsg( char * msg)
{
perror(msg);
exit( 1 );
}
// start write cpu and memory info by applend mode
void write_log( struct cpustatus * cpu_stat, struct meminfo * mem_info)
{
FILE * fp;
long seconds;
if ((fp = fopen( " cpustatuslog.txt " , " a+ " )) == NULL)
errmsg( " open cpustatuslog.txt error\n " );
else
{
seconds = time((time_t * )NULL);
fprintf(fp, " %s " ,ctime( & seconds));
fprintf(fp, " user\t nice\t system\t idle\n " );
fprintf(fp, " %4.2f\t %4.2f\t %4.2f\t %4.2f\n " ,cpu_stat -> user,cpu_stat -> nice,cpu_stat -> system,cpu_stat -> idle);
fprintf(fp, " total:\t%s\n " ,mem_info -> total);
fprintf(fp, " free:\t%s\n " ,mem_info -> free);
fprintf(fp, " -------------------------------\n\n " );
fclose(fp);
}
}
// get cpu status
void get_cpu_status( struct cpustatus * cpu_stat)
{
unsigned int total;
float user;
float nice;
float system;
float idle;
char cpu[ 21 ];
char text[ 201 ];
FILE * fp;
fp = fopen( " /proc/stat " , " r " );
while (fgets(text, 200 ,fp))
{
if (strstr(text, " cpu " ))
{
sscanf(text, " %s %f %f %f %f " ,cpu, & user, & nice, & system, & idle); }
}
fclose(fp);
total = (user + nice + system + idle);
user = (user / total) * 100 ;
nice = (nice / total) * 100 ;
system = (system / total) * 100 ;
idle = (idle / total) * 100 ;
cpu_stat -> total = total;
cpu_stat -> user = user;
cpu_stat -> nice = nice;
cpu_stat -> system = system;
cpu_stat -> idle = idle;
return ;
}
// get memory info
void get_mem_info( struct meminfo * minfo)
{
FILE * fp;
int i,j;
char total[ 20 ];
char free[ 20 ];
char temp[ 80 ];
if ((fp = fopen( " /proc/meminfo " , " r " )) == NULL)
errmsg( " open meminfo error.\n " );
else
{
for (i = 0 ;i < 5 ;i ++ )
{
fgets(temp, 80 ,fp);
if (i == 3 ) // total info at 3 row
strcpy(total,temp);
if (i == 4 ) // free info at 4 row
strcpy(free,temp);
}
}
if (strlen(total) > 0 )
{
for (i = 0 ,j = 0 ;i < strlen(total);i ++ )
{
if (isdigit(total[i]))
minfo -> total[j ++ ] = total[i];
}
minfo -> total[j] = ' \0 ' ;
}
if (strlen(free) > 0 )
{
for (i = 0 ,j = 0 ;i < strlen(free);i ++ )
{
if (isdigit(free[i]))
minfo -> free[j ++ ] = free[i];
}
minfo -> free[j] = ' \0 ' ;
}
return ;
}
* get cpu and memory info
* wangzeng 2009.05.04
*/
#include < stdio.h >
#include < unistd.h >
#include < time.h >
#include < sys / types.h >
// cpu
struct cpustatus
{
long total;
float user;
float nice;
float system;
float idle;
};
// memory
struct meminfo
{
char total[ 20 ];
char free[ 20 ];
};
void errmsg( char * msg);
void get_cpu_status( struct cpustatus * );
void get_mem_info( struct meminfo * );
void write_log( struct cpustatus * , struct meminfo * );
int main( int ac, char * av[])
{
struct cpustatus cpu_stat;
struct meminfo mem_info;
get_cpu_status( & cpu_stat);
printf( " user\t nice\t system\t idle\n " );
printf( " %4.2f\t %4.2f\t %4.2f\t %4.2f\n " ,cpu_stat.user,cpu_stat.nice,cpu_stat.system,cpu_stat.idle);
get_mem_info( & mem_info);
printf( " total:\t%s\n " ,mem_info.total);
printf( " free:\t%s\n " ,mem_info.free);
write_log( & cpu_stat, & mem_info);
return 0 ;
}
// deal error
void errmsg( char * msg)
{
perror(msg);
exit( 1 );
}
// start write cpu and memory info by applend mode
void write_log( struct cpustatus * cpu_stat, struct meminfo * mem_info)
{
FILE * fp;
long seconds;
if ((fp = fopen( " cpustatuslog.txt " , " a+ " )) == NULL)
errmsg( " open cpustatuslog.txt error\n " );
else
{
seconds = time((time_t * )NULL);
fprintf(fp, " %s " ,ctime( & seconds));
fprintf(fp, " user\t nice\t system\t idle\n " );
fprintf(fp, " %4.2f\t %4.2f\t %4.2f\t %4.2f\n " ,cpu_stat -> user,cpu_stat -> nice,cpu_stat -> system,cpu_stat -> idle);
fprintf(fp, " total:\t%s\n " ,mem_info -> total);
fprintf(fp, " free:\t%s\n " ,mem_info -> free);
fprintf(fp, " -------------------------------\n\n " );
fclose(fp);
}
}
// get cpu status
void get_cpu_status( struct cpustatus * cpu_stat)
{
unsigned int total;
float user;
float nice;
float system;
float idle;
char cpu[ 21 ];
char text[ 201 ];
FILE * fp;
fp = fopen( " /proc/stat " , " r " );
while (fgets(text, 200 ,fp))
{
if (strstr(text, " cpu " ))
{
sscanf(text, " %s %f %f %f %f " ,cpu, & user, & nice, & system, & idle); }
}
fclose(fp);
total = (user + nice + system + idle);
user = (user / total) * 100 ;
nice = (nice / total) * 100 ;
system = (system / total) * 100 ;
idle = (idle / total) * 100 ;
cpu_stat -> total = total;
cpu_stat -> user = user;
cpu_stat -> nice = nice;
cpu_stat -> system = system;
cpu_stat -> idle = idle;
return ;
}
// get memory info
void get_mem_info( struct meminfo * minfo)
{
FILE * fp;
int i,j;
char total[ 20 ];
char free[ 20 ];
char temp[ 80 ];
if ((fp = fopen( " /proc/meminfo " , " r " )) == NULL)
errmsg( " open meminfo error.\n " );
else
{
for (i = 0 ;i < 5 ;i ++ )
{
fgets(temp, 80 ,fp);
if (i == 3 ) // total info at 3 row
strcpy(total,temp);
if (i == 4 ) // free info at 4 row
strcpy(free,temp);
}
}
if (strlen(total) > 0 )
{
for (i = 0 ,j = 0 ;i < strlen(total);i ++ )
{
if (isdigit(total[i]))
minfo -> total[j ++ ] = total[i];
}
minfo -> total[j] = ' \0 ' ;
}
if (strlen(free) > 0 )
{
for (i = 0 ,j = 0 ;i < strlen(free);i ++ )
{
if (isdigit(free[i]))
minfo -> free[j ++ ] = free[i];
}
minfo -> free[j] = ' \0 ' ;
}
return ;
}