Login router of Nagios plugin for ping monitoring

Source: Internet
Author: User
Tags sprintf strtok

Router_check_apn_ping.c

#include <stdio.h> #include <stdlib.h> #include <string.h> #define OK 0#define WARNING 1#define crit ICAL 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; }return0;} int main (int argc, char *argv[]) {int Ret;char sh_cmd[len];char Active_status[len];char active_ip_addr[len];if (arg C<=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;}


The script that is called 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 + 10.7.0.186\r" expect "*ir t* "Send" quit "#expect EOF

Login router of Nagios plugin for ping monitoring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.