Let Nagios check on a non-default SSH port
Tagged:  •  

In Nagios, I wanted to let one particular host (out of many) to be checked with a different SSH port. In the checkcommands.cfg file, you will find commands which need to be checked remotely written as follows:

define command{
        command_name check_somecommand_remote
        command_line $USER1$/check_by_ssh -i /usr/local/etc/nagios/keys/id_dsa -l nagioscheck -H $HOSTADDRESS$ -C "/path/to/command -w $ARG1$ -c $ARG2$"
        }

This does not allow you to let a host use another SSH port. There are many other hosts which just work fine like this (since all hosts have the default port 22 set), it's just one particular host using SSH port 24. I could also redefine all remote commands by adding a -p 24 but this is inflexible. I would need a new set of commands for each different port number.

However, there's a solution. We add a new variable in each existing remote command, like this:

define command{
        command_name check_somecommand_remote
        command_line $USER1$/check_by_ssh -i /usr/local/etc/nagios/keys/id_dsa -l nagioscheck -H $HOSTADDRESS$ $ARG3$ -C "/path/to/command -w $ARG1$ -c $ARG2$"
        }

Note I added $ARG3$ to the command. By default, this is just empty. But it allows me to append a specific option to the check_by_ssh command for that particular host:

define service{
        use generic-service
        host_name somehost
        service_description Root Partition
        is_volatile 0
        check_period 24x7
        max_check_attempts 4
        normal_check_interval 5
        retry_check_interval 1
        contact_groups admins
        notification_interval 1440
        notification_period workhours
        notification_options w,c,r
        check_command check_somecommand_remote!80%!100%!-p 24
        }

So you can see I passed -p 24 to the check_by_ssh command. There's no need to touch all other hosts.

If you want to adapt the check command in the future and add another variable to it, use $ARG3$ for that, and use $ARG4 for the 'dummy' variable we just added. This way, this variable remains optional and can always be appended to a check.

The same trick also applies to the check_ssh command, which also accepts -p as port specification.