nagios插件之查询坐席使用情况

1、根据temp_Inbound.txt文件找出最大值
2、坐席状态有个Wer Server接口,将数据GET下来后再拆分


vi check_agent_status.c

#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 1023

#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.135"

// 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[LEN];

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

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

	char *temp;

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

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


	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(128);

				//	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(128);

				strcpy(temp,str);

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

				temp=(char *)malloc(128);

				strcpy(temp,str);

				Outbound[k++]=temp;
			}
		}
	}

//	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(Outbound[n])>=max) {
			max=atoi(Outbound[n]);
			m=i;
		}

	}

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

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

			if(atoi(InBound[n])>=InBound_max) {
				Outbound_max=n;
				InBound_max=atoi(InBound[n]);
			}
		}
	}

	sprintf(status_information,"Current InBound=%s, Outbound=%s, Waiting=%d",InBound[Outbound_max],Outbound[Outbound_max],atoi(InBound[Outbound_max])-atoi(Outbound[Outbound_max]));
	//	printf("status_information=%s\n",status_information);

	sprintf(performance_data,"InBound=%s;;;; Outbound=%s;;;; Waiting=%d;;;;",InBound[Outbound_max],Outbound[Outbound_max],atoi(InBound[Outbound_max])-atoi(Outbound[Outbound_max]));
	//	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")) {
                                if(mark==13)
			*/
                        for(p=strtok(readbuf,"\r\n");p;p=strtok(NULL,"\r\n")) {
                                if(mark==10)
                                        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");

//	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;
}


生成temp_Inbound.txt文件

vi check_listcalls.c

#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 [email protected] -t [email protected] [email protected] [email protected] -s 10.127.3.100 -u vp_sesin_er -xu bmw-alarm -xp 123456 -m "

//#define SENDMAIL "/usr/local/bin/sendEmail -f [email protected] -t [email protected] [email protected] -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");

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

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

					if(strstr(readbuf,"Outbound") && strstr(readbuf,"sipcall")) {
						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_call_file,"Outbound number is %d\n",Outbound);
			printf("Outbound number is %d\n",Outbound);

			if(InBound>=40) {
				sprintf(send_mail_cmd,"%s \"%s %d !\"",SENDMAIL,"Warn,VP(144) 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>=50) {
				sprintf(send_mail_cmd,"%s \"%s %d !\"",SENDMAIL,"Warn,VP(144) 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);
	}

	exit(0);
}

你可能感兴趣的:(nagios插件之查询坐席使用情况)