Multi-variable array custom command


I’m trying to build a command that checks multiple ports on multiple hosts. The command I’m trying to use is the ‘check_tcp’ command and trying to run it on custom variables (both the ports and hosts) that are setup as an array. Here is an example of how the command is currently running:
./check_tcp -H host1 host2 -p port1 port2

How I ideally want it to run is for it to cycle through each host in the array and each port, like so:
./check_tcp -H host1 -p port1
./check_tcp -H host1 -p port2
./check_tcp -H host2 -p port1
./check_tcp -H host2 -p port2

Has anyone encounter this issue had found a way to setup a service and command to do this?

Screen Shot 2023-04-20 at 4.11.21 PM

Thanks in advance

do you use icinga director or plain config files?

I would use a thin wrapper around the check_tcp that takes your array and launches the various needed
check_tcp commands and reports back the collected results. A check_tcp_multiple so to say.


It could be easly done. With nestead 2 for loops …

I had fun … I literally solved it in a couple of minutes. (lot of years in python and still lazy) :smiley:

IMPORTANT: use chat gpt tool with cation. Do not took it for graunted.

import argparse
import subprocess

# Parse command-line arguments
parser = argparse.ArgumentParser(description='Check TCP ports on multiple hosts')
parser.add_argument('-H', '--hosts', nargs='+', help='Hosts to check')
parser.add_argument('-p', '--ports', nargs='+', help='Ports to check')
args = parser.parse_args()

# Iterate over hosts and ports
for host in args.hosts:
    for port in args.ports:
        # Build the command
        command = ['./check_tcp', '-H', host, '-p', port]
        # Execute the command

To call script:

python3 -H host1 host2 -p port1 port2

You will need a bit play with script … like storing nagios exit code in list and try go trough this list and so but this should be nice template and direction where to start.

Have Fun and good luck!

1 Like

I use Icinga Director

You can use a service apply rule
with the “apply for” set to the array of ports on the host

you import your check_tcp and add $config$ as a port like in the description of the “apply for” field

Evaluates the apply for rule for all objects with the custom attribute specified. E.g selecting “host.vars.custom_attr” will generate “for (config in host.vars.array_var)” where “config” will be accessible through “$config$”. NOTE: only custom variables of type “Array” are eligible.