重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
监控Asterisk日志文件messages,过滤“ERROR”和"WARNING"字符串,统计包含该字符串的行数
创新互联是专业的公安网站建设公司,公安接单;提供成都网站建设、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行公安网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
vi check_ast_msg_log.c
[cpp] view plaincopy
#include
#include
#include
#include
#define OK 0
#define WARNING 1
#define CRITICAL 2
#define UNKNOWN 3
#define LEN 1024
#define LOG_FILE "/home/weihu/check_log/ast_messages/messages"
#define OLD_FILE_ERROR "/home/weihu/check_log/ast_messages/log_tmp_error.file"
#define OLD_FILE_WARNING "/home/weihu/check_log/ast_messages/log_tmp_warning.file"
char error_str_now_failed[LEN]={0};
char error_str_old_failed[LEN]={0};
char warning_str_now_failed[LEN]={0};
char warning_str_old_failed[LEN]={0};
char month_day[32];
int all_line=0;
int err_line=0;
int error_mark=0;
int error_line=0;
int error_count=0;
int warning_mark=0;
int warning_line=0;
int warning_count=0;
int check_old_file(void) {
int ret;
FILE *fp_old;
char readbuf[1024];
fp_old=fopen(OLD_FILE_ERROR,"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;
}
// printf("%s",error_str_old);
// printf("-------------------------\n");
//
//----------------------------------------------------------------------
fp_old=fopen(OLD_FILE_WARNING,"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(warning_str_old_failed,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,"parse_log_file() is fopen() error %s\n",log_file);
perror("parse_log_file() 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("parse_log_file() is fseek()");
return -1;
}
str=fgets(readbuf,sizeof(readbuf),fp);
if(str==NULL) {
fprintf(stderr,"parse_log_file() is fgets() error.\n");
return -1;
}
// strcpy(readbuf_tmp,readbuf);
// strtok
if(strstr(readbuf,month_day)>0 && line!=1) {
// printf("-----------------\n");
if(strstr(readbuf,"ERROR") && strstr(readbuf,"5060: Connection refused") && error_mark==0) {
// if(strstr(readbuf,"ERROR") && error_mark==0) {
// printf("++++++++++++++++++++++++++++++++\nn");
// if(strstr(readbuf,"FAILED")) {
if(strcmp(error_str_old_failed,readbuf)) {
error_line=line+1;
error_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(error_count==1) {
ret=write_old_file(OLD_FILE_ERROR,error_str_now_failed);
if(ret==-1) {
fprintf(stderr,"parse_log_file() is write_old_file() error_str_now_failed error.\n");
return -1;
}
}
}
else {
error_mark=1;
}
}
// printf("error_count=%d\n",error_count);
}
//----------------------------------------------------------------------------------------------------
//
if(strstr(readbuf,month_day)>0 && line!=1) {
// printf("-----------------\n");
if(strstr(readbuf,"WARNING") && strstr(readbuf,"sip") && error_mark==0) {
// if(strstr(readbuf,"WARNING") && warning_mark==0) {
// printf("++++++++++++++++++++++++++++++++\nn");
// if(strstr(readbuf,"FAILED")) {
if(strcmp(warning_str_old_failed,readbuf)) {
warning_line=line+1;
warning_count++;
// strcat(error_str,readbuf);
// printf("readbuf=%s\n",readbuf);
strcpy(warning_str_now_failed,readbuf);
// printf("error_str_now_failed=%s\n",error_str_now_failed);
if(warning_count==1) {
ret=write_old_file(OLD_FILE_WARNING,warning_str_now_failed);
if(ret==-1) {
fprintf(stderr,"parse_log_file() is write_old_file() warning_str_now_failed error.\n");
return -1;
}
}
}
else {
warning_mark=1;
}
}
// printf("warning_count=%d\n",warning_count);
}
}
// printf("error_count=%d,error_str_now_failed=%s\n",error_count,error_str_now_failed);
// printf("warning_count=%d,warning_str_now_failed=%s\n",warning_count,warning_str_now_failed);
ret=fclose(fp);
if(ret==EOF) {
fprintf(stderr,"parse_log_file() 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];
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 timestamp;
struct tm *p1;
timestamp=time(NULL);
p1=localtime(×tamp);
// sprintf(my_day,"%02d",p1->tm_mday);
sprintf(my_day,"%d",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);
// printf("day=%s,week=%s,month=%s,year=%s\n",my_day,my_week,my_month,my_year);
sprintf(month_day,"%s %d",month[p1->tm_mon],p1->tm_mday);
// printf("%s\n",month_day);
// 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,"parse_log_file() 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(error_count>0 || warning_count>0 ) {
exitstatus=CRITICAL;
}
/*
else if(error_count>1 || warning_count>5) {
exitstatus=CRITICAL;
}
*/
sprintf(status_information,"Current Error_num=%d, Warning_num=%d",error_count,warning_count);
sprintf(performance_data,"Error_num=%d;;;; Warning_num=%d;;;;",error_count,warning_count);
printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
exit(0);
}