监控Asterisk日志文件Master.cvs,过滤“FAILED”和"NO ANSWER"字符串,统计包含该字符串的行数
vi check_ast_log.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 102400
#define OLD_FILE_FAILED "/home/weihu/check_log/log_tmp_failed.file"
#define OLD_FILE_NO_ANSWER "/home/weihu/check_log/log_tmp_no_answer.file"
//#define LOG_FILE "/home/weihu/check_log/1.txt"
#define LOG_FILE "/home/weihu/check_log/Master.csv"
char error_str_now_failed[LEN]={0};
char error_str_old_failed[LEN]={0};
char error_str_now_no_answer[LEN]={0};
char error_str_old_no_answer[LEN]={0};
char today_start_time[32];
int all_line=0;
int err_line=0;
int failed_err_line=0;
int no_answer_err_line=0;
int failed_count=0;
int no_answer_count=0;
int failed_mark=0;
int no_answer_mark=0;
int check_old_file(void) {
int ret;
FILE *fp_old;
char readbuf[1024];
fp_old=fopen(OLD_FILE_FAILED,"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;
}
/*
while(fgets(readbuf,1024,fp_old)!=NULL) {
strcat(error_str_old_failed,readbuf);
}
*/
fgets(error_str_old_failed,1024,fp_old);
ret=fclose(fp_old);
if(ret==EOF) {
fprintf(stderr,"check_old_file() is fclose() error.\n");
return -1;
}
fp_old=fopen(OLD_FILE_NO_ANSWER,"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;
}
/*
while(fgets(readbuf,1024,fp_old)!=NULL) {
strcat(error_str_old_no_answer,readbuf);
}
*/
fgets(error_str_old_no_answer,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;
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);
}
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(strstr(readbuf,"Dial") && strstr(readbuf,"ANSWERED")) {
if(mark==12) break;
}
else if(strstr(readbuf,"Dial") && strstr(readbuf,"NO ANSWER")) {
if(mark==12) break;
}
else if(strstr(readbuf,"Hangup") && strstr(readbuf,"NO ANSWER")) {
if(mark==10) break;
}
else if(strstr(readbuf,"Hangup") && strstr(readbuf,"FAILED")) {
if(mark==10) 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");
if(strstr(readbuf,"FAILED") && failed_mark==0) {
// if(strstr(readbuf,"FAILED")) {
if(strcmp(error_str_old_failed,readbuf)) {
failed_err_line=line+1;
failed_count++;
// strcat(error_str,readbuf);
// printf("readbuf=%s\n",readbuf);
strcpy(error_str_now_failed,readbuf);
// printf("error_str_now_failed=%s\n",error_str_now_failed);
if(failed_count==1) {
ret=write_old_file(OLD_FILE_FAILED,error_str_now_failed);
if(ret==-1) {
fprintf(stderr,"mytail() is write_old_file() error_str_now_failed error.\n");
return -1;
}
}
}
else {
failed_mark=1;
}
}
if(strstr(readbuf,"NO ANSWER") && no_answer_mark==0) {
// if(strstr(readbuf,"NO ANSWER")) {
if(strcmp(error_str_old_no_answer,readbuf)) {
no_answer_err_line=line+1;
no_answer_count++;
// printf("readbuf=%s\n",readbuf);
strcpy(error_str_now_no_answer,readbuf);
// printf("error_str_now_no_answer=%s\n",error_str_now_no_answer);
if(no_answer_count==1) {
ret=write_old_file(OLD_FILE_NO_ANSWER,error_str_now_no_answer);
if(ret==-1) {
fprintf(stderr,"mytail() is write_old_file() error_str_now_no_answer error.\n");
return -1;
}
}
}
else {
no_answer_mark=1;
}
}
}
if(failed_mark==1 && no_answer_mark==1) {
break;
}
}
// 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);
ret=fclose(fp);
if(ret==EOF) {
fprintf(stderr,"mytail() is fclose() error\n");
}
return 0;
}
int main(void) {
int fd,ret;
int mark=0;
char if8_log_file[128];
char send_mail_cmd[LEN];
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;
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");
// 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_failed=%s\n",error_str_old_failed);
// printf("error_str_old_no_answer=%s\n",error_str_old_no_answer);
ret=parse_log_file(LOG_FILE);
if(ret==-1) {
fprintf(stderr,"mytail() 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 Failed_num=%d, No_answer_num=%d",failed_count,no_answer_count);
sprintf(performance_data,"Failed_num=%d;;;; No_answer_num=%d;;;;",failed_count,no_answer_count);
printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
return exitstatus;
}