2010/11/16

Nagios: Restart a Windows Failed Services (cmd)

CURRENT CONFIGURATION: Linux OpenSuSe, Nagios 3.0.2, NRPE; Windows 2003 x64 server, NSClient++ 0.3.8.76

OBJECTIVE: Building a Self-Healing Network

ISSUE: Need some script to Restart failed Windows Services by Nagios Client

SOLUTION:
I created and tested win_service_restart.cmd batch file on Microsoft Windows 2003 servers.

--- Start of code ---

@echo off
:: *****************************************************************************
:: File:    win_service_restart.cmd
:: Author:  Vadims Zenins http://vadimszenins.blogspot.com
:: Version: 1.07
:: Date:    16/11/2010 12:28:45
:: Windows Failed Service restart batch file for Nagios Event Handler
::
::  Copy win_service_restart.cmd to \NSClient++\scripts\ folder.
::
:: Nagios commands.cfg:
:: define command{
::        command_name    win_service_restart
::        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -p 5666 -c win_service_restart -a "$SERVICEDESC$" $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$
::        }
::
:: Nagios template-services_common-win.cfg
:: define service{
::         name                    generic-service-win-wuauserv
::         service_description     wuauserv
::         display_name            Automatic Updates
::         event_handler           win_service_restart
::         event_handler_enabled   1
::         check_command           check_nt!SERVICESTATE!-d SHOWALL -l $SERVICEDESC$
::         }
::
:: NSCLIENT++ version 0.3.8 NSC.ini:
::   [Settings]
::   allowed_hosts=192.168.1.1/32  ; your Nagios server IP
::   [NRPE]
::   allow_arguments=1
::   allow_nasty_meta_chars=1
::   [Script Wrappings]
::   cmd=scripts\%SCRIPT% %ARGS%
::   [External Script]
::   allow_arguments=1
::   allow_nasty_meta_chars=1
::   [External Scripts]
::   command[win_service_restart]=scripts\win_service_restart.cmd "$ARG1$" $ARG2$ $ARG3$ $ARG4$
::
::
:: Additional examples on http://vadimszenins.blogspot.com/2008/12/nagios-restart-windows-failed-services.html
::
:: Tested platform:
:: Windows 2003 R2 x64 SP2, Nagios 3.2.0, NSClient++ 0.3.8.76
::
:: Version 1.07 revision:
:: Description is changed for NSCLIENT++ version 0.3.8 NSC.ini
:: Version 1.06 revision:
:: Description is changed for NSCLIENT++ version 0.3.8 NSC.ini
:: Version 1.05 revision:
:: Logging changes, stop and start services commands nave changed. Logs examples added.
:: Version 1.04 revision:
:: Double restart of the servise is fixed
:: Version 1.03 revision:
:: Description is changed
:: Version 1.02 revision:
:: @NET changed to @SC
:: Version 1.01 revision:
:: Service name's with spase problem is fixed
::
:: This code is made available as is, without warranty of any kind. The entire
:: risk of the use or the results from the use of this code remains with the user.
:: *****************************************************************************

::echo 1: %1    2: %2    3: %3    4: %4

@SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
:: Grab a file name and extension only
SET SCRIPTNAME=%~nx0
:: Replace "
SET SCRIPTNAME=%SCRIPTNAME:"=%
SET LOGDIR=C:\tools\logs
SET SERVICENAME=%1
:: Replace "
SET SERVICENAME1=%SERVICENAME:"=%
SET LOGFILE=%1
SET LOGFILE=%LOGFILE:"=%
:: Replace space by _
SET LOGFILE=%LOGFILE: =_%
SET LOGFILE=%LOGDIR%\%LOGFILE%.log

if "%SERVICENAME1%"=="" SET LOGFILE=%LOGDIR%\NO_SERVICENAME.log
::@echo servicename:  %SERVICENAME%
::@echo logfile: %LOGFILE%
::@echo SERVICENAME1: %SERVICENAME1%

:: =============================================================================

if not exist %LOGDIR% md %LOGDIR%
echo. >>%LOGFILE%
echo =============================================================================  >>%LOGFILE%
echo %DATE% %TIME% %SCRIPTNAME% has started >>%LOGFILE%
echo =============================================================================  >>%LOGFILE%

@if "%SERVICENAME1%"=="" goto usage
@if "%SERVICENAME1%"=="/?" goto usage
@if "%SERVICENAME1%"=="-?" goto usage

@echo Variables 1: %1   2: %2   3: %3   4: %4 >>%LOGFILE%

@SC query %SERVICENAME% >>%LOGFILE%

@SC query %SERVICENAME% | FIND /I "RUNNING" >>%LOGFILE%
if .%ERRORLEVEL%.==.0. (
    SET RETURN=Service %SERVICENAME% is running
    goto END
)

:RESTART
@echo %DATE% %TIME% Restarting %SERVICENAME% services... >>%LOGFILE%
@SC stop %SERVICENAME% >>%LOGFILE% 2>&1
@sleep 2
SET RETURN=Service %SERVICENAME% start pending
@SC start %SERVICENAME% | FIND /I "FAILED"
if .%ERRORLEVEL%.==.0. (
    SET RETURN=Start Service %SERVICENAME% FAILED
    @SC start %SERVICENAME% >>%LOGFILE% 2>&1
    goto END
)
@sleep 5
@SC query %SERVICENAME% | FIND /I "RUNNING"
if .%ERRORLEVEL%.==.0. (
    SET RETURN=Service %SERVICENAME% has started
    @SC query %SERVICENAME% >>%LOGFILE%
    goto END
)
@goto end

:USAGE
@echo Usage: >>%LOGFILE%
@echo  win_service_restart "^" ^ ^ ^ >>%LOGFILE%
@echo  ^ is "Service name", do not mix with "Display name" >>%LOGFILE%
@echo  ^, ^ and ^ are optional >>%LOGFILE%
::exit 128

:END
echo %DATE% %TIME% %SCRIPTNAME% has finished with code >>%LOGFILE%
echo %RETURN% >>%LOGFILE%
@echo %SCRIPTNAME%: %RETURN%
exit 0

--- End of code ---

Additional examples:

template-services_common-win.cfg
define service{
name generic-service-win-backup-agent
service_description BackupExecAgentAccelerator
display_name Backup Exec Remote Agent
event_handler win_service_restart
event_handler_enabled 1
check_command check_nt!SERVICESTATE!-d SHOWALL -l $SERVICEDESC$
register 0
}

services_common-win.cfg
define service{
use generic-service-win-backup-agent,generic-service-office
hostgroup_name winsrv-office ; Assign group of servers
host_name !SERVER11,!SERVER12 ; use this to exclude some servers or delete this row
}

group_windows.cfg
define hostgroup{
hostgroup_name winsrv-office ; The name of the hostgroup
alias Office Servers
}

host-server01.cfg
define host{
use windows-server ; Inherit default values from a template
host_name server01 ; The name we're giving to this host
alias server 01 ; A longer name associated with the host
hostgroups winsrv-office ; Group of servers
address 192.168.1.1 ; IP address of the host
}


DOWNLOADS:
Download the script from exchange.nagios.org
Download the script latest version from mirror.
 
md5: dbf0663a9e6648886eb8015cee8c9ce0 *win_service_restart.zip

Download the script previous version 1.05 from mirror. md5: 8b90ba7654227f1bf07c694368843b9

Download the script previous version 1.04 from mirror. md5: fd00753533e5fb655d824c3bf1d36d4