Service check windows powershell script

Hi,
I am an absolute beginner of Icinga. I have a Linux server running Icinga2 and a Windows server for testing. So far I have only operated Icinga2 via the GUI. So far it works well with existing checks. Now I would like to install my own script and this dont work with the instructions found on the net.

e.g.

I have the file on the Linux server first:
/etc/icinga2/conf.d/commands.conf edited.
I simply copied the part from the link above and appended it to the end of the existing commands.conf.

object CheckCommand "powershell_check" {
  import "plugin-check-command"
  command = [ "C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe" ]
  arguments = {
    "-command" = {
    value = "$ps_command$"
    order = -1
    }
    "-warn" = {
    value = "$ps_warn$"
    }
    "-crit" = {
    value = "$ps_crit$"
    }
    ";exit" = {
    value = "$LastExitCode"
    }
  }
}

then I have the file on the Linux server
/etc/icinga2/conf.d/services.conf edited. Again, I simply copied the part from the example and appended it to services.conf.

apply Service "reboot_status_check" {
  import "generic-service"
  display_name = "Reboot Check"
  check_command = "powershell_check"
  vars.ps_command = "& 'C:\\Program Files\\ICINGA2\\sbin\\check_reboot.ps1'"
  command_endpoint = host.address
  assign where host.vars.os == "windows"
}

Now I would have expected to see the service somewhere in the GUI. The following parameters are also not entirely clear to me:
command_endpoint = host.address
assign where host.vars.os == “windows”

Do I have to set the variable beforehand or is this an example and individual?

How can I simply assign the service to my test server?

The Powershell script (plugin) must be on my test server. If it is not there I should at least get an error message in my gui?

When you put your check command in this file, you would have to do this on every Windows machine as well. Best practice is to put all new Windows command objects in a global zone that is also existing at your Windows machines. We are using windows-commands as global zones to achieve this.

assign where host.vars.os == “windows”

This just one example to assign a service to many hosts (which have definied `vars.os = “windows”)

For tests you can simply assign a service to test server with e.g.:

assign where host.name == “your hostname”

ok, i thought i just have to configure on the server. Now I have entered the entries in the commands.conf and in the Services.conf on the Windows test server. In the GUI I see neither the service nor the command. Should I see them in the GUI?

Yes, there is a way to configure it only on the server (which I’ve described). No seeing it in the GUI is most properly because your assign rule does not fit. After changing you conf-files run icinga2 daemon -C and check with icinga2 object list if your service object has been generated.

sorry for my stupid questions, but how do I get the new windows commands into a zone

Don’t mind, newbies questions are welcome. First you need to add such a section to every zones.conf:

object Zone "windows-commands" {
   global = true
}

Second, you need to create the according directory /etc/icinga2/zones.d/windows-commands/ at your master only. Third, place every command object that shall be executed on your Windows agent in that directory only. Every time icinga2 gets a reload trigger this directory incl. every sub directory is sychronized to all nodes that have windows-commands as global zone defined.

you mean aon the Windows Agents and the Icinga Server?

“every command object” you mean a file like the commands.conf?

Right.

The file name doesn’t matter at all. I’d even recommend to a have one file per command object.

Now I have insert the this

object Zone "windows-commands" {
   global = true
}

in the zones.conf of my Agent and in the /etc/icinga2/zones.conf from my Server.
I have create a folder /etc/icinga2/Zones.d/windows-commands/
In this folder I create a file “win_powershellcheck_x86.conf”

After a deploy from my Icinga2 GU I was hoping to find the win_powershell_x86.conf file on the agent. But it is not there. In the GUI cannot find the zone windows-command.

Linux file and directory names are case sensitive, hence, directory name needs to be /etc/icinga2/zones.d/windows-commands/

This was only a typing failure from me

pwd

/etc/icinga2/zones.d/windows-commands

have I to create the windows-commands folder on the Agent too?

No, icinga will do that (you’ll find it here C:\ProgramData\icinga2\var\lib\icinga2\api\zones\windows-commands).

ok… I looked only here:
c:\Program Files\ICINGA2\

“c:\ProgramData\icinga2\var\lib\icinga2\api\zones” there is only a director-global folder.

Does your agent recognize the windows-commands zone? Check with

“c:\Program Files\ICINGA2\sbin\icinga2.exe” object list -n windows-commands

The output is empty
which zones.conf have I to edit:

c:\ProgramData\icinga2\etc\icinga2\zones.conf

(it looks like only a sample)
or

c:\Program Files\ICINGA2\share\skel\etc\icinga2\zones.conf

Now I have both edit both
in the ProgarmData

 /*
  * Generated by Icinga 2 node setup commands
  * on 2020-02-10 11:14:45 +0100
  */
 
 object Endpoint "testICINGA.test.de" {
 	host = "testICINGA.test.de"
 	port = "5665"
 }
 
 object Zone "master" {
 	endpoints = [ "testICINGA.test.de" ]
 }
 
 object Endpoint "windowsAgent.test.de" {
 }
 
 object Zone "windowsAgent.test.de" {
 	endpoints = [ "windowsAgent.test.de" ]
 	parent = "master"
 }
 
 object Zone "global-templates" {
 	global = true
 }
 
 object Zone "director-global" {
 	global = true
 }
 
 object Zone "windows-commands" {
    global = true
 }

This is the zones.conf on the Server \etc\icinga2\zones.conf

 /*
  * Generated by Icinga 2 node setup commands
  * on 2020-02-10 15:19:05 +0100
  */
 
 object Endpoint "testICINGA.test.de" {
 }
 
 object Zone "testICINGA.test.de" {
         endpoints = [ "testICINGA.test.de" ]
 }
 
 object Zone "global-templates" {
         global = true
 }
 
 object Zone "director-global" {
         global = true
 }
 
 object Zone "windows-commands" {
    global = true
 }

I’m surprised that I don’t see the zone in Icinga Director

After changing zones.conf you need to run Kickstart Wizard and to reload icinga on your master and restart on you agent.

Now I have the zone “windows-commands” on the server deleted. Then created the zone “windows commands” via the director. Now I see the following output on the agent:

c:\Program Files\ICINGA2\sbin>icinga2.exe object list -n windows-commands
Object 'windows-commands' of type 'Zone':
  % declared in 'C:/ProgramData/icinga2/var/lib/icinga2/api/zones/director-global/director/zones.conf', lines 1:0-1:29
  * __name = "windows-commands"
  * endpoints = null
  * global = true
    % = modified in 'C:/ProgramData/icinga2/var/lib/icinga2/api/zones/director-global/director/zones.conf', lines 2:5-2:17
  * name = "windows-commands"
  * package = "_cluster"
  * parent = ""
  * source_location
    * first_column = 0
    * first_line = 1
    * last_column = 29
    * last_line = 1
    * path = "C:/ProgramData/icinga2/var/lib/icinga2/api/zones/director-global/director/zones.conf"
  * templates = [ "windows-commands" ]
    % = modified in 'C:/ProgramData/icinga2/var/lib/icinga2/api/zones/director-global/director/zones.conf', lines 1:0-1:29
  * type = "Zone"
  * zone = "director-global"

On The Server I get now a warning by the command icinga daemon -C

[2020-07-10 09:52:57 +0200] warning/config: Ignoring directory ‘/etc/icinga2/zones.d/windows-commands’ for unknown zone ‘windows-commands’.

Now I have run the kickstart and I see the zone “windows-commands” in the Director. The output from the command “icinga2.exe object list -n windows-commands” is empty.

For me it’s ok if you go your own way. However, I cannot help anymore.