监控oracle的alert日志文件的ORA-字符串,有则告警

vi alert_log_check.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <unistd.h>

#define LEN 102400
#define HISPORT 10010

#define OLD_FILE "/home/mtvwap/tmp.file"
#define LOG_FILE "/SERVERLOG/ALERT_LOG/alert_p650ora.log"

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 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;
        FILE *fp_old;
        char c;
        int line=0;
        off_t begin=0,end=-1;

        int i;
        char *str;
        int mark=0;
        char readbuf[1024];

        int ret;
        int fd;

        fd=open(log_file,O_RDONLY);
        if(fd==-1) {
                fprintf(stderr,"open() error %s\n",LOG_FILE);
                exit(-1);
        }

        while(begin=lseek(fd,end,SEEK_END)>=0) {
                if(read(fd,&c,1) != 1) { /* if read ok,ptr next*/
                        perror("read");
                        return -1;
                }

                fp=fdopen(fd,"r");
                if(fp==NULL) {
                        perror("fdopen()");
                        return -1;
                }

                if(c=='\n') {
                        if(mark==0) {
                                str=fgets(readbuf,1024,fp);
                                //      printf("readbuf=%s\n",readbuf);
                                /*
                                   if(str==NULL) {
                                        fprintf(stderr,"fgets() error.\n");
                                        return -1;
                                   }
                                 */

                                if(strstr(readbuf,"ORA-")) {
                                        mark=1;
                                }

                                if(strstr(readbuf,day) && strstr(readbuf,year)) {
                                        timeout=1;
                                        break;
                                }
                        }

                        if(mark==1)
                                line++;

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

                if(line==3) {
                        for(i=0;i<3;i++) {
                                fgets(readbuf,1024,fp);
                                        strcat(error_str,readbuf);
                                //      strcat(error_str,"\n");
                        }

                        ret=write_old_file(OLD_FILE);
                        if(ret==-1) {
                                fprintf(stderr,"write_old_file() error.\n");
                                return -1;
                        }

                        break;
                }

                end--;
        }

        ret=close(fd);
        if(fd==-1) {
                fprintf(stderr,"close() error %s\n",LOG_FILE);
                exit(-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);
        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",1900+p1->tm_year);

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

        exit(0);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值