⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 monitor_main.c

📁 UNIX环境下资源监控程序(包含对CPUMEMDISKIOPS等资源的监控)
💻 C
字号:
/************************************************************/
/*	文件名	:	monitor_main.c								*/
/*	描述	:	收集网络、系统CPU、内存、磁盘、进程资源	IO	*/
/*				和异常文件									*/
/*	编写人	:	genius.xuyong								*/
/*	创建日期:	19 Aug. 2002								*/
/*	修改	:	22 Oct. 2002								*/
/************************************************************/

#include "public.h"
/*
#include "msgqueue.h"
#include "ourlib.h"
*/
#include "sysmoni.h"
#include "standdb.h"
#include <fcntl.h>

int sig_int = 0;
/**	用户信号处理句柄	**/
void  sig_handle(int sig)
{
	sig_int = 1;
}

void redirect()
{
	char ttynam[120];

	int fd, n, total_bytes=0;
	
	fd = open("ttyfifo",O_RDONLY);

	if (fd < 0){
		perror(NULL);
		close(fd);
		return;
	}
	
	while((n = read(fd, &ttynam[total_bytes], 120-total_bytes))>0)
		total_bytes += n;
	
	if ( total_bytes > 0)
	{
		close(0);
		close(1);
		close(2);
	 	open(ttynam, O_RDONLY);
	 	open(ttynam, O_WRONLY);
	 	open(ttynam, O_WRONLY);
	}
	close(fd);
}

int	gi_error_type = 0;		/*	错误代码*/

/************************************************************/
/*	函数	:	main										*/
/*	描述	:	循环收集网络、系统CPU、内存、磁盘、进程资源	*/
/*				和异常文件									*/
/*	输入参数:	无											*/
/*	输出参数:	无											*/
/*	返回值	: 	无(不退出)									*/
/************************************************************/
char	gcz_device[LEN];	 /*本机设备号*/
/************************************************************/



/***************************/


/*long	gl_dealcount=0;*/	/*异常文件处理行数(未处理完时)*/
long	gl_qmsgsend;		/*消息队列号*/




int main(int argc,char *argv[])
{
	char username[20] = "";                /*数据库连接标识*/
	char password[20] = "";
	char db_string[20] ="";
	char modulename[20]="";
	
        char    gcz_machine_code[10]="";         /*机器代码*/
        char    gcz_module_code[10]="";        /*属性代码*/
        char    gcz_object_code[10]="";           /*对象代码*/
        char    gcz_content[100]="";             /*监控数据内容*/
        char    create_time[20]="";
        
	int  	li_terval  = 30;			/*循环时间间隔*/
	int  	li_port    = 0;				/*采集端口数目*/
	int  	li_process = 0;				/*进程数目*/
	
	char 	lcza_port[LEN][LEN];		/*端口名称数组*/
	char	lcza_processes[LEN][LEN];	/*进程名称数组*/
	int		i;
	
	char	lcz_lckfile[80];
	char	lcz_unusualfile[80];
	int		li_result;			/*读参数结果*/
	FILE*	lh_logfile;			/*参数修改标志文件*/
	FILE*	lh_rightfile;		/*参数文件*/
	FILE*	lh_backupfile;		/*备份参数文件*/
	int		li_return;
	char	lcz_system_command[100];	
	/**	建立后台守护进程,退出当前进程
		目的:为防止因窗口退出导致进程退出
		SIGHUP信号忽略为防止和窗口发生再联系
	

	
/**	读取参数信息	**/	

	if( NULL == (	lh_rightfile = 
					fopen("../Parameter/monitor_para.ini","r")))
	{
		gi_error_type = -6200;
		error_deal(gi_error_type);
		printf("no para file!error_type =%d\n",gi_error_type);
		exit( -1 );
	}

	li_return = read_para_file(	lh_rightfile,
							lcza_port,
							&li_port,
							lcza_processes,
							&li_process,
							&li_terval,
							lcz_unusualfile,
							lcz_lckfile
						 );

	fclose(lh_rightfile);
	if (li_return)
	{
		gi_error_type = li_return;
		error_deal(gi_error_type);
		printf("parafile is wrong,error_type =%d\n",gi_error_type);
		fflush( NULL );
		exit( -1 );
	}
	/**循环收集信息,正常情况下不会退出**/
	for(;;)
	{
		
		if (sig_int ==1)
		{
			sig_int = 0;
			redirect();
		}

		/*	检测是否有cm_fm.log文件,如果有,重新读入参数文件
			如果参数文件有错误,将参数文件的备份文件恢复为参数文件,
			再读参数文件*/
		if( NULL != (lh_logfile = fopen("../Parameter/monitor_para.tag","r")))
		{
			/*	如果有该文件,删除该文件*/
			fclose(lh_logfile);
			system("rm ../Parameter/monitor_para.tag");
			
			/*	重新读入参数	*/
			if( NULL == (	lh_rightfile = 
					fopen("../Parameter/monitor_para.ini","r")))
			{
				gi_error_type = -6200;
				error_deal(gi_error_type);
				printf("no para file! error_type =%d\n",gi_error_type);
				exit( -1 );
			}

			li_return = read_para_file(	lh_rightfile,
									lcza_port,
									&li_port,
									lcza_processes,
									&li_process,
									&li_terval,
									lcz_unusualfile,
									lcz_lckfile
								 );

			fclose(lh_rightfile);

			if ( li_return )
			{	
				/*如果参数文件有错误,将参数文件的备份文件
				恢复为参数文件  */
				printf("some error in parafile,");
				printf("restore the old parafile\n");	
				strcpy(lcz_system_command,"cp ../Parameter/");
				strcat(lcz_system_command,"monitor_para.bak ");
				strcat(lcz_system_command,"../Parameter/");
				strcat(lcz_system_command,"monitor_para.ini");
				system( lcz_system_command );

				/*	重新读入参数	*/
				lh_rightfile = fopen("../Parameter/monitor_para.ini",
									 "r"
									);

				li_return = read_para_file(	lh_rightfile,
										lcza_port,
										&li_port,
										lcza_processes,
										&li_process,
										&li_terval,
										lcz_unusualfile,
										lcz_lckfile
									 );
				fclose(lh_rightfile);
			}
		}
		
		
/* 根据输入参数连接数据库 */
         strcpy(username,"NEW_DBA");   
         strcpy(password,"DBA");
         strcpy(db_string,"yljf");

	if (Connect_Database(username,password,db_string)==0)
	{
		//exit(5);
		exit(0);
	}
	
	
          memset(modulename, 0x00, sizeof(modulename));
	  strcpy(modulename,"网络");
	  memset(gcz_machine_code, 0x00, sizeof(gcz_machine_code));
	  memset(gcz_module_code, 0x00, sizeof(gcz_module_code));
	  memset(gcz_object_code, 0x00, sizeof(gcz_object_code));
	  strcpy(gcz_device,prune(gcz_device));
	  strcpy(modulename,prune(modulename));
            if ( Read_Module(gcz_device,modulename,gcz_module_code,gcz_machine_code)==1)
		{
		  get_format_time(create_time,sizeof(create_time));
		  get_net(gcz_device,
		              gcz_machine_code,
		              gcz_module_code,
		              gcz_object_code,
		              create_time);		/*网络信息*/
	        }
	 
	 
	 
	 
	 
	  memset(modulename, 0x00, sizeof(modulename));
	  strcpy(modulename,"CPU");
	  memset(gcz_machine_code, 0x00, sizeof(gcz_machine_code));
	  memset(gcz_module_code, 0x00, sizeof(gcz_module_code));
	  memset(gcz_object_code, 0x00, sizeof(gcz_object_code));
	  strcpy(gcz_device,prune(gcz_device));
	  strcpy(modulename,prune(modulename));
            if ( Read_Module(gcz_device,modulename,gcz_module_code,gcz_machine_code)==1)			 
		   {  get_format_time(create_time,sizeof(create_time));
		      get_cpu(gcz_device,
		              gcz_machine_code,
		              gcz_module_code,
		              gcz_object_code,
		              create_time);		 /*CPU信息*/
		   }  
			
		
		
			
	memset(modulename, 0x00, sizeof(modulename));
	  strcpy(modulename,"IO");
	  memset(gcz_machine_code, 0x00, sizeof(gcz_machine_code));
	  memset(gcz_module_code, 0x00, sizeof(gcz_module_code));
	  memset(gcz_object_code, 0x00, sizeof(gcz_object_code));
	  strcpy(gcz_device,prune(gcz_device));
	  strcpy(modulename,prune(modulename));
            if ( Read_Module(gcz_device,modulename,gcz_module_code,gcz_machine_code)==1)			
                  {    get_format_time(create_time,sizeof(create_time));
		       get_io(gcz_device,
		              gcz_machine_code,
		              gcz_module_code,
		              gcz_object_code,
		              create_time);		/*IO信息*/
	          }
	
	memset(modulename, 0x00, sizeof(modulename));
	  strcpy(modulename,"内存");
	  memset(gcz_machine_code, 0x00, sizeof(gcz_machine_code));
	  memset(gcz_module_code, 0x00, sizeof(gcz_module_code));
	  memset(gcz_object_code, 0x00, sizeof(gcz_object_code));
	  strcpy(gcz_device,prune(gcz_device));
	  strcpy(modulename,prune(modulename));
            if ( Read_Module(gcz_device,modulename,gcz_module_code,gcz_machine_code)==1)			
                {
                 get_format_time(create_time,sizeof(create_time));
		get_mem(gcz_device,
		              gcz_machine_code,
		              gcz_module_code,
		              gcz_object_code,
		              create_time);
		  }  		/*内存信息*/
				
							
			
	memset(modulename, 0x00, sizeof(modulename));
	  strcpy(modulename,"内存");
	  memset(gcz_machine_code,0x00, sizeof(gcz_machine_code));
	  memset(gcz_module_code, 0x00, sizeof(gcz_module_code));
	  memset(gcz_object_code, 0x00, sizeof(gcz_object_code));
	  strcpy(gcz_device,prune(gcz_device));
	  strcpy(modulename,prune(modulename));
            if ( Read_Module(gcz_device,modulename,gcz_module_code,gcz_machine_code)==1)
                 {
                 get_format_time(create_time,sizeof(create_time));
		get_df(gcz_device,
		              gcz_machine_code,
		              gcz_module_code,
		              gcz_object_code,
		              create_time);	
		  }	/*磁盘信息*/ 
	
            
            memset(modulename, 0x00, sizeof(modulename));
	  strcpy(modulename,"进程");
	  memset(gcz_machine_code, 0x00, sizeof(gcz_machine_code));
	  memset(gcz_module_code, 0x00, sizeof(gcz_module_code));
	  memset(gcz_object_code, 0x00, sizeof(gcz_object_code));
	  strcpy(gcz_device,prune(gcz_device));
	  strcpy(modulename,prune(modulename));

		if ( Read_Module(gcz_device,modulename,gcz_module_code,gcz_machine_code)==1)
                 {
                        get_format_time(create_time,sizeof(create_time));
			   get_ps( gcz_device, 
					gcz_machine_code,
		              gcz_module_code,
		              gcz_object_code,
		              create_time);
		}      /*进程信息*/

		sleep( li_terval );
		

	}    /*end for*/    
	
}  // end of main Functions	

/********************************************************************/
/*																	*/
/*	函数名称	: 	read_para_file									*/
/*	描述		:	读出参数文件的内容								*/ 
/*	编写人		: 	genius.xuy										*/
/*	创建日期	: 	22.Oct.2002										*/
/*	输入输出	:	ah_file			:	参数文件指针
					acza_port		:	输出端口名称
					ai_port			:	端口数目
					acza_processes	:	输出进程名称
					ai_process		:	进程数目
					ai_terval		:	休息时间间隔
					acz_unusualfile	:	异常文件路径名称
					acz_lckfile		:	异常文件锁文件名称
					gcz_device		:	本采集机编码				*/
/********************************************************************/
int read_para_file(	FILE*	ah_file,
					char	acza_port[][LEN],
					int*	ai_port,
					char	acza_processes[][LEN],
					int*	ai_process,
					int*	ai_terval,
					char*	acz_unusualfile,
					char*	acz_lckfile
			 )
{
	char	lcz_terval[5];
	int	li_error_code;

	/**取得本设备名称**/
	/**如本例:[device]
				device = AAA
		获得结果:AAA
	**/
	memset(gcz_device,0x00,sizeof(gcz_device));
	li_error_code = get_para_info(	ah_file,
									"device",
									"device", 
									gcz_device
								 );
	if ( li_error_code )
	{
		printf("get device error!\n");
		fflush( NULL );
		return( -6201 );
	}

	/**取得端口**/
	*ai_port = get_para_array(	&li_error_code,	
								ah_file,	
								"gather_port",
								"port", 
								acza_port
							 );
	if ( li_error_code )
	{
		printf("get gather port error!\n");
		fflush( NULL );
		/*
		return( -6202 );
		*/
	}


	/**取得进程名**/
	*ai_process = get_para_array(	&li_error_code,
									ah_file,
									"processes",
									"process", 
									acza_processes
								 );
	if ( li_error_code )
	{
		printf("get process name error!\n");
		fflush( NULL );
		return( -6203 );
	}

	/**取得间隔时间**/
	li_error_code = get_para_info(	ah_file,
									"gather_time",
									"terval", 
									lcz_terval
								 );

	if ( li_error_code )
	{
		printf("get terval error!\n");
		fflush( NULL );
		return( -6204 );
	}
	*ai_terval = atoi (lcz_terval );

	/**取得异常写入文件**/
	li_error_code = get_para_info(	ah_file,
									"unusualy",
									"unusuality_file", 
									acz_unusualfile
								 );

	if ( li_error_code )
	{
		printf("get unusuality_file error!\n");
		fflush( NULL );
		return( -6205 );
	}

	/**取得锁文件**/
	li_error_code = get_para_info(	ah_file,
									"unusualy",
									"lock_file", 
									acz_lckfile
								 );

	if ( li_error_code )
	{
		printf("get lock_file error!\n");
		fflush( NULL );
		return( -6206 );
	}
	return ( 0 );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -