nagios插件之监控oracle日志

vi check_oracle_alert.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define OK       0   
#define WARNING  1   
#define CRITICAL 2   
#define UNKNOWN  3   

#define LEN 40960

int exitstatus=OK;
char *exit_status[4]={"OK","WARNING","CRITICAL","UNKNOWN"}; 

char status_information[LEN];
char performance_data[LEN];

//#define LOG_FILE_DIR "/home/nagios/check_wcar_log/113"
//#define LOG_FILE_DIR "/opt/smb_win_113_huapu_intface/gateWay/log"
//#define LOG_FILE_DIR "/home/neo/check_log/aes"
#define LOG_FILE "/home/neo/check_log/oracle_log/alert_cutp1.log"

//#define OLD_FILE_FAILED "/home/nagios/check_wcar_log/113/log_err_tmp.file"
//#define OLD_FILE_FAILED "/home/neo/check_log/aes/log_tmp.file"
#define OLD_FILE_FAILED "/home/neo/check_log/oracle_log/log_tmp.file"

char oracle_log_str_now[LEN]={0};
char oracle_log_str_old[LEN]={0};

//char today_start_time[32];

int all_line=0;

int err_line=0;

int err_count=0;

int err_mark=0;


int check_old_file(void) {
	int ret;
	FILE *fp_old;
	char readbuf[1024];

	//OLD_FILE_FAILED
	fp_old=fopen(OLD_FILE_FAILED,"a+");
	if(fp_old==NULL) {
		fprintf(stderr,"check_old_file() is fopen() error.\n");
		return -1;
	}

	ret=fseek(fp_old,0,SEEK_SET);
	if(ret==-1) {
		fprintf(stderr,"check_old_file() is fseek() error.\n");
		return -1;
	}
	else {
		fgets(oracle_log_str_old,1024,fp_old);
	}

        ret=fclose(fp_old);
	if(ret==EOF) {
		fprintf(stderr,"check_old_file() is fclose() error.\n");
		return -1;
	}

	printf("oracle_log_str_old=%s\n",oracle_log_str_old);
	printf("-------------------------\n");
	
	return 0;
}

int write_old_file(char *old_file,char *error_str) {
	int ret;
	FILE *fp_old;

	fp_old=fopen(old_file,"w");
	if(fp_old==NULL) {
       		fprintf(stderr,"write_old_file() is fopen() error.\n");
	}

	ret=fprintf(fp_old,"%s",error_str);
	if(ret<0) {
		fprintf(stderr,"write_old_file() if fprintf() fp_old error.\n");
		return -1;
	}

	ret=fclose(fp_old);
	if(ret==EOF) {
       		fprintf(stderr,"write_old_file() is fclose() error.\n");
	}

	return 0;
}

int parse_log_file(char *log_file,char *check_year,char *check_day) {
	FILE *fp;
	long int *position;
	char readbuf[1024];
	char readbuf_tmp[1024];
	int size=1024,line=0,line_bak;
	char *str;

	int ret;

	int mark;
	char *p,*str_date;

	position=(long int *)malloc(sizeof(long int)*size);
	position[0]=0;

	fp=fopen(log_file,"r");
	if(fp==NULL) {
	//	fprintf(stderr,"parse_log_file() is fopen() error %s\n",log_file);
	//	perror("parse_log_file() is fopen() error,");
	//	exit(-1);

	        sprintf(status_information,"Log file no create, Queue_full_num=%d",err_count);
		sprintf(performance_data,"Queue_full_num=%d;;;;",err_count);
		exitstatus=WARNING;
		printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
		exit(exitstatus);
	}

	while(fgets(readbuf,sizeof(readbuf),fp)!=NULL) {
		if(++line==size) {
			size*=2;
			position=(long int *)realloc(position,sizeof(long int)*size);
		}

		position[line]=ftell(fp);
	}

	all_line=line;
	line_bak=line;

	while(line--) {
		mark=0;

		ret=fseek(fp,position[line],SEEK_SET);	
		if(ret==-1) {
			perror("parse_log_file() is fseek()");
			return -1;
		}

		str=fgets(readbuf,sizeof(readbuf),fp);
		if(str==NULL) {
			fprintf(stderr,"parse_log_file() is fgets() error.\n");
			return -1;
		}
		printf("readbuf=%s",readbuf);

/*
		strcpy(readbuf_tmp,readbuf);
		for(p=strtok(readbuf_tmp,",\"");p;p=strtok(NULL,",\"")) {
			str_date=p;

			mark++;

			if(mark==12) break;
		}
*/

	//	printf("mark=%d,str_date=%s\n",mark,str_date);

/*
		if(strcmp(str_date,today_start_time)<0) {
			break;
		}
*/
	//	printf("mark=%d,str_date=%s\n",mark,str_date);

	//	if(strcmp(str_date,today_start_time)>=0 && line!=1) {
		//	printf("-----------------\n");

		//	if(strstr(readbuf,"ORA-") && err_mark==0) {
			if(strstr(readbuf,"ORA-")) {
		//	if(strstr(readbuf,"Exception") && err_mark==0) {
			//	printf("-----------------\n");

				if(strcmp(oracle_log_str_old,readbuf)) {
					err_line=line+1;
					err_count++;

					//	strcat(error_str,readbuf);
					//	printf("readbuf=%s\n",readbuf);
					strcpy(oracle_log_str_now,readbuf);
				//	printf("oracle_log_str_now=%s\n",oracle_log_str_now);

					if(err_count==1) {
						ret=write_old_file(OLD_FILE_FAILED,oracle_log_str_now);
						if(ret==-1) {
							fprintf(stderr,"parse_log_file() is write_old_file() oracle_log_str_now error.\n");
							return -1;
						}
					}
				} 
				else {
					break;
				}
			/*	else {
					err_mark=1;
				}
			*/
			}

	//	}

		/*	if(err_mark==1) {
				break;
			}
		*/
			if(strstr(readbuf,check_year) && !strstr(readbuf,check_day)) {
			//	if(!strstr(readbuf,check_day)) {
			//	}
				break;
			}
	}

//	printf("err_400_count=%d,oracle_log_str_now=%s\n",err_400_count,oracle_log_str_now);

//	printf("err_404_count=%d,error_str_now_404=%s\n",err_404_count,error_str_now_404);


	ret=fclose(fp);
	if(ret==EOF) {
		fprintf(stderr,"parse_log_file() is fclose() error\n");
	}

	return 0;
}

int main(void) {
	int fd,ret;

	char oracle_log_file[128];

	char nowtime[128];
	char hostname[128];

	char my_day[32];  
	char my_week[32];  
	char my_month[32];  
	char my_year[32];  

	char check_day[64];
	char check_year[32];
  
//	char *week[]={"Sat","Sun","Mon","Tue","Wed","Thu","Fri"};  
	char *week[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};  
	char *month[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};  

	time_t timestamp;
        struct tm *p1;

       // timestamp=time(NULL)-86400;
        timestamp=time(NULL);
        p1=localtime(×tamp);

//	sprintf(oracle_log_file,"%s/log.%d%02d%02d",LOG_FILE_DIR,1900+p1->tm_year,1+p1->tm_mon,p1->tm_mday);
////	printf("oracle_log_file=%s\n",oracle_log_file);

//	sprintf(today_start_time,"%d-%02d-%02d %s\n",p1->tm_year+1900,p1->tm_mon+1,p1->tm_mday,"16-00-00");
//	printf("today_start_time=%s\n",today_start_time);

	sprintf(my_day,"%02d",p1->tm_mday);  
	sprintf(my_week,"%s",week[p1->tm_wday]);  
	sprintf(my_month,"%s",month[p1->tm_mon]);  
	sprintf(my_year,"%d",1900+p1->tm_year);  
	printf("my_year=%s,my_month=%s,my_week=%s,my_day=%s\n",my_year,my_month,my_week,my_day);

//	Sat Aug 22 14:52:40 EAT 2015
	sprintf(check_year,"%d",1900+p1->tm_year);
	sprintf(check_day,"%s %s %02d",week[p1->tm_wday],month[p1->tm_mon],p1->tm_mday);
	printf("check_year=%s,check_day=%s\n",check_year,check_day);
	

	sprintf(nowtime,"%d-%02d-%02d-%02d-%02d-%02d",1900+p1->tm_year,(1+p1->tm_mon),p1->tm_mday,p1->tm_hour,p1->tm_min,p1->tm_sec);  
	printf("nowtime=%s\n",nowtime);


	ret=gethostname(hostname,sizeof(hostname));
        if(ret==-1) {
                fprintf(stderr,"gethostname() error.\n");
                exit(-1);
	}
	printf("hostname=%s\n",hostname);

	ret=check_old_file();
	if(ret==-1) {
		fprintf(stderr,"check_old_file() error.\n");
		exit(-1);
	}

//	printf("oracle_log_str_old=%s\n",oracle_log_str_old);
//	printf("error_str_old_404=%s\n",error_str_old_404);

//	ret=parse_log_file(oracle_log_file);
	ret=parse_log_file(LOG_FILE,check_year,check_day);
	if(ret==-1) {
		fprintf(stderr,"parse_log_file() error.\n");
		exit(-1);
	}

//	printf("%s\n",nowtime);
//	printf("hostname=%s\n",hostname);

//	printf("err_400_line=%d\n",err_400_line);
//	printf("err_404_line=%d\n",err_404_line);

//	printf("all_line=%d\n",all_line);


//	printf("-------------------------------\n");

        if(err_count>=1) {
                exitstatus=CRITICAL;
        }

	sprintf(status_information,"Current oracle_log_num=%d",err_count);

	sprintf(performance_data,"oracle_log_log=%d;;;;",err_count);

	printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
	
	return exitstatus;
}


你可能感兴趣的:(nagios插件之监控oracle日志)