📄 get_ps.c.bak
字号:
#include"public.h"
//#include"monitor.h"
//#include"msgqueue.h"
#include"sysmoni.h"
#include"standdb.h"
/************************************************/
/*FILE NAME: get_ps.c */
/*DESCRIPTION: use system command get proc info,
** decompose and analysis the data */
/*INCLUDE: */
/*FUNCTION: get_ps(char *acz_device,\
char *acz_procname), */
/*AUTHOR: genius.xuyong */
/*CREATE DATE: 2002-08-15 */
/************************************************/
extern long gl_qmsgsend;
int make_procmsg( char* acz_device,
char *acz_procname,
char* acz_state,
char* acz_unusualfile,
char* acz_lckfile,
char* acz_procnameself);
int get_ps(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;
strcat(lcz_command," >out/proc.dat");
system("ps -el >out/proc.dat");
lh_file = fopen("./out/proc.dat","r"); /*open a cpu info text*/
if (NULL == lh_file)
{
printf("open proc.dat error\n");
return ( -1 );
}
memset(lcz_buf,0x00,LEN); /*set lcz_buf string is null*/
i=0;
while (NULL != fgets(lcz_buf,sizeof(lcz_buf),lh_file))
{
if (NULL==strstr(lcz_buf,":"))
{
fclose( lh_file );
return ( -1 );
}
else
{
decompose_ps(lcz_buf,acz_machine_code,acz_module_code,acz_object_code,acz_create_time); /*deal data function*/
i++;
}
fclose( lh_file );
deal_proc( &ls_proc,
acz_device,
acz_procname,
acz_unusualfile,
acz_lckfile,
acz_procnameself
);
return ( 0 );
}
/************************************************************/
/*decompose a data string,change some char to long type data,
*and send them to a struct. the in_parameter are a string and
*a cpuinfo struct
************************************************************/
int decompose_ps(char *acz_buf,char* acz_machine_code,char* acz_module_code,char* acz_object_code,char * create_time)
{
int i,j,k,m;
char lcz_char[LEN];
char lc_char;
char lcz_tmp[20][19];
char complete_time[20]="";
long ll_array[20];
long ll_length;
char lc_state[20]; /*CPU属性状态*/
char lc_code[20][10]; /*CPU属性编码*/
char gcz_cpu[20][20]={"F","S","UID","PID","PPID","CLS","PRI","NI","C","ADDR","SZ","WCHAN","TTY","TIME","COMD"}; /*CPU模块属性数组*/
memset(lcz_tmp,0x00,10);/*set the lcz_tmp is null*/
memset(lcz_char,0x00,LEN);
j=0; /*set the first position is lcz_tmp[0]*/
k=0; /*set the first position is ll_array[0]*/
ll_length=strLen(acz_buf);
for(i=(ll_len-1);;i--)
if (acz_char[i]==0x20)
break;
for(k=0,j=(i+1);j<ll_len;j++)
lcz_char[k++]=acz_char[j];
lcz_char[k]='\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_char, 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++;
}
j=0; /*set the first position is lcz_tmp[0]*/
}
}
as_proc->proc_flag = atoi( lcz_tmp[0] );
memcpy(&as_proc->proc_state,lcz_tmp[1],sizeof(lcz_tmp[1]));
memcpy(&as_proc->proc_time,lcz_tmp[12],sizeof(lcz_tmp[12]));
return ( 0 );
}
int deal_proc( PROCINFO* as_proc,
char* acz_device,
char * acz_procname,
char * acz_unusualfile,
char * acz_lckfile,
char* acz_procnameself
)
{
if ( ( NULL != strstr( as_proc->proc_state,"Z" ) ) ||
( NULL != strstr( as_proc->proc_state,"T" ) )
)
{
make_procmsg(acz_device,\
acz_procname,\
as_proc->proc_state,
acz_unusualfile,
acz_lckfile,
acz_procnameself
);/* function of write message*/
}
if ( NULL != memcmp(&as_proc->proc_state,"00:00:00",8) )
{
}
return ( 0 );
}
int make_procmsg( char* acz_device,
char* acz_procname,
char* acz_state,
char* acz_unusualfile,
char* acz_lckfile,
char* acz_procnameself
)
{
MONIWARN ls_moniwarn;
char lcz_localtime[19+1];
char lcz_content[40+1];
int li_error=0;
int li_ret;
int i;
memset(&ls_moniwarn,0x00,sizeof(MONIWARN));
sprintf(lcz_content,"%s 状态:%s",acz_procname,acz_state);
strcpy(ls_moniwarn.warn_content,lcz_content);
ls_moniwarn.data_type = 'M';
strcpy(ls_moniwarn.object_code,acz_device);
strcpy(ls_moniwarn.info_code,"CM0100");
get_format_time(lcz_localtime,sizeof(lcz_localtime));
strcpy(ls_moniwarn.warn_datetime,lcz_localtime);
for(i=0;i<SENDCOUNT;i++)
{
li_ret = PutMsgData(&li_error,
(char*)&ls_moniwarn,
sizeof(MONIWARN),
gl_qmsgsend,
MYTYPES);
if (li_ret)
{
if ( i == SENDCOUNT - 1 )
{
write_unusuality( acz_unusualfile,
acz_lckfile,
(char*)&ls_moniwarn,\
sizeof(MONIWARN)
);
}
}
else
{
break;
}
}
return (0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -