0

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!

LbakerIT
  • 67
  • 1
  • 8

1 Answers1

1

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.

Katherine Villyard
  • 18,550
  • 4
  • 37
  • 59
  • 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 06 '14 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 07 '14 at 10:48