nagios插件之监控MQA日志文件--检查ERROR

vi check_mqa_log_error.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 204800L
#define LEN_SHORT 32L

#define LOG_FILE "/opt/MQ-Adapter/log/mqsgip.log"
//#define LOG_FILE "/home/nagios/check_log_error/mqsgip.log.2013-12-10.log"

#define OLD_ERROR_FILE "/home/nagios/check_log_error/log_tmp_error.file"


/*
 
SW send error
startJMS error 
 
*/

char log_error_old[LEN]={0};
char log_error_now[LEN]={0};

int all_line=0;
int err_line=0;

int log_error_line=0;

int log_error_count=0;

int log_error_mark=0;

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

	time_t timestamp;
        struct tm *p1;


	//OLD_ERROR_FILE
	fp_old=fopen(OLD_ERROR_FILE,"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(log_error_old,1024,fp_old);
	}

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

//	printf("%s",error_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) {
	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;

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

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

	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() is fseek()");
			printf("--------------\n");
			return -1;
			printf("--------------\n");
		}

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

		strcpy(readbuf_tmp,readbuf);

	//	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,"ERROR") && strstr(readbuf,"IOException") && mo_sms_delivery_mark==0) {
	//

		if(strstr(readbuf,"ERROR") && strstr(readbuf,"IOException")) {
			if(strcmp(log_error_old,readbuf)) {
				log_error_line=line+1;
				log_error_count++;

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

				if(log_error_count==1) {
					ret=write_old_file(OLD_ERROR_FILE,log_error_now);
					if(ret==-1) {
						fprintf(stderr,"parse() is write_old_file() log_error_now error.\n");
						return -1;
					}
				}

			} 
			else {
				break;
			}
		}

	}

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

//	printf("failed_count=%d,error_str_now_failed=%s\n",failed_count,error_str_now_failed);

//	printf("no_answer_count=%d,error_str_now_no_answer=%s\n",no_answer_count,error_str_now_no_answer);

	return 0;
}


int main(void) {
	int fd,ret;
	int mark=0;

	char hostname[LEN_SHORT];

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

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

	/*
	time_t timestamp;
        struct tm *p1;

        timestamp=time(NULL);
        p1=localtime(×tamp);

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

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

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

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

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

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

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

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


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

	/*
	if(failed_count==1 || no_answer_count==1) {
		exitstatus=WARNING;
	}
	else if(failed_count>1 || no_answer_count>1) {
		exitstatus=CRITICAL;
	}
	*/

	sprintf(status_information,"Current log_error_count=%d",log_error_count);

	sprintf(performance_data,"log_error_count=%d;;;;",log_error_count);

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

你可能感兴趣的:(nagios插件之监控MQA日志文件--检查ERROR)