nagios外掛程式之登陸路由器實現ping監控

來源:互聯網
上載者:User

標籤:

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;//Sunmark1++;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 22set user xxxoooset host 114.66.80.49set password [email protected]set timeout 30spawn ssh [email protected]$hostexpect "*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監控

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.