Batch script to monitor ORDS service and send mail notification it goes down

 

Hi Everyone,

 

Recently I came across an issue where ords service on windows was getting stopped due to some reason. It was hard to monitor as they had no mechanism to monitor this.

 

To mitigate the problem, I wrote a batch script which will monitor ords service’s uptime on windows . The script will send a mail once notifying service has gone down.

 

Save the following script in .bat format and schedule it as per your requirement in task scheduler.

 

@echo off

setlocal enabledelayedexpansion

set my_date=%date%

set my_time=%TIME%

set my_hours=%my_time:~0,2%

set my_minutes=%my_time:~3,2%

set my_seconds=%my_time:~6,2%

 

REM Define variables

set APEX_URL=http://server1.example.com:7799/ords

set MAIL_RECIPIENT='soumya.das@testmail.com'

set SMTP_SERVER=smtp.office365.com

set SMTP_PORT=587

set MAIL_SENDER=notification@example.com

set MAIL_SUBJECT="Oracle APEX URL Status"

set SMTP_USER=emr-notification@example.com

set SMTP_PASSWORD=xyz123#

set SUBJECT="Apex down Alert"

set HOSTNAME=%COMPUTERNAME%

 

REM Check if the URL is accessible

curl -s -o NUL -w "%%{http_code}" %APEX_URL% > D:\response.txt

 

REM Check if D:\response.txt exists

if not exist D:\response.txt (

    REM If D:\response.txt is not present, send email notification

               powershell.exe -command "Send-MailMessage -From '%MAIL_SENDER%' -To %MAIL_RECIPIENT% -Subject '%SUBJECT%' -Body 'Oracle APEX URL is not accessible at %HOSTNAME% .Apex Service is down.' -SmtpServer '%SMTP_SERVER%' -Port %SMTP_PORT% -UseSsl -Credential (New-Object PSCredential('%SMTP_USER%', (ConvertTo-SecureString '%SMTP_PASSWORD%' -AsPlainText -Force)))"

)

 

REM Read the HTTP status code

set /p HTTP_STATUS=<D:\response.txt

 

REM Check if HTTP status code is 000, indicating a failure

if "%HTTP_STATUS%" equ "503" (

    powershell.exe -command "Send-MailMessage -From '%MAIL_SENDER%' -To %MAIL_RECIPIENT% -Subject '%SUBJECT%' -Body 'Oracle APEX URL is not accessible at %HOSTNAME%.Apex Service is down.' -SmtpServer '%SMTP_SERVER%' -Port %SMTP_PORT% -UseSsl -Credential (New-Object PSCredential('%SMTP_USER%', (ConvertTo-SecureString '%SMTP_PASSWORD%' -AsPlainText -Force)))"

) else if "%HTTP_STATUS%" equ "000" (

              powershell.exe -command "Send-MailMessage -From '%MAIL_SENDER%' -To %MAIL_RECIPIENT% -Subject '%SUBJECT%' -Body 'Oracle APEX URL is not accessible at %HOSTNAME%.Apex Service is down.' -SmtpServer '%SMTP_SERVER%' -Port %SMTP_PORT% -UseSsl -Credential (New-Object PSCredential('%SMTP_USER%', (ConvertTo-SecureString '%SMTP_PASSWORD%' -AsPlainText -Force)))"

)

 

REM Cleanup temporary files

del D:\response.txt

 

endlocal

 

No comments:

Post a Comment