Learning NAGIOS 3.0
上QQ阅读APP看书,第一时间看更新

Installation

This section discusses the installation of Nagios 3. The following points are discussed:

  • Upgrading from previous versions
  • Prerequisites
  • Obtaining Nagios
  • Setting up users and groups
  • Nagios compilation
  • Registering Nagios as a system service

Let us look at each point in detail.

Upgrading from Previous Versions

If you already have Nagios 1 or 2, upgrading to Nagios 3 would be worthwhile. In such cases, you should proceed with the same steps as when performing a fresh installation. You need to use the same user name, groups and directories that you have used for previous Nagios installations.

It is also necessary to stop all Nagios processes before performing an upgrade. This can usually be done by invoking the following command:

/etc/init.d/nagios stop

This way, both Nagios and the plugins will install smoothly. As Nagios 3 configuration parameters are backwards compatible, your current configuration will work fine after upgrading.

Prerequisites

This section applies to people compiling Nagios from sources and installing them manually. Almost all modern Linux distributions include Nagios (1.x, 2.x or 3.0) in their packages. The Nagios website also offers instructions for automated installation on several operating systems. In such cases, all related packages will be installed by the underlying system (such as APT in Debian and Ubuntu systems). Usually, a system with the development set of packages already installed contains all the packages needed to build Nagios.

Building Nagios from sources requires a C compiler, standard C library development files and the make/imake command. Additionally, the development files for OpenSSL should be installed so that the network-based plugins will be able to communicate over the SSL layer. The MySQL and PostgreSQL development packages should also be installed so that database checks can be run.

First of all, if we are planning to build the Nagios system, a compiler along with several build tools such as gcc, make, cpp, and binutils are required. It will also need the standard C library development files. Although these packages usually come installed, make sure that they are actually there before compilation.

Nagios by itself does not have a large number of packages that offer some of the basic functionalities that you would want on your system. So, if you want to use all the functionalities that Nagios can offer, you will have to install additional software.

OpenSSL development files (includes and libraries) are required for Nagios plugins to communicate over SSL. If you plan to monitor MySQL or PostgreSQL databases, you should install header files and libraries for these databases. This will allow building check plugins to monitor databases.

If we want to use the Nagios web interface, a web server capable of serving CGI scripts is required. Apache web server (1.x or 2.x) is recommended, and is also the most popular web server on a Linux installation. Although Nagios works with any web server that supports CGI, this book covers configuring Apache.

Additionally, several plugins from the Nagios standard distribution are written in Perl, and will not work if Perl is not installed. Some plugins also need Perl's Net::Snmp package to communicate with devices over SNMP protocol.

Moreover, the GD graphics library is needed for the Nagios web interface to create a status map and trends images. We will also need to install libraries for JPEG and PNG images so that GD can create images in these formats.

All of the packages mentioned above are usually installed with many operating systems, and most of them are already available for almost any UNIX based platform.

Throughout this chapter, we will use the Ubuntu Linux 7.10 'Gutsy' distribution, as it is very popular. All newer Ubuntu platforms use the same package names, so commands used here will work without any problems.

We will also install Apache 2.2.4 and Perl 5.8.8 from Ubuntu packages. For different operating systems, the packages are similar though they may have different names. The following is a command to install all the packages for our chosen distribution:

apt-get install gcc make binutils cpp libpq-dev libmysqlclient15-dev\
                libssl0.9.8 libssl-dev pkg-config apache2 \
                libgd2-xpm libgd2-xpm-dev libgd-tools \
                libpng12-dev libjpeg62-dev \
                perl libperl-dev libperl5.8 libnet-snmp-perl

Package names may also be different for other operating systems and distributions. The command to install corresponding packages may also be different. For RPM packages, the naming convention is a bit different—the development packages have a suffix of devel. Libraries themselves are also named in a slightly different manner.

For Red Hat Enterprise Linux and Fedora Core operating systems with yum installed, the command to install all prerequisites would be:

yum install gcc imake binutils cpp postgresql-devel \
            libmysql15lib mysql15-devel \
            openssl openssl-devel pkg-config httpd \
            libgd2 libgd2-devel libgd-progs libpng libpng-devel \
            libjpeg libjpeg-devel perl perl-devel perl-Net-SNMP

A system with the development packages already installed, usually, contains all the packages needed to build Nagios.

Obtaining Nagios

Nagios is an open-source application, which means that the source code of all Nagios components is freely available from the Nagios home page. Nagios is distributed under GNU GPL (General Public License) version 2 (visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html), which means that Nagios source code can be redistributed and modified freely under the condition that all changes are also distributed as source code. Nagios also has a standard set of plugins. They are not only developed independently as SourceForge projects (see http://sourceforge.net/projects/nagiosplug/), but are also distributed under GPL version 2 license.

First of all, many operating systems already have binary distributions of Nagios. If you are not an IT expert and just want to try out or learn Nagios in your environment, it is best to use binary distributions instead of compiling Nagios by yourself. Therefore, it is recommended that you check to see if your distribution has a compiled version of Nagios 3 available.

For Red Hat and Fedora Linux systems, the Nagios download page contains RPMs that can simply be installed onto your system. For other distributions, their package repository may contain binary Nagios packages. The Nagios Exchange website http://www.nagiosexchange.org/ also hosts Nagios builds for various platforms such as AIX or SUSE Linux. All binary distributions of Nagios are split into packages (rpm, dpkg, pkg, or bin file) that contain the Nagios daemon. It is usually called Nagios, and the standard set of plugins is usually called Nagios Plugins.

If you are an experienced user and want to control software installed on your machines, it's recommended that you install Nagios from the source. In this case, you should also download sources of both Nagios and the Nagios plugins.

In order to download the Nagios source packages, please go to the Nagios download page at http://www.nagios.org/download/. All Nagios downloads are hosted on SourceForge, so the download links will redirect you to the SourceForge download pages. The download process should begin automatically.

You should start by downloading the source tarball of the latest Nagios 3.x branch. It is available under the Step 1: Get Nagios section. Please make sure that you download a stable version (such as 3.0, 3.1, and so on), instead of the most recent CVS snapshot, as these versions might not always be as stable as the users expect. The filename of the source tarball should be similar to nagios-3.0.tar.gz, depending on the exact version you are attempting to download.

You should also download the source tarball of the latest official Nagios plugins from the same downloads page. It is available under the Step 2: Get Plugins section. The filename for the plugins should be similar to nagios-plugins-1.4.11.tar.gz, again depending on the exact version.

These files are used in the next section of this chapter to build Nagios from the source.

Setting up Users and Groups

This section describes how to compile and install Nagios and standard Nagios plugins from source tarballs. If you plan to install Nagios from binary distributions, you should skip this section and proceed to the next sections that describe exact Nagios configurations. You might also need to adjust the parameters mentioned in this book to specify the directories your Nagios installation uses.

If you are upgrading from a previous Nagios version, you will have all of the users and groups set up already. In this case, you should proceed to the next section.

The first thing that needs to be done is to decide where to install Nagios. In this section, we will install the Nagios binaries in the /opt/nagios directory, and all configuration files will be based on these locations. This is a location for all Nagios binaries, plugins, and additional files. Nagios data will be stored in the /var/nagios directory, where it keeps information about its current and historical status. It can be part of the Nagios binaries installation directory or a separate directory, as in our case. Nagios configuration will be put into /etc/nagios.

After we have decided on our directory structure, we need to set up users and groups for Nagios data. We will also create a system user and a group, nagios, which will be used by the daemon. We will also set up a group, nagioscmd, which can communicate with the daemon. The system user will be a member of the nagios and nagioscmd groups.

The following commands will create the groups and user mentioned above:

groupadd -g 5000 nagios
groupadd -g 5001 nagioscmd
useradd -u 5000 -g nagios -G nagioscmd -d /opt/nagios nagios

We create a new user and groups because Nagios processes run as separate users. This increases the security and allows a more flexible set up. Nagios also communicates with external components over a Unix socket – this is a socket that works in a similar way to a file on your file system. All commands are passed to Nagios via the pipe, and therefore, if you want your processes to be able to send reports or changes to Nagios, you need to make sure that they have access to the socket. One of the common uses for this is that the Nagios web interface needs to be able to send commands to the monitoring process.

If you want to use the web interface, it is necessary to add the user that your web server runs as, to the nagioscmd group. This will allow the web interface to send commands to Nagios.

The user that the web server is working as is usually www-data, apache or httpd. It can be checked with a simple grep command:

root@ubuntu:~# grep ^User /etc/apache*/* /etc/httpd*/*
/etc/apache2/apache2.conf:User www-data

For our preceding example, we now know the user name is www-data. So now, we'll add this user to the group nagioscmd. This requires a simple command to be run:

usermod -G nagioscmd www-data

Nagios Compilation

The next step is to set up Nagios destination directories and change their owners accordingly. The following commands will create the directories, and change their owner, user, and group to nagios.

mkdir -p /opt/nagios /etc/nagios /var/nagios
chown nagios.nagios /opt/nagios /etc/nagios /var/nagios

We will now create a source directory where all of our builds will take place. For the purpose of this book, this will be /usr/src/nagios3. We need to extract our Nagios and standard plugins into that directory. The extraction will create nagios-3.0 and nagios-plugins-1.4.11 subdirectories (or similar ones, depending on your source versions).

Now, let's go to the directory where the Nagios source is located – in our case, this is /usr/src/nagios3/nagios-3.0. We'll configure Nagios parameters for the directories we plan to install into by running the configure script. Some of the options that the script accepts are described here:

The --enable-embedded-perl option will cause Nagios to use the embedded Perl interpreter instead of forking child processes to run plugins written in Perl. This option is not needed for Nagios to function, but in case you plan to run many plugins written in Perl, it might improve Nagios's overall performance. You can also enable it during compilation, and if your Nagios setup seems to behave incorrectly, you can always turn embedded Perl off in Nagios's main configuration file. If you are new to Nagios, it is not recommended that you turn this option on.

For the directory structure that was described earlier in this section, the following configure script should be used:

sh configure \
    --prefix=/opt/nagios \
    --sysconfdir=/etc/nagios \
    --localstatedir=/var/nagios \
    --libexecdir=/opt/nagios/plugins \
    --with-command-group=nagioscmd

The script may take time to complete as it will try to guess the configuration of your machine, and verify how to build Nagios.

If the configure script failed, the most probable reason is that one or more prerequisites are missing. At that point, you will need to analyze which test failed, and install or configure additional packages. Most of the times, the output is quite clear, and it is easy to understand what went wrong.

Assuming the configure command works, we now need to build Nagios. The build process uses make command, similar to almost all Unix programs. The following commands can be used to build or install Nagios:

First, we'll need to build every module within Nagios. To do this, simply run the following command:

make all

An error may occur in case some header files are missing, or when a development package is not installed.

The following is a sample output from a successful Nagios build. It finishes with a friendly message saying that it has completed successfully.

cd ./base && make
make[1]: Entering directory '/usr/src/nagios3/base'
[…]
*** Compile finished ***
[…]
*************************************************************
Enjoy.

In case an error occurs during the build, the information about the same is also shown. For example, consider the following sample output from build:

[…]
In file included from checks.c:40:
../include/config.h:163:18: error: ssl.h: No such file or directory
[…]
make[1]: *** [checks.o] Error 1
make[1]: Leaving directory '/usr/src/nagios3/base'
make: *** [all] Error 2

If this or a similar error occurs, please make sure that you have all the prerequisites, mentioned earlier, installed. Also, please make sure that you have enough memory and storage space during compilation, as this might also cause unexpected crashes during builds.

On Ubuntu systems, it is possible to look for development packages using the apt-cache search command. For example, apt-cache search ssl will find all packages related to OpenSSL. The development packages always have the -dev suffix in their package name—in this case, it would be the libssl-dev package.

Now, we need to install Nagios by running the commands:

make install
make install-commandmode

For a fresh install, it is recommended to also install sample configuration files that will be used later for configuring Nagios.

make install-config

The installation of Nagios is now complete. It is recommended that you keep all your Nagios sources as well as prepare dedicated scripts that install Nagios. This is just in case you decide to enable/disable specific options and don't want to guess exactly how Nagios was configured to build the last time it was installed.

The next step to be carried out is compilation of standard Nagios plugins.

Now, let's go to the directory where Nagios plugins source code is located—in our case, it is /usr/src/nagios3/nagios-plugins-1.4.11. We will configure Nagios plugins parameters for the directories we plan to install it into by running the configure script. Some of the options that the script accepts are described here:

The option --enable-perl-modules allows installing additional Perl modules that aid in developing your own Nagios plugins in Perl. It is useful to enable this option if you are familiar with Perl.

Options --with-pgsql and --with-mysql allow us to specify the locations for the installation of PostgreSQL and/or MySQL databases. It is used to create plugins for monitoring PostgreSQL and/or MySQL. If not specified, the build process will look for the development files for these databases in their default locations. Installing development files for these databases is described in the Prerequisites section.

For the directory structure that was described earlier in this section, the following configure script should be used:

sh configure \
    --prefix=/opt/nagios \
    --sysconfdir=/etc/nagios \
    --localstatedir=/var/nagios \
    --libexecdir=/opt/nagios/plugins \
    --enable-perl-modules

The script should run for some time and succeed, assuming that all prerequisites are installed. If not, the script should indicate what the missing component is.

The build process also uses the make command in a manner similar to the way Nagios is compiled. In this case, only the targets all and install will be used. Therefore, the next step is to run make commands as shown here:

make all
make install

If any of these steps fail, an investigation on what exactly has failed is needed, and if it is due to a missing library or a development package, they will have to be installed and the commands tried again.

If all of the above commands have succeeded, you now have a fully installed Nagios setup. Congratulations!

Registering Nagios as a System Service

After installing Nagios, it is worth making sure that the daemon is running as a system service, and will start up properly during system boot.

In order to do that, go to the source directory (in our case it is be /usr/src/nagios3/ nagios-3.0) and then run the following command:

make install-init

This will install a script in our init.d directory (this usually is /etc/init.d or /etc/rc.d/init.d). The script is automatically created, and will contain the usernames and paths that were created when the configure script was run.

The next step is to set up a system to stop and start this service automatically. Depending on your system, the command to do that can be one of the following:

chkconfig --add nagios ; chkconfig nagios on
update-rc.d nagios defaults

After Nagios has been set up as a system service, it is recommended that you reboot your system to verify that it is actually starting. After your system has fully restarted, making sure Nagios is running can be done by checking the process list as follows:

root@ubuntu:~# ps –ef|grep ^nagios
nagios 796 1  0 00:00:00 /opt/nagios/bin/nagios –d /etc/nagios/nagios.cfg

If at least one process is found, it means that Nagios has been properly started. If not, please read the Nagios log file (whose name is /var/nagios/nagios.log assuming a Nagios installation as described earlier) and see exactly why it is failing. This usually relates to incorrect permissions. In such a case, you should perform all of the steps mentioned in the previous sections and reinstall Nagios from the beginning.

The results of the startup is mentioned at the end of the log file and an error indication should also be present of what the issue might be. For example, a part of the log for an error related to incorrect permissions is as follows:

[1217273964] Nagios 3.0 starting... (PID=5509)
[1217273964] Local time is Mon Jul 28 21:39:24 CEST 2008
[1217273964] LOG VERSION: 2.0
 [1217273964] Finished daemonizing... (New PID=5510)
[1217273964] Error: Could not create external command file '/var/nagios/rw/nagios.cmd' as named pipe: (13) -> Permission denied. If this file already exists and you are sure that another copy of Nagios is not running, you should delete this file.
[1217273964] Bailing out due to errors encountered while trying to initialize the external command file... (PID=5510)

By default, Nagios also sends its logs to the syslog daemon. So if the Nagios log file does not exist, looking in the system log (usually /var/log/messages) might provide some information about the problem.

If you wish to start or stop Nagios manually, please run the nagios script from the init.d directory with one of the parameters shown.

/etc/init.d/nagios stop|start|restart

Please note that path to the init.d directory might be different for your operating system.