Restarting Windows Services

dchatterton's picture

Proactive monitoring continues to prove its significance for all types of organizations. Rather than just simply taking preventive measures, being proactive allows you to have high visibility over the various components of your IT environment. As a way of showcasing the value of proactive monitoring, this post provides easy-to-follow steps on how to configure Opsview to restart Windows services, with a print stooler being our specific example. 

Configuring the Windows host

On your Windows host, you must have NSClient++ installed and configured. You can create any batch script to restart services as seen fit for your system. Here is an example script that will take a service as an argument from Opsview, and then restart it on the Windows host:

@echo off
net stop %1
net start %1
@exit 0

Once you have added this script, save it as runcmd.bat to the scripts directory under the NSClient++ program files. Next, add the following line to nsc.ini:

runcmd=scripts\runcmd.bat $ARG1$

We can test the script as the Nagios user on the master server by running:

nagios@ov-trn:/usr/local/nagios/libexec$ ./check_nrpe -H -p 5666 -c runcmd -a spooler

The Print Spooler service is stopping.
The Print Spooler service was stopped successfully.
The Print Spooler service is starting.
The Print Spooler service was started successfully.

Next, create your service check. I have created a Print Spooler status check below:

NB: the exact service check arguments, ‘\”Name of Service\’”

Restarting Windows Services

Within the Service Check, go to ‘Advanced’ and enter the Event Handler listed below:

windows_service_restart $SERVICESTATE$ $HOSTADDRESS$ spooler

Restarting Windows Services

On the Opsview master, create a file called ‘windows_service_restart’ in /usr/local/nagios/libexec/eventhandlers with contents:

# Event Handler for Restarting Windows Services
case "$1" in
              /usr/local/nagios/libexec/check_nrpe -H "$2" -p 5666 -c runcmd -a "$3"
exit 0

Next, make the file executable and owned by Nagios:

chmod +x windows_service_restart
chown nagios. windows_service_restart

Once complete, reload Opsview, then stop the Print Spooler on your Windows host. By the second failure in Opsview for the service check, the Spooler will be restarted.

You can apply the logic above to ANY service you wish to restart. For example, if you want to restart the IIS Admin Service, you would just change the $3 variable in the EVENTHANDLER box to ‘IISADMIN’ instead of ‘spooler’.