监控MQA日志文件mqsgip.log,过滤<MO-SMS> smsgw_delivery(主动上行)、<MO-SMS> report(报告上行)、<MT-SMS>(短信下行)字符串,统计包含该字符串的行数。
vi check_mqa_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 204800L
#define LEN_SHORT 32L
#define LOG_FILE "/home/weihu/check_log/mqa/mqsgip.log"
#define OLD_FILE_MO_SMS_DELIVERY "/home/weihu/check_log/mqa/log_tmp_mo_sms_delivery.file"
#define OLD_FILE_MO_SMS_REPORT "/home/weihu/check_log/mqa/log_tmp_mo_sms_report.file"
#define OLD_FILE_MT_SMS "/home/weihu/check_log/mqa/log_tmp_mt_sms.file"
#define OLD_FILE_MO_SMS_DELIVERY_SUM "/home/weihu/check_log/mqa/log_tmp_mo_sms_delivery_sum.file"
#define OLD_FILE_MO_SMS_REPORT_SUM "/home/weihu/check_log/mqa/log_tmp_mo_sms_report_sum.file"
#define OLD_FILE_MT_SMS_SUM "/home/weihu/check_log/mqa/log_tmp_mt_sms_sum.file"
#define FILE_SUM_DATE "/home/weihu/check_log/mqa/log_tmp_file_sum_date.file"
/*
SW send error
startJMS error
*/
char mo_sms_delivery_old[LEN]={0};
char mo_sms_delivery_now[LEN]={0};
char mo_sms_report_old[LEN]={0};
char mo_sms_report_now[LEN]={0};
char mt_sms_old[LEN]={0};
char mt_sms_now[LEN]={0};
int all_line=0;
int err_line=0;
int mo_sms_delivery_line=0;
int mo_sms_report_line=0;
int mt_sms_line=0;
int mo_sms_delivery_count=0;
int mo_sms_report_count=0;
int mt_sms_count=0;
int mo_sms_delivery_mark=0;
int mo_sms_report_mark=0;
int mt_sms_mark=0;
char mo_sms_delivery_count_sum[LEN_SHORT]={0};
char mo_sms_report_count_sum[LEN_SHORT]={0};
char mt_sms_count_sum[LEN_SHORT]={0};
int mo_sms_delivery_sum=0;
int mo_sms_report_sum=0;
int mt_sms_sum=0;
char file_mqsgip_date[LEN_SHORT];
char file_sum_date[LEN_SHORT];
int check_old_file(void) {
int ret;
FILE *fp_old;
char readbuf[1024];
time_t timestamp;
struct tm *p1;
//OLD_FILE_MO_SMS_DELIVERY
fp_old=fopen(OLD_FILE_MO_SMS_DELIVERY,"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;
}
else {
fgets(mo_sms_delivery_old,1024,fp_old);
}
ret=fclose(fp_old);
if(ret==EOF) {
fprintf(stderr,"check_old_file() is fclose() error.\n");
return -1;
}
//OLD_FILE_MO_SMS_REPORT
fp_old=fopen(OLD_FILE_MO_SMS_REPORT,"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;
}
else {
fgets(mo_sms_report_old,1024,fp_old);
}
ret=fclose(fp_old);
if(ret==EOF) {
fprintf(stderr,"check_old_file() is fclose() error.\n");
return -1;
}
//OLD_FILE_MT_SMS
fp_old=fopen(OLD_FILE_MT_SMS,"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;
}
else {
fgets(mt_sms_old,1024,fp_old);
}
ret=fclose(fp_old);
if(ret==EOF) {
fprintf(stderr,"check_old_file() is fclose() error.\n");
return -1;
}
//FILE_SUM_DATE
fp_old=fopen(FILE_SUM_DATE,"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;
}
else {
ret=fgets(file_sum_date,1024,fp_old);
if(ret==0) {
timestamp=time(NULL);
p1=localtime(×tamp);
sprintf(file_sum_date,"%d-%02d-%02d",p1->tm_year+1900,p1->tm_mon+1,p1->tm_mday);
fprintf(fp_old,"%s",file_sum_date);
}
// printf("file_sum_date=%s\n",file_sum_date);
}
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;
//file_mqsgip_date
fp=fopen(log_file,"r");
if(fp==NULL) {
fprintf(stderr,"fopen() log_file error.\n");
return -1;
}
else {
ret=fread(file_mqsgip_date,1,10,fp);
if(ret!=10) {
fprintf(stderr,"fread() file_mqsgip_date error.\n");
return -1;
}
// printf("file_mqsgip_date=%s\n",file_mqsgip_date);
}
ret=fclose(fp);
if(ret==EOF) {
fprintf(stderr,"parse() is fclose() error\n");
}
//malloc
position=(long int *)malloc(sizeof(long int)*size);
position[0]=0;
fp=fopen(log_file,"r");
if(fp==NULL) {
// fprintf(stderr,"parse() is fopen() error %s\n",log_file);
perror("parse() is fopen() error,");
return -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() is fseek()");
printf("--------------\n");
return -1;
printf("--------------\n");
}
str=fgets(readbuf,sizeof(readbuf),fp);
if(str==NULL) {
fprintf(stderr,"parse() is fgets() error.\n");
return -1;
}
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,"<MO-SMS> smsgw_delivery") && mo_sms_delivery_mark==0) {
if(strcmp(mo_sms_delivery_old,readbuf)) {
mo_sms_delivery_line=line+1;
mo_sms_delivery_count++;
// strcat(error_str,readbuf);
// printf("readbuf=%s\n",readbuf);
strcpy(mo_sms_delivery_now,readbuf);
// printf("error_str_now_failed=%s\n",error_str_now_failed);
if(mo_sms_delivery_count==1) {
ret=write_old_file(OLD_FILE_MO_SMS_DELIVERY,mo_sms_delivery_now);
if(ret==-1) {
fprintf(stderr,"parse() is write_old_file() mo_sms_delivery_now error.\n");
return -1;
}
}
}
else {
mo_sms_delivery_mark=1;
}
}
if(strstr(readbuf,"<MO-SMS> report") && mo_sms_report_mark==0) {
if(strcmp(mo_sms_report_old,readbuf)) {
mo_sms_report_line=line+1;
mo_sms_report_count++;
// printf("readbuf=%s\n",readbuf);
strcpy(mo_sms_report_now,readbuf);
// printf("error_str_now_no_answer=%s\n",error_str_now_no_answer);
if(mo_sms_report_count==1) {
ret=write_old_file(OLD_FILE_MO_SMS_REPORT,mo_sms_report_now);
if(ret==-1) {
fprintf(stderr,"parse() is write_old_file() mo_sms_report_now error.\n");
return -1;
}
}
}
else {
mo_sms_report_mark=1;
}
}
if(strstr(readbuf,"<MT-SMS>") && mt_sms_mark==0) {
if(strcmp(mt_sms_old,readbuf)) {
mt_sms_line=line+1;
mt_sms_count++;
// printf("readbuf=%s\n",readbuf);
strcpy(mt_sms_now,readbuf);
// printf("error_str_now_no_answer=%s\n",error_str_now_no_answer);
if(mt_sms_count==1) {
ret=write_old_file(OLD_FILE_MT_SMS,mt_sms_now);
if(ret==-1) {
fprintf(stderr,"parse() is write_old_file() mt_sms_now error.\n");
return -1;
}
}
}
else {
mt_sms_mark=1;
}
}
// }
if(mo_sms_delivery_mark==1 && mo_sms_report_mark==1 && mt_sms_mark==1) {
break;
}
}
ret=fclose(fp);
if(ret==EOF) {
fprintf(stderr,"parse() is fclose() error\n");
}
// 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);
return 0;
}
int write_sum_file(void) {
int ret;
char readbuf[LEN_SHORT];
FILE *fp_old;
int mark=0;
//OLD_FILE_MO_SMS_DELIVERY_SUM
fp_old=fopen(OLD_FILE_MO_SMS_DELIVERY_SUM,"a+");
if(fp_old==NULL) {
fprintf(stderr,"write_sum_file() is fopen() error.\n");
return -1;
}
else {
fseek(fp_old,0,SEEK_SET);
ret=fgets(mo_sms_delivery_count_sum,LEN_SHORT,fp_old);
if(ret==NULL) {
mo_sms_delivery_count_sum[0]='0';
}
if(!strcmp(file_mqsgip_date,file_sum_date)) {
// printf("111111111111111111111111111\n");
mo_sms_delivery_sum=atoi(mo_sms_delivery_count_sum)+mo_sms_delivery_count;
}
else {
// printf("2222222222222222222222222222\n");
mo_sms_delivery_sum=mo_sms_delivery_count;
mark=1;
}
fp_old=fopen(OLD_FILE_MO_SMS_DELIVERY_SUM,"w");
fprintf(fp_old,"%d",mo_sms_delivery_sum);
// printf("mo_sms_delivery_count=%d,mo_sms_delivery_sum=%d\n",mo_sms_delivery_count,mo_sms_delivery_sum);
}
ret=fclose(fp_old);
if(ret==EOF) {
fprintf(stderr,"write_sum_file() is fclose() error.\n");
return -1;
}
//OLD_FILE_MO_SMS_REPORT_SUM
fp_old=fopen(OLD_FILE_MO_SMS_REPORT_SUM,"a+");
if(fp_old==NULL) {
fprintf(stderr,"write_sum_file() is fopen() error.\n");
return -1;
}
else {
fseek(fp_old,0,SEEK_SET);
ret=fgets(mo_sms_report_count_sum,LEN_SHORT,fp_old);
if(ret==NULL) {
mo_sms_report_count_sum[0]='0';
}
if(mark==0) {
mo_sms_report_sum=atoi(mo_sms_report_count_sum)+mo_sms_report_count;
}
else {
mo_sms_report_sum=mo_sms_report_count;
}
fp_old=fopen(OLD_FILE_MO_SMS_REPORT_SUM,"w");
fprintf(fp_old,"%d",mo_sms_report_sum);
//
// printf("mo_sms_report_count=%d,mo_sms_report_sum=%d\n",mo_sms_report_count,mo_sms_report_sum);
}
ret=fclose(fp_old);
if(ret==EOF) {
fprintf(stderr,"write_sum_file() is fclose() error.\n");
return -1;
}
//OLD_FILE_MT_SMS_SUM
fp_old=fopen(OLD_FILE_MT_SMS_SUM,"a+");
if(fp_old==NULL) {
fprintf(stderr,"write_sum_file() is fopen() error.\n");
return -1;
}
else {
fseek(fp_old,0,SEEK_SET);
ret=fgets(mt_sms_count_sum,LEN_SHORT,fp_old);
if(ret==NULL) {
mt_sms_count_sum[0]='0';
}
if(mark==0) {
mt_sms_sum=atoi(mt_sms_count_sum)+mt_sms_count;
}
else {
mt_sms_sum=mt_sms_count;
}
fp_old=fopen(OLD_FILE_MT_SMS_SUM,"w");
fprintf(fp_old,"%d",mt_sms_sum);
// printf("mt_sms_count=%d,mt_sms_sum=%d\n",mt_sms_count,mt_sms_sum);
}
ret=fclose(fp_old);
if(ret==EOF) {
fprintf(stderr,"write_sum_file() is fclose() error.\n");
return -1;
}
//FILE_SUM_DATE
fp_old=fopen(FILE_SUM_DATE,"w");
if(fp_old==NULL) {
fprintf(stderr,"write_sum_file() is fopen() error.\n");
return -1;
}
else {
fprintf(fp_old,"%s",file_mqsgip_date);
}
ret=fclose(fp_old);
if(ret==EOF) {
fprintf(stderr,"write_sum_file() is fclose() error.\n");
return -1;
}
return 0;
}
int main(void) {
int fd,ret;
int mark=0;
char hostname[LEN_SHORT];
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);
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");
sprintf(today,"%d-%02d-%02d",p1->tm_year+1900,p1->tm_mon+1,p1->tm_mday);
*/
// 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);
}
ret=parse_log_file(LOG_FILE);
if(ret==-1) {
fprintf(stderr,"parse() error.\n");
exit(-1);
}
ret=write_sum_file();
if(ret==-1) {
fprintf(stderr,"check_sum_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(failed_count==1 || no_answer_count==1) {
exitstatus=WARNING;
}
else if(failed_count>1 || no_answer_count>1) {
exitstatus=CRITICAL;
}
*/
sprintf(status_information,"Current mo_sms_delivery_count=%d, mo_sms_report_count=%d, mt_sms_count=%d, mo_sms_delivery_count_sum=%d, mo_sms_report_count_sum=%d, mt_sms_count_sum=%d",mo_sms_delivery_count,mo_sms_report_count,mt_sms_count,mo_sms_delivery_sum,mo_sms_report_sum,mt_sms_sum);
sprintf(performance_data,"mo_sms_delivery_count=%d;;;; mo_sms_report_count=%d;;;; mt_sms_count=%d;;;; mo_sms_delivery_count_sum=%d;;;; mo_sms_report_count_sum=%d;;;; mt_sms_count_sum=%d;;;;",mo_sms_delivery_count,mo_sms_report_count,mt_sms_count,mo_sms_delivery_sum,mo_sms_report_sum,mt_sms_sum);
printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
return 0;
}