Page 1 of 2

Run Check Command and Custom Variables

Posted: Fri Oct 03, 2025 7:01 am
by AngeloMileto
I don't know if it's been asked/answered/reported before - my advanced search turned up nothing recent - and I don't know if it's a bug/feature/whatever so I figured I'd post and ask.

I just started reading up on Custom Variables and though Wow! I could use that for my HTTP checks as I wanted to eliminate having a separate service check for each host because they use different ports. The example https://library.nagios.com/techtips/cus ... nagios-xi/ is great and actually showed exactly what I was looking for - Great job XI!

SO off I went to my host, created a custom variable _http_port, setup a service check with $ARG1$ as $_HOSTHTTP_PORT$ and proceeded to "Run Check Command" which is what I typically do before saving anything so I can fix any typos or incorrect parameters. Well, it failed saying $_HOSTHTTP_PORT$ is an invalid port number.

I spent about an hour - since this was my first time in this arena it had to be me doing something wrong - re-reading, deleting, re-entering, re-testing etc. and got absolutely nowhere. So I figure, MAYBE I have to save it first. So I saved my service check and the host records and went back to the main XI page and tried the new check out - AND IT WORKED! :)

So I figured GREAT, now I should be able to do that on other hosts and the Run Check Command should work, right? WRONG. Apparently, the interpretation/extraction of the $_HOST custom variable does not happen during the Run Check Command.

Sooooooo, if this is correct, should it be?? If it should, then what is the best way to test things that use custom variables? I'd really hate to change something and have to wait for the XI normal timed check to verify it's working as desired. Please let me know what I'm missing here.

Thanks!!

Re: Run Check Command and Custom Variables

Posted: Fri Oct 03, 2025 9:38 am
by AngeloMileto
Along with this, I have another question:

If I use $_HOSTHTTP_PORT$ in the parameter for the check, how do I make a default port for any host without a custom variable?

In my case, I have 20-30 hosts that I'm checking http/certificates for but only a few that are using something other than 443. So how to I make 443 the default for everything and then just put the individual custom ports in for the few machines that need it?

I tried making a custom variable on the service check itself and calling it _http_port as well but when I run that check against a "normal" host, it says "check_http: Invalid port number - $"

So am I left with if I put a custom port in one host I have to do it for every host?

Re: Run Check Command and Custom Variables

Posted: Fri Oct 03, 2025 5:21 pm
by kg2857
The default port is 80 for HTTP and 443 for HTTPS.
I'm not sure why a custom macro/var is needed. Just run the service with an argument with the port.
Macros won't be interpreted when you run the service check from CCM.
You could define a small number of services each with a different port.
You could make a wrapper script for check_http but its probably simpler to just run it with whatever the port is.

Re: Run Check Command and Custom Variables

Posted: Mon Oct 06, 2025 8:40 am
by AngeloMileto
kg2857 wrote: Fri Oct 03, 2025 5:21 pm The default port is 80 for HTTP and 443 for HTTPS.
I'm not sure why a custom macro/var is needed. Just run the service with an argument with the port.
Macros won't be interpreted when you run the service check from CCM.
You could define a small number of services each with a different port.
You could make a wrapper script for check_http but its probably simpler to just run it with whatever the port is.
I explained why I needed to run a custom var. While we have a large number of hosts that are just using 443, there are also a fairly large number that aren't and still need to be checked. So that custom var seems like that's exactly what it's for.

However, it does not appear to be working as expected so my assumption is that I have something configured incorrectly which is why I'm asking here.

If I have the main service check checking 443 then what you are saying is I should make a separate service check for each other port other than 443 that I need to check. Doesn't that defeat the purpose of the custom variables?

So my thought was to have the main check use the custom variable and then make that assignment at the template level to 443 - the default as you pointed out. Then, for the individual hosts that I needed to check a different port, they could redefine the port to something other then 443.

Guess it doesn't work that way for the hierarchy/inheritance so that's why I'm tying to figure out.

Re: Run Check Command and Custom Variables

Posted: Tue Oct 07, 2025 1:36 am
by kg2857
After thinking about it some more I don't see why what you're doing doesn't work.

Re: Run Check Command and Custom Variables

Posted: Tue Oct 07, 2025 5:27 am
by AngeloMileto
I know. I must have something in the wrong place in the hierarchy. The problem is that I can't test it directly from the CCM because, as you pointed out, it doesn't do interpretation of the Macros - which is why I guess it asks for the HostAddress :)

I have a few things broke - not in Nagios - that I need to get working back in production and then I'll map this out with what templates are being used - since, in XI it's pretty well nested - or just make my own and move every host to that one.

Thanks!!

Re: Run Check Command and Custom Variables

Posted: Tue Oct 07, 2025 12:58 pm
by AngeloMileto
Got it. I'll use our esxi hosts as an example. Also apologize that I can't get screen shots but the system is not connected to the outside networks so if anyone has any questions, please ask and I'll provide as much as I can to include retyping some of the config files.

The host itself has no check command. Everything comes from some other relationship. The host is a member of the esxi host group. The esxi hostgroup is also a member of the HTTP_Service hostgroup. Obviously - or not - hostgroups do not have service checks associated with them but the HTTP_Service hostgroup is assigned the HTTP_Service check - it also checks the certificate expiration but basically check_http command.

None of these allow for, or at least react to the adding of a custom variable PROBABLY because the variable/macro is $HOST...$ and this is a service check. BUT I still needed to put the $_HOSTHTTP_PORT$ macro into $ARG1$ for the check which is using check_xi_service_http_cert command.

So now the trick was where to tell the service check to use 443 as a default since I've basically overridden it? Well that would be a host template. Sepcifically, I found xiwizard_website_host_v2. That template is setup to use a check_tcp command but, since it's a host template, I created the _http_port=443 var/macro there.

Assigning that host template to the esxi host (all of them actually) then resulted in a service check that pulls the port from the macro and the rest of the parameters from the service check.

Now trying to figure out how to get the custom ports. Right now, I'm guessing that if I put a custom variable/macro on an individual host, that will override the template value but haven't fully tested that yet. Hopefully I'll get that done in the next day or so and report back.

Re: Run Check Command and Custom Variables

Posted: Tue Oct 07, 2025 9:31 pm
by kg2857
I'm not sure what problem you're trying to solve at this point.
The value is set in the template for most hosts and on the host for the rest. This should work in theory.
If the variable appears in the args when trying to run from CCM, it will fail because the var isn't expanded. The same is true for macros such as $USERn$ so this is nothing new.

Re: Run Check Command and Custom Variables

Posted: Wed Oct 08, 2025 10:44 am
by AngeloMileto
Sorry for the confusion but my "Got it" at the beginning was that I figured it out and it was working. I probably should have stated it a lot clearer. I got the custom variables to work and it was, as I thought, just a hierarchy issue as I had put the default 443 in the wrong place and it was being removed.

ANYWAY, now I can just add any hosts to the standard checks and it will verify the certificate as reported by the site as well as that the site is up. A lot of our sites are authenticated and since we don't have a consistent signon, I just put an --expect="HTTP/1.1" which should basically say the site it up.

Re: Run Check Command and Custom Variables

Posted: Thu Oct 16, 2025 8:17 am
by PeterDK2
Hello,
I think I get what Angelo is trying here, because I was looking for the same kind of solution when I learned about the use of Custom Variables. And it's somewhat confusing.
Goal: create only one ping service for a group of hosts, but I'd like to be able to override the warning and critical thresholds for a few hosts

Step 1 - I created a service template
define service {
name example_template_srv_win_ping
service_description SRV_Ping
display_name SRV_Ping
check_command check_xi_service_ping!$_SERVICERTA_WARN$!5%!$_SERVICERTA_CRIT$!10%!!!!
...(other settings)
_rta_crit 250
_rta_warn 100
register 0
}
By the way, custom variables seem to be case insenstive. When you type them upper case in the UI, they are saved lower case.
But it seems to work even if the arguments for the command are upper case

Step 2 - create the generic service
define service {
service_description SRV_Ping
use example_template_srv_win_ping
hostgroup_name hg_os_windows_2025
display_name SRV_Ping
register 1
}

So here, all windows 2025 hosts get the SRV_Ping service with the "$_SERVICE-" customs variables
But here is where the confusion starts: it works because you add a "$_SERVICE"-custom variable into a service template
If you want to override it for a host, you can't do it on the service because the service is not available in CCM for that host (it's assigned to a host group)
And using custom variables _rta_warn and _rta_crit on the host don't work because they are converted to $_HOST which the check_command doesn't use

What are the possible solutions?
Option 1:
- Convert the $_SERVICE to $_HOST
- Make sure the custom variables are available in a host template assign to all hosts in the host group
- Don't use the service template anywhere else where the $_HOST variables are unavailable or your thresholds will by 0
Option 2:
- Create a separate service for the hosts you want the override for
- Ignore the duplicate service config warnings when you apply your config, which I don't like to be honest

Any idea's how to do this better?