nagios插件之监控多个tomcat日志

vi check_multi_tomcat_log_error.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

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

#define LEN 204800L
#define LEN_SHORT 200L

/*
#define LOG_FILE_7041 "/opt/bmw/apache-tomcat-7.0.41/logs/bmw_mall.log"
#define OLD_ERROR_FILE_7041 "/home/nagios/check_log_error/log_tmp_error_7041.file"

#define LOG_FILE_CUSERVICE "/opt/bmw/apache-tomcat-cuservice/logs/bmw_cuservice.log"
#define OLD_ERROR_FILE_CUSERVICE "/home/nagios/check_log_error/log_tmp_error_cuservice.file"

#define LOG_FILE_FINANCE "/opt/bmw/apache-tomcat-finance/logs/bmw_finance.log"
#define OLD_ERROR_FILE_FINANCE "/home/nagios/check_log_error/log_tmp_error_.file"

#define LOG_FILE_INTERFACE "/opt/bmw/apache-tomcat-interface/logs/bmw_mall.log"
#define OLD_ERROR_FILE_INTERFACE "/home/nagios/check_log_error/log_tmp_error_interface.file"

#define LOG_FILE_PAYMENT "/opt/bmw/apache-tomcat-payment/logs/bmw_payment.log"
#define OLD_ERROR_FILE_PAYMENT "/home/nagios/check_log_error/log_tmp_error_payment.file"
*/

char *log_file_array[]={"/opt/bmw/apache-tomcat-7.0.41/logs/bmw_mall.log","/opt/bmw/apache-tomcat-cuservice/logs/bmw_cuservice.log","/opt/bmw/apache-tomcat-finance/logs/bmw_finance.log","/opt/bmw/apache-tomcat-interface/logs/bmw_mall.log","/opt/bmw/apache-tomcat-payment/logs/bmw_payment.log"};

char *log_file_old_array[]={"/home/nagios/check_log_error/log_tmp_error_7041.file","/home/nagios/check_log_error/log_tmp_error_cuservice.file","/home/nagios/check_log_error/log_tmp_error_finance.file","/home/nagios/check_log_error/log_tmp_error_interface.file","/home/nagios/check_log_error/log_tmp_error_payment.file"};


char log_error_old_array[5][LEN_SHORT]={0};

char log_error_now_array[5][LEN_SHORT]={0};

/*
 
SW send error
startJMS error 
 
*/

char log_error_7041_old[LEN]={0};
char log_error_7041_now[LEN]={0};
int log_error_7041_count=0;
char log_error_7041_create_mark[10]={0};

char log_error_cuservice_old[LEN]={0};
char log_error_cuservice_now[LEN]={0};
int log_error_cuservice_count=0;
char log_error_cuservice_create_mark[10]={0};

char log_error_finance_old[LEN]={0};
char log_error_finance_now[LEN]={0};
int log_error_finance_count=0;
char log_error_finance_create_mark[10]={0};

char log_error_interface_old[LEN]={0};
char log_error_interface_now[LEN]={0};
int log_error_interface_count=0;
char log_error_interface_create_mark[10]={0};

char log_error_payment_old[LEN]={0};
char log_error_payment_now[LEN]={0};
int log_error_payment_count=0;
char log_error_payment_create_mark[10]={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 i=0,j=0;

char system_now_time[LEN_SHORT];
char file_now_time[LEN_SHORT];

int get_file_time(char *log_file) {
        int ret;

        struct stat buf;

        struct tm *p1;

        ret=stat(log_file, &buf);
//      ret=stat(LOG_FILE_7041, &buf);
        printf("ret=%d\n",ret);
        printf("buf.st_ctime=%d\n",buf.st_ctime);
        if(ret!=0) {
                fprintf(stderr,"stat() error.\n");
                return -1;
        }

        p1=localtime(&(buf.st_mtime));
       // printf("%d-%02d-%02d\n",p1->tm_year+1900,p1->tm_mon+1,p1->tm_mday);
        snprintf(file_now_time,LEN_SHORT,"%d-%02d-%02d",p1->tm_year+1900,p1->tm_mon+1,p1->tm_mday);
//      printf("file_now_time=%s\n",file_now_time);

        return 0;
}



int check_old_file(char *old_log_file) {
        int ret;
        FILE *fp_old;
        char readbuf[LEN_SHORT];

        time_t timestamp;
        struct tm *p1;


        //OLD_ERROR_FILE
        fp_old=fopen(old_log_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(readbuf,LEN_SHORT,fp_old);
        }

        switch (i) {  
            case 0:  
                strncpy(log_error_old_array[0],readbuf,LEN_SHORT);  
            //  printf("0000000000\n");   
                break;  
  
            case 1:  
                strncpy(log_error_old_array[1],readbuf,LEN_SHORT);  
            //  printf("1111111111\n");   
                break;  
  
            case 2:  
                strncpy(log_error_old_array[2],readbuf,LEN_SHORT);  
            //  printf("2222222222\n");   
                break;  
  
            case 3:  
                strncpy(log_error_old_array[3],readbuf,LEN_SHORT);  
            //  printf("3333333333\n");   
                break;  
  
            case 4:  
                strncpy(log_error_old_array[4],readbuf,LEN_SHORT);  
            //  printf("4444444444\n");   
                break;  
  
            default:  
            //  printf("5555555555\n");   
                break;  
        }   

/*
        switch (i) {  
            case 0:  
                strncpy(log_error_7041_old,readbuf,LEN_SHORT);  
            //  printf("0000000000\n");   
                break;  
  
            case 1:  
                strncpy(log_error_cuservice_old,readbuf,LEN_SHORT);  
            //  printf("1111111111\n");   
                break;  
  
            case 2:  
                strncpy(log_error_finance_old,readbuf,LEN_SHORT);  
            //  printf("2222222222\n");   
                break;  
  
            case 3:  
                strncpy(log_error_interface_old,readbuf,LEN_SHORT);  
            //  printf("3333333333\n");   
                break;  
  
            case 4:  
                strncpy(log_error_payment_old,readbuf,LEN_SHORT);  
            //  printf("4444444444\n");   
                break;  
  
            default:  
            //  printf("5555555555\n");   
                break;  
        }   
*/

        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) {
//      printf("i=%d,j=%d,log_file=%s\n",i,j,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;

        //new add
        int log_error_line=0;
        int log_error_count=0;
        int log_error_mark=0;

        //get file time

        //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(strstr(readbuf,"ERROR")) {
                        if(strcmp(log_error_old_array[j],readbuf)) {
                                log_error_line=line+1;
                                log_error_count++;

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

                        ////    strcpy(log_error_7041_now,readbuf);



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

                                if(log_error_count==1) {
                                        switch (j) {  
                                                case 0:  
                                                        strncpy(log_error_now_array[0],readbuf,LEN_SHORT);  
                                                        //  printf("0000000000\n");   
                                                        break;  

                                                case 1:  
                                                        strncpy(log_error_now_array[1],readbuf,LEN_SHORT);  
                                                        //  printf("1111111111\n");   
                                                        break;  

                                                case 2:  
                                                        strncpy(log_error_now_array[2],readbuf,LEN_SHORT);  
                                                        //  printf("2222222222\n");   
                                                        break;  

                                                case 3:  
                                                        strncpy(log_error_now_array[3],readbuf,LEN_SHORT);  
                                                        //  printf("3333333333\n");   
                                                        break;  

                                                case 4:  
                                                        strncpy(log_error_now_array[4],readbuf,LEN_SHORT);  
                                                        //  printf("4444444444\n");   
                                                        break;  

                                                default:  
                                                        //  printf("5555555555\n");   
                                                        break;  
                                        }   
                        /*
                                        printf("i=%d,j=%d\n",i,j);

                                        printf("\n--------------------------------------------\nlog_file=%s\n",log_file);
                                        printf("readbuf=%s\n--------------------------------------------\n",readbuf);
                        */

                                //      ret=write_old_file(OLD_ERROR_FILE_7041,log_error_7041_now);
                                        ret=write_old_file(log_file_old_array[j],log_error_now_array[j]);
                                //      printf("========i=%d,j=%d,%s,%s\n",i,j,log_file_old_array[j],log_error_now_array[j]);
                                        if(ret==-1) {
                                                fprintf(stderr,"parse() is write_old_file() log_error_7041_now error.\n");
                                                return -1;
                                        }
                                }
                        } 
                        else {
                                break;
                        }
                }

        }

        switch (j) {  
                case 0:  
                        log_error_7041_count=log_error_count;
                        //  printf("0000000000\n");   
                        break;  

                case 1:  
                        log_error_cuservice_count=log_error_count;
                        //  printf("1111111111\n");   
                        break;  

                case 2:  
                        log_error_finance_count=log_error_count;
                        //  printf("2222222222\n");   
                        break;  

                case 3:  
                        log_error_interface_count=log_error_count;
                        //  printf("3333333333\n");   
                        break;  

                case 4:  
                        log_error_payment_count=log_error_count;
                        //  printf("4444444444\n");   
                        break;  

                default:  
                        //  printf("5555555555\n");   
                        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");
        snprintf(system_now_time,LEN_SHORT,"%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);
        }


        for(i=0;i<5;i++) {
                ret=check_old_file(log_file_old_array[i]);
                if(ret!=0) {
                        fprintf(stderr,"check_old_file() error.\n");
                        exit(-1);
                }
        }

        for(j=0;j<5;j++) {
        //      get_file_time(LOG_FILE_7041);
        /*
                ret=get_file_time(log_file_array[j]);
                if(ret!=0) {
                        fprintf(stderr,"get_file_time() error.\n");
                }
        */

/*
                if(strcmp(system_now_time,file_now_time)) {
                        switch (j) {  
                                case 0:  
                                //      sprintf(log_error_7041_create_mark,"%s","1");
                                        log_error_7041_create_mark[0]="1";
                                        break;  

                                case 1:  
                                //      sprintf(log_error_cuservice_create_mark,"%s","1";
                                        log_error_cuservice_create_mark[0]="1";
                                        break;  

                                case 2:  
                                //      sprintf(log_error_finance_create_mark,"%s","1");
                                        log_error_finance_create_mark[0]="1";
                                        break;  

                                case 3:  
                                //      sprintf(log_error_interface_create_mark,"%s","1");
                                        log_error_interface_create_mark[0]="1";
                                        break;  

                                case 4:  
                                //      sprintf(log_error_payment_create_mark,"%s","1");
                                        log_error_payment_create_mark[0]="1";
                                        break;  

                                default:  
                                        break;  
                        }   
                }
*/

                ret=parse_log_file(log_file_array[j]);
                if(ret!=0) {
                        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;
        }
        */

//      printf("7041=%d,cuservice=%d,finance=%d,interface=%d,payment=%d\n",log_error_7041_count,log_error_cuservice_count,log_error_finance_count,log_error_interface_count,log_error_payment_count);

        if(log_error_7041_count>10 || log_error_cuservice_count>10 || log_error_finance_count>10 || log_error_interface_count>10 || log_error_payment_count>10) {
                exitstatus=CRITICAL;
        }
        else if(log_error_7041_count>=1 && log_error_7041_count<=10 || log_error_cuservice_count>=1 && log_error_cuservice_count<=10 || log_error_finance_count>=1 && log_error_finance_count<=10 || log_error_interface_count>=1 && log_error_interface_count<=10 || log_error_payment_count>=1 && log_error_payment_count<=10) {
                exitstatus=WARNING;
        }

        sprintf(status_information,"Current log_error_7041_count=%d,log_error_cuservice_count=%d,log_error_finance_count=%d,log_error_interface_count=%d,log_error_payment_count=%d",log_error_7041_count,log_error_cuservice_count,log_error_finance_count,log_error_interface_count,log_error_payment_count);
//      sprintf(status_information,"Current log_error_7041_count=%s,log_error_cuservice_count=%s,log_error_finance_count=%s,log_error_interface_count=%s,log_error_payment_count=%s", atoi(log_error_7041_create_mark)?"log no create":atol(log_error_7041_count), log_error_cuservice_create_mark?"log no create":atol(log_error_cuservice_count), log_error_finance_create_mark?"log no create":atol(log_error_finance_count), log_error_interface_create_mark?"log no create":atol(log_error_interface_count), log_error_payment_create_mark?"log no create":atol(log_error_payment_count));

        sprintf(performance_data,"log_error_7041_count=%d;;;; log_error_cuservice_count=%d;;;; log_error_finance_count=%d;;;; log_error_interface_count=%d;;;; log_error_payment_count=%d;;;;",log_error_7041_count,log_error_cuservice_count,log_error_finance_count,log_error_interface_count,log_error_payment_count);
//      sprintf(performance_data,"log_error_7041_count=%s;;;; log_error_cuservice_count=%s;;;; log_error_finance_count=%s;;;; log_error_interface_count=%s;;;; log_error_payment_count=%s;;;;",log_error_7041_create_mark?"log no create":atol(log_error_7041_count), log_error_cuservice_create_mark?"log no create":atol(log_error_cuservice_count), log_error_finance_create_mark?"log no create":atol(log_error_finance_count), log_error_interface_create_mark?"log no create":atol(log_error_interface_count), log_error_payment_create_mark?"log no create":atol(log_error_payment_count));

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

//      get_file_time(LOG_FILE_7041);
//      printf("system_now_time=%s,file_now_time=%s\n",system_now_time,file_now_time);
//      printf("%d\n",strcmp(system_now_time,file_now_time));

        return exitstatus;
}

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