# Nagios Installation and Configuration

# Introduction

## Goals

- Nagios Configuration Continued

## Notes

- Commands preceded with “$” imply that you should execute the command as a general user - not as root.
- Commands preceded with “#” imply that you should be working as root.
- Commands with more specific command lines (e.g. “rtrX>” or “mysql>”) imply that you are executing commands on remote equipment, or within another program.

# Exercises

# PART IV - Adding Parent Relationships

If you look at the Nagios interface for your server and select Status Maps you will see your campus servers and devices centered around your Nagios instance. In order for Nagios to work efficiently you need to include parent relationships for each device defined.

Go to http://oob-hostX-campusY.ws.nsrc.org/nagios4 and click on the “Map” link.

Now we will add parent relationships for router, switch and server.

## Adding Parents to routers.cfg

    $ cd /etc/nagios4/conf.d
    $ sudo editor routers.cfg

And, update bdr1-campusY and core1-campusY entries with a parents line as shown below. The transit1-nren entry can be left alone.

    define host {
        use         generic-switch
        host_name   transit1-nren
        alias       Campus Y Transit Router
        address     transit1-nren.ws.nsrc.org
    }

    define host {
        use         generic-switch
        host_name   bdr1-campusY
        alias       Border Router 1, Campus Y
        address     bdr1-campusY.ws.nsrc.org
        parents     transit1-nren
    }

    define host {
        use         generic-switch
        host_name   core1-campusY
        alias       Core Router Campus Y
        address     core1-campusY.ws.nsrc.org
        parents     bdr1-campusY
    }

Save and exit from the file.

## Adding Parents to switches.cfg

    $ sudo editor switches.cfg

    define host {
        use         generic-switch
        host_name   dist1-b1-campusY
        alias       Distribution Switch 1, Building 1, Campus Y
        address     dist1-b1-campusY.ws.nsrc.org
        parents     core1-campusY
    }

    define host {
        use         generic-switch
        host_name   dist1-b2-campusY
        alias       Distribution Switch 1, Building 2, Campus Y
        address     dist1-b2-campusY.ws.nsrc.org
        parents     core1-campusY
    }

Do you understand why both switches have the same parent? If not, ask your instructor or assistant to explain.

Save and exit from the file.

## Adding Parents to pcs.cfg

We will leave this exercise up to you. It should be fairly simple. All your campus servers have the same parent: core1-campusY.

Edit the pcs.cfg file:

    $ sudo editor pcs.cfg

and at the of each entry add the line:

        parents     core1-campusY

Be sure to change “Y” to be your campus number.

Be sure that you do not add core1-campusY as a parent for your own host. If you don’t understand why ask your instructor.

Save and exit from the file when you are done with all entries.

## Reload the Nagios Configuration and See the Updated Status Map

    $ sudo nagios4 -v /etc/nagios4/nagios.cfg
    $ sudo systemctl reload nagios4

If you see errors, fix them and try reloading again.

Open a web browser to http://oob-hostX-campusY.ws.nsrc.org/nagios4 and click on the “Map” link on the left. Your map should now look quite different. You should see a map that represents the Nagios world point of view from your machine, but with everything in proper hierarchy based on the “parents” entries that you have just done.

# PART V - Create More Host Groups

## Preparation

In the web view, look at the pages “Hostgroups”, “Hostgroup Summary”, “Hostgroup Grid”. This gives a convenient way to group together hosts which are related (e.g. in the same site, serving the same purpose).

## Update /etc/nagios4/conf.d/hostgroups.cfg

For the following exercises it will be very useful if we have created or update the following hostgroups:

          ubuntu-servers
          routers
          switches

Edit the file /etc/nagios4/conf.d/hostgroups.cfg:

    $ cd /etc/nagios4/conf.d                                (just to be sure)
    $ sudo editor /etc/nagios4/conf.d/hostgroups.cfg

Create a new hostgroup called “ubuntu-servers” which includes all your campus servers:

    # A list of your Ubuntu Linux servers
    define hostgroup {
      hostgroup_name  ubuntu-servers
      alias           Ubuntu Linux Servers
      members         host1-campusY, host2-campusY, host3-campusY, host4-campusY, \
                      host5-campusY, host6-campusY, srv1-campusY
    }

Be sure that the end of the line has a “" to indicate a new line. Otherwise you will get an error when you go to reload the Nagios configuration.

Once you have done this, add one more host group for our classroom switch(es).

    # A list of our switches
    define hostgroup {
      hostgroup_name  switches
      alias           Classroom Switches
      members         dist1-b1-campusY,dist1-b2-campusY
    }

When you are done be sure to verify your work and reload the Nagios configuration.

    $ sudo nagios4 -v /etc/nagios4/nagios.cfg
    $ sudo systemctl restart nagios4

## Go back to the web interface and look at your new Host Groups in Nagios.

# PART VI - Extended Host Information (“making your graphs pretty”)

## Update icons for each network device (routers, switches, servers)

If you would like to use appropriate icons for your defined hosts in Nagios this is where you do this. We have the three types of devices:

- Cisco routers
- Cisco switches
- Ubuntu servers

There is a fairly large repository of icon images available for you to use located here:

    /usr/share/nagios4/htdocs/images/logos/

these were installed by default as dependent packages of the nagios4 package in Ubuntu. In some cases you can find model-specific icons for your hardware, but to make things simpler we will use the following icons for our hardware:

- /usr/share/nagios4/htdocs/images/logos/debian.*
- /usr/share/nagios4/htdocs/images/logos/router40.*
- /usr/share/nagios4/htdocs/images/logos/switch40.*

The next step is to edit the files and update each entry for routers, switches, and hosts to include the icon_image and statusmap_image options with images that correspond to each device type.

Open routers.cfg:

    $ cd /etc/nagios4/conf.d                                (just to be sure)
    $ sudo editor routers.cfg

Add these two lines to the end of each device definition:

      icon_image      router40.png
      statusmap_image router40.png

For example, for transit1-nren the entry will now look like this:

    define host {
      use           generic-switch
      host_name     transit1-nren
      alias         Transit Router 1
      address       transit1-nren.ws.nsrc.org
      icon_image      router40.png
      statusmap_image router40.png
    }

Change each device. Then exit the file, and do something similar to the switches:

    $ sudo editor switches.cfg

      icon_image      switch40.png
      statusmap_image switch40.png

Change each device. Then exit the file, and do something similar to the PCs (servers) in pcs.cfg:

      icon_image      debian.png
      statusmap_image debian.png

Change each device. Then exit the file and check that everything is OK:

    $ sudo nagios4 -v /etc/nagios4/nagios.cfg
    $ sudo systemctl restart nagios4

If everything is OK, you should see the new icons in the “Map” view of the Nagios web interface.

# PART VII - Create Service Groups

## Create service groups for ssh and http for your campus servers.

The idea is to create service groups for your 7 campus servers. Servicegroups consider the service defined by the combined services to be down if any of the services in a group are down.

In this case we’ll group together ssh and http. In real life you might do msyql, imap, smtp, http and your mta (postfix, mail, exim) if those were services required to deliver a mail interface to your users.

We start by editing the file:

    $ cd /etc/nagios4/conf.d            (just to be sure)
    $ sudo editor servicegroups.cfg

For campus 1 this service group would look like this (please change “1” to the number of your campus)

    define servicegroup {
        servicegroup_name   campus1-ssh-http
        alias           Campus 1 SSH and Web
        members  host1-campus1,SSH,host1-campus1,HTTP,host2-campus1,SSH,host2-campus1,HTTP, \
                 host3-campus1,SSH,host3-campus1,HTTP,host4-campus1,SSH,host4-campus1,HTTP, \
                 host5-campus1,SSH,host5-campus1,HTTP,host6-campus1,SSH,host6-campus1,HTTP, \
                 srv1-campus1,SSH,srv1-campus1,HTTP
                }

We used “\” to indicate a new line. Without this you will see errors.

Note that “SSH” and “HTTP” need to be uppercase as this is how the service_description is written in the file /etc/nagios4/conf.d/services.cfg

Save your changes, verify your work and reload Nagios. Now if you click on the Service Groups menu item in the Nagios web interface you should see this information grouped together.

# PART VIII - Configure Guest Access to the Nagios Web Interface

You will edit the file /etc/nagios4/cgi.cfg to give read-only guest user access to the Nagios web interface.

By default Nagios is configured to give full read/write access via the Nagios web interface to the user “nagiosadmin”. You can change the name of this user, add other users, change how you authenticate users, what users have access to what resources and more via the cgi.cfg file.

First, lets create a “guest” user and password in the htpasswd.users file.

    $ sudo htpasswd /etc/nagios4/htdigest.users guest

You can use any password you want (or none). A password of “guest” is not a bad choice if you plan for this to be a read-only account.

Next, edit the file /etc/nagios4/cgi.cfg and look for what type of access has been given to the nagiosadmin user. By default you will see the following directives (note, there are comments and lines between each directive):

    authorized_for_system_information=nagiosadmin
    authorized_for_configuration_information=nagiosadmin
    authorized_for_system_commands=nagiosadmin
    authorized_for_all_services=nagiosadmin
    authorized_for_all_hosts=nagiosadmin
    authorized_for_all_service_commands=nagiosadmin
    authorized_for_all_host_commands=nagiosadmin

Now let’s tell Nagios to allow the “guest” user some access to information via the web interface. You can choose whatever you would like, but what is pretty typical is this:

    authorized_for_system_information=nagiosadmin,guest
    authorized_for_configuration_information=nagiosadmin,guest
    authorized_for_system_commands=nagiosadmin
    authorized_for_all_services=nagiosadmin,guest
    authorized_for_all_hosts=nagiosadmin,guest
    authorized_for_all_service_commands=nagiosadmin
    authorized_for_all_host_commands=nagiosadmin

Note that we do not give the guest user access to system commands, service commands nor host commands.

Once you make the changes, save the file cgi.cfg, verify your work and reload Nagios.

To see if you can log in as the “guest” user you will need to clear the cookies in your web browser or open an alternate web browser if you have one. You will not notice any difference in the web interface. The difference is that a number of items that are available via the web interface (forcing a service/host check, scheduling checks, comments, etc.) will not work for the guest user.

## Enable External commands in nagios.cfg

Edit the file “/etc/nagios4/nagios.cfg” and check that it has this line:

    check_external_commands=1

If not, change it, save the file and exit.

Once this is done, go to ‘Problems’ > ‘Services (Unhandled)’ and find a service in the red (critical) or yellow (warning) state. Click on the service name. Then under “Service commands” click on “Acknowledge this service problem”. Enter a comment and click Commit.

The problem should disappear from the list of unhandled problems.

(Login as username “nagiosadmin” and the class password)