13.11. Monitoring a Mail Server

You want to know how to use Nagios to monitor your mail server. You want it to keep an eye on SMTP, POP, SSH, and IMAP services.

Add new host and service definition entries to the hosts.cfg and services.cfg files. You may also need some new command definitions in commands.cfg.

First, make sure these entries exist in commands.cfg:

	# 'check_pop' command definition
	define command{
	        command_name    check_pop
	        command_line    $USER1$/check_pop -H $HOSTADDRESS$
	        }
	
	# 'check_smtp' command definition
	define command{
	        command_name    check_smtp
	        command_line    $USER1$/check_smtp -H $HOSTADDRESS$
	        }
	
	# 'check_imap' command definition
	define command{
	        command_name    check_imap
	        command_line    $USER1$/check_imap -H $HOSTADDRESS$
	        }
	
	# 'check_ssh' command definition
	define command{
	        command_name    check_ssh
	        command_line    $USER1$/check_ssh -H $HOSTADDRESS$
	        }

Next, create a host definition for the server:

	# define a Mail server host
	define host{
	        use                     generic-host
	        host_name               postfix1
	        alias                   mail server1
	        address                 192.168.1.27
	        check_command           check-host-alive
	        max_check_attempts      10
	        check_period           24x7
	        notification_interval   120
	        notification_period     24x7
	        notification_options    d,r
	        contact_groups          admins
	        }

Add your new server to an existing group; or, create a new group for it, as this example shows:

	define hostgroup{
	        hostgroup_name  mail_servers
	        alias           Mail Servers
	        members         postfix1
	        }

Next, define the four services (POP, IMAP, SMTP, and SSH) in services.cfg. Each service requires a separate definition. The easy way is to copy and paste the following example, replacing only the hostname, service_description, and check_command values:

	# Define a service to monitor POP/SMTP/IMAP/SSH
	define service{
	        use                      generic-service
	        host_name                postfix1
	        service_description      POP
	        is_volatile              0
	        check_period             24x7
	        max_check_attempts       4
	        normal_check_interval    5
	        retry_check_interval     1
	        contact_groups           admins
	        notification_options        w,u,c,r
	        notification_interval    960
	        notification_period      24x7
	        check_command            check_pop
	        }

If any of these services are already defined, all you do is add the hostnames or hostgroups to the existing service definition:

	host_name                   postfix1,postfix2,exim1

or:

	hostgroup_name              mail_servers

Run the syntax checker, then restart Nagios:

	# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
	# /etc/init.d/nagios restart

Refresh the Nagios web interface, and you'll see the new entries listed as PENDING. In a few minutes, Nagios will run the new service checks, and they will no longer be PENDING, but will display status information.

Reuse and recycle are the keys to Nagios sanity. Remember:

commands.cfg contains your command definitions. Each command definition only needs to be created once.

Each new host needs its own host definition in hosts.cfg.

Service definitions are created once per service in services.cfg, then simply add additional host_name or hostgroup_name entries.

Using hostgroups is one way to organize related servers; another way is by using servicegroups. Servicegroups let you group related services in the Nagios web interface. See the next recipe to learn how to do this.