0

I need to configure a service to on Oracle application server to test connectivity with database and then send an email notification about status. Can anyone help me please.

Thanks,

1 Answers1

0

Here's one option:

  • TNSPING database you're interested in
  • evaluate the result (you're interested in the last line)
  • send the result by e-mail

A few remarks:

  • tnsping %1 > ping_result.txt pings database passed as a parameter (%1) and stores the result into a .txt file
  • middle part of a script is modified code written by @Aacini (you'll see a link)
  • finally, mailing the result is done by bmail (you can use any other command line SMTP mailer; or, Google for "bmail download" - it is free)

Here you go:

rem PING_DB.BAT

@echo off
setlocal EnableDelayedExpansion

tnsping %1 > ping_result.txt

rem Littlefoot slightly adjusted code: https://stackoverflow.com/questions/27416341/how-can-i-read-the-last-2-lines-of-a-file-in-batch-script
rem Tail command in pure Batch, version 2: Tail.bat filename numOfLines
rem Antonio Perez Ayala

set /A firstTail=1, lastTail=0
for /F "delims=" %%a in (ping_result.txt) do (
   set /A lastTail+=1, lines=lastTail-firstTail+1
   set "lastLine[!lastTail!]=%%a"
   if !lines! gtr 1 (
      set "lastLine[!firstTail!]="
      set /A firstTail+=1
   )
)
for /L %%i in (%firstTail%,1,%lastTail%) do set result=!lastLine[%%i]!

rem end of LF's adjustment

echo %result%

echo off
bmail -s smtp.server.name -f tnspinger@me.com -t my_real_mail@gmail.com -a "%result%" -c

bmail's options:

Command Line SMTP Emailer V1.07
Copyright(C) 2002-2004 Craig.Peacock@beyondlogic.org
Date: Wed, 20 Jun 2018 10:08:15 +0200
Usage: bmail [options]
        -s    SMTP Server Name
        -p    SMTP Port Number (optional, defaults to 25)
        -t    To: Address
        -f    From: Address
        -b    Text Body of Message (optional)
        -h    Generate Headers
        -a    Subject (optional)
        -m    Filename (optional) Use file as Body of Message
        -c    Prefix above file with CR/LF to separate body from header
        -d    Debug (Show all mail server communications)

Testing (I've removed bmail's output):

M:\>ping_db xe
TNS-12541: TNS:no listener     --> this is being sent by e-mail

M:\>ping_db orcl
OK (10 msec)                   --> this is being sent by e-mail
Littlefoot
  • 131,892
  • 15
  • 35
  • 57
  • Thank you for looking into this. I am new student so can you please elaborate it more step vise how can i do it. Appreciate your help. My database is sqlserver. – user9812642 Jun 20 '18 at 15:57
  • I've said everything I meant to say. I don't know what kind of an *elaboration* you expect; I believe example & explanation I provided is quite enough, especially if your database is MS SQL Server, while the question is tagged as Oracle. Make up your mind. – Littlefoot Jun 20 '18 at 20:02
  • Thank you so much for your help. Its my mistake I tagged the post with Oracle. I should tag it with SQL server. – user9812642 Jun 22 '18 at 13:33
  • All other things are clear to me just need one help, for TNSPING database i am not clear how to ping sql server database from TNSPING? any help ? – user9812642 Jun 22 '18 at 13:34