nagios插件之监控if8接口日志(新接口)

vi check_if8_log.c

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

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

#define LEN 400000

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

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

//#define LOG_FILE_DIR "/opt/smb_win_113_dir/log"
//#define LOG_FILE_DIR "/home/weihu/tmp_2_140"
//#define LOG_FILE_DIR "/opt/smb_win_113_dir/log"
//#define LOG_FILE_DIR "/home/neo/check_log/cti_log/if8_log_test"
#define LOG_FILE_DIR "/opt/smb/win_10_28_dir/UltraRouter/log"

//#define OLD_FILE_FAILED "/home/nagios/check_if8_log/113/log_tmp_400.file"
//#define OLD_FILE_NO_ANSWER "/home/nagios/check_if8_log/113/log_tmp_404.file"
//#define OLD_FILE_QUEUE "/home/nagios/check_if8_log/113/log_tmp_queue.file"

//#define OLD_FILE_FETCH_400_FAILED "/home/neo/check_log/cti_log/if8_log_test/log_tmp_fetch_400.file"
#define OLD_FILE_FETCH_400_FAILED "/home/nagios/check_log/UltraRouter/10_28/log_tmp_fetch_400.file"
#define OLD_FILE_FETCH_404_FAILED "/home/nagios/check_log/UltraRouter/10_28/log_tmp_fetch_404.file"

//#define OLD_FILE_FETCH_TIMEOUT_FAILED "/home/neo/check_log/cti_log/if8_log_test/log_tmp_fetch_Timeout.file"
#define OLD_FILE_FETCH_TIMEOUT_FAILED "/home/nagios/check_log/UltraRouter/10_28/log_tmp_fetch_Timeout.file"

//#define OLD_FILE_VOICE_TIMEOUT_FAILED "/home/neo/check_log/cti_log/if8_log_test/log_tmp_voice_timeout.file"

//#define OLD_FILE_INFORM_400_FAILED "/home/neo/check_log/cti_log/if8_log_test/log_tmp_inform_400.file"
#define OLD_FILE_INFORM_400_FAILED "/home/nagios/check_log/UltraRouter/10_28/log_tmp_inform_400.file"
#define OLD_FILE_INFORM_404_FAILED "/home/nagios/check_log/UltraRouter/10_28/log_tmp_inform_404.file"

//#define OLD_FILE_INFORM_TIMEOUT_FAILED "/home/neo/check_log/cti_log/if8_log_test/log_tmp_inform_Timeout.file"
#define OLD_FILE_INFORM_TIMEOUT_FAILED "/home/nagios/check_log/UltraRouter/10_28/log_tmp_inform_Timeout.file"

//#define OLD_FILE_QUEUE "/home/neo/check_log/cti_log/if8_log_test/log_tmp_queue.file"

//#define OLD_FILE_HTTP_CODE_500_FAILED "/home/neo/check_log/cti_log/if8_log_test/log_tmp_http_code_500.file"
#define OLD_FILE_HTTP_CODE_500_FAILED "/home/nagios/check_log/UltraRouter/10_28/log_tmp_http_code_500.file"
#define OLD_FILE_HTTP_CODE_502_FAILED "/home/nagios/check_log/UltraRouter/10_28/log_tmp_http_code_502.file"
#define OLD_FILE_HTTP_CODE_503_FAILED "/home/nagios/check_log/UltraRouter/10_28/log_tmp_http_code_503.file"

//----------
//http response code返回500
char error_str_now_http_code_500[LEN]={0};
char error_str_old_http_code_500[LEN]={0};

//http code 500 line
int err_http_code_500_line=0;
int err_http_code_500_count=0;
int err_http_code_500_mark=0;
//--------------------

//----------
//http response code返回502
char error_str_now_http_code_502[LEN]={0};
char error_str_old_http_code_502[LEN]={0};

//http code 502 line
int err_http_code_502_line=0;
int err_http_code_502_count=0;
int err_http_code_502_mark=0;
//--------------------

//----------
//http response code返回503
char error_str_now_http_code_503[LEN]={0};
char error_str_old_http_code_503[LEN]={0};

//http code 503 line
int err_http_code_503_line=0;
int err_http_code_503_count=0;
int err_http_code_503_mark=0;
//--------------------
//----------
//fetchRoutingData返回400
char error_str_now_fetch_resp_400[LEN]={0};
char error_str_old_fetch_resp_400[LEN]={0};

//fetchRoutingData line
int err_fetch_resp_400_line=0;
int err_fetch_resp_400_count=0;
int err_fetch_resp_400_mark=0;
//--------------------
//----------
//fetchRoutingData返回404
char error_str_now_fetch_resp_404[LEN]={0};
char error_str_old_fetch_resp_404[LEN]={0};

//fetchRoutingData line
int err_fetch_resp_404_line=0;
int err_fetch_resp_404_count=0;
int err_fetch_resp_404_mark=0;
//--------------------
//----------
//fetchRoutingData Timtout
char error_str_now_fetch_resp_Timeout[LEN]={0};
char error_str_old_fetch_resp_Timeout[LEN]={0};

// fetch resp timeout line
int err_fetch_resp_Timeout_line=0;
int err_fetch_resp_Timeout_count=0;
int err_fetch_resp_Timeout_mark=0;
//--------------------
//----------
//informRoutingResult返回400
char error_str_now_inform_resp_400[LEN]={0};
char error_str_old_inform_resp_400[LEN]={0};

//informRoutingResult line
int err_inform_resp_400_line=0;
int err_inform_resp_400_count=0;
int err_inform_resp_400_mark=0;
//--------------------
//----------
//informRoutingResult返回404
char error_str_now_inform_resp_404[LEN]={0};
char error_str_old_inform_resp_404[LEN]={0};

//informRoutingResult line
int err_inform_resp_404_line=0;
int err_inform_resp_404_count=0;
int err_inform_resp_404_mark=0;
//--------------------
//----------

//informRoutingResult Timtout
char error_str_now_inform_resp_Timeout[LEN]={0};
char error_str_old_inform_resp_Timeout[LEN]={0};

// inform resp timeout line
int err_inform_resp_Timeout_line=0;
int err_inform_resp_Timeout_count=0;
int err_inform_resp_Timeout_mark=0;
//--------------------

//------------下行可以去掉了
//char error_str_now_404[LEN]={0};
//char error_str_old_404[LEN]={0};

//char error_str_now_queue[LEN]={0};
//char error_str_old_queue[LEN]={0};

//char today_start_time[32];

int all_line=0;
int err_line=0;

//int err_400_line=0;

// queue full
//int err_queue_line=0;

//wait voice timtout
//char error_str_now_wait_voice_timeout[LEN]={0};
//char error_str_old_wait_voice_timeout[LEN]={0};

//wait voice timeout line
//int err_wait_voice_timeout_line=0;
//int err_wait_voice_timeout_count=0;
//int err_wait_voice_timeout_mark=0;

//fetch and inform response 400 count

//fetch and inform response 400 mark

//fetch and inform response 404 line

//fetch and inform response 404 count

//fetch and  inform response 404 mark

////int err_queue_count=0;
////int err_queue_mark=0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	//OLD_FILE_NO_ANSWER,inform resp 404
/*	fp_old=fopen(OLD_FILE_INFORM_404_FAILED,"a+");
	if(fp_old==NULL) {
		fprintf(stderr,"check_old_file() is fopen() OLD_FILE_INFORM_404_FAILED error.\n");
		return -1;
	}

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

        ret=fclose(fp_old);
	if(ret==EOF) {
		fprintf(stderr,"check_old_file() is fclose() OLD_FILE_INFORM_404_FAILEDerror.\n");
		return -1;
	}
*/
	//OLD_FILE_QUEUE
/*	fp_old=fopen(OLD_FILE_QUEUE,"a+");
	if(fp_old==NULL) {
		fprintf(stderr,"check_old_file() is fopen() OLD_FILE_QUEUE error.\n");
		return -1;
	}

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

        ret=fclose(fp_old);
	if(ret==EOF) {
		fprintf(stderr,"check_old_file() is fclose() OLD_FILE_QUEUE 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[4000];
	char readbuf_tmp[4000];
	int size=4000,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,"mytail() is fopen() error %s\n",log_file);
        //      perror("mytail() is fopen() error,");
        //      exit(-1);

		if(errno==ENOENT) {
                //	sprintf(status_information,"Log file no create, Fetch_resp_400_num=%d, Fetch_resp_404_num=%d, Fetch_resp_Timeout_num=%d, Wait_voice_timeout_num=%d, Inform_resp_400_num=%d, Inform_resp_404_num=%d, Error_queue_num=%d",err_fetch_resp_400_count, err_fetch_resp_404_count, err_fetch_resp_Timeout_count, err_wait_voice_timeout_count, err_inform_resp_400_count, err_inform_resp_404_count, err_queue_count);
                //	sprintf(status_information,"Log file no create, Fetch_resp_400_num=%d, Inform_resp_400_num=%d",err_fetch_resp_400_count, err_inform_resp_400_count);
			sprintf(status_information,"Log file no create, Fetch_resp_400_1001_num=%d Fetch_resp_404_num=%d Fetch_resp_Timeout_num=%d Inform_resp_400_1004_num=%d Inform_resp_404_num=%d Inform_resp_Timeout_num=%d Http_code_500_num=%d Http_code_502_num=%d Http_code_503_num=%d",err_fetch_resp_400_count, err_fetch_resp_404_count, err_fetch_resp_Timeout_count, err_inform_resp_400_count, err_inform_resp_404_count, err_inform_resp_Timeout_count, err_http_code_500_count, err_http_code_502_count, err_http_code_503_count);

                //	sprintf(performance_data,"Fetch_resp_400_num=%d;;;; Fetch_resp_404_num=%d;;;; Fetch_resp_Timeout_num=%d;;;; Wait_voice_timeout_num=%d;;;; Inform_resp_400_num=%d;;;; Inform_resp_404_num=%d;;;; Error_queue_num=%d;;;;",err_fetch_resp_400_count, err_fetch_resp_404_count, err_fetch_resp_Timeout_count, err_wait_voice_timeout_count, err_inform_resp_400_count, err_inform_resp_404_count, err_queue_count);
                //	sprintf(performance_data,"Fetch_resp_400_num=%d;;;; Inform_resp_400_num=%d;;;;",err_fetch_resp_400_count, err_inform_resp_400_count);
			sprintf(performance_data,"Fetch_resp_400_1001_num=%d;;;; Fetch_resp_404_num=%d;;;; Fetch_resp_Timeout_num=%d;;;; Inform_resp_400_1004_num=%d;;;; Inform_resp_404_num=%d;;;; Inform_resp_Timeout_num=%d;;;; Http_code_500_num=%d;;;; Http_code_502_num=%d;;;; Http_code_503_num=%d;;;;",err_fetch_resp_400_count, err_fetch_resp_404_count, err_fetch_resp_Timeout_count, err_inform_resp_400_count, err_inform_resp_404_count, err_inform_resp_Timeout_count,err_http_code_500_count, err_http_code_502_count, err_http_code_503_count);

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

                	exit(exitstatus);
		} else {
                        exitstatus=WARNING;
                        printf("parse() fopen() other error.\n");

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

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

/*
		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");

			//fetch resp 400
		//	if(strstr(readbuf,"(400)") && err_400_mark==0) {
		//	if(strstr(readbuf,"(400)") && strstr(readbuf,"fetchRoutingData]Response") && err_fetch_resp_400_mark==0) {
			if(strstr(readbuf,"myHttpWebRequest") && strstr(readbuf,">1001<") && strstr(readbuf,"routingdata") && err_fetch_resp_400_mark==0) {
			//	printf("-----------------\n");

				if(strcmp(error_str_old_fetch_resp_400,readbuf)) {
					err_fetch_resp_400_line=line+1;
					err_fetch_resp_400_count++;

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

					if(err_fetch_resp_400_count==1) {
						ret=write_old_file(OLD_FILE_FETCH_400_FAILED,error_str_now_fetch_resp_400);
						if(ret==-1) {
							fprintf(stderr,"parse_log_file() is write_old_file() error_str_now_fetch_resp_400 error.\n");
							return -1;
						}
					}

				}
				else {
					err_fetch_resp_400_mark=1;
				}

			}

			//fetch resp 404
		//	if(strstr(readbuf,"(404)") && strstr(readbuf,"fetchRoutingData]Response") && err_fetch_resp_404_mark==0) {
			if(strstr(readbuf,"远程服务器返回错误: (404)") && strstr(readbuf,"routingdata") && err_fetch_resp_404_mark==0) {
			//	printf("-----------------\n");

				if(strcmp(error_str_old_fetch_resp_404,readbuf)) {
					err_fetch_resp_404_line=line+1;
					err_fetch_resp_404_count++;

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

					if(err_fetch_resp_404_count==1) {
						ret=write_old_file(OLD_FILE_FETCH_404_FAILED,error_str_now_fetch_resp_404);
						if(ret==-1) {
							fprintf(stderr,"parse_log_file() is write_old_file() error_str_now_fetch_resp_404 error.\n");
							return -1;
						}
					}

				}
				else {
					err_fetch_resp_404_mark=1;
				}

			}

			// fetch resp Timeout
		//	if(strstr(readbuf,"Timeout") && strstr(readbuf,"fetchRoutingData]Response") && err_fetch_resp_Timeout_mark==0) {
			if(strstr(readbuf,"myHttpWebRequest") && strstr(readbuf,"操作超时") && strstr(readbuf,"routingdata") && err_fetch_resp_Timeout_mark==0) {
			//	printf("-----------------\n");

				if(strcmp(error_str_old_fetch_resp_Timeout,readbuf)) {
					err_fetch_resp_Timeout_line=line+1;
					err_fetch_resp_Timeout_count++;

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

					if(err_fetch_resp_Timeout_count==1) {
						ret=write_old_file(OLD_FILE_FETCH_TIMEOUT_FAILED,error_str_now_fetch_resp_Timeout);
						if(ret==-1) {
							fprintf(stderr,"parse_log_file() is write_old_file() error_str_now_fetch_resp_Timeout error.\n");
							return -1;
						}
					}

				}
				else {
					err_fetch_resp_Timeout_mark=1;
				}

			}

			// wait voice timeout
		//	if(strstr(readbuf,"timeout") && strstr(readbuf,"wait call arrive") && err_voice_timeout_mark==0) {
		/*	if(strstr(readbuf,"wait call arrive timeout") && err_wait_voice_timeout_mark==0) {
			//	printf("-----------------\n");

				if(strcmp(error_str_old_wait_voice_timeout,readbuf)) {
					err_wait_voice_timeout_line=line+1;
					err_wait_voice_timeout_count++;

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

					if(err_wait_voice_timeout_count==1) {
						ret=write_old_file(OLD_FILE_VOICE_TIMEOUT_FAILED,error_str_now_wait_voice_timeout);
						if(ret==-1) {
							fprintf(stderr,"parse_log_file() is write_old_file() error_str_now_fetch_resp_Timeout error.\n");
							return -1;
						}
					}

				}
				else {
					err_wait_voice_timeout_mark=1;
				}

			}
*/
			//inform resp 400
		//	if(strstr(readbuf,"(400)") && strstr(readbuf,"informRoutingResult]Response") && err_inform_resp_400_mark==0) {
			if(strstr(readbuf,"myHttpWebRequest") && strstr(readbuf,">1004<") && strstr(readbuf,"routingresult") && err_inform_resp_400_mark==0) {
			//	printf("-----------------\n");

				if(strcmp(error_str_old_inform_resp_400,readbuf)) {
					err_inform_resp_400_line=line+1;
					err_inform_resp_400_count++;

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

					if(err_inform_resp_400_count==1) {
						ret=write_old_file(OLD_FILE_INFORM_400_FAILED,error_str_now_inform_resp_400);
						if(ret==-1) {
							fprintf(stderr,"parse_log_file() is write_old_file() error_str_now_inform_resp_400 error.\n");
							return -1;
						}
					}

				}
				else {
					err_inform_resp_400_mark=1;
				}

			}	

			//inform resp 404
		//	if(strstr(readbuf,"(404)") && strstr(readbuf,"informRoutingResult]Response") && err_inform_resp_404_mark==0) {
			if(strstr(readbuf,"远程服务器返回错误: (404)") && strstr(readbuf,"routingresult") && err_inform_resp_404_mark==0) {
			//	printf("-----------------\n");

				if(strcmp(error_str_old_inform_resp_404,readbuf)) {
					err_inform_resp_404_line=line+1;
					err_inform_resp_404_count++;
					//	printf("readbuf=%s\n",readbuf);
					strcpy(error_str_now_inform_resp_404,readbuf);
				//	printf("error_str_now_404=%s\n",error_str_now_404);

					if(err_inform_resp_404_count==1) {
						ret=write_old_file(OLD_FILE_INFORM_404_FAILED,error_str_now_inform_resp_404);
						if(ret==-1) {
							fprintf(stderr,"parse_log_file() is write_old_file() error_str_now_inform_resp_404 error.\n");
							return -1;
						}
					}
				}
				else {
					err_inform_resp_404_mark=1;
				}
			}

			// inform resp Timeout
		//	if(strstr(readbuf,"Timeout") && strstr(readbuf,"fetchRoutingData]Response") && err_fetch_resp_Timeout_mark==0) {
			if(strstr(readbuf,"myHttpWebRequest") && strstr(readbuf,"操作超时") && strstr(readbuf,"routingresult") && err_inform_resp_Timeout_mark==0) {
			//	printf("-----------------\n");

				if(strcmp(error_str_old_inform_resp_Timeout,readbuf)) {
					err_inform_resp_Timeout_line=line+1;
					err_inform_resp_Timeout_count++;

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

					if(err_inform_resp_Timeout_count==1) {
						ret=write_old_file(OLD_FILE_INFORM_TIMEOUT_FAILED,error_str_now_inform_resp_Timeout);
						if(ret==-1) {
							fprintf(stderr,"parse_log_file() is write_old_file() error_str_now_inform_resp_Timeout error.\n");
							return -1;
						}
					}

				}
				else {
					err_inform_resp_Timeout_mark=1;
				}

			}

			// http resp 500
			if(strstr(readbuf,"远程服务器返回错误: (500) 内部服务器错误") && err_http_code_500_mark==0) {
			//	printf("-----------------\n");

				if(strcmp(error_str_old_http_code_500,readbuf)) {
					err_http_code_500_line=line+1;
					err_http_code_500_count++;

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

					if(err_http_code_500_count==1) {
						ret=write_old_file(OLD_FILE_HTTP_CODE_500_FAILED,error_str_now_http_code_500);
						if(ret==-1) {
							fprintf(stderr,"parse_log_file() is write_old_file() error_str_now_http_code_500 error.\n");
							return -1;
						}
					}

				}
				else {
					err_http_code_500_mark=1;
				}

			}

			// http resp 502
			if(strstr(readbuf,"远程服务器返回错误: (502) 错误的网关") && err_http_code_502_mark==0) {
			//	printf("-----------------\n");

				if(strcmp(error_str_old_http_code_502,readbuf)) {
					err_http_code_502_line=line+1;
					err_http_code_502_count++;

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

					if(err_http_code_502_count==1) {
						ret=write_old_file(OLD_FILE_HTTP_CODE_502_FAILED,error_str_now_http_code_502);
						if(ret==-1) {
							fprintf(stderr,"parse_log_file() is write_old_file() error_str_now_http_code_502 error.\n");
							return -1;
						}
					}

				}
				else {
					err_http_code_502_mark=1;
				}

			}

			// http resp 503
			if(strstr(readbuf,"远程服务器返回错误: (503) 服务器不可用") && err_http_code_503_mark==0) {
			//	printf("-----------------\n");

				if(strcmp(error_str_old_http_code_503,readbuf)) {
					err_http_code_503_line=line+1;
					err_http_code_503_count++;

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

					if(err_http_code_503_count==1) {
						ret=write_old_file(OLD_FILE_HTTP_CODE_503_FAILED,error_str_now_http_code_503);
						if(ret==-1) {
							fprintf(stderr,"parse_log_file() is write_old_file() error_str_now_http_code_503 error.\n");
							return -1;
						}
					}

				}
				else {
					err_http_code_503_mark=1;
				}

			}
			//check queue
		/*	if(strstr(readbuf,"队列已满") && err_queue_mark==0) {
			//	printf("-----------------\n");

				if(strcmp(error_str_old_queue,readbuf)) {
					err_queue_line=line+1;
					err_queue_count++;
					//	printf("readbuf=%s\n",readbuf);
					strcpy(error_str_now_queue,readbuf);
				//	printf("error_str_now_404=%s\n",error_str_now_404);

					if(err_queue_count==1) {
						ret=write_old_file(OLD_FILE_QUEUE,error_str_now_queue);
						if(ret==-1) {
							fprintf(stderr,"parse_logfile() is write_old_file() error_str_now_queue error.\n");
							return -1;
						}
					}
				}
				else {
					err_queue_mark=1;
				}
			}
*/

	//	}

	//	if(err_fetch_resp_400_mark==1 && err_fetch_resp_404_mark==1 && err_fetch_resp_Timeout_mark==1 && err_wait_voice_timeout_mark==1 && err_inform_resp_400_mark==1 && err_inform_resp_404_mark==1 && err_queue_mark==1) {
		if(err_fetch_resp_400_mark==1 && err_fetch_resp_404_mark==1 && err_fetch_resp_Timeout_mark==1 && err_inform_resp_400_mark==1 && err_inform_resp_404_mark==1 && err_inform_resp_Timeout_mark==1 && err_http_code_500_mark==1 && err_http_code_502_mark==1 && err_http_code_503_mark==1) {
			break;
		}
	}

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

//	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,"mytail() is fclose() error\n");
	}

	return 0;
}

int main(void) {
	int fd,ret;

	char if8_log_file[128];

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

/*
	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)-86400;
        timestamp=time(NULL);
        p1=localtime(×tamp);

//	sprintf(if8_log_file,"%s/log%d%02d%02d.Log",LOG_FILE_DIR,1900+p1->tm_year,1+p1->tm_mon,p1->tm_mday);
	sprintf(if8_log_file,"%s/UltraRouter%d%02d%02d.log",LOG_FILE_DIR,1900+p1->tm_year,1+p1->tm_mon,p1->tm_mday);
////	printf("if8_log_file=%s\n",if8_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);

	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);
	}
//	printf("error_str_old_400=%s\n",error_str_old_400);
//	printf("error_str_old_404=%s\n",error_str_old_404);

	ret=parse_log_file(if8_log_file);
	if(ret==-1) {
		fprintf(stderr,"mytail() 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_400_count>=10 && err_400_count<=20 || err_404_count>=10 && err_404_count<=20) {
//	if(err_fetch_resp_400_count>=10 && err_fetch_resp_400_count<=20 || err_fetch_resp_404_count>=10 && err_fetch_resp_404_count<=20 || err_fetch_resp_Timeout_count>=10 && err_fetch_resp_Timeout_count<=20 || err_wait_voice_timeout_count>=20 && err_wait_voice_timeout_count<=30 || err_inform_resp_400_count>=10 && err_inform_resp_400_count<=20  || err_inform_resp_404_count>=10 && err_inform_resp_404_count<=20) {
	if(err_fetch_resp_400_count>=10 && err_fetch_resp_400_count<=20 || err_fetch_resp_404_count>=10 && err_fetch_resp_404_count<=20 || err_fetch_resp_Timeout_count>=10 && err_fetch_resp_Timeout_count<=20 || err_inform_resp_400_count>=10 && err_inform_resp_400_count<=20 || err_inform_resp_404_count>=10 && err_inform_resp_404_count<=20 || err_inform_resp_Timeout_count>=10 && err_inform_resp_Timeout_count<=20 || err_http_code_500_count>=10 && err_http_code_500_count<=20 || err_http_code_502_count>=10 && err_http_code_502_count<=20 || err_http_code_503_count>=10 && err_http_code_503_count<=20) {
		exitstatus=WARNING;
	}
//	else if(err_400_count>11 && err_400_count<=20 || err_404_count>11 && err_404_count<=20 || err_queue_count>=1) {
//	else if(err_fetch_resp_400_count>20 || err_fetch_resp_404_count>20 || err_fetch_resp_Timeout_count>20 || err_wait_voice_timeout_count>30 || err_inform_resp_400_count>20 || err_inform_resp_404_count>20 || err_queue_count>0) {
	else if(err_fetch_resp_400_count>20 || err_fetch_resp_404_count>20 || err_fetch_resp_Timeout_count>20 || err_inform_resp_400_count>20 || err_inform_resp_404_count>20 || err_inform_resp_Timeout_count>20 || err_http_code_500_count>20 || err_http_code_502_count>20 || err_http_code_503_count>20) {
		exitstatus=CRITICAL;
	}

//	sprintf(status_information,"Current Fetch_resp_400_num=%d, Fetch_resp_404_num=%d, Fetch_resp_Timeout_num=%d, Wait_voice_timeout_num=%d, Inform_resp_400_num=%d, Inform_resp_404_num=%d, Error_queue_num=%d",err_fetch_resp_400_count, err_fetch_resp_404_count, err_fetch_resp_Timeout_count, err_wait_voice_timeout_count, err_inform_resp_400_count, err_inform_resp_404_count, err_queue_count);
	sprintf(status_information,"Current Fetch_resp_400_1001_num=%d Fetch_resp_404_num=%d Fetch_resp_Timeout_num=%d Inform_resp_400_1004_num=%d Inform_resp_404_num=%d Inform_resp_Timeout_num=%d Http_code_500_num=%d Http_code_502_num=%d Http_code_503_num=%d",err_fetch_resp_400_count, err_fetch_resp_404_count, err_fetch_resp_Timeout_count, err_inform_resp_400_count, err_inform_resp_404_count, err_inform_resp_Timeout_count, err_http_code_500_count, err_http_code_502_count, err_http_code_503_count);

//	sprintf(performance_data,"Fetch_resp_400_num=%d;;;; Fetch_resp_404_num=%d;;;; Fetch_resp_Timeout_num=%d;;;; Wait_voice_timeout_num=%d;;;; Inform_resp_400_num=%d;;;; Inform_resp_404_num=%d;;;; Error_queue_num=%d;;;;",err_fetch_resp_400_count, err_fetch_resp_404_count, err_fetch_resp_Timeout_count, err_wait_voice_timeout_count, err_inform_resp_400_count, err_inform_resp_404_count, err_queue_count);
	sprintf(performance_data,"Fetch_resp_400_1001_num=%d;;;; Fetch_resp_404_num=%d;;;; Fetch_resp_Timeout_num=%d;;;; Inform_resp_400_1004_num=%d;;;; Inform_resp_404_num=%d;;;; Inform_resp_Timeout_num=%d;;;; Http_code_500_num=%d;;;; Http_code_502_num=%d;;;; Http_code_503_num=%d;;;;",err_fetch_resp_400_count, err_fetch_resp_404_count, err_fetch_resp_Timeout_count, err_inform_resp_400_count, err_inform_resp_404_count, err_inform_resp_Timeout_count,err_http_code_500_count, err_http_code_502_count, err_http_code_503_count);

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

	return exitstatus;
}
时间: 2024-10-17 04:46:03

nagios插件之监控if8接口日志(新接口)的相关文章

nagios插件之监控if8接口日志

vi check_if8_log.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <errno.h> #define OK 0 #define WARNING 1 #define CRITICAL 2 #define UNKNOWN 3 #define LEN 400000 int exitstatus=OK; char

nagios插件之监控多个tomcat线程数

vi check_tomcat_threads.c #include <stdio.h> #include <stdlib.h> #include <string.h> #define OK 0 #define WARNING 1 #define CRITICAL 2 #define UNKNOWN 3 #define LEN 1000 #define MIN_LEN 100 #define CMD1 "ps -efL | grep apache-tomcat

nagios插件之监控f5pool中cngw主机session数

使用方法:f5_session_cngw_prod -H host -o oid vi f5_session_cngw_prod.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define OK 0 #define WARNING 1 #define CRITICAL 2 #define UNKNOWN 3 #define LEN 1000

自定义nagios插件实现主动被动模式以及nagios基于mail的简单告警

nagios插件程序提供两个返回值:一个是插件的退出状态码,另一个是插件在控制台上打印的第一行数据.退出状态码可以被nagios主程序 作为判断被监控系统服务状态的依据,控制台打印的第一行数据可以被nagios主程序作为被监控系统服务状态的补充说明 会显示在管理页面里面. 为了管理nagios插件,nagios每查询一个服务的状态时,就会产生一个子进程,并且它使用来自该命令的输出和退出状态码来 确定具体的状态.nagios主程序可识别的状态码和说明如下: OK            退出代码 0

实战Nagios NSCA方式监控Linux系统资源使用情况 -- Nagios配置篇 -- 被监控端

Nagios要求被监控端按照约定格式定时将数据发送到Nagios端.监控包括节点和服务2种. 节点监控约定数据格式如下: [<timestamp>] PROCESS_HOST_CHECK_RESULT;<host_name>;<host_status>;<plugin_output> 格式很容易理解,数据提交时间戳,被监控节点名称,节点状态(UP/DOWN/UNREARCHABLE),插件自定义的额外数据.状态具体每个字段的解释如下: 1. timestam

Nagios利用NSClient++监控Windows主机(4)

在Nagios的libexec下有check_nt这个插件,它就是用来检查windows机器的服务的.其功能类似于check_nrpe.不过还需要搭配另外一个软件NSClient++,它则类似于NRPE.我们需要下载NSClient合适的版本,然后安装在被监控的windows主机上. Overview of NRPE NSClient++的原理如下图: 可以看到NSClient与nrpe最大的区别就是: NRPE: 被监控机上安装有nrpe,并且还有插件,最终的监控是由这些插件来进行的.当监控主

Nagios中NSClient++监控Windows主机

Nagios中自身是不提供相应的监控,它实现监控是依赖于SNMP协议或者是相关的插件来完成相应的监控的,而平时为了部署快速方便,在Linux主机上一般多用NRPE扩展,而在Windows的主机上使用比较多的是NSClient++扩展,两者的区别是NRPE还是通过插件完成监控,而NSClient++没有插件,是由Nagios监控主机发送监控请求给NSClient++来完成监控任务的,使用NSClient++可以满足日常的Windows主机监控,但是缺点是不够灵活.这个具体可以参看:http://j

监控利器Nagios之一:监控本地NFS和外部HTTP、MySQL服务

监控利器Nagios之一:监控本地NFS和外部HTTP.MySQL服务 Nagios是一款开源的免费网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员,在状态恢复后发出正常的邮件或短信通知. Nagios的特点: 1.监控服务http.MySQL.nfs.tcp.ping等 2.监控主机资源cpu.负载.I/O.虚拟及内存磁盘利用率等 3.支持邮件微信等报警通信. 4.可选we

nagios 插件ndoutils 安装配置

nagios 插件ndoutils 安装配置 原文地址:http://www.cnblogs.com/caoguo/p/5022645.html # Nagios install ndoutils # 安装ndoutils插件 [[email protected] ~]# yum install -y mysql-devel perl-DBD-MySQL [[email protected] ~]# wget http://nchc.dl.sourceforge.net/project/nagi