Add Hosts to Nagios Server For Monitoring

|
Last Updated:
|
|

In this guide, we are going to learn how to add hosts to Nagios Server for monitoring. 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.

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

Adding 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
}

Save and exit the config

Create Nagios Contacts

Note that in the above service template, we defined our contact group as kifarunix-demo-admins. Hence, you need to create a contact group, otherwise you can use the default nagiosadmins group.

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

# Define Your Contacts Here
define contact {
    contact_name            koromicha         ; Short name of user
    use                     generic-contact   ; Inherit default values from generic-contact template (defined above)
    alias                   Koromicha         ; Full name of user
    email                   [email protected] ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
define contact {
    contact_name            kifarunixdemoadmin
    use                     generic-contact
    alias                   Kifarunix-demo-admin            
    email                   [email protected] 
}
# Create Contact Group
define contactgroup {

    contactgroup_name       kifarunix-demo-admins
    alias                   Kifarunix-demo Administrators
    members                 koromicha,kifarunixdemoadmin
}

Note that we are using default contact template as defined on the templates.cfg config file.

Save and exit the configuration.

Define Service Metric Checks for the Remote Hosts

Let say you want to monitor remote hosts metrics such as disk space, users, load average, running procs, then below is a sample service metric check configuration file;

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

define service {
        use                     kifarunix-demo-service
        hostgroup_name          Kifarunix-demo-env01
        service_description     Logged in Users
        check_command           check_metric!check_users
}
define service {
        use                     kifarunix-demo-service
        hostgroup_name          Kifarunix-demo-env01
        service_description     Load Average
        check_command           check_metric!check_load
}
define service {
        use                     kifarunix-demo-service
        hostgroup_name          Kifarunix-demo-env01
        service_description     Disk Usage
        check_command           check_metric!check_disk
}
define service {
        use                     kifarunix-demo-service
        hostgroup_name          Kifarunix-demo-env01
        service_description     Running Zombie Processes
        check_command           check_metric!check_zombie_procs
}
define service {
        use                     kifarunix-demo-service
        hostgroup_name          Kifarunix-demo-env01
        service_description     Running Processes
        check_command           check_metric!check_total_procs
}

If you check the service definition above, the check command is defined as, below, for example.

check_command           check_metric!check_total_procs

The check_total_procs is the command defined on the remote agent NRPE configuration file. For example;

command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200

While the check_metric is the name of the command that defines how Nagios server will use check_nrpe plugin to query the specific metric on the remote host.

For example, we have defined the command in /usr/local/nagios/etc/objects/kifarunix-demo/commands.cfg file;

vim /usr/local/nagios/etc/objects/kifarunix-demo/commands.cfg
define command {
    command_name    check_metric
    command_line    /usr/lib/nagios/plugins/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

Now, test this command on the terminal to ensure that metrics are fetched;

/usr/lib/nagios/plugins/check_nrpe -H 192.168.56.109 -c check_load
OK - load average: 0.05, 0.02, 0.01|load1=0.050;15.000;30.000;0; load5=0.020;10.000;25.000;0; load15=0.005;5.000;20.000;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
Add Hosts to Nagios Server For Monitoring

Service Status;

current status services

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

Some Documentations and more guides

Nagios Core Documentation

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
koromicha
I am the Co-founder of Kifarunix.com, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

Leave a Comment