Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm just barely getting into programming so I do apologize for my ignorance. I'm trying to create a .bat file that will check if a service is running on XP Pro.

If service is running it will exit 0. If the service is stopped start service wait 10 seconds (via ping i'm guessing) check if service is running if service is running exit 0 if service is stopped start service wait 10 seconds

Do this check a total of 3 times. if service does not come up within that time: exit 2

Exit 0 = ok exit 1 = warning exit 3 = critical (and this will alert)

I need to do this for 3 different services but i'm expecting that it would be better to create one per service. That way you get notified on the specific service that is not coming back up.

The goal is that if the service stops it will start it. If after 30 seconds it is unable to start the service then it will send an alert.

The reason I'm trying to do it with a .bat is this is consistent with all other scripts and I did not want to complicate it further by adding different kinds of code. Yay for consistency!

Again I do apologize for my ignorance I've been thrown into this project last minute.

Thank you for the help and reading my question!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Checking to see if a service is running is actually built in to NSClient++. I'm restarting the service if it's down with an NRPE event handler. Basically, if the service is stopped, NRPE will run a script.

The script is

@echo off
net start %1
@exit 0

and I've defined the event handler in nsclient.ini as, for example:

restartwsus=scripts\runcmd.bat wsusservice

under the ; A list of scripts available to run from the CheckExternalScripts module. Syntax is: <command>=<script> <arguments> header.

(Restart NSClient++)

On the Nagios server, I've defined the command in commands.cfg as:

define command{
 command_name restartwsus
 command_line /usr/lib/nagios/plugins/check_nrpe -H '$HOSTADDRESS$' -c restartwsus
}

and have defined the service as:

define service{
        use                     generic-service
        host_name               wsusserver
        service_description     WSUS
        contacts                me
        notification_options    w,c,r
        notification_period     24x7
        notification_interval   0
        check_command           check_nt!SERVICESTATE!-d SHOWALL -l WsusService
        event_handler           restartwsus
        }

I hope that helps.

share|improve this answer
    
Hey Katherine, Thanks for the help! I'll test the script in a lab and let you know how it works. The nagios client is installed on these systems so I know NSClient++ is in use. Do you know how to make it try 3x and if it fails to start the service it goes to a critical status? –  LbakerIT Jun 28 '13 at 21:50
    
Trying a service check three times and alerting on the fourth is default Nagios behavior, so you should be good. –  Katherine Villyard Jun 28 '13 at 21:53
    
Hi Katherine, I know it's been one year since you wrote all this, but i'm kinda stuck. I've followed every step and i still encounter one error: Either 'No handler for command:restartwsus' or 'No handler for command:script\run.bat' I did not have that section in my ini file, so i created a section: [settings/external scripts/wrapped] as i found online, but i'm still getting that error. Any idea? I'm even just trying the command /usr/lib/nagios/plugins/check_nrpe -H '$HOSTADDRESS$' -c restartwsus to test faster and i also get that, but i see it on the host log, so i know it arrives there... –  Horaceman Aug 6 at 18:26
1  
It worked using the following section: [/settings/external scripts/scripts] The rest of the instructions are perfect. Thanks so much Katherine! –  Horaceman Aug 7 at 10:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.