Add Hosts to Nagios Server For Monitoring

0
272

Well, in order to monitor your remote hosts with Nagios server for availability and metric checks, you need to add them hosts to the Nagios server. In this guide, we are going to learn how to add hosts to Nagios Server for monitoring.

Before you can proceed, ensure that your Nagios is up and running;

Install Nagios Server on CentOS 8

Install Nagios Core on Debian 10 Buster

How to Install and Configure Nagios Core From the Source Ubuntu 18.04

Add Hosts to Nagios Server For Monitoring

Once the Nagios server is up and running, ssh into it and add the hosts as follows.

Create Nagios Host Object Definition

To begin with, create Nagios host object definition. A host definition is used to define a physical server, workstation, device within a network.

By default, Nagios creates a localhost definition file under the Objects configuration directory, /usr/local/nagios/etc/objects, if at all you installed Nagios from the source. Other object definitions are stored in this directory too.

Therefore, create a sub-directory to store your own objects.

mkdir /usr/local/nagios/etc/objects/kifarunix-demo

If you are using a custom directory as above where you will define your own object definition configurations, you need to tell Nagios to process all config files in that directory.

Open main Nagios configuration file and insert the line, cfg_dir=/usr/local/nagios/etc/objects/kifarunix-demo under the OBJECT CONFIGURATION FILE(S) section.

vim /usr/local/nagios/etc/nagios.cfg
...
# OBJECT CONFIGURATION FILE(S)
# These are the object configuration files in which you define hosts,
# host groups, contacts, contact groups, services, etc.
# You can split your object definitions across several config files
# if you wish (as shown below), or keep them all in a single config file.

# You can specify individual object config files as shown below:
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
...
...
# Definitions for Monitoring Kifarunix-demo hosts
cfg_dir=/usr/local/nagios/etc/objects/kifarunix-demo

Next, create your host object definition file.

vim /usr/local/nagios/etc/objects/kifarunix-demo/hosts.cfg

Next, define you hosts details such as hostname, IP address and the host template to use.

###############################################################################
#
# REMOTE HOST DEFINITION
#
###############################################################################

define host {
    use                     kifarunix-demo-hosts 
    host_name               web01.kifarunix-demo.com
    alias                   web01
    address                 192.168.2.10
}

define host {
    use                     kifarunix-demo-hosts 
    host_name               hrm.kifarunix-demo.com
    alias                   hrm
    address                 192.168.56.109
}

The line, use kifarunix-demo-hosts specifies the host template to use by the defined hosts. This ensures that the configurations defined in the specified template applies to the defined host.

As such we will have to create the host template. You can as well choose to use the default host templates defined in the /usr/local/nagios/etc/objects/templates.cfg configuration file.

Create Nagios Host Group Object Definition

A host group definition is used to group one or more hosts. It makes it easy to specify the common checks that applies to similar hosts.

For example, we can group the above two hosts in a group, Kifarunix-demo-env01 aliased as Linux Servers.

vim /usr/local/nagios/etc/objects/kifarunix-demo/hostgroups.cfg
###############################################################################
# 
# REMOTE HOST GROUP DEFINITION
#
###############################################################################
define hostgroup {
    hostgroup_name         Kifarunix-demo-env01
    alias                  Linux Servers
    members                web01.kifarunix-demo.com,hrm.kifarunix-demo.com
}

You can simply use the same file for the hosts and host groups object definition.

vim /usr/local/nagios/etc/objects/kifarunix-demo/host-hostgroup.cfg
###############################################################################
#
# REMOTE HOST DEFINITION
#
###############################################################################

define host {
    use                     kifarunix-demo-hosts 
    host_name               web01.kifarunix-demo.com
    alias                   web01
    address                 192.168.2.10
}

define host {
    use                     kifarunix-demo-hosts 
    host_name               hrm.kifarunix-demo.com
    alias                   hrm
    address                 192.168.56.109
}
###############################################################################
# 
# REMOTE HOST GROUP DEFINITION
#
###############################################################################
define hostgroup {
    hostgroup_name         Kifarunix-demo-env01
    alias                  Linux Servers
    members                web01.kifarunix-demo.com,hrm.kifarunix-demo.com
}

Create Host and Service Template Configuration

Create a custom host and service definition template as shown below.

The configurations defined in this template will be applied on any host that is defined to use it, for example the above.

vim /usr/local/nagios/etc/objects/kifarunix-demo/hosts-service-template.cfg
# Host Template Definition
define host{
name                         kifarunix-demo-hosts
notifications_enabled        1
event_handler_enabled        1
flap_detection_enabled       1
process_perf_data            1
retain_status_information    1
retain_nonstatus_information 1
        check_command                check-host-alive
        check_interval               5
        max_check_attempts           2
        notification_interval        0
        notification_period          24x7
        notification_options         d,u,r
        contact_groups               kifarunix-demo-admins
register                     0
}

# Service Template definition
define service{
name                         kifarunix-demo-service
active_checks_enabled        1
passive_checks_enabled       1
parallelize_check            1
obsess_over_service          1
check_freshness              0
notifications_enabled        1
event_handler_enabled        1
flap_detection_enabled       1
process_perf_data            1
retain_status_information    1
retain_nonstatus_information 1
        notification_interval        0
        is_volatile                  0
        check_period                 24x7
        check_interval               5
        retry_interval               1
        max_check_attempts           2
        notification_period          24x7
        notification_options         w,u,c,r
        contact_groups               kifarunix-demo-admins
register                     0
}

Verify Nagios Configuration

It is a good idea to always run a Nagio configuration file syntax check whenever you make any changes. This ensures that you fix any would be errors before restarting Nagios.

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

You should get such an output. (Snipped)

Nagios Core 4.4.5
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 2019-08-20
License: GPL

Website: https://www.nagios.org
Reading configuration data...
   Read main config file okay...
   Read object config files okay...

Running pre-flight check on configuration data...
...
(Output cut)
...

Total Warnings: 0
Total Errors:   0

Things look okay - No serious problems were detected during the pre-flight check

If there are no errors, restart Nagios service.

systemctl restart nagios

To confirm that the hosts and host groups have been added, login to Nagios on the browser and click Hosts and Host Groups under the Current Status section on the left pane.

Add Hosts to Nagios Server For Monitoring
Nagios Hosts Groups

Well, there you go. You have successfully added hosts to Nagios Server for Monitoring.

You can now proceed to monitor hosts services and metrics.

You can as well install Nagios Agents on the remote host for metric and service monitoring.

Monitor Linux Hosts using Nagios check_by_ssh Plugin

Install Nagios NRPE Monitoring Agent on Linux Host From the Source

Install Nagios NSClient++ Monitoring Agent on Windows System

LEAVE A REPLY

Please enter your comment!
Please enter your name here