nagios插件之监控日志中index增长情况
来源:互联网 发布:java输入scanner 编辑:程序博客网 时间:2024/06/05 15:08
vi check_cdstore_log_3.c
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <errno.h>#define OK 0 #define WARNING 1 #define CRITICAL 2 #define UNKNOWN 3 #define LEN 40960#define LEN_S 10240int exitstatus=OK;char *exit_status[4]={"OK","WARNING","CRITICAL","UNKNOWN"}; char status_information[LEN];char performance_data[LEN];//#define LOG_FILE_DIR "/home/nagios/check_wcar_log/113"//#define LOG_FILE_DIR "/opt/smb_win_113_huapu_intface/gateWay/log"//#define LOG_FILE_DIR "/home/neo/check_log/aes"//#define LOG_FILE_DIR "/home/neo/check_log/tomcat_log"#define LOG_FILE "/opt/bmw/apache-tomcat-7.0.41/logs/bmw_mall.log"//#define LOG_FILE "/tmp/bmw_mall.log"//#define OLD_FILE_FAILED "/home/nagios/check_wcar_log/113/log_err_tmp.file"//#define OLD_FILE_FAILED "/home/neo/check_log/aes/log_tmp.file"//#define OLD_FILE_FAILED "/home/neo/check_log/bmw_cdstore/log_tmp.file"//#define OLD_FILE_FAILED "/home/nagios/check_log/log_tmp.file"#define OLD_FILE_FAILED "/tmp/log_tmp.file"//#define LONG_TIME_FILE "/tmp/log_tmp_long.file"//#define SHORT_TIME_FILE "/tmp/log_tmp_short.file"#define LONG_TIME_FILE "/home/nagios/check_log/log_tmp_long.file"#define SHORT_TIME_FILE "/home/nagios/check_log/log_tmp_short.file"int hour;char count[10]={0};char tomcat_log_str_now[LEN_S]={0};char tomcat_log_str_old[LEN_S]={0};int error_mark=0;int error_mark_total=0;char old_statusIndex[30]="0";char new_statusIndex[30]="0";int start_time_mark=0;char start_time[100]={0};char start_time_1[100]={0};char start_time_2[100]={0};char start_time_timestamp[100]={0};int response_time_mark=0;char response_time[100]={0};char response_time_1[100]={0};char response_time_2[100]={0};char response_time_timestamp[100]={0};int resp_gthan_3s=0;int resp_lthan_3s=0;int resp_total_count=0;int resp_total_count_error=0;//char today_start_time[32];int all_line=0;int err_line=0;int err_count=0;int err_mark=0;int date_to_timestamp(char *time_str,char *time_timestamp) { int ret; time_t timep1; struct tm p1;// printf("time_str=%s\n",time_str); sscanf(time_str,"%d-%d-%d %d:%d:%d",&p1.tm_year,&p1.tm_mon,&p1.tm_mday,&p1.tm_hour,&p1.tm_min,&p1.tm_sec); p1.tm_year=p1.tm_year-1900; p1.tm_mon=p1.tm_mon-1; timep1=mktime(&p1); ret=sprintf(time_timestamp,"%ld",timep1); if(ret==-1) { fprintf(stderr,"date_to_timestamp sprintf error.\n"); return -1; } return 0;}int check_old_file(int hour) { int ret,n=0; FILE *fp_old; char readbuf[LEN_S];/* //OLD_FILE_FAILED fp_old=fopen(OLD_FILE_FAILED,"a+"); if(fp_old==NULL) { fprintf(stderr,"check_old_file() is fopen() error.\n"); return -1; }*/ if(hour>=8 && hour<=18) { fp_old=fopen(SHORT_TIME_FILE,"a+"); // printf("check_old_file() read %s file.\n",SHORT_TIME_FILE); if(fp_old==NULL) { fprintf(stderr,"check_old_file() is fopen() SHORT_TIME_FILE error.\n"); return -1; } } else { fp_old=fopen(LONG_TIME_FILE,"a+"); // printf("check_old_file() read %s file.\n",LONG_TIME_FILE); if(fp_old==NULL) { fprintf(stderr,"check_old_file() is fopen() LONG_TIME_FILE 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; } else { while(fgets(readbuf,LEN_S,fp_old)!=NULL) { n++; if(n==1) { strcpy(count,readbuf); } else if(n==2){ strcpy(tomcat_log_str_old,readbuf); sscanf(tomcat_log_str_old,"%*s%*[^:]:%s",old_statusIndex); } } // printf("count=%s,tomcat_log_str_old=%s,old_statusIndex=%s\n",count,tomcat_log_str_old,old_statusIndex); } ret=fclose(fp_old); if(ret==EOF) { fprintf(stderr,"check_old_file() is fclose() error.\n"); return -1; }// printf("tomcat_log_str_old=%s,log_strlen=%d,log_sizeof=%d\n",tomcat_log_str_old,strlen(tomcat_log_str_old),sizeof(tomcat_log_str_old));// printf("-------------------------\n"); return 0;}int write_old_file(int count,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() fopen() %s error.\n",old_file); } ret=fprintf(fp_old,"%d\n%s",count,error_str); if(ret<0) { fprintf(stderr,"write_old_file() fprintf() %d and %s error.\n",count,old_file); return -1; }/* ret=fprintf(fp_old,"%s",error_str); if(ret<0) { fprintf(stderr,"write_old_file() fprintf() %s error.\n",old_file); return -1; }*/ ret=fclose(fp_old); if(ret==EOF) { fprintf(stderr,"write_old_file() fclose() %s error.\n",old_file); } return 0;}/*int write_old_file(int hour,int count,char *old_file,char *error_str) { int ret; FILE *fp_old; char readbuf[LEN_S]; fp_old=fopen(old_file,"a+"); if(fp_old==NULL) { fprintf(stderr,"write_old_file() is fopen() error.\n"); }*///ret=write_old_file(hour,0,SHORT_TIME_FILE,tomcat_log_str_now);/* if(hour>=8 && hour<=18) { fp_old=fopen(SHORT_TIME_FILE,"a+"); printf("write_old_file() read %s \n",SHORT_TIME_FILE); if(fp_old==NULL) { fprintf(stderr,"write_old_file() fopen() SHORT_TIME_FILE error.\n"); return -1; } } else { fp_old=fopen(LONG_TIME_FILE,"a+"); printf("write_old_file() read %s file.\n",LONG_TIME_FILE); if(fp_old==NULL) { fprintf(stderr,"write_old_file() fopen() LONG_TIME_FILE error.\n"); return -1; } }*//* fp_old=fopen(old_file,"w+"); printf("write_old_file() open %s file.\n",old_file); if(fp_old==NULL) { fprintf(stderr,"write_old_file() read %s \n",old_file); return -1; } ret=fprintf(fp_old,"%s",error_str); if(ret<0) { fprintf(stderr,"write_old_file() if fprintf() fp_old error.\n"); return -1; } ret=fseek(fp_old,0,SEEK_SET); if(ret==-1) { fprintf(stderr,""); return -1; } else {*/ /* while(fgets(readbuf,LEN_S,fp_old)!=NULL) { if(readbuf=="") { printf("111111111111111111111111111111111111111111111111111111111111111111111111\n"); fprintf(fp_old,"%s\n","0"); fprintf(fp_old,"%s",error_str); } else { printf("222222222222222222222222222222222222222222222222222222222222222222222222\n"); } } char *ch; ch=fgets(readbuf,LEN_S,fp_old); if(ch==NULL) { printf("111111111111111111111111111111111111111111111111111111111111111111111111\n"); fprintf(fp_old,"%d\n",count); fprintf(fp_old,"%s",error_str); } else { printf("222222222222222222222222222222222222222222222222222222222222222222222222\n"); } } 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,char *check_year,char *check_day) {int parse_log_file(char *log_file) { FILE *fp; long int *position; char readbuf[LEN_S]; char readbuf_tmp[LEN_S]; int size=1024,line=0,line_bak; char *str; int ret; int ret1; int ret2; 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); if(errno==ENOENT) { exitstatus=WARNING; // sprintf(status_information,"Log file no create, Queue_full_num=%d",err_count); sprintf(status_information,"No such file or directory."); // sprintf(performance_data,"Queue_full_num=%d;;;;",err_count); } else { exitstatus=WARNING; // sprintf(status_information,"Log file no create, Queue_full_num=%d",err_count); sprintf(status_information,"Other error."); // sprintf(performance_data,"Queue_full_num=%d;;;;",err_count); } printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data); exit(exitstatus); } 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;// printf("all_line=%d\n",all_line);// printf("position[0]=%d,position[1]=%d,position[5113]=%d,position[5114]=%d\n",position[0],position[1],position[5113],position[5114]);//position[5113]=1022496,position[5114]=1022667// fseek(fp,1022496,SEEK_SET);// fgets(readbuf,sizeof(readbuf),fp);// printf("readbuf5113=%s\n",readbuf);// fseek(fp,1022667,SEEK_SET);// fgets(readbuf,sizeof(readbuf),fp);// printf("readbuf5114=%s\n",readbuf); while(line--) {// while(line) { // printf("++++++++++++++++++line=%d\n",line); mark=0; // printf("++++++++++++++++++position[%d]=%d\n",line,position[line]); 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; } // printf("line=%d,readbuf=%s",line,readbuf);/* strcpy(readbuf_tmp,readbuf); for(p=strtok(readbuf_tmp,",\"");p;p=strtok(NULL,",\"")) { str_date=p; mark++; if(mark==12) 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,"ORA-") && err_mark==0) { // if(strstr(readbuf,"ERROR")) { // if(strstr(readbuf,"statusIndex")) { // if(strstr(readbuf,"返回结果===")) { // mark++; // } // printf("line=%d,readbuf=%s",line,readbuf); /* if(strstr(readbuf,"接口传过来BODY的JSON数据")) { if(error_mark==1) { error_mark=0; continue; } else { sscanf(readbuf,"%s %s",start_time_1,start_time_2); // printf("start_time_1=%s,start_time_2=%s\n",start_time_1,start_time_2); sprintf(start_time,"%s %s",start_time_1,start_time_2); // printf("start_time=%s\n",start_time); ret1=date_to_timestamp(start_time,start_time_timestamp); // printf("start_time_timestamp=%s\n",start_time_timestamp); start_time_mark=1; } } else if(strstr(readbuf,"回结果===")) { sscanf(readbuf,"%s %s",response_time_1,response_time_2); // printf("response_time_1=%s,response_time_2=%s\n",response_time_1,response_time_2); sprintf(response_time,"%s %s",response_time_1,response_time_2); // printf("response_time=%s\n",response_time); ret1=date_to_timestamp(response_time,response_time_timestamp); // printf("response_time_timestamp=%s\n",response_time_timestamp); response_time_mark=1; } if(start_time_mark==1 && response_time_mark==1) { ret2=atoi(response_time_timestamp)-atoi(start_time_timestamp); // printf("---------------------retponse time=%d\n",atoi(response_time_timestamp)-atoi(start_time_timestamp)); // printf("---------------------retponse time=%d\n",ret2); if(ret2>3) { resp_gthan_3s++; } else { resp_lthan_3s++; } resp_total_count++; start_time_mark=0; response_time_mark=0; } */ // if(strstr(readbuf,"ERROR:")) { // if(strstr(readbuf,"Exception") && err_mark==0) { // printf("-----------------\n"); if(strstr(readbuf,"TransactionStatusServiceImpl") && strstr(readbuf,"statusIndex")) { // printf("line=%d,readbuf=%s",line,readbuf); sscanf(readbuf,"%*s%*[^:]:%s",new_statusIndex); // printf("new_statusIndex=%s\n",new_statusIndex); if(strcmp(tomcat_log_str_old,readbuf)) { err_line=line+1; err_count++; // strcat(error_str,readbuf); // printf("readbuf=%s\n",readbuf); strcpy(tomcat_log_str_now,readbuf); // printf("tomcat_log_str_now=%s\n",tomcat_log_str_now); // if(atoi(new_statusIndex)!=atoi(old_statusIndex)) { if(atoi(new_statusIndex)>atoi(old_statusIndex)) { exitstatus=OK; sprintf(status_information,"%s","statusIndex is increase"); } else { exitstatus=CRITICAL; sprintf(status_information,"%s","statusIndex is not increase"); } if(err_count==1) { if(hour>=8 && hour<=18) { ret=write_old_file(0,SHORT_TIME_FILE,tomcat_log_str_now); if(ret==-1) { fprintf(stderr,"write_old_file() SHORT_TIME_FILE error.\n"); return -1; } } else { ret=write_old_file(0,LONG_TIME_FILE,tomcat_log_str_now); if(ret==-1) { fprintf(stderr,"write_old_file() LONG_TIME_FILE error.\n"); return -1; } } /* ret=write_old_file(hour,OLD_FILE_FAILED,tomcat_log_str_now); if(ret==-1) { fprintf(stderr,"parse_log_file() is write_old_file() tomcat_log_str_now error.\n"); return -1; } */ } break; } else { if(hour>=8 && hour<=18) { if(atoi(count)>=3) { exitstatus=CRITICAL; sprintf(status_information,"%s%d","statusIndex is not increase,count=",atoi(count)); } else { exitstatus=OK; sprintf(status_information,"%s%d","statusIndex is not increase,count=",atoi(count)); } ret=write_old_file(atoi(count)+1,SHORT_TIME_FILE,readbuf); if(ret==-1) { fprintf(stderr,"write_old_file() LONG_TIME_FILE error.\n"); return -1; } /* } else { exitstatus=OK; sprintf(status_information,"%s","statusIndex is not increase"); } */ // ret=write_old_file(atoi(count)+1,SHORT_TIME_FILE,tomcat_log_str_now); /* ret=write_old_file(atoi(count)+1,SHORT_TIME_FILE,readbuf); ret=write_old_file(atoi(count)+1,SHORT_TIME_FILE,readbuf); if(ret==-1) { fprintf(stderr,"write_old_file() SHORT_TIME_FILE error.\n"); return -1; } */ } else { if(atoi(count)>=10) { exitstatus=CRITICAL; sprintf(status_information,"%s%d","statusIndex is not increase,count=",atoi(count)); } else { exitstatus=OK; sprintf(status_information,"%s%d","statusIndex is not increase,count=",atoi(count)); } // ret=write_old_file(atoi(count)+1,LONG_TIME_FILE,tomcat_log_str_now); ret=write_old_file(atoi(count)+1,LONG_TIME_FILE,readbuf); if(ret==-1) { fprintf(stderr,"write_old_file() LONG_TIME_FILE error.\n"); return -1; } } break; // sprintf(status_information,"resp_lthan_3s=%d,resp_gthan_3s=%d,resp_total_count=%d,resp_total_count_error=%d",resp_lthan_3s,resp_gthan_3s,resp_total_count,resp_total_count_error); // sprintf(performance_data,"resp_lthan_3s=%d;;;; resp_gthan_3s=%d;;;; resp_total_count=%d;;;; resp_total_count_error=%d;;;;",resp_lthan_3s,resp_gthan_3s,resp_total_count,resp_total_count_error); } /* else { err_mark=1; } */ } // } // } /* if(err_mark==1) { break; } */ /* if(strstr(readbuf,check_year) && !strstr(readbuf,check_day)) { // if(!strstr(readbuf,check_day)) { // } break; } */ // line--; }// printf("err_400_count=%d,tomcat_log_str_now=%s\n",err_400_count,tomcat_log_str_now);// printf("err_404_count=%d,error_str_now_404=%s\n",err_404_count,error_str_now_404);// printf("-----------------------error_mark_total=%d\n",error_mark_total); ret=fclose(fp); if(ret==EOF) { fprintf(stderr,"parse_log_file() is fclose() error\n"); } return 0;}int main(void) { int fd,ret; char tomcat_log_file[128]; char nowtime[128]; char hostname[128];/* char my_day[32]; char my_week[32]; char my_month[32]; char my_year[32]; char check_day[64]; char check_year[32]; // char *week[]={"Sat","Sun","Mon","Tue","Wed","Thu","Fri"}; char *week[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; char *month[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; time_t timestamp; struct tm *p1; // timestamp=time(NULL)-86400; timestamp=time(NULL); p1=localtime(×tamp); //catalina.2015-09-20.log sprintf(tomcat_log_file,"%s/catalina.%d-%02d-%02d.log",LOG_FILE_DIR,1900+p1->tm_year,1+p1->tm_mon,p1->tm_mday); printf("tomcat_log_file=%s\n",tomcat_log_file);// 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); 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); printf("my_year=%s,my_month=%s,my_week=%s,my_day=%s\n",my_year,my_month,my_week,my_day);// Sat Aug 22 14:52:40 EAT 2015 sprintf(check_year,"%d",1900+p1->tm_year);// sprintf(check_day,"%s %s %02d",week[p1->tm_wday],month[p1->tm_mon],p1->tm_mday); sprintf(check_day,"%d-%02d-%02d",1900+p1->tm_year,1+p1->tm_mon,p1->tm_mday); printf("check_year=%s,check_day=%s\n",check_year,check_day); 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); printf("nowtime=%s\n",nowtime);*/ time_t timestamp; struct tm *p1; timestamp=time(NULL); p1=localtime(×tamp);// printf("tm_hour=%d\n",p1->tm_hour); hour=p1->tm_hour;// printf("hour=%d\n",hour);/* ret=gethostname(hostname,sizeof(hostname)); if(ret==-1) { fprintf(stderr,"gethostname() error.\n"); exit(-1); } printf("hostname=%s\n",hostname);*/// ret=check_old_file(); ret=check_old_file(hour); if(ret==-1) { fprintf(stderr,"check_old_file() error.\n"); exit(-1); }// printf("tomcat_log_str_old=%s\n",tomcat_log_str_old);// printf("error_str_old_404=%s\n",error_str_old_404);// ret=parse_log_file(tomcat_log_file);//// ret=parse_log_file(LOG_FILE,check_year,check_day);// ret=parse_log_file(tomcat_log_file,check_year,check_day); 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("err_400_line=%d\n",err_400_line);// printf("err_404_line=%d\n",err_404_line);// printf("all_line=%d\n",all_line);// printf("-------------------------------\n");/* if(err_count>=1) { exitstatus=CRITICAL; }*/// printf("\n------------------------resp_gthan_3s=%d,resp_lthan_3s=%d,resp_total_count=%d,resp_total_count_error=%d\n",resp_gthan_3s,resp_lthan_3s,resp_total_count,resp_total_count_error);/* if(resp_gthan_3s>=1) { exitstatus=CRITICAL; } sprintf(status_information,"resp_lthan_3s=%d,resp_gthan_3s=%d,resp_total_count=%d,resp_total_count_error=%d",resp_lthan_3s,resp_gthan_3s,resp_total_count,resp_total_count_error); sprintf(performance_data,"resp_lthan_3s=%d;;;; resp_gthan_3s=%d;;;; resp_total_count=%d;;;; resp_total_count_error=%d;;;;",resp_lthan_3s,resp_gthan_3s,resp_total_count,resp_total_count_error); printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data); return exitstatus;*/ printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data); return exitstatus;}
0 0
- nagios插件之监控日志中index增长情况
- nagios插件之监控日志中CASA接口响应时间
- nagios插件之监控POI搜索情况
- nagios插件之监控MQA日志文件
- nagios插件之监控if8接口日志
- nagios插件之监控单个tomcat日志
- nagios插件之监控oracle日志
- nagios插件之监控Asterisk日志文件--Master.cvs
- nagios插件之监控Asterisk日志文件--messages
- nagios插件之监控MQA日志文件--检查ERROR
- nagios插件之监控AES日志文件--检查ERROR
- nagios插件之监控if8接口日志(新接口)
- nagios插件之监控多个tomcat日志
- nagios插件之监控f5pool中cngw主机session数
- nagios插件之监控RTTI返回情况(模拟坐席发起)
- nagios插件之查询坐席使用情况
- nagios插件之监控web页面登陆
- nagios插件之监控获取driverID
- codeforces 755D PolandBall and Polygon(直线分割平面-树状数组优化查询)
- 【Android】给RecyclerView添加分割线的步骤
- 关于Oracle误操作--数据被Commit后的数据回退恢复(闪回)
- Tomcat(一) Tomcat是什么:Tomcat与Java技术 Tomcat与Web应用 以及 Tomcat基本框架及相关配置
- Tomcat(二) Tomcat实现:Servlet与web.xml介绍 以及 源码分析Tomcat实现细节
- nagios插件之监控日志中index增长情况
- 反思与感悟
- 总结 mysql error 1130 hy000:Host'localhost'解决方案
- Hexo学习(1) Mac中Hexo与GithubPages环境搭建
- 自圆其说并发编程之——认识线程开销
- Android中的Handler机制分析(二) MessageQueue分析
- stm32f070 软件复位
- 文章标题
- [代码实例][SQLServer]关闭连接