agios插件之监控AEP主机InBound和Awaiting数量

本文介绍了一个电话门户系统的监控脚本,该脚本周期性地收集系统中的InBound和Awaiting呼叫统计数据,并记录到日志文件中。此外,还提供了一个分析脚本用于处理这些日志文件,提取过去一段时间内的最大值并进行平均计算。

vi check_vp_session_3.c (每5秒写入文件vp_InBound_log.txt-年-月-日中InBound值和Awaiting值)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define LEN 1023

#define LOG_FILE "/home/craft/check_log/temp_InBound.txt"

#define SENDMAIL "/usr/local/bin/sendEmail -f bmw-alarm@wo116114.com -t chenhb55@chinaunicom.cn 13011111024@wo.com.cn -s 10.127.3.100 -u vp_sesin_er -xu bmw-alarm -xp 123456 -m "

//#define SENDMAIL "/usr/local/bin/sendEmail -f bmw-alarm@wo116114.com -t chenhb55@chinaunicom.cn send2@m165.com -s 10.127.3.100 -u vp_sesin_er -xu bmw-alarm -xp 123456 -m "
//

void safe_sleep(unsigned int sec) {
	while(sec=sleep(sec));
}

void become_daemon() {
        //--
        pid_t pid;
        int fd;

        pid=fork();
        if(pid==-1) {
                perror("fork()");
                exit(-1);
        }
        
        if(pid!=0)
                exit(0);
        else {  
                fd=open("/dev/null",O_RDONLY);
                if(fd==-1) {
                        perror("open()");
                } else {
                        close(STDIN_FILENO);
                        dup(fd);
                        close(fd);
                }
                
                fd=open("/dev/null",O_WRONLY);
                if(fd==-1) {
                        perror("open()");
                } else {
                        close(STDOUT_FILENO);
                        dup(fd);
                        close(STDERR_FILENO);
                        dup(fd);
                        close(fd);
                }

                setsid();

                chdir("/");
        }
        //--
}

int main(void) {
	FILE *fp,*fp_temp;
	FILE *fp_call_file;
	char listcalls[LEN];

	char *p,*str;
	char readbuf[LEN];
	char call_filename[LEN];

	int InBound;
	int Outbound;

	char send_mail_cmd[LEN];

	int ret;
	int fd;

	time_t timep;
	struct tm *t1;

	char nowtime[128];
	char hostname[128];

	// backend process
	become_daemon();

	while(1) {
		InBound=0;
		Outbound=0;

		fp_temp=fopen(LOG_FILE,"a");
		if(fp_temp==NULL) {
			perror("fopen()");
			exit(-1);
		}

		fd=fileno(fp_temp);

		ret=lockf(fd,F_LOCK,0);
		if(ret==-1) {
			fprintf(stderr,"flock() error.\n");
			exit(-1);
		} 
		else {
			ret=gethostname(hostname,sizeof(hostname));
			if(ret==-1) {
				fprintf(stderr,"gethostname() error.\n");
				exit(-1);
			}

			timep=time(NULL);
			t1=localtime(&timep);

			sprintf(nowtime,"%d-%02d-%02d-%02d-%02d-%02d",1900+t1->tm_year,(1+t1->tm_mon),t1->tm_mday,t1->tm_hour,t1->tm_min,t1->tm_sec);

			sprintf(call_filename,"/home/craft/check_log/vp_InBound_log.txt-%d-%02d-%02d",1900+t1->tm_year,1+t1->tm_mon,t1->tm_mday);

			fp_call_file=fopen(call_filename,"a");
			if(fp_call_file==NULL) {
				perror("fopen()");
				exit(-1);
			}

			//	sprintf(cmd,"%s","cat 1.log");
		//	sprintf(listcalls,"%s","/opt/Avaya/VoicePortal/MPP/bin/listcalls.php");
		//	sprintf(listcalls,"%s","/opt/Avaya/ExperiencePortal/MPP/bin/listcalls.php | grep BMWcallin | wc -l");
			sprintf(listcalls,"%s","/opt/Avaya/ExperiencePortal/MPP/bin/listcalls.php | grep InBound");

			fp=popen(listcalls,"r");
			if(fp!=NULL) {
				while(fgets(readbuf,1023,fp)!=NULL) {
					
				//	printf("%s",readbuf);

					if(strstr(readbuf,"BMWcallin") && strstr(readbuf,"InBound")) {
						InBound++;
					}

					if(!strstr(readbuf,"BMWcallin") && strstr(readbuf,"InBound")) {
						Outbound++;
					}
				}
			}
			else {
				perror("popen()");
				exit(-1);
			}

			ret=pclose(fp);
			if(ret==-1) {
				perror("pclose()");
			}

			fprintf(fp_temp,"%s\n",nowtime);
			fprintf(fp_call_file,"%s\n",nowtime);

			fprintf(fp_temp,"hostname=%s\n",hostname);
			fprintf(fp_call_file,"hostname=%s\n",hostname);

			fprintf(fp_temp,"InBound number is %d\n",InBound);
			fprintf(fp_call_file,"InBound number is %d\n",InBound);
			printf("InBound number is %d\n",InBound);

		//	fprintf(fp_temp,"call number is %.1f\n",(float)call_number/(float)2);
		//	fprintf(fp_temp,"call number is %d\n",call_number/2);
		//	fprintf(fp_call_file,"call number is %d\n",call_number/2);
		//	fprintf(fp_temp,"Outbound number is %d\n",Outbound);
			fprintf(fp_temp,"Awaiting number is %d\n",Outbound);
		//	fprintf(fp_call_file,"Outbound number is %d\n",Outbound);
			fprintf(fp_call_file,"Awaiting number is %d\n",Outbound);
		//	printf("Outbound number is %d\n",Outbound);

	/*
			if(InBound>=90) {
				sprintf(send_mail_cmd,"%s \"%s %d !\"",SENDMAIL,"Warn,VP(143) In-Bound number is",InBound);

				fprintf(fp_temp,"%s\n",send_mail_cmd); 
				fprintf(fp_call_file,"%s\n",send_mail_cmd); 

				system(send_mail_cmd);
			}

			if(Outbound>=90) {
				sprintf(send_mail_cmd,"%s \"%s %d !\"",SENDMAIL,"Warn,VP(143) Out-bound number is",Outbound);

				fprintf(fp_temp,"%s\n",send_mail_cmd);
				fprintf(fp_call_file,"%s\n",send_mail_cmd);

				system(send_mail_cmd);
			}
	*/

			fprintf(fp_temp,"-------------------------------\n");
			fprintf(fp_call_file,"-------------------------------\n");

		}

		ret=lockf(fd,F_ULOCK,0);
		if(ret==-1) {
			perror("lockf() F_ULOCK");
		}

		ret=fclose(fp_temp);
		if(ret==EOF) {
			fprintf(stderr,"fclose() error.\n");
			exit(-1);
		}

		ret=fclose(fp_call_file);
		if(ret==EOF) {
			fprintf(stderr,"fclose() error.\n");
			exit(-1);
		}

		safe_sleep(5);
	//	safe_sleep(2);
	}

	exit(0);
}

vi t4_5.c(分析上面文件vp_InBound_log.txt-年-月-日文件内容,输出每5分钟InBound和Awaiting的最大值)

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <sys/select.h>


#define OK       0   
#define WARNING  1   
#define CRITICAL 2   
#define UNKNOWN  3   

#define LEN 40000
//#define LEN 100

#define LOG_FILE "/home/craft/check_log/temp_InBound.txt"

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

char status_information[LEN];
char performance_data[LEN];

char url_status_information[LEN];
char url_performance_data[LEN];

//#define LEN 1024
#define HIS_PORT 80
#define HIS_IPADDR "10.127.2.123"

// ICall_English
char ICall_English_staff[LEN];
char ICall_English_available[LEN];
char ICall_English_waiting[LEN];

// ICall_Chinese
char ICall_Chinese_staff[LEN];
char ICall_Chinese_available[LEN];
char ICall_Chinese_waiting[LEN];

// ICall_All
char ICall_All_staff[LEN];
char ICall_All_available[LEN];
char ICall_All_waiting[LEN];

int check_InBound_file() {
	FILE *fp;
	int ret;
	int fd;

	char *p,*str;
	char readbuf[100];

	int m,n;
	int max=0;
	int Awaiting_max=0;
	int InBound_max=0;

	int i=0,j=0,k=0;

	char *temp;

	char *date[LEN];
	char *InBound[LEN];
//	char *Outbound[LEN];
	char *Awaiting[LEN];

	struct tm *tm_p;
	time_t timep;
	char year[LEN];
	char new_filename[LEN];
	char Outbound_filename[LEN];

        int sum_InBound=0,sum_Awaiting=0,line=0;


	fp=fopen(LOG_FILE,"r+");
	if(fp==NULL) {
		fprintf(stderr,"fopen() error. ");
		exitstatus=CRITICAL;
		printf("%s: - %s | %s\n",exit_status[exitstatus],status_information,performance_data);
		return exitstatus;
	}

	fd=fileno(fp);

	ret=lockf(fd,F_LOCK,0);
	if(ret==-1) {
		perror("lockf() F_LOCK");
		exit(-1);
	}
	else {
		timep=time(NULL);
		tm_p=localtime(&timep);

	//	printf("%s\n",new_filename);
	//
	//	printf("%s\n",Outbound_filename);
		
		sprintf(year,"%d",1900+tm_p->tm_year);
	//	printf("year=%s\n",year);

		while(fgets(readbuf,LEN,fp)!=NULL) {
		//	if(strstr(readbuf,"2013")) {
			if(strstr(readbuf,year)) {
				//	for(p=strtok(readbuf," \n");p;p=strtok(NULL," \n"))
				//		str=p;

				temp=(char *)malloc(32);

				//	m=strlen(readbuf);
				readbuf[strlen(readbuf)-1]='\0';
				strcpy(temp,readbuf);

				date[i++]=temp;
			}
			else if(strstr(readbuf,"InBound")) {
				for(p=strtok(readbuf," \n");p;p=strtok(NULL," \n"))
					str=p;

				temp=(char *)malloc(32);

				strcpy(temp,str);

				InBound[j++]=temp;
			}
		//	else if(strstr(readbuf,"Outbound")) {
			else if(strstr(readbuf,"Awaiting")) {
				for(p=strtok(readbuf," \n");p;p=strtok(NULL," \n")) 
					str=p;

				temp=(char *)malloc(32);

				strcpy(temp,str);

				Awaiting[k++]=temp;
			}
		}

                line=i;
	////	printf("line=%d\n",line);
	}

//	unlink file
	ret=unlink(LOG_FILE);
	if(ret==-1) {
		perror("unlink()");
	}

//	unlock
	ret=lockf(fd,F_ULOCK,0);
	if(ret==-1) {
		perror("lockf() F_ULOCK");
		exit(-1);
	}

	ret=fclose(fp);
	if(ret==EOF) {
		fprintf(stderr,"fclose() error.\n");
		exit(-1);
	}

	////	printf("i=%d\n",i);

	//	for(n=0;n<i;n++)
	//		printf("%s,%s,%s\n",date[n],InBound[n],Outbound[n]);

	//----------------------------------------

	//	printf("max=%d\n",max);

	for(n=0;n<i;n++) {
	//	if(atoi(Awaiting[n])>=max) {
		if(atoi(InBound[n])>=max) {
		//	max=atoi(Awaiting[n]);
			max=atoi(InBound[n]);
			m=i;
		}

                sum_InBound+=atoi(InBound[n]);
                sum_Awaiting+=atoi(Awaiting[n]);
	}

////	printf("m=%d\n",m);
////	printf("max=%d\n",max);

////	printf("sum_InBound=%d\n",sum_InBound);
////	printf("sum_Awaiting=%d\n",sum_Awaiting);

	for(n=0;n<i;n++) {
	//	if(atoi(Awaiting[n])==max) {
		if(atoi(InBound[n])==max) {
			//	printf("%s,InBound=%s,Awaiting=%s\n",date[n],InBound[n],Awaiting[n]);

		//	if(atoi(InBound[n])>=InBound_max) {
			if(atoi(Awaiting[n])>=Awaiting_max) {
			//	Awaiting_max=n;
				InBound_max=n;
			//	InBound_max=atoi(InBound[n]);
				Awaiting_max=atoi(Awaiting[n]);
			}
		}
	}

//	sprintf(status_information,"Current InBound=%s, InBound_avg=%.1f, Outbound=%s, Outbound_avg=%.1f, Waiting=%d",InBound[Outbound_max],(float)sum_inbound/(float)line,Outbound[Outbound_max],(float)sum_outbound/(float)line,atoi(InBound[Outbound_max])-atoi(Outbound[Outbound_max]));

	sprintf(status_information,"Current InBound=%s,InBound_avg=%.1f,Awaiting=%s,Awaiting_avg=%.1f",InBound[InBound_max],(float)sum_InBound/(float)line,Awaiting[InBound_max],(float)sum_Awaiting/(float)line);
	//	printf("status_information=%s\n",status_information);

//	sprintf(performance_data,"InBound=%s;;;; InBound_avg=%.1f;;;; Outbound=%s;;;; Outbound_avg=%.1f;;;; Waiting=%d;;;;",InBound[Outbound_max],(float)sum_inbound/(float)line,Outbound[Outbound_max],(float)sum_outbound/(float)line,atoi(InBound[Outbound_max])-atoi(Outbound[Outbound_max]));

	sprintf(performance_data,"InBound=%s;;;; InBound_avg=%.1f;;;; Awaiting=%s;;;; Awaiting_avg=%.1f;;;;",InBound[InBound_max],(float)sum_InBound/(float)line,Awaiting[InBound_max],(float)sum_Awaiting/(float)line);
	//	printf("performance_data=%s\n",performance_data);
	//
	
//	printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data);	

	return 0;
}

int get_url(char url_request[LEN],char url_response[LEN]) {
	int ret,i;
	char readbuf[LEN];

        int mark=0;
        char *p,*str;

	fd_set   rfds;
	struct timeval  time;

	int sd;
	struct sockaddr_in his_end;

//	printf("------------------------------\n");
//	printf("%s\n",url_request);

	sd=socket(AF_INET,SOCK_STREAM,0);
	if(sd==-1) {
		fprintf(stderr,"socket error.\n");
		exit(-1);
	}

	his_end.sin_family=AF_INET;
	his_end.sin_port=htons(HIS_PORT);
	his_end.sin_addr.s_addr=inet_addr(HIS_IPADDR);

	ret=connect(sd,(struct sockaddr *)&his_end,sizeof(his_end));
	if(ret==-1) {
		perror("connect()");
		exit(-1);
	}

	//send data
	ret=write(sd,url_request,strlen(url_request));
	if(ret<0) {
		perror("write() error");
		exit(-1);
	}
	else {
	//	printf("client write %d bytes .\n",ret);
	}

        while(1) {
		FD_ZERO(&rfds);
		FD_SET(sd,&rfds);

                time.tv_sec=1;
                time.tv_usec=0;

                ret=select(sd+1,&rfds,NULL,NULL,&time);
                if(ret<0) {
                        fprintf(stderr,"select() error.\n");
                        return -1;
                }

                if(ret>0) {
                        memset(readbuf,0,LEN);

                        ret=read(sd,readbuf,LEN);
                        if(ret==0){
                                close(sd);
                                return -1;
                        }

             //           printf("%s\n",readbuf);
                       // printf("--------------------\n");

                       // for(p=strtok(readbuf,"^M$\r\n");p;p=strtok(NULL,"^M$\r\n")) {
                        for(p=strtok(readbuf,"\r\n");p;p=strtok(NULL,"\r\n")) {
                               // if(mark==13)
                                //if(mark==10)
                                if(mark==9)
                                        break;

                                str=p;
                                mark++;
                        }
                      //  printf("%s\n",str);
			strcpy(url_response,str);

                        break;
                }
	}

	ret=close(sd);
	if(ret==-1) {
		fprintf(stderr,"close() error.\n");
	}

	return 0;
}

int parse_url(char get_url_respons[LEN]) {
	int mark=0;
	char *p,*str;
	char GetRealTimeInfo[LEN];

	if(strstr(get_url_respons,"RealTimeInfo")) {

		for(p=strtok(get_url_respons," ");p;p=strtok(NULL," ")) {
		//	printf("----------%s\n",get_url_respons);

			str=p;

			mark++;

                        // ICall_English
                        if(mark==5) {
				sscanf(str,"waiting=\"%s\"",ICall_English_waiting);
                                ICall_English_waiting[strlen(ICall_English_waiting)-1]='\0';
                        //	printf("ICall_English_waiting=%s\n",ICall_English_waiting);
                        }
                        else if(mark==7) {
                        	sscanf(str,"staff=\"%s\"",ICall_English_staff);
                                ICall_English_staff[strlen(ICall_English_staff)-1]='\0';
                        //	printf("ICall_English_staff=%s\n",ICall_English_staff);
                        }
                        else if(mark==8) {
                                sscanf(str,"available=\"%s\"",ICall_English_available);
                                ICall_English_available[strlen(ICall_English_available)-1]='\0';
                        //	printf("ICall_English_available=%s\n",ICall_English_available);
                        }
                        
                        // ICall_Chinese
                        else if(mark==15) {
                        	sscanf(str,"waiting=\"%s\"",ICall_Chinese_waiting);
                                ICall_Chinese_waiting[strlen(ICall_Chinese_waiting)-1]='\0';
                        //	printf("ICall_Chinese_waiting=%s\n",ICall_Chinese_waiting);
                        }
                        else if(mark==17) {
                                sscanf(str,"staff=\"%s\"",ICall_Chinese_staff);
                                ICall_Chinese_staff[strlen(ICall_Chinese_staff)-1]='\0';
                        //	printf("ICall_Chinese_staff=%s\n",ICall_Chinese_staff);
                        }
                        else if(mark==18) {
                                sscanf(str,"available=\"%s\"",ICall_Chinese_available);
                                ICall_Chinese_available[strlen(ICall_Chinese_available)-1]='\0';
                        //	printf("ICall_Chinese_available=%s\n",ICall_Chinese_available);
                        }

			// ICall_All
			else if(mark==25) {
				sscanf(str,"waiting=\"%s\"",ICall_All_waiting);
				ICall_All_waiting[strlen(ICall_All_waiting)-1]='\0';
			//	printf("ICall_All_waiting=%s\n",ICall_All_waiting);
			}
			else if(mark==27) {
				sscanf(str,"staff=\"%s\"",ICall_All_staff);
				ICall_All_staff[strlen(ICall_All_staff)-1]='\0';
			//	printf("ICall_All_staff=%s\n",ICall_All_staff);
			}
			else if(mark==28) {
				sscanf(str,"available=\"%s\"",ICall_All_available);
				ICall_All_available[strlen(ICall_All_available)-1]='\0';
			//	printf("ICall_All_available=%s\n",ICall_All_available);
				break;
			}

		}
	}

//	printf("---------------------------------------------\n");

//	sprintf(url_status_information,"Current ICall_English_staff=%s, ICall_English_available=%s, ICall_English_waiting=%s, ICall_Chinese_staff=%s, ICall_Chinese_available=%s, ICall_Chinese_waiting=%s, ICall_All_staff=%s, ICall_All_available=%s, ICall_All_waiting=%s",ICall_English_staff, ICall_English_available, ICall_English_waiting, ICall_Chinese_staff, ICall_Chinese_available, ICall_Chinese_waiting, ICall_All_staff, ICall_All_available, ICall_All_waiting);
//
//	sprintf(url_status_information,", ICall_English_staff=%s, ICall_English_available=%s, ICall_English_waiting=%s, ICall_Chinese_staff=%s, ICall_Chinese_available=%s, ICall_Chinese_waiting=%s, ICall_All_staff=%s, ICall_All_available=%s, ICall_All_waiting=%s",ICall_English_staff, ICall_English_available, ICall_English_waiting, ICall_Chinese_staff, ICall_Chinese_available, ICall_Chinese_waiting, ICall_All_staff, ICall_All_available, ICall_All_waiting);

	sprintf(url_status_information,", I_English_staff=%s, I_English_available=%s, I_English_waiting=%s, I_Chinese_staff=%s, I_Chinese_available=%s, I_Chinese_waiting=%s, I_All_staff=%s, I_All_available=%s, I_All_waiting=%s",ICall_English_staff, ICall_English_available, ICall_English_waiting, ICall_Chinese_staff, ICall_Chinese_available, ICall_Chinese_waiting, ICall_All_staff, ICall_All_available, ICall_All_waiting);
	//	printf("status_information=%s\n",status_information);

//	sprintf(url_performance_data,"ICall_English_staff=%s;;;; ICall_English_available=%s;;;; ICall_English_waiting=%s;;;; ICall_Chinese_staff=%s;;;; ICall_Chinese_available=%s;;;; ICall_Chinese_waiting=%s;;;; ICall_All_staff=%s;;;; ICall_All_available=%s;;;; ICall_All_waiting=%s;;;;",ICall_English_staff, ICall_English_available, ICall_English_waiting, ICall_Chinese_staff, ICall_Chinese_available, ICall_All_staff, ICall_All_available, ICall_All_waiting);

	sprintf(url_performance_data," ICall_English_staff=%s;;;; ICall_English_available=%s;;;; ICall_English_waiting=%s;;;; ICall_Chinese_staff=%s;;;; ICall_Chinese_available=%s;;;; ICall_Chinese_waiting=%s;;;; ICall_All_staff=%s;;;; ICall_All_available=%s;;;; ICall_All_waiting=%s;;;;",ICall_English_staff, ICall_English_available, ICall_English_waiting, ICall_Chinese_staff, ICall_Chinese_available, ICall_Chinese_waiting, ICall_All_staff, ICall_All_available, ICall_All_waiting);
	//	printf("performance_data=%s\n",performance_data);
	//
	
//	printf("%s: %s | %s\n",exit_status[exitstatus],url_status_information,url_performance_data);

	return 0;
}

int main(int argc, char *argv[]) {
	int ret,i;

/*
	char http_get_url_request[LEN];
	char http_get_url_response[LEN];
	
	//prepare date
	memset(http_get_url_request,0,LEN);
	strcat(http_get_url_request,"GET /ccinterface/Service.asmx/GetRealTimeInfo?queueid= HTTP/1.1\n");
//	strcat(http_get_url_request,"Host: 10.127.2.123\n\r\n");
	strcat(http_get_url_request,"Host: 10.127.2.135\n\r\n");
*/

//	printf("request=%s",http_get_url_request);

	ret=check_InBound_file();
	if(ret!=0) {
		fprintf(stderr,"check_file() error.\n");
	}

/*
	ret=get_url(http_get_url_request,http_get_url_response);
	if(ret!=0) {
		fprintf(stderr,"get_url() error.\n");
	}
//	printf("response=%s\n",http_get_url_response);

	ret=parse_url(http_get_url_response);
	if(ret!=0) {
		fprintf(stderr,"parse_url() error.\n");
	}
*/

//	printf("=======================================================================================================\n");

/*
	strcat(status_information,url_status_information);

	strcat(performance_data,url_performance_data);
*/


	printf("%s: %s | %s\n",exit_status[exitstatus],status_information,performance_data);

	return exitstatus;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值