Batch file, CLI, Discoverer, Hints and Tips, hobby, Hyperion, Monitor, Oracle, Services, Windows, Windows Services

Monitoring Windows Services – The quick and dirty way

You’ve probably heard me mention that I don’t like for my users to be my “system unavailable” alerts.  I want to know as soon as, if not before, they do.  The challenge is how to monitor these service states (Discoverer, Hyperion, Informatica, and Glassfish) on Windows boxes in the absence of some tool I have to purchase.  I did a bit of research and have, from a number of other sites, combined some solutions to developed a quick and dirty way to check the status of Windows services right here from my desktop.  If you haven’t already, check out the ‘sc’ command.  There’s a lot of stuff you can do.  I am going to focus on the ‘query’ option and creating a .bat file to check your services.

Here is my code: (Feel free to copy)
—————————————————————————–
@echo off

:start

FOR /F “tokens=1-2 delims= ” %%x in filename.txt) DO (^
for /F “tokens=3 delims=: ” %%D in (‘sc %%x query %%y ^| findstr ”        STATE”‘) do (^
if %%D NEQ RUNNING (
msg dbryant %%y %%D on %%x reported at %time%
echo %%y %%D on %%x reported at %time%
echo %%y %%D on %%x reported at %time% >> svclog.txt
)

)
)

echo Next poll in 5 seconds
ping -n 5 127.0.0.1>nul
goto start
——————————————————————————-

Let’s break this down a bit:

Create a file with the following construct ‘\servername servicename  and list your servers and services one per line.

example:
\DISCOVER-02 Glassfish
\ETL-01 AxInstSV
\ETL-02 AxInstSV
\APEX-03 Glassfish
\DISCOVERER-02 OracleCSService
\DISCOVERER-02 Oracleoracleas10GTNSListener
\DISCOVERER-02 OracleServiceDISCO32

and save it as filename.txt

Create a .bat file and for ease of use, save in the same directory as the .txt file.
The important part in the BAT file (example above) is:

for /F “tokens=3 delims=: ” %%H in (‘sc %%x query %%y ^| findstr ”        STATE”‘) do (^
if %%H NEQ RUNNING (

The preceding line is for looping through the text file.

Using the first line of the text file, this line, in essence executes the following:

sc \DISCOVER-02 query Glassfish
and looks for the service STATE to return its status.

    If the STATE is not equal to RUNNING, write the server name, service name, and date to a file, and send a console message to the user’s desktop.
    If the service is running, loop to the next line in the text file.
    This line: ping -n 5 127.0.0.1>nul  is a way to insert a ‘pause’ that will resume without the user intervention unlike the ‘PAUSE’ command. You can change the parameter for the -n switch to increase the time of the pause.  It might be a little overhead because it’s pinging, but not enough to be concerned about.  
    Guess that’s it for now.  Not really oracle per se, but you can definitely use it to find out if oracle services are running or not.
    Related Posts Plugin for WordPress, Blogger...

    Leave a Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.