nagios插件之监控f5主备状态(ssh实现)

本文介绍了一种通过脚本来检测F5负载均衡器的主备状态的方法,包括SSH连接、命令执行及状态解析。

访问f5-1调用脚本,vi auto_ssh_1.sh:

#!/usr/bin/expect -f

#set port 22
set user admin
set host 114.xx.xx.1
set password bmw@123!!!
set timeout 10

spawn ssh $user@$host b failover show \; ip ad | grep 'inet 114.xx.xx.1'

expect "*assword:*"

send "$password\r"

expect eof

访问f5-2调用脚本,vi auto_ssh_2.sh:

#!/usr/bin/expect -f

#set port 22
set user admin
set host 114.xx.xx.2
set password bmw@123!!!
set timeout 10

spawn ssh $user@$host b failover show \; ip ad | grep 'inet 114.xx.xx.2'

expect "*assword:*"

send "$password\r"

expect eof


检测f5主备状态时,调用上面脚本,vi f5_active_status.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define OK       0
#define WARNING  1
#define CRITICAL 2
#define UNKNOWN  3

#define LEN 1023

#define TCL_CMD "/usr/local/nagios/libexec/"

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

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

int parse_status(char *sh_cmd,char *active_status,char *active_ip_addr) {
	int ret;
        FILE *fp;
        char readbuf[LEN];

        char *p,*str;

	int line=0;
	int mark1=0;
	int mark2=0;

//	fp=popen("/home/neo/check_log/tcl/auto_ssh.sh","r");
        fp=popen(sh_cmd,"r");
        if(fp==NULL) {
                fprintf(stderr,"popen() error. ");
                exitstatus=CRITICAL;
                printf("%s: - %s | %s\n",exit_status[exitstatus],status_information,performance_data);
                exit(exitstatus);
        }

        while(fgets(readbuf,LEN,fp)!=NULL) {
		line++;
	//	printf("line=%d,readbuf=%s",line,readbuf);

		if(line==3) {
			for(p=strtok(readbuf," ");p;p=strtok(NULL," ")) {
				//      str=p;

				//Sun
				mark1++;

				if(mark1==2) {
				//	printf("p=%s\n",p);

					strcpy(active_status,p);
				//	printf("active_status=%s\n",active_status);
				}
			}
		}

		if(line==4) {
			for(p=strtok(readbuf," ");p;p=strtok(NULL," /")) {
				mark2++;

				if(mark2==2) {
				//	printf("p=%s\n",p);

					strcpy(active_ip_addr,p);
				//	printf("active_ip_addr=%s\n",active_ip_addr);
				}
			}

			break;
		}
        }

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

	return 0;
}

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

	char active_status[LEN];
	char active_ip_addr[LEN];

	if(argc<=1) {
		printf("%s %s\n",argv[0],"auto_ssh_1.sh | auto_ssh_2.sh");
		exit(-1);
	}

	sprintf(sh_cmd,"%s%s",TCL_CMD,argv[1]);
//	printf("sh_cmd=%s\n",sh_cmd);

	ret=parse_status(sh_cmd,active_status,active_ip_addr);
	if(ret!=0) {
		fprintf(stderr,"parse_status() error.\n");
		exit(-1);
	}	

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


	if(strstr(argv[1],"148")) {

		if(!strcmp(active_ip_addr,"114.xx.xx.1") && !strcmp(active_status,"active")) {
			exitstatus=OK;
	
			sprintf(status_information,"f5 Active=%s, Standby=%s","114.xx.xx.1","114.xx.xx.2");

			sprintf(performance_data,"Master=%d;;;; Slave=%d;;;;",1,0); 
		}
	//	else if(!strcmp(active_ip_addr,"114.66.80.149") && !strcmp(active_status,"active")) {
		else if(!strcmp(active_ip_addr,"114.66.80.148") && !strcmp(active_status,"standby")) {
			exitstatus=WARNING;

			sprintf(status_information,"f5 Active=%s, Standby=%s","114.xx.xx.1","114.xx.xx.2");

			sprintf(performance_data,"Master=%d;;;; Slave=%d;;;;",0,1); 
		}
	}
	else if(strstr(argv[1],"149")) {

		if(!strcmp(active_ip_addr,"114.66.80.149") && !strcmp(active_status,"standby")) {
			exitstatus=OK;
	
			sprintf(status_information,"f5 Active=%s, Standby=%s","114.xx.xx.1","114.xx.xx.2");

			sprintf(performance_data,"Master=%d;;;; Slave=%d;;;;",1,0); 
		}
	//	else if(!strcmp(active_ip_addr,"114.66.80.148") && !strcmp(active_status,"active")) {
		else if(!strcmp(active_ip_addr,"114.66.80.149") && !strcmp(active_status,"active")) {
			exitstatus=WARNING;

			sprintf(status_information,"f5 Active=%s, Standby=%s","114.xx.xx.1","114.xx.xx.2");

			sprintf(performance_data,"Master=%d;;;; Slave=%d;;;;",0,1); 
		}
	}

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

        return exitstatus;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值