nagios插件之监控POI搜索情况

vi check_nrpe_poi_search.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 LEN 40000

#define HIS_PORT 80
#define HIS_IPADDR "114.66.xxx.xxx"

//cookie
char Cookie_JSESSIONID[LEN];

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

int get_url(char url_request[LEN],char url_response[LEN]) {
	int ret,i;
//	char readbuf[40960];
	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);
////	printf("------------------------sd=%d\n",sd);
	if(sd==-1) {
		fprintf(stderr,"socket error.\n");
		return -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()");
		return -1;
	}

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


        while(1) {
		memset(readbuf,0,LEN);

		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,ret=%d.\n",ret);
                	return -1;
		//	continue;
                }
		else if(ret>0) {
                        ret=read(sd,readbuf,LEN);
                        if(ret<0){
				fprintf(stderr,"read() error.\n");
				return -1;
				
                               // close(sd);
                           ////     return -1;
                        }
			else if(ret>0) {

	                   ////     printf("readbuf start--------------------\n");
             	//	printf("%s\n",readbuf);
		//	strcpy(url_response,readbuf);
				strcat(url_response,readbuf);
       			////	printf("readbuf end--------------------\n");

			}
			else {
				break;
			}
		//	printf("------------------test---------------------\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")) {
                                str=p;
                                mark++;

                               // if(mark==13)
                                if(mark==7)
                                        break;

                        }
                      //  printf("%s\n",str);
			strcpy(url_response,str);
		      */

		//	break;
	//	}

                }
		else {
                      break;
		}
	//	sleep(2);
	}

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

	return 0;
}

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

	strcpy(readbuf,get_url_respons);

	/*
	for(p=strtok(readbuf,"\r\n");p;p=strtok(NULL,"\r\n")) {
       		str=p;
               	mark++;
		*/

		if(str=strstr(readbuf,"Set-Cookie: JSESSIONID=")) {
//			printf("1111111111111111111111\n");
			ret=sscanf(str,"Set-Cookie: JSESSIONID=%32s;",Cookie_JSESSIONID);
//			printf("ret=%d\n",ret);

		//	sscanf(str,"Set-Cookie: JSESSIONID=%[^;]",JSESSIONID);
		//	for(p=strtok(get_url_respons,"=;");p;p=strtok(NULL,"=;")) {

		}

		/*
               // if(mark==13)
             	if(mark==7) {
			strcpy(readbuf_cookie,str);

			for(p=strtok(get_url_respons,"=;");p;p=strtok(NULL,"=;")) {
				
			}

			break;
		}
		*/
//	}

////	printf("JSESSIONID=%s\n",JSESSIONID);

/*	
//	if(strstr(get_url_respons,"JSESSIONID")) {

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

			str=p;

			mark++;
			if(mark==6) {
				strcpy(JSESSIONID,str);
				break;
			}

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

		printf("JSESSIONID=%s\n",JSESSIONID);
//	}
	*/


//

	return 0;
}

int search_word_count(char *buffer,char *word) {
        int num=0;
	char *str;
	char tmp_str[LEN];
	char separator[]="\"";

	strcpy(tmp_str,buffer);

//	printf("find_the\n");

	for (str=strtok(tmp_str,separator);str!=NULL;str=strtok(NULL,separator)) {
       		if (strcmp(word,str)==0) {
               		num++; 
		} 
        }

        return num;
}


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

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

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

	//------------------------------------------------------------------------------
	//wss login.action
	char http_url_login_action_request[LEN];
	char http_url_login_action_response[LEN];

	memset(http_url_login_action_request,0,LEN);
	memset(http_url_login_action_response,0,LEN);

	strcat(http_url_login_action_request,"POST /csp/login/login.action HTTP/1.1\n");
	strcat(http_url_login_action_request,"Accept: image/jpeg, image/gif, image/pjpeg, */*\n");
	strcat(http_url_login_action_request,"Referer: http://114.66.80.122/csp/login/init.action\n");
	strcat(http_url_login_action_request,"Accept-Language: zh-CN\n");
	strcat(http_url_login_action_request,"User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727)\n");
	strcat(http_url_login_action_request,"Content-Type: application/x-www-form-urlencoded\n");
	strcat(http_url_login_action_request,"Accept-Encoding: gzip, deflate\n");
	strcat(http_url_login_action_request,"Host: 114.66.80.122\n");
	strcat(http_url_login_action_request,"Content-Length: 31\n");
	strcat(http_url_login_action_request,"Connection: Keep-Alive\n");
	strcat(http_url_login_action_request,"Cache-Control: no-cache\n");
	strcat(http_url_login_action_request,"Cookie: JSESSIONID=337E3862BB20E7B21C298CB923061A07\n\r\n");
	strcat(http_url_login_action_request,"username=xxxxxx&password=xxxxxx");

////	printf("login.action request=%s",http_url_login_action_request);
////	printf("\n----------------------------------------------------------\n");

	//get_url
	ret=get_url(http_url_login_action_request,http_url_login_action_response);
	if(ret!=0) {
		printf("get_url ret=%d\n",ret);
		fprintf(stderr,"get_url() error.\n");
	}
////	printf("login.action response=%s\n",http_url_login_action_response);
////	printf("\n----------------------------------------------------------\n");

	//parse_url
	ret=parse_url(http_url_login_action_response);
	if(ret!=0) {
		fprintf(stderr,"parse_url() error.\n");
	}
////	printf("JSESSIONID=%s\n",Cookie_JSESSIONID);
////	printf("\n----------------------------------------------------------\n");
////	printf("\n----------------------------------------------------------\n");

	//------------------------------------------------------------------------------
	//initSession.action -- first
	char http_url_initSession_action_request[LEN];
	char http_url_initSession_action_response[LEN];

	memset(http_url_initSession_action_request,0,LEN);
	memset(http_url_initSession_action_response,0,LEN);

	strcat(http_url_initSession_action_request,"GET /csp/csp/initSession.action HTTP/1.1\n");
	strcat(http_url_initSession_action_request,"Accept: image/jpeg, image/gif, image/pjpeg, */*\n");
	strcat(http_url_initSession_action_request,"Referer: http://114.66.80.122/csp/login/init.action\n");
	strcat(http_url_initSession_action_request,"Accept-Language: zh-CN\n");
	strcat(http_url_initSession_action_request,"User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2)\n");
	strcat(http_url_initSession_action_request,"UA-CPU: AMD64\n");
	strcat(http_url_initSession_action_request,"Accept-Encoding: gzip, deflate\n");
	strcat(http_url_initSession_action_request,"Host: 114.66.80.122\n");
	strcat(http_url_initSession_action_request,"Connection: Keep-Alive\n");
	strcat(http_url_initSession_action_request,"Cache-Control: no-cache\n");
//	strcat(http_url_initSession_action_request,"Cookie: JSESSIONID=B19DEFB41CCD39058A036E618F5B709B\n\r\n");

	strcat(http_url_initSession_action_request,"Cookie: JSESSIONID=");
	strcat(Cookie_JSESSIONID,"\n\r\n");
	strcat(http_url_initSession_action_request,Cookie_JSESSIONID);

////	printf("initSession.action request=%s",http_url_initSession_action_request);
////	printf("*************************************************************\n");

	//get_url
	ret=get_url(http_url_initSession_action_request,http_url_initSession_action_response);
	if(ret!=0) {
		printf("get_url ret=%d\n",ret);
		fprintf(stderr,"get_url() error.\n");
	}
////	printf("initSession.action response=%s\n",http_url_initSession_action_response);
////	printf("\n----------------------------------------------------------\n");


	//------------------------------------------------------------------------------
	//j_security_check
	char http_url_j_security_check_request[LEN];
	char http_url_j_security_check_response[LEN];

	memset(http_url_j_security_check_request,0,LEN);
	memset(http_url_j_security_check_response,0,LEN);

	strcat(http_url_j_security_check_request,"GET /csp/j_security_check?j_username=CU-TSP&j_password=123456 HTTP/1.1\n");
	strcat(http_url_j_security_check_request,"Accept: image/jpeg, image/gif, image/pjpeg, */*\n");
	strcat(http_url_j_security_check_request,"Referer: http://114.66.80.122/csp/login/init.action\n");
	strcat(http_url_j_security_check_request,"Accept-Language: zh-CN\n");
	strcat(http_url_j_security_check_request,"User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2)\n");
	strcat(http_url_j_security_check_request,"UA-CPU: AMD64\n");
	strcat(http_url_j_security_check_request,"Accept-Encoding: gzip, deflate\n");
	strcat(http_url_j_security_check_request,"Host: 114.66.80.122\n");
	strcat(http_url_j_security_check_request,"Connection: Keep-Alive\n");
	strcat(http_url_j_security_check_request,"Cache-Control: no-cache\n");

	strcat(http_url_j_security_check_request,"Cookie: JSESSIONID=");
////	strcat(Cookie_JSESSIONID,"\n\r\n");
	strcat(http_url_j_security_check_request,Cookie_JSESSIONID);

////	printf("j_security_check request=%s",http_url_j_security_check_request);
////	printf("*************************************************************\n");

	//get_url
	ret=get_url(http_url_j_security_check_request,http_url_j_security_check_response);
	if(ret!=0) {
		printf("get_url ret=%d\n",ret);
		fprintf(stderr,"get_url() error.\n");
	}
////	printf("j_security_check response=%s\n",http_url_j_security_check_response);
////	printf("\n----------------------------------------------------------\n");


	//------------------------------------------------------------------------------
	//initSession.action -- second
	memset(http_url_initSession_action_request,0,LEN);
	memset(http_url_initSession_action_response,0,LEN);

	strcat(http_url_initSession_action_request,"GET /csp/csp/initSession.action HTTP/1.1\n");
	strcat(http_url_initSession_action_request,"Accept: image/jpeg, image/gif, image/pjpeg, */*\n");
	strcat(http_url_initSession_action_request,"Referer: http://114.66.80.122/csp/login/init.action\n");
	strcat(http_url_initSession_action_request,"Accept-Language: zh-CN\n");
	strcat(http_url_initSession_action_request,"User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2)\n");
	strcat(http_url_initSession_action_request,"UA-CPU: AMD64\n");
	strcat(http_url_initSession_action_request,"Accept-Encoding: gzip, deflate\n");
	strcat(http_url_initSession_action_request,"Host: 114.66.80.122\n");
	strcat(http_url_initSession_action_request,"Connection: Keep-Alive\n");
	strcat(http_url_initSession_action_request,"Cache-Control: no-cache\n");
//	strcat(http_url_initSession_action_request,"Cookie: JSESSIONID=B19DEFB41CCD39058A036E618F5B709B\n\r\n");

	strcat(http_url_initSession_action_request,"Cookie: JSESSIONID=");
////	strcat(Cookie_JSESSIONID,"\n\r\n");
	strcat(http_url_initSession_action_request,Cookie_JSESSIONID);

////	printf("initSession.action request=%s",http_url_initSession_action_request);
////	printf("*************************************************************\n");

	//get_url
	ret=get_url(http_url_initSession_action_request,http_url_initSession_action_response);
	if(ret!=0) {
		printf("get_url ret=%d\n",ret);
		fprintf(stderr,"get_url() error.\n");
	}
////	printf("initSession.action response=%s\n",http_url_initSession_action_response);
////	printf("\n----------------------------------------------------------\n");


	//---------------------------------------------------------------------------------
	//poi init.action
	char http_url_poi_init_action_request[LEN];
	char http_url_poi_init_action_response[LEN];

	memset(http_url_poi_init_action_request,0,LEN);
	memset(http_url_poi_init_action_response,0,LEN);
	
	strcat(http_url_poi_init_action_request,"GET /csp/cic/poi/init.action?dojo.preventCache=1394865269928 HTTP/1.1\n");
	strcat(http_url_poi_init_action_request,"Accept: */*\n");
	strcat(http_url_poi_init_action_request,"Accept-Language: zh-cn\n");
	strcat(http_url_poi_init_action_request,"Referer: http://114.66.80.122/csp/csp/initSession.action\n");
	strcat(http_url_poi_init_action_request,"x-requested-with: XMLHttpRequest\n");
	strcat(http_url_poi_init_action_request,"Content-Type: application/x-www-form-urlencoded\n");
	strcat(http_url_poi_init_action_request,"UA-CPU: AMD64\n");
	strcat(http_url_poi_init_action_request,"Accept-Encoding: gzip, deflate\n");
	strcat(http_url_poi_init_action_request,"User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2)\n");
	strcat(http_url_poi_init_action_request,"Host: 114.66.80.122\n");
	strcat(http_url_poi_init_action_request,"Connection: Keep-Alive\n");

//	strcat(http_url_poi_init_action_request,"Cookie: JSESSIONID=B19DEFB41CCD39058A036E618F5B709B\");
	strcat(http_url_poi_init_action_request,"Cookie: JSESSIONID=");
////	strcat(Cookie_JSESSIONID,"\n\r\n");
	strcat(http_url_poi_init_action_request,Cookie_JSESSIONID);


////	printf("poi init.action request=%s",http_url_poi_init_action_request);
////	printf("-----------------------\n");

	//get_url
	ret=get_url(http_url_poi_init_action_request,http_url_poi_init_action_response);
	if(ret!=0) {
		printf("get_url ret=%d\n",ret);
		fprintf(stderr,"get_url() error.\n");
	}
////	printf("poi init.action response=%s\n",http_url_poi_init_action_response);
////	printf("\n----------------------------------------------------------\n");
	

	//---------------------------------------------------------------------------------
	//poi loadPoiFavoritesAsJson.action
	//
	//
	/*
	char http_url_poi_loadPoiFavoritesAsJson_action_request[LEN];
	char http_url_poi_loadPoiFavoritesAsJson_action_response[LEN];

	memset(http_url_poi_loadPoiFavoritesAsJson_action_request,0,LEN);
	memset(http_url_poi_loadPoiFavoritesAsJson_action_response,0,LEN);	

	strcat(http_url_poi_loadPoiFavoritesAsJson_action_request,"POST /csp/loadPoiFavoritesAsJson.action HTTP/1.1\n");
	*/
//	strcat(http_url_poi_loadPoiFavoritesAsJson_action_request,"Accept: */*\n");
	/*
	strcat(http_url_poi_loadPoiFavoritesAsJson_action_request,"Accept-Language: zh-cn\n");
	strcat(http_url_poi_loadPoiFavoritesAsJson_action_request,"Referer: http://114.66.80.122/csp/csp/initSession.action\n");
	strcat(http_url_poi_loadPoiFavoritesAsJson_action_request,"x-requested-with: XMLHttpRequest\n");
	strcat(http_url_poi_loadPoiFavoritesAsJson_action_request,"Content-Type: application/x-www-form-urlencoded\n");
	strcat(http_url_poi_loadPoiFavoritesAsJson_action_request,"UA-CPU: AMD64\n");
	strcat(http_url_poi_loadPoiFavoritesAsJson_action_request,"Accept-Encoding: gzip, deflate\n");
	strcat(http_url_poi_loadPoiFavoritesAsJson_action_request,"User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2)\n");
	strcat(http_url_poi_loadPoiFavoritesAsJson_action_request,"Host: 114.66.80.122\n");
	strcat(http_url_poi_loadPoiFavoritesAsJson_action_request,"Content-Length: 31\n");
	strcat(http_url_poi_loadPoiFavoritesAsJson_action_request,"Connection: Keep-Alive\n");
	strcat(http_url_poi_loadPoiFavoritesAsJson_action_request,"Cache-Control: no-cache\n");

//	strcat(http_url_poi_init_action_request,"Cookie: JSESSIONID=B19DEFB41CCD39058A036E618F5B709B\n\r\n");
	strcat(http_url_poi_loadPoiFavoritesAsJson_action_request,"Cookie: JSESSIONID=");
////	strcat(Cookie_JSESSIONID,"\n\r\n");
	strcat(http_url_poi_loadPoiFavoritesAsJson_action_request,Cookie_JSESSIONID);

	strcat(http_url_poi_loadPoiFavoritesAsJson_action_request,"dojo.preventCache=1394865270115\n");


	printf("poi loadPoiFavoritesAsJson.action request=%s",http_url_poi_loadPoiFavoritesAsJson_action_request);
	printf("-----------------------\n");
	*/

	/*
	//get_url
	ret=get_url(http_url_poi_loadPoiFavoritesAsJson_action_request,http_url_poi_loadPoiFavoritesAsJson_action_response);
	if(ret!=0) {
		printf("get_url ret=%d\n",ret);
		fprintf(stderr,"get_url() error.\n");
	}
	printf("poi loadPoiFavoritesAsJson.action response=%s\n",http_url_poi_loadPoiFavoritesAsJson_action_response);
	printf("\n----------------------------------------------------------\n");
	*/
	
	
	//
	//
	//--------------------------------------------------------------------
	//poi search.action
	
	char http_url_poi_search_action_request[LEN];
	char http_url_poi_search_action_response[LEN];

	memset(http_url_poi_search_action_request,0,LEN);

	strcat(http_url_poi_search_action_request,"POST /csp/cic/poi/search.action HTTP/1.1\n");
	strcat(http_url_poi_search_action_request,"Accept: */*\n");
	strcat(http_url_poi_search_action_request,"Accept-Language: zh-cn\n");
	strcat(http_url_poi_search_action_request,"Referer: http://114.66.80.122/csp/csp/initSession.action\n");
	strcat(http_url_poi_search_action_request,"x-requested-with: XMLHttpRequest\n");
	strcat(http_url_poi_search_action_request,"Content-Type: application/x-www-form-urlencoded\n");
	strcat(http_url_poi_search_action_request,"UA-CPU: AMD64\n");
	strcat(http_url_poi_search_action_request,"Accept-Encoding: gzip, deflate\n");
	strcat(http_url_poi_search_action_request,"User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2)\n");
	strcat(http_url_poi_search_action_request,"Host: 114.66.80.122\n");
	strcat(http_url_poi_search_action_request,"Content-Length: 175\n");
	strcat(http_url_poi_search_action_request,"Connection: Keep-Alive\n");
	strcat(http_url_poi_search_action_request,"Cache-Control: no-cache\n");
	//
//	strcat(http_poi_search_action_url,"Cookie: JSESSIONID=last return value\n\r\n");
	strcat(http_url_poi_search_action_request,"Cookie: JSESSIONID=");
////	strcat(Cookie_JSESSIONID,"\n\r\n");
	strcat(http_url_poi_search_action_request,Cookie_JSESSIONID);
	//
	strcat(http_url_poi_search_action_request,"eventId=&searchType=atAddress&search.language=ZH&search.countries=CN&search.where=&search.what=%E5%A4%A9%E5%AE%89%E9%97%A8&search.categoryTerm=&dojo.preventCache=1394865280879\n");

////	printf("poi search.action request=%s",http_url_poi_search_action_request);
////	printf("-----------------------\n");

	//get_url
	ret=get_url(http_url_poi_search_action_request,http_url_poi_search_action_response);
	if(ret!=0) {
		printf("get_url ret=%d\n",ret);
		fprintf(stderr,"get_url() error.\n");
	}
////	printf("poi search.action response=%s\n",http_url_poi_search_action_response);
////	printf("\n----------------------------------------------------------\n");
	
	//------------------------------------------------------------------------------
	//poi page.action
	
	char http_url_poi_page_action_request[LEN];
	char http_url_poi_page_action_response[LEN];

	memset(http_url_poi_page_action_request,0,LEN);
	memset(http_url_poi_page_action_response,0,LEN);

	strcat(http_url_poi_page_action_request,"POST /csp/cic/poi/page.action HTTP/1.1\n"); 
	strcat(http_url_poi_page_action_request,"Accept: */*\n");
	strcat(http_url_poi_page_action_request,"Accept-Language: zh-cn\n");
	strcat(http_url_poi_page_action_request,"Referer: http://114.66.80.122/csp/csp/initSession.action\n");
	strcat(http_url_poi_page_action_request,"x-requested-with: XMLHttpRequest\n");
	strcat(http_url_poi_page_action_request,"Content-Type: application/x-www-form-urlencoded\n");
	strcat(http_url_poi_page_action_request,"UA-CPU: AMD64\n");
	strcat(http_url_poi_page_action_request,"Accept-Encoding: gzip, deflate\n");
	strcat(http_url_poi_page_action_request,"User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2)\n");
	strcat(http_url_poi_page_action_request,"Host: 114.66.80.122\n");
	strcat(http_url_poi_page_action_request,"Content-Length: 67\n");
	strcat(http_url_poi_page_action_request,"Connection: Keep-Alive\n");
	strcat(http_url_poi_page_action_request,"Cache-Control: no-cache\n");

//	strcat(http_url_poi_page_action_request,"Cookie: JSESSIONID=B19DEFB41CCD39058A036E618F5B709B\n\r\n");
	strcat(http_url_poi_page_action_request,"Cookie: JSESSIONID=");
////	strcat(Cookie_JSESSIONID,"\n\r\n");
	strcat(http_url_poi_page_action_request,Cookie_JSESSIONID);

	strcat(http_url_poi_page_action_request,"eventId=&from=0&to=10&sort=distance&dojo.preventCache=1394865282517\n");

	//sleep 2
////	printf("sleep 2 second! \n");
	safe_sleep(2);
	
////	printf("poi page.action request=%s",http_url_poi_page_action_request);
////	printf("-----------------------\n");

	/*
	//get_url
	ret=get_url(http_url_poi_page_action_request,http_url_poi_page_action_response);
	if(ret!=0) {
		printf("get_url ret=%d\n",ret);
		fprintf(stderr,"get_url() error.\n");
	}
	printf("poi page.action response=%s\n",http_url_poi_page_action_response);
	printf("\n----------------------------------------------------------\n");

	printf("country=%d\n",search_word_count(http_url_poi_page_action_response,"country"));
	printf("NavInfo=%d\n",search_word_count(http_url_poi_page_action_response,"NavInfo"));
	printf("Baidu=%d\n",search_word_count(http_url_poi_page_action_response,"Baidu"));
	printf("\n----------------------------------------------------------\n");
	*/
	
	//get_url
	int sec_sum=0;
	int country=0;
	int navinfo=0;
	int baidu=0;
	while(1) {
		memset(http_url_poi_page_action_response,0,LEN);

		ret=get_url(http_url_poi_page_action_request,http_url_poi_page_action_response);
		if(ret!=0) {
			printf("get_url ret=%d\n",ret);
			fprintf(stderr,"get_url() error.\n");
		}
	
		/*
		printf("poi page.action response=%s\n",http_url_poi_page_action_response);
		printf("\n----------------------------------------------------------\n");

		printf("country=%d\n",country=search_word_count(http_url_poi_page_action_response,"country"));
		printf("NavInfo=%d\n",navinfo=search_word_count(http_url_poi_page_action_response,"NavInfo"));
		printf("Baidu=%d\n",baidu=search_word_count(http_url_poi_page_action_response,"Baidu"));
		printf("\n----------------------------------------------------------\n");
		*/

		country=search_word_count(http_url_poi_page_action_response,"country");
		if(country<0) {
			fprintf(stderr,"search_word_count() country error.\n");
			return -1;
		}

		navinfo=search_word_count(http_url_poi_page_action_response,"NavInfo");
		if(navinfo<0) {
			fprintf(stderr,"search_word_count() navinfo error.\n");
			return -1;
		}

		baidu=search_word_count(http_url_poi_page_action_response,"Baidu");
		if(baidu<0) {
			fprintf(stderr,"search_word_count() baidu error.\n");
			return -1;
		}
	

	//	if(!strcmp(http_url_poi_search_action_response,http_url_poi_page_action_response)) {
		if(sec_sum>=20) {
			break;
		}
		else if(strlen(http_url_poi_page_action_response)<500) {
			sleep(1);
			sec_sum++;
		////	printf("sleep 1 seconds,sec_sum=%d\n",sec_sum);
		}
		else {
			break;
		}
	}

	if(sec_sum==0 && country>0) {
//		printf("poi search ok, sec_sum=0\n");

		sprintf(status_information,"POI Search Time_sec=%d, Resp_num=%d, NavInfo=%d, Baidu=%d",sec_sum,country,navinfo,baidu);

		sprintf(performance_data,"Time_Sec=%d;;;; Resp_num=%d;;;; NavInfo=%d;;;; Baidu=%d;;;;",sec_sum,country,navinfo,baidu);
	}
	else if(sec_sum>5 && sec_sum<=10 && country>0) {
//		printf("poi search warning, sec_sum=%d\n",sec_sum);
		exitstatus=WARNING;

		sprintf(status_information,"POI Search Time_sec=%d, Resp_num=%d, NavInfo=%d, Baidu=%d",sec_sum,country,navinfo,baidu);

		sprintf(performance_data,"Time_Sec=%d;;;; Resp_num=%d;;;; NavInfo=%d;;;; Baidu=%d;;;;",sec_sum,country,navinfo,baidu);
	}
	else if(sec_sum>10 || country==0) {
//		printf("poi search critical, sec_sum=%d\n",sec_sum);
		exitstatus=CRITICAL;

		sprintf(status_information,"POI Search Time_sec=%d, Resp_num=%d, Navinfo=%d, Baidu=%d",sec_sum,country,navinfo,baidu);

		sprintf(performance_data,"Time_Sec=%d;;;; Resp_num=%d;;;; NavInfo=%d;;;; Baidu=%d;;;;",sec_sum,country,navinfo,baidu);
	}
	/*
	else {
		printf("poi search critical, sec_sum > %d\n",sec_sum);

		exitstatus=CRITICAL;

		sprintf(status_information,"POI Search Critical! Use time > %d",sec_sum);

		sprintf(performance_data,"POI_Search_Time_Sec=%d;;;;",sec_sum+10);
	}
	*/

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

	return exitstatus;

//	exit(0);
}

你可能感兴趣的:(nagios插件之监控POI搜索情况)