nagios插件之监控web页面登陆

vi check_nrpe_wss_login.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 4000

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

//second
int sec_num=0;

//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 {
			sec_num++;
                //	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);

	return 0;
}


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");

	//------------------------------------------------------------------------------
	
	//get_url

	if(strlen(http_url_login_action_response)>32 && sec_num<=5) {
	//	printf("login ok, sec_sum=0\n",sec_num);
		exitstatus=OK;

		sprintf(status_information,"WSS Login Time_sec=%d, JSESSIONID=%s",sec_num,Cookie_JSESSIONID);

		sprintf(performance_data,"Login_Time_Sec=%d;;;;",sec_num);
	}
	else if(strlen(http_url_login_action_response)>32 && sec_num>5 && sec_num<=10) {
	//	printf("warning, login ok, sec_num=0\n",sec_num);
		exitstatus=WARNING;

		sprintf(status_information,"WSS Login Time_sec=%d, JSESSIONID=%s",sec_num,Cookie_JSESSIONID);

		sprintf(performance_data,"Login_Time_Sec=%d;;;;",sec_num);
	}
//	else if(sec_num<=20) {
	else {
	//	printf("critical, login error, sec_num=0\n",sec_num);
		exitstatus=CRITICAL;

		sprintf(status_information,"WSS Login Time_sec=%d, JSESSIONID=%s",sec_num,Cookie_JSESSIONID);

		sprintf(performance_data,"Login_Time_Sec=%d;;;;",sec_num);
	}

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

	return exitstatus;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值