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

Popular posts from this blog

non-existent process lock port on windows server

I have a database link created between oracle and sqlserver using oracle tg4odbc, the product is installed on windows server and run as service "OracleOraGtw11g_home1TNSListener", but sometime the service cannot started, the root cause of this problem is that the port number 1521 is used by an non-existent process. The first step is to use netstat -bano|find "1521" to get the process id, in my case it's 5844, which shows the connection is from my oracle server 10.8.0.169 H:\>netstat -bano|find "1521"   TCP    0.0.0.0:1521           0.0.0.0:0              LISTENING       5844   TCP    10.14.45.33:1521       10.8.0.169:42987       ESTABLISHED     5844 however the process id does not show in either task manager or process explorer. The next step is to run tcpview, which shows non-existent under process column, there are three rows, two show status as "listening", the other one shows status "established", right click and k

Opatch apply/lsinventory error: oneoff is corrupted or does not exist

I am applying the quarterly patch for 19c RDBMS, I tried using napply but failed, but somehow it corrupted the inventory though nothing applied. further apply and lsinventory command ran into error like this: $ ./OPatch/opatch lsinventory Oracle Interim Patch Installer version 12.2.0.1.21 Copyright (c) 2020, Oracle Corporation.  All rights reserved. Oracle Home       : /u02/app/oracle/19.0.0 Central Inventory : /u01/app/oraInventory    from           : /u02/app/oracle/19.0.0/oraInst.loc OPatch version    : 12.2.0.1.21 OUI version       : 12.2.0.7.0 Log file location : /u02/app/oracle/19.0.0/cfgtoollogs/opatch/opatch2020-09-08_13-35-59PM_1.log Lsinventory Output file location : /u02/app/oracle/19.0.0/cfgtoollogs/opatch/lsinv/lsinventory2020-09-08_13-35-59PM.txt -------------------------------------------------------------------------------- Inventory load failed... OPatch cannot load inventory for the given Oracle Home. LsInventorySession failed: Unable to create patchObject Possible ca

shell script to clean up oracle dumpfile

https://github.com/iacosta/personal/blob/master/shells/cleanhouse.sh #!/bin/ksh # # Script used to cleanup any Oracle environment. # # Cleans:      audit_file_dest #              background_dump_dest #              core_dump_dest #              user_dump_dest #              Clusterware logs # # Rotates:     Alert Logs #              Listener Logs # # Scheduling:  00 00 * * * /networkdrive/dba/scripts/purge/cleanup.sh -d 7 > /u01/dba/bin/cleanup.log 2>&1 # # Created By:  Lei Dao # # # RM="rm -f" RMDIR="rm -rf" LS="ls -l" MV="mv" TOUCH="touch" TESTTOUCH="echo touch" TESTMV="echo mv" TESTRM=$LS TESTRMDIR=$LS SUCCESS=0 FAILURE=1 TEST=0 HOSTNAME=`hostname` ORAENV="oraenv" TODAY=`date +%Y%m%d` ORIGPATH=/usr/local/bin:$PATH ORIGLD=$LD_LIBRARY_PATH export PATH=$ORIGPATH # Usage function. f_usage(){   echo "Usage: `basename $0` -d DAYS [-a DAYS] [-b DAYS] [