nagios插件之监控获取driverID

vi vehicle_getdriverid.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 2000

#define HIS_PORT 8080
//#define HIS_IPADDR "114.66.80.122"
#define HIS_IPADDR "114.66.80.126"

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


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

	//------------------------------------------------------------------------------
	//vehicle getDriveId
	char http_url_getDriveId_request[LEN];
	char http_url_getDriveId_response[LEN];

	memset(http_url_getDriveId_request,0,LEN);
	memset(http_url_getDriveId_response,0,LEN);

	strcat(http_url_getDriveId_request,"GET http://b2v.bmwgroup.cn/com/dm_cn/vehicle/getDriveId HTTP/1.1\n");
	strcat(http_url_getDriveId_request,"Proxy-Authorization: Basic YjJ2X2NoaW5hOmJjcGhyaTJvbnhheXY=\n");
	strcat(http_url_getDriveId_request,"Host: b2v.bmwgroup.cn\n");
	strcat(http_url_getDriveId_request,"Accept: */*\n");
	strcat(http_url_getDriveId_request,"Proxy-Connection: Keep-Alive\n");
	strcat(http_url_getDriveId_request,"User-Agent: NBT/1540000/03\n");
	strcat(http_url_getDriveId_request,"BMW-Vin: H008779\n");
	strcat(http_url_getDriveId_request,"BMW-OTA-ID: 20140128-052216\n");
	strcat(http_url_getDriveId_request,"BMW-DAS-ID: 20121011-110700\n\r\n");

////	printf("getDriveId request=%s",http_url_getDriveId_request);
////	printf("\n----------------------------------------------------------\n");

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

	//parse_url

	ret=parse_url(http_url_getDriveId_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);
}

你可能感兴趣的:(nagios插件之监控获取driverID)