码迷,mamicode.com
首页 > 移动开发 > 详细

nagios插件之登陆路由器实现ping监控

时间:2015-02-14 16:15:16      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

router_check_apn_ping.c

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

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

#define LEN 1000

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

//
char loss_packet[32];
char ping_min[32];
char ping_avg[32];
char ping_max[32];
char ping_unit[32];

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(strstr(readbuf,"loss")) {
			sscanf(readbuf,"%s packet loss",loss_packet);
		//	printf("loss_packet=%s\n",loss_packet);
		}

		if(strstr(readbuf,"round-trip")) {
		//	printf("readbf=%s",readbuf);
			sscanf(readbuf," round-trip min/avg/max = %[0-9]/%[0-9]/%[0-9] %s",ping_min,ping_avg,ping_max,ping_unit);
		//	printf("ping_min=%s,ping_avg=%s,ping_max=%s,ping_unit=%s\n",ping_min,ping_avg,ping_max,ping_unit);
		}
/*
		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_route01_gglc_80_49.sh | auto_ssh_route01_gglc_80_50.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(atoi(ping_avg)<200) {
		exitstatus=OK;	
	}
	else if(atoi(ping_avg)>=200 && atoi(ping_avg)<500) {
		exitstatus=WARNING;	
	}
	else if(atoi(ping_avg)>=500) {
		exitstatus=CRITICAL;	
	}
	else	{
		exitstatus=CRITICAL;	
	}


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

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

	//|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 - 10.7.0.186: %s | %s\n",exit_status[exitstatus],status_information,performance_data);
	}

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

        return exitstatus;
}


调用的脚本 auto_ssh_route01_gglc_80_49.sh

#!/usr/bin/expect -f

#set port 22
set user xxxooo
set host 114.66.80.49
set password xxxooo@2014
set timeout 30

spawn ssh $user@$host

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

expect "*IRT*"
send "ping -c 5 -m 1000 10.7.0.186\r"

expect "*IRT*"
send "quit"
#expect eof

nagios插件之登陆路由器实现ping监控

标签:

原文地址:http://blog.csdn.net/ccjsj1/article/details/43817651

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!