nagios插件之登陆router监控vrrp状态

登陆router脚本 cat auto_ssh_route_dis_vrrp.sh

#!/usr/bin/expect -f

#set port 22
set user xxxxxx
set host [lindex $argv 0 0]
set password xxxxxx
set timeout 30

spawn ssh $user@$host

expect "*assword:*"
send "$password\r"

expect "*IRT*"
send "display vrrp\r"

expect "*IRT*"
send "quit"


调用登陆router脚本插件 

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>

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

#define LEN 1000L
#define MIN_LEN 32L

//#define TCL_CMD "/home/weihu/tcl/"
#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 write_resp_log(char *response) {
        int ret;
        FILE *fp;

//      fp=fopen("/tmp/getderiver_id.log","a+");
        fp=fopen("/tmp/router_vrrp_49.log","a+");
        if(fp==NULL) {
                fprintf(stderr,"fopen() error.\n");
                return -1;
        }

        ret=fprintf(fp,"%s%s%s","---------------------------\n",response,"\n-------------------------------------------------\n\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 parse_status(char *sh_cmd,char *active_status,char *active_ip_addr,char ip_addr[MIN_LEN]) {
        int ret;
        FILE *fp;
        char readbuf[LEN];

        char *p,*str;

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

        // master and backup
        char apn_master[MIN_LEN]={0};
        char apn_master_ip[MIN_LEN]={0};

        char apn_backup[MIN_LEN]={0};
        char apn_backup_ip[MIN_LEN]={0};

        char sms_master[MIN_LEN]={0};
        char sms_master_ip[MIN_LEN]={0};

        char sms_backup[MIN_LEN]={0};
        char sms_backup_ip[MIN_LEN]={0};

        char insidegw_master[MIN_LEN]={0};
        char insidegw_master_ip[MIN_LEN]={0};

        char insidegw_backup[MIN_LEN]={0};
        char insidegw_backup_ip[MIN_LEN]={0};
/*
        char tmp1[LEN];
        char tmp2[LEN];
        char tmp3[LEN];
        char tmp4[LEN];
*/

        int mark=0;
        char tmp1[MIN_LEN];
        char tmp2[MIN_LEN];

////    printf("argv[2]=%s\n",ip_addr);

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

                //GGLC
                if(!strcmp("114.66.80.49",ip_addr)) {
                        if(strstr(readbuf,"10.7.0.188")) {
                                //      sscanf(readbuf,"Current session(s):%s",cur_session);
                                //      sscanf(readbuf,"GE4/0/2 188 %s [^0-9]",apn_master);
                                ////    sscanf(readbuf,"%*s %*d %s [^0-9]",apn_master);
                                sscanf(readbuf,"%*s %*d %s %*s %*s %*s %s",apn_master,apn_master_ip);
                                //      sscanf(readbuf,"%*s %*s %s %*s",apn_master);
                                //      printf("apn_master=%s\n",apn_master);
                        ////    printf("apn_master=%s,apn_master_ip=%s\n",apn_master,apn_master_ip);
                        }

                        if(strstr(readbuf,"10.7.0.196")) {
                                //      sscanf(readbuf,"Current session(s):%s",cur_session);
                                //      sscanf(readbuf,"GE4/0/2 188 %s [^0-9]",apn_master);
                                ////    sscanf(readbuf,"%*s %*d %s [^0-9]",apn_backup);
                                sscanf(readbuf,"%*s %*d %s %*s %*s %*s %s",apn_backup,apn_backup_ip);
                                //      sscanf(readbuf,"%*s %*s %s %*s",apn_master);
                                //      printf("apn_backup=%s\n",apn_backup);
                        ////    printf("apn_backup=%s,apn_backup_ip=%s\n",apn_backup,apn_backup_ip);
                        }

                        if(strstr(readbuf,"134.77.67.28")) {
                                //      sscanf(readbuf,"Current session(s):%s",cur_session);
                                //      sscanf(readbuf,"GE4/0/2 188 %s [^0-9]",apn_master);
                                ////    sscanf(readbuf,"%*s %*d %s [^0-9]",sms_status);
                                sscanf(readbuf,"%*s %*d %s %*s %*s %*s %s",sms_master,sms_master_ip);
                                //      sscanf(readbuf,"%*s %*s %s %*s",apn_master);
                                //      printf("sms_status=%s\n",sms_status);
                        ////    printf("sms_master=%s,sms_master_ip=%s\n",sms_master,sms_master_ip);
                        }

                        if(strstr(readbuf,"114.66.80.51")) {
                                //      sscanf(readbuf,"Current session(s):%s",cur_session);
                                //      sscanf(readbuf,"GE4/0/2 188 %s [^0-9]",apn_master);
                                ////    sscanf(readbuf,"%*s %*d %s [^0-9]",sms_status);
                                sscanf(readbuf,"%*s %*d %s %*s %*s %*s %s",insidegw_master,insidegw_master_ip);
                                //      sscanf(readbuf,"%*s %*s %s %*s",apn_master);
                                //      printf("sms_status=%s\n",sms_status);
                        ////    printf("insidegw_master=%s,insidegw_master_ip=%s\n",insidegw_master,insidegw_master_ip);
                        }
                }
                //YZ
                else if(!strcmp("114.66.80.50",ip_addr)) {
                        if(strstr(readbuf,"10.7.0.188")) {
                                //      sscanf(readbuf,"Current session(s):%s",cur_session);
                                //      sscanf(readbuf,"GE4/0/2 188 %s [^0-9]",apn_master);
                                ////    sscanf(readbuf,"%*s %*d %s [^0-9]",apn_master);
                                sscanf(readbuf,"%*s %*d %s %*s %*s %*s %s",apn_backup,apn_backup_ip);
                                //      sscanf(readbuf,"%*s %*s %s %*s",apn_master);
                                //      printf("apn_master=%s\n",apn_master);
                        ////    printf("apn_backup=%s,apn_backup_ip=%s\n",apn_backup,apn_backup_ip);
                        }

                        if(strstr(readbuf,"10.7.0.196")) {
                                //      sscanf(readbuf,"Current session(s):%s",cur_session);
                                //      sscanf(readbuf,"GE4/0/2 188 %s [^0-9]",apn_master);
                                ////    sscanf(readbuf,"%*s %*d %s [^0-9]",apn_backup);
                                sscanf(readbuf,"%*s %*d %s %*s %*s %*s %s",apn_master,apn_master_ip);
                                //      sscanf(readbuf,"%*s %*s %s %*s",apn_master);
                                //      printf("apn_backup=%s\n",apn_backup);
                        ////    printf("apn_master=%s,apn_master_ip=%s\n",apn_master,apn_master_ip);
                        }

                        if(strstr(readbuf,"134.77.67.28")) {
                                //      sscanf(readbuf,"Current session(s):%s",cur_session);
                                //      sscanf(readbuf,"GE4/0/2 188 %s [^0-9]",apn_master);
                                ////    sscanf(readbuf,"%*s %*d %s [^0-9]",sms_status);
                                sscanf(readbuf,"%*s %*d %s %*s %*s %*s %s",sms_backup,sms_backup_ip);
                                //      sscanf(readbuf,"%*s %*s %s %*s",apn_master);
                                //      printf("sms_status=%s\n",sms_status);
                        ////    printf("sms_backup=%s,sms_backup_ip=%s\n",sms_backup,sms_backup_ip);
                        }

                        if(strstr(readbuf,"114.66.80.51")) {
                                //      sscanf(readbuf,"Current session(s):%s",cur_session);
                                //      sscanf(readbuf,"GE4/0/2 188 %s [^0-9]",apn_master);
                                ////    sscanf(readbuf,"%*s %*d %s [^0-9]",sms_status);
                                sscanf(readbuf,"%*s %*d %s %*s %*s %*s %s",insidegw_backup,insidegw_backup_ip);
                                //      sscanf(readbuf,"%*s %*s %s %*s",apn_master);
                                //      printf("sms_status=%s\n",sms_status);
                        ////    printf("insidegw_backup=%s,insidegw_backup_ip=%s\n",insidegw_backup,insidegw_backup_ip);
                        }
                }
/*
                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;
                }
*/
        }

//      printf("------------------------------\n");
        //
        if(!strcmp("114.66.80.49",ip_addr)) {
                if(!strcmp(apn_master_ip,"10.7.0.188") && !strcmp(apn_backup_ip,"10.7.0.196") && !strcmp(sms_master,"Master") && !strcmp(insidegw_master,"Master")) {
                        exitstatus=OK;
                }
                else {
                        exitstatus=CRITICAL;
                }

        //      sprintf(status_information,"gglc_apn_master=%s, gglc_apn_master_ip=%s, gglc_apn_backup=%s, gglc_apn_backup_ip=%s, gglc_sms_status=%s, gglc_sms_status_ip=%s", apn_master, apn_master_ip, apn_backup, apn_backup_ip, sms_status, sms_status_ip);
        //      sprintf(status_information,"gglc_apn_master=%s(%s), gglc_apn_backup=%s(%s), gglc_sms_master=%s(%s), gglc_insidegw_master=%s(%s)", apn_master, apn_master_ip, apn_backup, apn_backup_ip, sms_master, sms_master_ip, insidegw_master, insidegw_master_ip);
                sprintf(status_information,"gglc_apn_master=%s:%s, gglc_apn_backup=%s:%s, gglc_sms_master=%s:%s, gglc_insidegw_master=%s:%s", apn_master, apn_master_ip, apn_backup, apn_backup_ip, sms_master, sms_master_ip, insidegw_master, insidegw_master_ip);
        }
        else if(!strcmp("114.66.80.50",ip_addr)) {
                if(!strcmp(apn_master_ip,"10.7.0.196") && !strcmp(apn_backup_ip,"10.7.0.188") && !strcmp(sms_backup,"Backup") && !strcmp(insidegw_backup,"Backup")) {
                        exitstatus=OK;
                }
                else {
                        exitstatus=CRITICAL;
                }

        //      sprintf(status_information,"yz_apn_master=%s, yz_apn_master_ip=%s, yz_apn_backup=%s, yz_apn_backup_ip=%s, yz_sms_status=%s, yz_sms_status_ip=%s", apn_master, apn_master_ip, apn_backup, apn_backup_ip, sms_status, sms_status_ip);
        //      sprintf(status_information,"yz_apn_master=%s(%s), yz_apn_backup=%s(%s), yz_sms_backup=%s(%s), yz_insidegw_backup=%s(%s)", apn_master, apn_master_ip, apn_backup, apn_backup_ip, sms_backup, sms_backup_ip, insidegw_backup, insidegw_backup_ip);
                sprintf(status_information,"yz_apn_master=%s:%s, yz_apn_backup=%s:%s, yz_sms_backup=%s:%s, yz_insidegw_backup=%s:%s", apn_master, apn_master_ip, apn_backup, apn_backup_ip, sms_backup, sms_backup_ip, insidegw_backup, insidegw_backup_ip);
        }

        //my add log
        if(!strcmp("114.66.80.49",ip_addr)) {
                write_resp_log(status_information);
        }

//      printf("exitstatus=%d\n",exitstatus);
//      printf("------------------------------\n");


//      sprintf(performance_data,"%s","0");
        memset(performance_data,0,LEN);

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

//      printf("line=%d\n",line);

        ret=pclose(fp);
        if(ret==-1) {
                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_route_dis_vrrp.sh + ip");
                exit(-1);
        }

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

        ret=parse_status(sh_cmd,active_status,active_ip_addr,argv[2]);
        if(ret!=0) {
                fprintf(stderr,"parse_status() error.\n");

        //      sprintf(status_information,"cur_all_session=%s, cur_all_session_new=%s, tcp_all_session=%s, tcp_all_session_new=%s, tcp_half_open=%s, tcp_half_close=%s, udp_session=%s, udp_session_new=%s, icmp_session=%s, icmp_session_new=%s, rawip_session=%s, rawip_session_new=%s", cur_session, cur_new_session, tcp_session, tcp_new_session, half_open, half_close, udp_session, udp_new_session, icmp_session, icmp_new_session, rawip_session, rawip_new_session);


                sprintf(status_information,"gglc_apn_master=%s:%s, gglc_apn_backup=%s:%s, gglc_sms_master=%s:%s, gglc_insidegw_master=%s:%s", "unknown", "unknown", "unknown", "unknown", "unknown", "unknown", "unknown", "unknown");

                exitstatus=CRITICAL;

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

                return exitstatus;
        }

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

//      if(atoi(ping_avg)<200 && atoi(loss_packet)==0) {
        if(atoi(ping_avg)<200 && atoi(loss_packet_int)==0) {
                exitstatus=OK;
        }
//      else if(atoi(ping_avg)>=200 && atoi(ping_avg)<500 || atoi(loss_packet)>=10 && atoi(loss_packet)<=50) {
        else if(atoi(ping_avg)>=200 && atoi(ping_avg)<500 || atoi(loss_packet_int)>=10 && atoi(loss_packet_int)<=50) {
                exitstatus=WARNING;
        }
//      else if(atoi(ping_avg)>=500 || atoi(loss_packet)>50) {
        else if(atoi(ping_avg)>=500 || atoi(loss_packet_int)>50) {
                exitstatus=CRITICAL;
        }
        else    {
                exitstatus=CRITICAL;
        }


//      sprintf(status_information,"rta %s%s, loss %s",ping_avg,ping_unit,loss_packet);
        sprintf(status_information,"rta %s%s, loss %s%%",ping_avg,ping_unit,loss_packet_int);

//      sprintf(performance_data,"rta=%s%s;200.000;500.000;0; pl=%s;40;80;; rtmax=%s%s;;;; rtmin=%s%s;;;;",ping_avg,ping_unit,loss_packet,ping_max,ping_unit,ping_min,ping_unit); 
        sprintf(performance_data,"rta=%s%s;200.000;500.000;0; pl=%s%%;40;80;; rtmax=%s%s;;;; rtmin=%s%s;;;;",ping_avg,ping_unit,loss_packet_int,ping_max,ping_unit,ping_min,ping_unit); 

        //|rta=0.056ms;200.000;500.000;0; pl=0%;40;80;; rtmax=0.084ms;;;; rtmin=0.029ms;;;; 

        if(strstr(argv[1],"80_49")) {
                printf("%s - 192.20.198.121: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
        }

        if(strstr(argv[1],"80_50")) {
                printf("%s - 192.20.198.181: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
        }

        return exitstatus;
        */


/*      sprintf(status_information,"cur_all_session=%s, cur_all_session_new=%s, tcp_all_session=%s, tcp_all_session_new=%s, tcp_half_open=%s, tcp_half_close=%s, udp_session=%s, udp_session_new=%s, icmp_session=%s, icmp_session_new=%s, rawip_session=%s, rawip_session_new=%s", cur_session, cur_new_session, tcp_session, tcp_new_session, half_open, half_close, udp_session, udp_new_session, icmp_session, icmp_new_session, rawip_session, rawip_new_session);

        sprintf(performance_data,"cur_all_session=%s;;;; cur_all_session_new=%s;;;; tcp_all_session=%s;;;; tcp_all_session_new=%s;;;; tcp_half_open=%s;;;; tcp_half_close=%s;;;; udp_session=%s;;;; udp_session_new=%s;;;; icmp_session=%s;;;; icmp_session_new=%s;;;; rawip_session=%s;;;; rawip_session_new=%s;;;;", cur_session, cur_new_session, tcp_session, tcp_new_session, half_open, half_close, udp_session, udp_new_session, icmp_session, icmp_new_session, rawip_session, rawip_new_session);
*/

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

        return exitstatus;
}

执行方法如下:

./router_check_apn_vrrp auto_ssh_route_dis_vrrp.sh + ipaddr

你可能感兴趣的:(nagios插件之登陆router监控vrrp状态)