How to setup eventhandler on Icinga2 to start any Windows service

Hi Team,

I am trying to use Eventhandler functionality of Icinga2 but seems like something is going wrong and i am unable to get the desired result. If i execute the check manually by calling this script,it works as expected.

[root@or1icinga.connect plugins]# ./restart_win_service.sh CRITICAL host123 wuauserv
The Windows Update service is stopping.
The Windows Update service was stopped successfully.

The Windows Update service is starting.
The Windows Update service was started successfully.

I am sharing the details below:

1: I have a script “restart_win_service.sh” located under /usr/lib64/nagios/plugins

#!/bin/sh
case "$1" in
   OK)
        ;;

   WARNING)
        ;;

   UNKNOWN)
        ;;

   CRITICAL)
        /usr/lib64/nagios/plugins/check_nrpe -H "$2" -p 5666 -c restart_service -a "$3"
   ;;
esac
exit 0

2: restart_service.bat exists on windows machine and corresponding entries are there in nsclient.ini

[/settings/external scripts/scripts]
restart_service = scripts\restart_service.bat "$ARG1$"

3: my commands.conf file looks like below:

object EventCommand "restart_win_service" {
 command = [ PluginContribDir + "/restart_win_service.sh" ]

 arguments = {

   "-s" = "CRITICAL"
   "-H" = "$rpc_host_address$"
   "-n" = "$rpc_service_name$"
    }
   vars.rpc_host_address = "$hostname$"
   vars.rpc_service_name = "$service_name$"
}

4: My services.conf file looks like below:

object Service "Restart_Splunkforwarder_Service" {
 import "generic-service"
 check_command = "check_servicestatus"
 event_command = "restart_win_service"
 vars.service_name = "splunkforwarder"
 vars.rpc_host_address = "host123"
 vars.hostname = "host123"
 vars.rpc_service_name = "splunkforwarder"
 host_name = "host123"
 
}

Now i stopped splunkforwarder service and waited for eventhandler to trigger but it doesn’t. I am sure i must be doing something wrong but not sure what. Can anyone please guide me? I also followed https://www.linuxfrickeln.de/2016/07/16/icinga2-eventcommands-und-automatisiertes-neustarten-von-diensten/ but still no luck.
If anyone has any simple example please share.

Thanks,
Rahul

You might want to take a look at the documentation which shows an example for a Windows service.

I have already gone through it. I could have used it but on Windows host i am using NSClient++ and i think example has been given using icinga on client machine as well.

Please enable debugging and take a look at the logs. I’d guess that the parameters passed to restart_win_service.sh don’t have the contents you expect.

unfortunately debug log didn’t throw any error. It is just that script is not getting called when service gets stopped. Can you check the script i shared in my original post and let me know if i missed out anything?

I’d have expected messages that the service’s state changed to a non-ok state resulting in an event handler being called showing the parameter of the call.