nagios插件之發起http的purchase購買請求(cdstore)

vi check_purchase.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 40000
#define MIN_LEN 50

//#define HIS_PORT 8080
#define HIS_PORT 8084
//#define HIS_IPADDR "114.66.80.122"
//#define HIS_IPADDR "114.66.80.126"
#define HIS_IPADDR "10x.4.17x.13x"

//cookie
char Cookie_JSESSIONID[LEN];

//http response
char http_response[LEN];
char http_return_code[LEN];

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

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

//second
int sec_num=0;

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

write_resp_log(char *response) {
	int ret;
	FILE *fp;
	
	fp=fopen("/tmp/getderiver_id.log","a+");
	if(fp==NULL) {
		fprintf(stderr,"fopen() error.\n");
		return -1;
	}

	ret=fprintf(fp,"%s%s%s","---------------------------\n",response,"-------------------------------\n");
	if(ret<0) {
		fprintf(stderr,"fprintf() error.\n");
		return -1;
	}

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

	return 0;
}

int get_url(char url_request[LEN],char url_response[LEN]) {
	int ret,ret1,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) {
                        ret1=read(sd,readbuf,LEN);
                        if(ret1<0){
				fprintf(stderr,"read() error.\n");
				return -1;
				
                               // close(sd);
                           ////     return -1;
                        }
			else if(ret1>0) {

				printf("readbuf start--------------------\n");
             	////	printf("%s\n",readbuf);
		//	strcpy(url_response,readbuf);
			strcat(url_response,readbuf);
				break;
       				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);
		if(ret==0) {
			sec_num++;
		}
	
		if(sec_num>=20) {
			exitstatus=CRITICAL;
			sprintf(status_information,"sec_num=%d,http_response=%s",sec_num,"time out");
			sprintf(performance_data,"sec_num=%d;;;; http_return_code=%s;;;;",sec_num,"0");

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

			exit(exitstatus);	
		}
	}

	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_tmp[LEN];
	char readbuf_cookie[LEN];

//	strcpy(readbuf,get_url_respons);

	memset(readbuf,0,LEN);
	memset(readbuf_tmp,0,LEN);

	memset(http_response,0,LEN);
	memset(http_return_code,0,LEN);

	/*
	for(p=strtok(readbuf,"\r\n");p;p=strtok(NULL,"\r\n")) {
		str=p;
		printf("str=%s\n",str);
	}
	*/

//	str=gets(readbuf);
//	fgets(str,LEN,&readbuf);
//
////	ret=sscanf(readbuf,"%[^M\n]",readbuf_tmp);
	ret=sscanf(get_url_respons,"%[^M\n]",readbuf);
	if(ret==EOF) {
	//	printf("sscanf() error.\n");
		write_resp_log(get_url_respons);

		if(strlen(get_url_respons)==0) {
			exitstatus=CRITICAL;
			sprintf(status_information,"sec_num=%d,http_response=proxy not response",sec_num);
			sprintf(performance_data,"sec_num=%d;;;; http_return_code=%s;;;;",sec_num,"0");

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

			exit(exitstatus);
			
		}

		return -1;
	}
	strncpy(http_response,readbuf,strlen(readbuf)-1);
//	strcpy(http_response,readbuf);
//	printf("ret=%d,readbuf_tmp=%s\n",ret,readbuf_tmp);
////	printf("ret=%d,readbuf=%s\n",ret,readbuf);

	sscanf(get_url_respons,"HTTP/1.1 %3s",readbuf_tmp);
	strcpy(http_return_code,readbuf_tmp);
////	printf("http return code=%s\n",readbuf_tmp);

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

	time_t tstamp,tstamp1,tstamp2;
	struct tm *tp;

	char timestamp[MIN_LEN];
	char validUntil[MIN_LEN];

	memset(status_information,0,LEN);
	memset(performance_data,0,LEN);

	//get time
	tstamp=time(NULL);
	tstamp1=tstamp-28800;
	tp=localtime(&tstamp1);	

	sprintf(timestamp,"%d-%02d-%02dT%02d:%02d:%02dZ",1900+tp->tm_year,(1+tp->tm_mon),tp->tm_mday,tp->tm_hour,tp->tm_min,tp->tm_sec);
	printf("timestamp=%s\n",timestamp);

	tstamp2=tstamp-28800+2592000;
	tp=localtime(&tstamp2);	
	sprintf(validUntil,"%d-%02d-%02dT%02d:%02d:%02dZ",1900+tp->tm_year,(1+tp->tm_mon),tp->tm_mday,tp->tm_hour,tp->tm_min,tp->tm_sec);
	printf("validUntil=%s\n",validUntil);

	//------------------------------------------------------------------------------
	//vehicle cdstore
	char http_url_cdstore_request[LEN];
	char http_url_cdstore_response[LEN];

	memset(http_url_cdstore_request,0,LEN);
	memset(http_url_cdstore_response,0,LEN);

//	strcat(http_url_cdstore_request,"PUT /v2/store/transactions?callerId=internet HTTP/1.1\n");
//	strcat(http_url_cdstore_request,"PUT /v2/store/transactions?callerId=dealer HTTP/1.1\n");
	strcat(http_url_cdstore_request,"PUT /v2/store/transactions?callerId=vehicle HTTP/1.1\n");
	strcat(http_url_cdstore_request,"Content-Type: application/json;charset=utf-8\n");
	strcat(http_url_cdstore_request,"User-Agent: Jakarta Commons-HttpClient/3.1\n");
	strcat(http_url_cdstore_request,"Host: 103.4.170.130:8084\n");
	strcat(http_url_cdstore_request,"Content-Length: 477\n\r\n");

	
//	strcat(http_url_cdstore_request,"{'type': 'PURCHASE','mobilePhoneNo': '18210336775','language': 'zh','validUntil':'");
//	strcat(http_url_cdstore_request,"{'type': 'PURCHASE','mobilePhoneNo': '18280984740','language': 'zh','validUntil':'");
//	strcat(http_url_cdstore_request,"{'type': 'PURCHASE','mobilePhoneNo': '18601172250','language': 'zh','validUntil':'");
	strcat(http_url_cdstore_request,"{'type': 'PURCHASE','mobilePhoneNo': '13011111024','language': 'zh','validUntil':'");
	strcat(http_url_cdstore_request,validUntil);
	strcat(http_url_cdstore_request,"','status': {'type': 'CREATED','timestamp': '");
	strcat(http_url_cdstore_request,timestamp);
	strcat(http_url_cdstore_request,"'},'vehicle': {'vin': 'X7Z1024','model': 'BMW 535d','soldBy': 'BBA'},'offers': [{'id': '6MRTTI','label': '瀹濋┈涓浗6涓湀RTTI','description': 'description for the Real Time Traffic Information','imageUrl': 'http://bmw.com/images/store/CN/image1.png','grossPrice': '0.01','currency': 'RMB','period': '6m'}]}");

	printf("cdstore request=%s",http_url_cdstore_request);
	printf("\n----------------------------------------------------------\n");

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

	printf("sec_num=%d\n",sec_num);

	//parse_url

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

////	printf("\n*********************************************************\n");
//	printf("http_response=%s",http_response);
////	printf("http_response=%s,strlen=%d\n",http_response,strlen(http_response));
////	printf("http_return_code=%s,strlen=%d\n",http_return_code,strlen(http_return_code));
////	printf("\n----------------------------------------------------------\n");



/*
////	if(!strcmp("HTTP/1.1 200 OK",http_response)) {
	if(!strcmp("200",http_return_code)) {
		exitstatus=OK;

		sprintf(status_information,"sec_num=%d,http_response=%s",sec_num,http_response);
	//	snprintf(status_information,strlen(http_response)-1,"http_response=%s",http_response);
////		printf("status_information=%s",status_information);
////	printf("\n000000000000000000000000000000000000000000000000000000000000\n");

		sprintf(performance_data,"sec_num=%d;;;; http_return_code=%s;;;;",sec_num,http_return_code);
	//	snprintf(performance_data,strlen(http_return_code),"http_return_code=%s;;;;",http_return_code);
////		printf("performance_data=%s",performance_data);

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

	}
	else {
		exitstatus=CRITICAL;

		sprintf(status_information,"sec_num=%d,http_response=%s",sec_num,http_response);

		sprintf(performance_data,"sec_num=%d;;;; http_return_code=%s;;;;",sec_num,http_return_code);
////	printf("\n2222222222222222222222222222222222222222222222222222222222222\n");
	}
////	printf("JSESSIONID=%s\n",Cookie_JSESSIONID);
////	printf("\n----------------------------------------------------------\n");
////	printf("\n----------------------------------------------------------\n");

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


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

	return exitstatus;
*/

//	exit(0);
}

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章