恒晖瑶 发表于 2019-1-14 09:45:19

nagios插件之监控Asterisk日志文件

  监控Asterisk日志文件messages,过滤“ERROR”和"WARNING"字符串,统计包含该字符串的行数
  

  vi check_ast_msg_log.c
  

   view plaincopy
  #include
  #include
  #include
  #include
  

  #define OK       0
  #define WARNING1
  #define CRITICAL 2
  #define UNKNOWN3
  

  #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={0};
  char error_str_old_failed={0};
  

  char warning_str_now_failed={0};
  char warning_str_old_failed={0};
  

  

  char month_day;
  

  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;
  

  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(ret0 && 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;
  

  char send_mail_cmd;
  

  char nowtime;
  char hostname;
  

  int exitstatus=OK;
  char *exit_status={"OK","WARNING","CRITICAL","UNKNOWN"};
  

  char status_information;
  char performance_data;
  

  char my_day;
  char my_week;
  char my_month;
  char my_year;
  

  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);
  sprintf(my_month,"%s",month);
  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_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,status_information,performance_data);
  

  exit(0);
  }
  




页: [1]
查看完整版本: nagios插件之监控Asterisk日志文件