📄 get_cpu.c
字号:
#include "public.h"
//#include "monitor.h"
//#include "msgqueue.h"
#include "sysmoni.h"
#include "standdb.h"
/************************************************************/
/* 文件名 : get_cpu.c */
/* 描述 : 使用unix命令形成有关数据的文本文件 */
/* 分析该文件,将超出警戒的数据报警写入消息队列
如果消息队列满则写入异常文件 */
/* 编写人 : genius.xuyong */
/* 创建日期: 19 Aug. 2002 */
/* 修改 : 22 Oct. 2002 */
/************************************************************/
int get_cpu ( char *acz_device,
char *acz_machine_code,
char *acz_module_code,
char *acz_object_code,
char *acz_create_time )
{
FILE *lh_file;
char lcz_buf[LEN];
int i=0;
system("sar -u >out/cpu.dat"); /*command get resourinfo*/
lh_file = fopen("./out/cpu.dat","r"); /*open a resource info file*/
if (NULL == lh_file )
{
printf("open file resource error!\n");
fclose( lh_file );
return ( -1 );
}
memset(lcz_buf,0x00,LEN); /*set lcz_buf string is null*/
while (NULL != fgets(lcz_buf,sizeof(lcz_buf),lh_file))
/*read a line from file,if is eof t hen break the while cycle*/
{
/*if the line include char '0' then deal the line ,
*else continue the cycle*/
if (NULL==strstr(lcz_buf,"0"))
{
continue;
}
else
{
decompose_cpu(lcz_buf,acz_machine_code,acz_module_code,acz_object_code,acz_create_time);
i++;
/*deal data function,decompose a buf string
* to a cpu struct array*/
}
}
fclose( lh_file );
return ( 0 );
}
int decompose_cpu(char *acz_buf,char* acz_machine_code,char* acz_module_code,char* acz_object_code,char * create_time)
{
int i,j,k;
char lc_char;
char lcz_tmp[8][19];
char complete_time[20]="";
long ll_array[20];
long ll_pos;
char lc_state[10]; /*CPU属性状态*/
char lc_code[11][10]; /*CPU属性编码*/
char gcz_cpu[6][20]={"00:00:00","%usr","%sys","%wio","%idle","%intr"}; /*CPU模块属性数组*/
memset(lcz_tmp,0x00,10);/*set the lcz_tmp is null*/
j=0; /*set the first position is lcz_tmp[0]*/
k=0; /*set the first position is ll_array[0]*/
for (i=0;;i++)/*this cycle is read a char from the buffer char*/
{
lc_char=acz_buf[i];/*put the buffer char to lc_char*/
if (lc_char=='\n')/*if the char is the end of the buffer*/
{
if (j>0) /*if counter j >0,mean lcz_tmp not null*/
{
lcz_tmp[k][j]='\0';
if (lc_state[k]=='1')
{
get_format_time(complete_time,sizeof(complete_time));
Write_MonitorInfo(lc_code[k], acz_object_code,lcz_tmp[k],create_time,complete_time);
}
}
break; /* the exit of the cycle*/
}
if (lc_char!=0x20) /*if the char is not null*/
{
lcz_tmp[k][j]=lc_char;
j++; /*the value of lc_char append to lcz_tmp */
}
else
{
if (j>0)
{
lcz_tmp[k][j]='\0';
memset(lc_state,0x00,strlen(lc_state));
memset(lc_code,0x00,10);
if (k==0)
{ memset(lc_state,0x00,strlen(lc_state));
memset(lc_code,0x00,10);
Read_Property(gcz_cpu,acz_module_code,lc_state,lc_code);
if ( Read_Object(lcz_tmp[k], acz_module_code,acz_object_code)==0)
break;
}
if (lc_state[k]=='1')
{
get_format_time(complete_time,sizeof(complete_time));
Write_MonitorInfo(lc_code[k], acz_object_code,lcz_tmp[k],create_time,complete_time);
}
k++;
}
memset(lcz_tmp,0x00,10);/*set lcz_tmp is null*/
j=0; /*set the first position is lcz_tmp[0]*/
}
}
return ( 0 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -