tomcat应用日志过滤,有ORA-则发邮件告警

来源:互联网 发布:高中英语单词录音软件 编辑:程序博客网 时间:2024/06/05 17:19

vi tomcat_server_log.c

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <sys/socket.h>#include <netinet/in.h>#define LEN 102400#define HISPORT 10010#define OLD_FILE "/home/mtvwap/check_log/test/tomcat_server_log_tmp.file"#define LOG_FILE "/home/mtvwap/check_log/test/server.log.1"char error_str[LEN];char error_str_old[LEN];char error_str_new[LEN];int timeout=0;char day[32],year[32];struct packet {        char data[LEN];};int all_line=0;int check_old_file(char *old_file) {        int ret;        FILE *fp_old;        char readbuf[1024];        fp_old=fopen(old_file,"a+");        fseek(fp_old,0,SEEK_SET);        while(fgets(readbuf,1024,fp_old)!=NULL) {                strcat(error_str_old,readbuf);        }        fclose(fp_old);//      printf("%s",error_str_old);//      printf("-------------------------\n");        return 0;}int write_old_file(char *old_file) {        int ret;        FILE *fp_old;        fp_old=fopen(old_file,"w");        if(fp_old==NULL) {                fprintf(stderr,"fopen()2 error.\n");        }        ret=fprintf(fp_old,"%s",error_str);        if(ret<0) {                fprintf(stderr,"fprintf() fp_old error.\n");                return -1;        }        fclose(fp_old);        return 0;}int mytail(char *log_file) {        FILE *fp;        long int *position;        char readbuf[1024];        int size=1024,line=0,line_bak;        char *str;        int mark=0;        int ret;        position=(long int *)malloc(sizeof(long int)*size);        position[0]=0;        fp=fopen(log_file,"r");        if(fp==NULL) {                fprintf(stderr,"fopen() error %s\n",LOG_FILE);                exit(-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);        }        line_bak=line;        all_line=line;        while(line--) {                ret=fseek(fp,position[line],SEEK_SET);                if(ret==-1) {                        perror("fseek()");                        return -1;                }                str=fgets(readbuf,sizeof(readbuf),fp);                if(str==NULL) {                        fprintf(stderr,"fgets() error.\n");                        return -1;                }        //      if(strstr(readbuf,day) && strstr(readbuf,year)) {                if(strstr(readbuf,year)) {                        timeout=1;                        all_line=line_bak-line;                        break;                }                if(strstr(readbuf,"ORA-")) {                        strcat(error_str,readbuf);                        ret=write_old_file(OLD_FILE);                        if(ret==-1) {                                fprintf(stderr,"write_old_file() error.\n");                                return -1;                        }                        all_line=line_bak-line;                        break;                }                all_line=line_bak-line;        }        ret=fclose(fp);        if(ret==-1) {                fprintf(stderr,"fclose() error %s\n",LOG_FILE);                return -1;        }        return 0;}int send_to_169() {        int sd;        int ret;        struct sockaddr_in his_end;        sd=socket(PF_INET,SOCK_STREAM,0);        if(sd==-1) {                fprintf(stderr,"socket error.\n");                return -1;        }        //      printf("sd=%d\n",sd);        his_end.sin_family=AF_INET;        his_end.sin_port=htons(HISPORT);        his_end.sin_addr.s_addr=inet_addr("10.199.75.169");        ret=connect(sd,(struct sockaddr *)&his_end,sizeof(his_end));        if(ret==-1) {                perror("connect()");                return -1;        }        ret=write(sd,error_str,strlen(error_str));        if(ret==-1) {                fprintf(stderr,"write error.\n");                return -1;        }        printf("client write %d bytes.\n",ret);        ret=close(sd);        if(ret==-1) {                fprintf(stderr,"close() error.\n");                return -1;        }        return 0;}int main(void) {        int fd,ret;        int mark=0;        char nowtime[128];        char hostname[128];        char my_day[32];        char my_week[32];        char my_month[32];        char my_year[32];        char *week[]={"Sat","Sun","Mon","Tue","Wed","Thu","Fri"};        char *month[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};        time_t timep;        struct tm *p1;        timep=time(NULL)-86400;        p1=localtime(&timep);        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);//      sprintf(day,"%s %s %d",week[p1->tm_wday],month[p1->tm_mon],p1->tm_mday-1);        sprintf(year,"%d-%02d-%02d",1900+p1->tm_year,(1+p1->tm_mon),p1->tm_mday);        printf("day=%s,year=%s\n",day,year);        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);        ret=gethostname(hostname,sizeof(hostname));        if(ret==-1) {                fprintf(stderr,"gethostname() error.\n");                exit(-1);        }        ret=check_old_file(OLD_FILE);        if(ret==-1) {                fprintf(stderr,"check_old_file() error.\n");                exit(-1);        }        ret=mytail(LOG_FILE);        if(ret==-1) {                fprintf(stderr,"mytail() error.\n");                exit(-1);        }//      strcpy(error_str_new,error_str);//      if(!strcmp(error_str_new,error_str_old)) {        if(!strcmp(error_str,error_str_old)) {                mark=0;        //      printf("log not change!\n");        }        else if(timeout==1) {                mark=0;        }        else {                mark=1;        //      printf("log is changed!\n");                ret=send_to_169();                if(ret==-1) {                        fprintf(stderr,"send_to_169() error.\n");                        exit(-1);                }        //      printf("22222222222222222222222222\n");        }        printf("mark=%d\n",mark);        if(timeout==1)                printf("timeout\n");        printf("%s\n",nowtime);        printf("hostname=%s\n",hostname);        printf("%s\n\n",error_str);        printf("all_line=%d\n",all_line);        exit(0);}
原创粉丝点击