Home Directory

Search Exchange

Search All Sites

Nagios Live Webinars

Let our experts show you how Nagios can help your organization.

Contact Us

Phone: 1-888-NAGIOS-1
Email: sales@nagios.com

Login

Remember Me

Directory Tree

Directory

briandent

Reviews(1)
bybriandent, September 20, 2012
1 of 1 people found this review helpful
This is an excellent script to call from within Windows to verify another device is available.

I've added three enhancements: 1) randomized the temp file name to avoid a collision if called concurrently (eg: two nagios servers), 2) explicitly delete the temp file and 3) added perfdata in the return.

I also capitalized the file.

@ECHO OFF
SETLOCAL


REM ####### ENSURE ALL REQUIRED INFO IS PRESENT --UNIX VAR--######
@ECHO %1%2%3%4%5%6|FIND "ARG"
IF NOT ERRORLEVEL 1 GOTO MSEOF

REM ####### ENSURE ALL REQUIRED INFO IS PRESENT --WIN VAR--######
IF "%1"=="" GOTO MSEOF
IF "%2"=="" GOTO MSEOF
IF "%3"=="" GOTO MSEOF
IF "%4"=="" GOTO MSEOF
IF "%5"=="" GOTO MSEOF
IF "%6"=="" GOTO MSEOF

REM PAUSE

@ECHO %4 |FIND "%%%"
IF NOT ERRORLEVEL 1 GOTO MSEOF
@ECHO %6 |FIND "%%%"
IF NOT ERRORLEVEL 1 GOTO MSEOF



REM ####### ASSIGN EACH TO A VARIABLE TO REFERENCE IT LATER..######
SET IP=%1
SET PKT=%2
SET WRTA=%3
SET WPL=%4
SET CRTA=%5
SET CPL=%6

REM ######## CAPTURE FRESH DATA TO A FILE #######
REM 20120920 TO AVOID COLLISIONS DURING CONCURRENT CALLS CREATE A SEMI-RANDOM FILE NAME
SET RANDOMFILENAME=%RANDOM%-%1-TMP
@ECHO ->%RANDOMFILENAME%

PING %IP% -n %PKT% >>%RANDOMFILENAME%

REM ######## PICKOUT THE DATA WE NEED FROM THE FILE #######
FOR /F "TOKENS=11 DELIMS= " %%K IN ('findstr /c:"Lost" %RANDOMFILENAME%') DO SET LST=%%K

IF ERRORLEVEL 1 GOTO TIMEOUT
FOR /F "TOKENS=9 DELIMS= " %%K IN ('findstr /c:"Average" %RANDOMFILENAME%') DO SET AVG=%%K


REM PAUSE
DEL /Q %RANDOMFILENAME%


REM ######## TRIM THE VARIABLES...####
SET AVG=%AVG:M=%
SET AVG=%AVG:S=%

:TIMEOUT
SET LST=%LST:(=%
SET LST=%LST:~0,-1%


REM ######## NOW THE FUN STUFF, COMPARE THE WARNING, CRITICAL VALUES..####

IF %LST% GEQ %CPL% GOTO CPL-2
IF %AVG% GEQ %CRTA% GOTO CRTA-2
IF %LST% GEQ %WPL% GOTO WPL-1
IF %AVG% GEQ %WRTA% GOTO WRTA-1

REM PAUSE

GOTO OK-0

:CPL-2
@ECHO REMOTE CONNECTION TO %IP% IS DOWN: LOSS=%LST%%% (%PKT% PKTS)^|rta=%CRTA%;%WRTA% pl=%LST%%%;%WPL%;%CPL%
REM GOTO EOF
@EXIT 2

:CRTA-2
@ECHO REMOTE CONNECTION TO %IP% IS DOWN: LOSS=%LST%%%, AVE=%AVG%ms (%PKT% PKTS)^|rta=%AVG%ms;%WRTA%;%CRTA% pl=%LST%%%;%WPL%;%CPL%
REM GOTO EOF
@EXIT 2

:WPL-1
@ECHO REMOTE CONNECTION TO %IP% IS SLOW: LOSS=%LST%%%, AVE=%AVG%ms (%PKT% PKTS)^|rta=%WRTA%;%CRTA% pl=%LST%%%;%WPL%;%CPL%
REM GOTO EOF
@EXIT 1

:WRTA-1
@ECHO REMOTE CONNECTION TO %IP% IS SLOW: LOSS=%LST%%%, AVE=%AVG%ms (%PKT% PKTS)^|rta=%AVG%ms;%WRTA%;%CRTA% pl=%LST%%%;%WPL%;%CPL%
REM GOTO EOF
@EXIT 1



:OK-0
@ECHO REMOTE CONNECTION TO %IP% IS UP: LOSS=%LST%%%, AVE=%AVG%ms (%PKT% PKTS)^|rta=%AVG%ms;%WRTA%;%CRTA% pl=%LST%%%;%WPL%;%CPL%
REM GOTO EOF
@EXIT 0




:MSEOF
@ECHO USAGE:PING_REMOTE ^ ^^,^ ^,^
@ECHO EXAMPLE: /USR/LOCAL/NAGIOS/LIBEXEC/CHECK_NRPE -H 172.19.48.139 -C PING_REMOTE -T 90 -A 172.19.88.30 65 450,1 700,5 (FROM NAGIOS SVR.)
@ECHO : PING_REMOTE 192.168.0.1 5 200,1 400,10 (FROM A LOCAL WIN WKS WHERE PING_REMOTE.BAT RESIDES)
@ECHO : (WPL\CPL ARE IN PERCENT, WITHOUT THE "%%%" SYMBOL!!)

REM GOTO EOF
@EXIT 0



:EOF