Nagios - Add New Hostgroup & Administrator - FreeBSD 12.0

Share on:

Nagios is a tool for system monitoring. Nagios constantly checks if other machines are working properly. It also verifies that various services on those machines are working fine. In addition, Nagios accepts other processes or machines reporting their status, for example, a web server can directly report if it is not overloaded to Nagios. The main purpose of system monitoring is to detect as soon as possible any system that is not working properly so that users of that system will not report the issue to you first.

It is a common task to add new Hostgroups to Nagios, this entails setting up the Hostgroup, hosts and the associated services. You may also want to give access to the Nagios Web Interface for the Hostgroup Administrator, which will involve creating a new contact. You would only want to give this Administrator access to the hosts that they are responsible for, so I will cover all these themes today.

Describe Directory Structure

Before starting out, I will list my /usr/local/etc/nagios directory structure so as to visualize the connections between the configuration files.

/usr/local/etc/nagios # tree
|-- cgi.cfg
|-- config
|   |-- commands
|   |   |-- default-commands.cfg
|   |   `-- extra-commands.cfg
|   |-- contactgroups
|   |-- contacts
|   |   |-- default-contacts.cfg
|   |   `--
|   |-- hostgroups
|   |   |--
|   |   `-- servers-freebsd-hostgroups.cfg
|   |-- hosts
|   |   |--
|   |   |--
|   |   |--
|   |   |--
|   |   |--
|   |   `--
|   |-- servicegroups
|   |-- services
|   |   |--
|   |   |--
|   |   |--
|   |   |-- servers-freebsd-services.cfg
|   |   |--
|   |   |--
|   |   `--
|   |-- templates
|   |   |-- default-templates.cfg
|   |   `-- extra-templates.cfg
|   `-- timeperiods
|       `-- default-timeperiods.cfg
|-- nagios.cfg
|-- resource.cfg

As you can see I have all the configuration files that do not need to be in the root nagios directory in a directory named config. This is set in nagios.cfg as follows:

# You can also tell Nagios to process all config files (with a .cfg
# extension) in a particular directory by using the cfg_dir 
# directive as shown below:


Define a New Hostgroup

In the file I define the new Hostgroup.

define hostgroup{
  alias    servers 
  members ,,

Define New Hosts

In the config/hosts directory, I add 3 new files for each of the hosts I will be monitoring. Follows is the file which defines

define host {
    alias                   xray

The directive use determines which template file to use as a base configuration for the host. I have defined a new template in config/templates/extra-templates.cfg as follows:

define host {

    name               ; The name of this host template
    use                       generic-host                  ; This template inherits other values from the generic-host template
    check_period              24x7                          ; By default, hosts are checked round the clock
    check_interval            5                             ; Actively check the host every 5 minutes 
    retry_interval            1                             ; Schedule host check retries at 1 minute intervals
    max_check_attempts        10                            ; Check each host 10 times (max)
    check_command             check-host-alive-4            ; Default command to check hosts
    notification_period       workhours                     ; admins hate to be woken up, so we only notify during the day 
                                                            ; Note that the notification_period variable is being overridden from
                                                            ; the value that is inherited from the generic-host template!
    notification_interval     120                           ; Resend notifications every 2 hours 
    notification_options      d,u,r                         ; Only send notifications for specific host states
    contact_groups              ; Notifications get sent to the admins by default 
    register                  0                             ; DON'T REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!

Add Services

In the config/services directory, I add 3 new files for each of the hosts I will be monitoring. Follows is a sample of the file which defines the services for the host

define service{
        use                     generic-service
        service_description     HTTPS Certificate
        check_command           check_http_cert_sni

define service{
        use                     generic-service
        service_description     Web Server HTTPS
        check_command           check_http_ssl_sni

define service{
        use                     passive_service
        service_description     Disk Usage

define service{
        use                     passive_service
        service_description     Swap Usage

Add Contacts

In the template, I defined the contact_groups as This is a group of contacts that needs to contain individual members. Below I will define a single administrator, and then define the contact group and add this administrator as a member. If you would like to add more members to the group you will need to define a contact for each individual member.

define contact {

    contact_name         ; Short name of user 
    use                     generic-contact         ; Inherit default values from generic-contact template (defined above)
    alias                ; Full name of user 
    email                ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******

define contactgroup {

    alias           Administrators

This is a good time to create a htpasswd password for the administrator. This will enable the administrator access to only the hosts that have been configured with their contact details.

Use the htpasswd command for the user as follows:

htpasswd /usr/local/etc/nagios/.htpasswd.users

Test Configuration & Restart

WIth all the configuration taken care off, I can check the files to search for any mistakes that may have been made before attempting to restart Nagios.

Use nagios -v nagios.cfg to verify everything is correct:

nagios -v nagios.cfg

Now I see that everything is in order, I restart Nagios.

service nagios restart

Verify Nagios Web Interface

Logging into the Nagios Web Interface with the new user, the new Hostgroup is visible. Of note, this will also be visible to the nagiosadmin user.

Navigating to the Host Groups view, you will see a new Host Group similar to below:

Nagios Hostgroup

Wrapping Up

That's all for today! I highly recommend Learning Nagios, Third Edition by Wojciech Kocjan if you are interested in learning the full workings of Nagios.