Monitor ssh on non-default port with Nagios

I just deployed Nagios on a Gentoo server and everything is fine except ssh, which it marks as "CRITICAL" because it's refusing connections. But that's because it's running on a port different from the default 22. How do I change it so that it monitors the right port?


Solution 1:

host:~$ /usr/lib/nagios/plugins/check_ssh --help
check_ssh v1991 (nagios-plugins 1.4.12)
Copyright (c) 1999 Remi Paulmier <[email protected]>
Copyright (c) 2000-2007 Nagios Plugin Development Team
        <[email protected]>

Try to connect to an SSH server at specified server and port


Usage:check_ssh [-46] [-t <timeout>] [-r <remote version>] [-p <port>] <host>

does this answer your question? -p parameter lets you specify the port, make custom check in /etc/nagios/nrpe.cfg and put there:

command[check_remote_ssh]= /usr/lib/nagios/plugins/check_ssh -p 1234 some.host

Solution 2:

In ssh pluging config /etc/nagios-plugins/config/ssh.cfg, there an alias named check_ssh_port. if it not existe you may define it like above:


$ cat >> /etc/nagios-plugins/config/ssh.cfg
define command{
        command_name    check_ssh_port
        command_line    /usr/lib/nagios/plugins/check_ssh -p '$ARG1$' '$HOSTADDRESS$'
        }

in the service file /etc/nagios3/conf.d/services_nagios2.cfg, define you ssh service to monitor like this:


define service {
        hostgroup_name                  ssh2-servers
        service_description             SSH2
        check_command                   check_ssh_port!12000!server
        use                             generic-service
        notification_interval           240 ; set > 0 if you want to be renotified
}
replace 12000 by you default ssh port and server by your target.

Solution 3:

As none of the solutions above worked for me, I'll post this small variation.

This definition is the default one (e.g.: localhost.cfg) with the only addition of the -p option and the space in between.

I guess it's a hybrid version of two of the solutions given.

It works on Nagios Core 4 with nagios-plugins 2.1.1

define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             SSH
        check_command                   check_ssh!-p 12345
        notifications_enabled           1
        }

Solution 4:

You can also define the second parameter, "server", in the host_name parameter this way:

    define host{
        use                     generic-host            ; Name of host template to use
        host_name               host
        alias                   host
        address                 92.193.170.124
}


# Define a service to check if ssh services are running
define service {
        use                     generic-service         ; Name of service template to use
        host_name               host
        service_description     SSH Port 4959
        check_command           check_ssh_port!4959
        notification_interval   0 ; set > 0 if you want to be renotified
}

So, if the address of the host changes, you only have to modify this parameter once, for all services defined for this host.