Monitor Services with Nagios on Ubuntu 12.04

Nagios is a monitoring tool that allows you to monitor services on a single server or a pool of servers. It can keep an eye on a broad range of network services, including SMTP and POP3 (email), HTTP (web), ICMP (ping), and SSH. In addition to simple uptime monitoring, Nagios also allows administrators to create their own plugins to monitor additional services or devices.

Install Nagios

Before installing Nagios, make sure your hostname is properly set by following the steps outlined in the Getting Started guide. You also need to have a functioning LAMP stack set up on your Linode.

Now you’re ready to install Nagios. Here’s how:

Ensure that your system’s package repository and installed packages are up to date by entering the following commands, one by one:

1
2

sudo apt-get update
sudo apt-get upgrade

Install Nagios from the Ubuntu repository by entering the following command:

1

sudo apt-get install -y nagios3

Enter an administrator password to complete the installation.

If you do not already have an email server installed on your Linode, Postfix will be installed automatically. The Internet Site configuration will be used by default. You’ll also have to enter a mail name for the system.

Access the Nagios Web Interface

You can now access the Nagios web interface for administration and reporting by visiting http://example.com/nagios3/, where example.com refers to your Linode’s default virtual host. You may also access this interface by visiting http://12.34.56.78/nagios3/, where 12.34.56.78 is the IP address of your Linode. You will need to authenticate with the nagiosadmin user you created earlier.

The above example does not use SSL, and your password will be sent unencrypted. If you want to use encryption, you will need to generate (or purchase) and install an SSL certificate. Steps for generating and using your own certificate can be found in our SSL guide.

Configure Notifications

A great deal of the power of Nagios is its ability to send notifications and alerts regarding the status of services and devices. While most of this fine-grained configuration is beyond the scope of this document, we have outlined some basic notifications below.

Email Alerts

Nagios can send alerts by email, but to receive them you’ll need to add your email address to the Nagios configuration file. Here’s how:

Open the Nagios configuration file for editing by entering the following command:

1

sudo nano /etc/nagios3/conf.d/contacts_nagios2.cfg

Enter your system username and your email address by replacing your_username with your username, and youremail@example.com with your email address.

To send email alerts to more than one user, duplicate the define contact section for as many users as you want. Or, to configure notifications to a group, edit the define contactgroup section.

Save the changes to the configuration file by pressing Control + x and then pressing y.

Restart Nagios to apply the changes:

1

sudo service nagios3 restart

IRC Notifications

You can also configure Nagios to send notifications to an IRC channel through a bot. Here’s how:

Enter the following command to install the IRC bot:

1

sudo apt-get install nagircbot

You’ll need to provide a few parameters to the IRC bot - such as its nickname and server - in order to use it. To see a list of parameters, enter the following command:

1

nagircbot -h

To start the Nagios IRC bot, enter the following command:

1

sudo nagircbot

Now you need to set those parameters. Use the following example as a starting point. Replace irc.example.com with the address of your IRC network, and #example with the name of the IRC channel to which the bot should send notifications. Replace nagircbot with the desired nickname for your bot, and ident and realname with the ident and real name strings for the IRC bot.

In the above example, -f /usr/local/nagios/var/status.dat tells the bot where to get status updates. The -C flag allows the bot to send colored messages to the channel depending on the status of the service. When services are down, red status messages are displayed. These messages turn green when the service has recovered. Warnings are displayed in yellow, but do not typically represent a critical issue.

The -I 900 parameter tells the bot to send a status message to the channel every 900 seconds (15 minutes). For example, the bot may send something like Critical: 0, warning: 1, ok: 6, up: 2, down: 0, unreachable: 0, pending: 0, which indicates that there are no critical messages and 1 warning.

Accept External Commands

Nagios can accept external commands so that you can acknowledge problems, add comments, and more. Here’s how to enable external commands:

Open the Nagios configuration file for editing by entering the following command:

1

sudo nano /etc/nagios3/nagios.cfg

Change the check_external_commands setting to check_external_commands=1.

Save the changes to the configuration file by pressing Control + x and then pressing y.

Restart Apache by entering the following command:

1

sudo service apache2 restart

Open the system group file for editing by entering the following command:

1

sudo nano /etc/group

Change the nagios:x:118 setting to nagios:x:118:www-data.

Save the changes to the configuration file by pressing Control + x and then pressing y.

Enter the following command to change the permissions of a Nagios configuration file:

1

sudo chmod g+x /var/lib/nagios3/rw

Enter the following command the change the permissions of the Nagios directory:

1

sudo chmod g+x /var/lib/nagios3

Restart Nagios by entering the following command:

1

sudo service nagios3 restart

Congratulations! External commands are now enabled.

Next Steps

Nagios contains numerous features that are beyond the scope of this document. You are encouraged to explore the resources listed below and the administrative interface for more information regarding the setup and configuration of Nagios.

Congratulations on your new Nagios monitoring and notification system!

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.