Skip to main content

getty to test URLs, send email alert when URL fail

This is the Powershell script:

#This script monitor DS critical URLs, send email alert if URL is not "0% fail"
$ErrorActionPreference = 'SilentlyContinue'
c:
cd C:\DBA\bin\getty
$URLSource="c:\dba\bin\getty\URLsource.txt"
$LOG="c:\dba\bin\getty\DSMonitoringURLsPS.log"
$GETTYDIR="C:\DBA\bin\getty"
$GETTY="C:\DBA\bin\getty\getty_NO_Save_Log.bat"
remove-item -Path $LOG -ErrorAction Ignore
#foreach($ThisURL in Get-Content $URLSource| Where {$_ -notmatch '^#.*'})
foreach($ThisURL in Get-Content $URLSource)
 {
if ($ThisURL -notmatch '^#.*')
{
$ThisURL=$ThisURL.trim()
cd $GETTYDIR
& $GETTY $ThisURL 3 >> $LOG
}
else
{
"skipped checking this URL :"+$ThisURL >> $LOG
}
 }
if (!(Test-Path $LOG)) {
  Write-Warning "outputfile absent, exiting"
  exit
}
$SEL = get-content $LOG|select-string " fail"|select-string -notMatch -simplematch "(0% fail)"
if ($SEL -ne $null)
{
    echo "found URLs errors"
$Recipient="me@mycompany.com"
$Sender=$env:computername+"@mycompany.com"
$MailServer="mail.mycompany.com"
$Subj="DS monitored critical urls failed"
Send-MailMessage -From $sender -To $recipient -Subject $subj -Body "Check the attachment for information" -Attachments $LOG -DeliveryNotificationOption OnSuccess, OnFailure -SmtpServer $MailServer
}
else
{
    echo "does not found keyword"
}


Now this is the getty.bat which is called from above powershell script:
:: getty.bat
@echo off
SETLOCAL EnableDelayedExpansion

GOTO :START

:: ABOUT/README
::
:: VERSION/HISTORY
::      2018-JAN-17 R.Ross (CSA)    1.2 - build base agent string from wget --version
::                                      - added user-specified string to append to agent
::                                        to make it easier to parse HTTP access logs
::                                      - added attempt counter at end of agent string
::                                        when attempts > 1
::      2017-DEC-20 R.Ross (CSA)    1.0 - created

:USAGE --- echo usage hint
   echo.
   echo.: %_name% %_version% (repeatedly GET something, like a PING but with HTTP)
   echo.:
   echo.:  USAGE: %_namelower% ^<URL^> [^<attempts^>] ["<append-agent-string>"]
   echo.: LEGEND: .)OK x)NOT-OK
   echo.:
   echo.: See %_namelower%.log for detailed output. Requires WGET.
   echo.
   GOTO :END

:START

:: --- display usage for missing required args
   if "%1"=="" GOTO :USAGE


:: -- check if WGET is available
    WHERE wget >nul 2>nul
    IF %ERRORLEVEL% NEQ 0 (
        echo.
        echo ERROR: %_name% requires WGET ^(https://www.gnu.org/software/wget/^)
        GOTO :END
    )


:: INTERPRET ARGS
    :: default
    set _runs=0

    set _url=%1

    :: args for numericity re: run count
    IF NOT [%2] == [] ( if "%_runs%"=="0" set /a "_runs=%2" )
    IF NOT [%3] == [] ( if "%_runs%"=="0" set /a "_runs=%3" )
    IF "%_runs%"=="0" (
        set /a _runs=1
        set _custagent="%~2"
    ) ELSE (
        set _custagent="%~3"
    )
 
    :: strip delimiters from args (just in case)
    set _custagent=%_custagent:/=%
    set _custagent=%_custagent:-=%
    set _custagent=%_custagent:"=%

    FOR /F "delims=" %%i IN ('wget --version ^| find /n /i "wget" ^| find "[1]"') DO (
        set _agent=%%i
        set _agent=!_agent:[1]=!
        set _agent=!_agent: built on =+!
        set _agent=!_agent:. =!
    )


    :: user-specified agent/bot string or derive from WGET
    IF NOT "%_custagent%"=="" (
        set _agent=%_agent%+%_custagent%
    )




:: CONFIG
    set _logfile=%_namelower%.log
    set _tmplogfile=%_namelower%.log.tmp
    set _useragent=%_name%/%_version% (%_agent%)
    set _basewgetargs=--no-check-certificate --no-dns-cache --no-cache --output-file=%_tmplogfile% --output-document=%_namelower%.tmp --no-verbose
    set _tmpfile=%_namelower%.tmp
    set /a _runcount=0


:: ASSEMBLE BASE URL+PATH FROM CONFIG
    ::set _url=%_protocol%://%_host%:%_port%/%_path%
    set _fails=0
    set _goods=0
    set _storederrorlevel=0

:: RESET LOG and TMP file
    echo %_runs% x wget %_url% @ %date% %time% > %_logfile%
    IF EXIST %_tmplogfile% DEL %_tmplogfile%

:: GREET ETC
    echo.
    <nul set /p=%_runs% x wget %_url% @ %date% %time% as %_useragent: =+%
    echo.

:: DO IT
    for /l %%i in (1, 1, %_runs%) do (

        set /a _runcount=!_runcount!+1

        :: build agent string dynamically with attempt # baked in if  runs > 1
        IF "%_runs%" NEQ "1" (
            set _wgetargs=!_basewgetargs! --user-agent="!_useragent!x!_runcount!"
        ) ELSE (
            set _wgetargs=!_basewgetargs! --user-agent="!_useragent!"
        )


        set _storederrorlevel=1
        wget !_wgetargs! %_url% && (
            set _storederrorlevel=0
        ) || (
            set _storederrorlevel=1
        )

        if !_storederrorlevel! EQU 0 (
            <nul set /p=.
            set _crawl=!_crawl!.
            set /a _goods=!_goods! + 1
        ) else (
            <nul set /p=x
            set _crawl=!_crawl!x
            set /a _fails=!_fails! + 1

            :: append tmplog to log
            findstr "ERROR" %_tmplogfile% >> %_logfile%
        )

        IF EXIST %_tmpfile% del %_tmpfile%
        IF EXIST %_tmplogfile% del %_tmplogfile%

    )
    echo.

    set /a _pctfail=(!_fails!*100)/!_runs!

    echo.
    <nul set /p=%_goods%/%_runs% OK ^(%_pctfail%%% fail^)
    echo %_goods%/%_runs% OK ^(%_pctfail%%% fail^) >> %_logfile%


    :: delete log if no fails, append count if fails
    if !_fails! EQU 0 (
        IF EXIST %_logfile% DEL %_logfile%
    ) else (
        IF !ERRORLEVEL! EQU 1 (
            findstr "ERROR" %_logfile%
        ) else (
            DEL %_logfile%
        )
    )



GOTO :END


:END
    echo.
    ENDLOCAL

Comments