Breadcrumbs

1.Introduction

Nagios is a powerful monitoring server to monitor your computer on your network like harddrive space available, service status, etc. In this article i will show you how to install and configure a simple nagios server and how to configure your client to send info to your server. 

This is yet another Nagios tutorial as many as you can on internet, but i will cover the whole server installation and also some sample for client. It's more or less my whole configuration since i'm using this blog as a personal reminder ;-)

 

2. server installation

On a working debian linux you must first install all the required package :

:~# apt-get install apache2 libapache2-mod-php5 php5-gd php5 make gcc build-essential wget libgd2-xpm-dev libnet-snmp-perl libssl-dev snmp daemon unzip

 

then for security purpose we will create dedicated user:

- nagios for service

- nagcmd to execute command through web interface (that's why we had it to www-data, the apache group)

root@monitoring:~# groupadd nagios
root@monitoring:~# useradd -m -g nagios nagios
root@monitoring:~# passwd nagios
Entrez le nouveau mot de passe UNIX :
Retapez le nouveau mot de passe UNIX :
passwd : le mot de passe a été mis à jour avec succès
root@monitoring:~# groupadd nagcmd
root@monitoring:~# usermod -g nagcmd nagios
root@monitoring:~# usermod -g nagcmd www-data

 

 

then you must download the last version. currently the last one is the 4.3.1, but you must check directly on nagios website: https://www.nagios.org/downloads/

 

:~# wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.3.1.tar.gz
:~# wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
:~# ls -lh
total 14M
-rw-r--r-- 1 root root  11M févr. 23 21:03 nagios-4.3.1.tar.gz
-rw-r--r-- 1 root root 2,7M avril 19 18:04 nagios-plugins-2.2.1.tar.gz
:~# tar xzf nagios-4.3.1.tar.gz
:~# tar xzf nagios-plugins-2.2.1.tar.gz
:~# ls -lh
total 14M
drwxrwxr-x 18 root root 4,0K févr. 23 21:00 nagios-4.3.1
-rw-r--r--  1 root root  11M févr. 23 21:03 nagios-4.3.1.tar.gz
drwxr-xr-x 15 root root 4,0K avril 19 18:04 nagios-plugins-2.2.1
-rw-r--r--  1 root root 2,7M avril 19 18:04 nagios-plugins-2.2.1.tar.gz
 
:~#:~# cd nagios-4.3.1/
:~/nagios-4.3.1# ls -lh
total 536K
drwxrwxr-x  2 root root 4,0K févr. 23 21:00 base
drwxrwxr-x  2 root root 4,0K févr. 23 21:00 cgi
-rw-rw-r--  1 root root  22K févr. 23 21:00 Changelog
drwxrwxr-x  2 root root 4,0K févr. 23 21:00 common
-rwxrwxr-x  1 root root  43K févr. 23 21:00 config.guess
-rwxrwxr-x  1 root root  36K févr. 23 21:00 config.sub
-rwxrwxr-x  1 root root 203K févr. 23 21:00 configure
-rw-rw-r--  1 root root  26K févr. 23 21:00 configure.ac
drwxrwxr-x  4 root root 4,0K févr. 23 21:00 contrib
-rw-rw-r--  1 root root 7,4K févr. 23 21:00 daemon-init.in
drwxrwxr-x  2 root root 4,0K févr. 23 21:00 docs
-rw-rw-r--  1 root root  886 févr. 23 21:00 doxy.conf
-rwxrwxr-x  1 root root 6,9K févr. 23 21:00 functions
drwxrwxr-x 11 root root 4,0K févr. 23 21:00 html
drwxrwxr-x  2 root root 4,0K févr. 23 21:00 include
-rwxrwxr-x  1 root root   77 févr. 23 21:00 indent-all.sh
-rwxrwxr-x  1 root root  161 févr. 23 21:00 indent.sh
-rw-rw-r--  1 root root  422 févr. 23 21:00 INSTALLING
-rwxrwxr-x  1 root root 5,8K févr. 23 21:00 install-sh
-rw-rw-r--  1 root root  841 févr. 23 21:00 LEGAL
drwxrwxr-x  2 root root 4,0K févr. 23 21:00 lib
-rw-rw-r--  1 root root  18K févr. 23 21:00 LICENSE
-rw-rw-r--  1 root root  15K févr. 23 21:00 Makefile.in
-rwxrwxr-x  1 root root  461 févr. 23 21:00 make-tarball
-rwxrwxr-x  1 root root  818 févr. 23 21:00 mkpackage
drwxrwxr-x  2 root root 4,0K févr. 23 21:00 module
-rw-rw-r--  1 root root 8,9K févr. 23 21:00 nagios.spec
-rw-rw-r--  1 root root  244 févr. 23 21:00 nagios.sysconfig
-rw-rw-r--  1 root root  210 févr. 23 21:00 pkginfo.in
-rw-rw-r--  1 root root   15 févr. 23 21:00 README
-rw-rw-r--  1 root root 3,1K févr. 23 21:00 README.asciidoc
drwxrwxr-x  3 root root 4,0K févr. 23 21:00 sample-config
-rwxrwxr-x  1 root root 1,3K févr. 23 21:00 subst.in
drwxrwxr-x  5 root root 4,0K févr. 23 21:00 t
drwxrwxr-x  4 root root 4,0K févr. 23 21:00 tap
drwxrwxr-x  2 root root 4,0K févr. 23 21:00 test
-rw-rw-r--  1 root root 5,4K févr. 23 21:00 THANKS
drwxrwxr-x  4 root root 4,0K févr. 23 21:00 t-tap
-rwxrwxr-x  1 root root 2,6K févr. 23 21:00 update-version
-rw-rw-r--  1 root root  642 févr. 23 21:00 UPGRADING
drwxrwxr-x  3 root root 4,0K févr. 23 21:00 worker
drwxrwxr-x  2 root root 4,0K févr. 23 21:00 xdata

Then you must prepare the compilation with this command:

 

:~/nagios-4.3.1# ./configure --with-nagios-group=nagios --with-command-group=nagcmd
checking for a BSD-compatible install... /usr/bin/install -c
checking build system type... x86_64-pc-linux-gnu
[...]
 
Creating sample config files in sample-config/ ...
 
 
*** Configuration summary for nagios 4.3.1 02-23-2017 ***:
 
 General Options:
 -------------------------
        Nagios executable:  nagios
        Nagios user/group:  nagios,nagios
       Command user/group:  nagios,nagcmd
             Event Broker:  yes
        Install ${prefix}:  /usr/local/nagios
    Install ${includedir}:  /usr/local/nagios/include/nagios
                Lock file:  ${prefix}/var/nagios.lock
   Check result directory:  ${prefix}/var/spool/checkresults
           Init directory:  /etc/init.d
  Apache conf.d directory:  /etc/apache2/sites-available
             Mail program:  /usr/bin/mail
                  Host OS:  linux-gnu
          IOBroker Method:  epoll
 
 Web Interface Options:
 ------------------------
                 HTML URL:  http://localhost/nagios/
                  CGI URL:  http://localhost/nagios/cgi-bin/
 Traceroute (used by WAP):  /usr/sbin/traceroute
 
 
Review the options above for accuracy.  If they look okay,
type 'make all' to compile the main program and CGIs.
 

 

 

:~/nagios-4.3.1# make all
[..]
  inflating: angular-1.3.9/docs/examples/example-example65/style.css
   creating: angular-1.3.9/docs/examples/example-example66/
  inflating: angular-1.3.9/docs/examples/example-example66/index-debug.html
  inflating: angular-1.3.9/docs/examples/example-example66/index-jquery.html
[..]
*** Compile finished ***
 
If the main program and CGIs compiled without any errors, you
can continue with installing Nagios as follows (type 'make'
without any arguments for a list of all possible options):
 
  make install
     - This installs the main program, CGIs, and HTML files
 
  make install-init
     - This installs the init script in /etc/init.d
 
  make install-commandmode
     - This installs and configures permissions on the
       directory for holding the external command file
 
  make install-config
     - This installs *SAMPLE* config files in /usr/local/nagios/etc
       You'll have to modify these sample files before you can
       use Nagios.  Read the HTML documentation for more info
       on doing this.  Pay particular attention to the docs on
       object configuration files, as they determine what/how
       things get monitored!
 
  make install-webconf
     - This installs the Apache config file for the Nagios
       web interface
 
  make install-exfoliation
     - This installs the Exfoliation theme for the Nagios
       web interface
 
  make install-classicui
     - This installs the classic theme for the Nagios
       web interface
 
 
*** Support Notes *******************************************
 
If you have questions about configuring or running Nagios,
please make sure that you:
 
     - Look at the sample config files
     - Read the documentation on the Nagios Library at:
           https://library.nagios.com
 
before you post a question to one of the mailing lists.
Also make sure to include pertinent information that could
help others help you.  This might include:
 
     - What version of Nagios you are using
     - What version of the plugins you are using
     - Relevant snippets from your config files
     - Relevant error messages from the Nagios log file
 
For more information on obtaining support for Nagios, visit:
 
       https://support.nagios.com
 
*************************************************************
 
Enjoy.

 

:~/nagios-4.3.1# make install
cd ./base && make install
make[1]: Entering directory '/root/nagios-4.3.1/base'
make install-basic
[...]
 
*** Main program, CGIs and HTML files installed ***
 
You can continue with installing Nagios as follows (type 'make'
without any arguments for a list of all possible options):
 
  make install-init
     - This installs the init script in /etc/init.d
 
  make install-commandmode
     - This installs and configures permissions on the
       directory for holding the external command file
 
  make install-config
     - This installs sample config files in /usr/local/nagios/etc
 
make[1]: Leaving directory '/root/nagios-4.3.1'

 

 

:~/nagios-4.3.1# make install-config
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc/objects
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/nagios.cfg /usr/local/nagios/etc/nagios.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/cgi.cfg /usr/local/nagios/etc/cgi.cfg
/usr/bin/install -c -b -m 660 -o nagios -g nagios sample-config/resource.cfg /usr/local/nagios/etc/resource.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/templates.cfg /usr/local/nagios/etc/objects/templates.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/commands.cfg /usr/local/nagios/etc/objects/commands.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/contacts.cfg /usr/local/nagios/etc/objects/contacts.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/timeperiods.cfg /usr/local/nagios/etc/objects/timeperiods.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/localhost.cfg /usr/local/nagios/etc/objects/localhost.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/windows.cfg /usr/local/nagios/etc/objects/windows.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/printer.cfg /usr/local/nagios/etc/objects/printer.cfg
/usr/bin/install -c -b -m 664 -o nagios -g nagios sample-config/template-object/switch.cfg /usr/local/nagios/etc/objects/switch.cfg
 
*** Config files installed ***
 
Remember, these are *SAMPLE* config files.  You'll need to read
the documentation for more information on how to actually define
services, hosts, etc. to fit your particular needs.

 

:~/nagios-4.3.1# make install-init
/usr/bin/install -c -m 755 -d -o root -g root /etc/init.d
/usr/bin/install -c -m 755 -o root -g root daemon-init /etc/init.d/nagios
 
*** Init script installed ***
 
:~/nagios-4.3.1# make install-commandemode
make: *** No rule to make target 'install-commandemode'. Arrêt.
root@monitoring:~/nagios-4.3.1# make install-commandmode
/usr/bin/install -c -m 775 -o nagios -g nagcmd -d /usr/local/nagios/var/rw
chmod g+s /usr/local/nagios/var/rw
 
*** External command directory configured ***
 

 

once you did all the make required, you will probably require to create a systemd service file since the init doesn't create it. To make it simple, try to start nagios and you will probably get this error:

 

:~/nagios-4.3.1# service nagios start
Failed to start nagios.service: Unit nagios.service failed to load: No such file or directory.

so you must create the unit file " /etc/systemd/system/nagios.service":

[Unit]
Description=Nagios
BindTo=network.target
 
[Install]
WantedBy=multi-user.target
 
[Service]
User=nagios
Group=nagios
Type=simple
ExecStart=/usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg

once it's done, you must reload the sysctl and you can start nagios with unit.d or sysctl as you prefer:

 

:~/nagios-4.3.1# systemctl daemon-reload
:~/nagios-4.3.1# service nagios start
root@monitoring:~/nagios-4.3.1# service nagios status
 nagios.service - Nagios
   Loaded: loaded (/etc/systemd/system/nagios.service; disabled)
   Active: active (running) since ven. 2017-04-28 20:51:26 CEST; 1s ago
 Main PID: 18146 (nagios)
   CGroup: /system.slice/nagios.service
           ─18146 /usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg
           ─18147 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
           ─18148 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
           ─18149 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
           ─18150 /usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
           ─18151 /usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg
 
avril 28 20:51:26 monitoring nagios[18146]: wproc: Registry request: name=Core Worker 18150;pid=18150
avril 28 20:51:26 monitoring nagios[18146]: wproc: Registry request: name=Core Worker 18149;pid=18149
avril 28 20:51:26 monitoring nagios[18146]: wproc: Registry request: name=Core Worker 18149;pid=18149
avril 28 20:51:26 monitoring nagios[18146]: wproc: Registry request: name=Core Worker 18148;pid=18148
avril 28 20:51:26 monitoring nagios[18146]: wproc: Registry request: name=Core Worker 18148;pid=18148
avril 28 20:51:26 monitoring nagios[18146]: wproc: Registry request: name=Core Worker 18147;pid=18147
avril 28 20:51:26 monitoring nagios[18146]: wproc: Registry request: name=Core Worker 18147;pid=18147
avril 28 20:51:27 monitoring nagios[18146]: Successfully launched command file worker with pid 18151
avril 28 20:51:27 monitoring nagios[18146]: Successfully launched command file worker with pid 18151
avril 28 20:51:27 monitoring nagios[18146]: HOST ALERT: localhost;DOWN;SOFT;1;(No output on stdout) stderr: execvp(/usr/local/nagios/libexec/check_ping, ...) failed. errno is 2: No such file or directory

 

2.1. web interface configuration 

to install the web interface you must type in this command into the source directory nagios:

:~/nagios-4.3.1# make install-webconf
/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-available/nagios.conf
if [ 1 -eq 1 ]; then \
        ln -s /etc/apache2/sites-available/nagios.conf /etc/apache2/sites-enabled/nagios.conf; \
fi
 
*** Nagios/Apache conf file installed ***
 

you must then type these commands:

:~# a2enmod rewrite && a2enmod cgi
Enabling module rewrite.
To activate the new configuration, you need to run:
  service apache2 restart
Enabling module cgi.
To activate the new configuration, you need to run:
  service apache2 restart
:~# service apache2 restart

  then you must create a user/password to access the web interface

 

htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

if you doesnt use this default user name you must change it also into this file on every line: /usr/local/nagios/etc/cgi.cfg

 

once everything is done, you must restart nagios and apache using this command:

service nagios restart
service apache2 restart

and you can access nagios web interface through your brower using the ip of your nagios server (ifconfig) : http://<ip>/nagios and using the login/password you set before.

once it's done, you can click on "host" or "service" to see your localhost

 

 

2.2. plugin installation

Nagios works with a plugin architecture, you can find a lot on Internet. the second tarball we took from nagios.com are the common plugins. go to the directory and type in these command to compile them:

 

:~/nagios-4.3.1# cd
:~# cd nagios-plugins-2.2.1/
:~/nagios-plugins-2.2.1#
:~/nagios-plugins-2.2.1# ./configure -with-nagios-user=nagios --with-nagios-group=nagios --with-openssl
[..]
:~/nagios-plugins-2.2.1# make
[..]
:~/nagios-plugins-2.2.1# make install
[..]

 

3. Configuration

 The use of plugin and command isn't very intuitive at first. to help you here is what you can need to know:

/usr/local/nagios/etc/objects/commands.cfg

This file contain all the commands available to check and how nagios should execute them on the host. for Example:

# 'check_local_load' command definition
define command{
        command_name    check_local_load
        command_line    $USER1$/check_load -w $ARG1-c $ARG2$
        }

All these command use binary located in /usr/local/nagios/libexec

let's start with localhost. the localhost file is located also in the objects directory

 if you see your localhost on the webinterface, we can add some check, for example i'm using lvm to separate my file system, and by default it only check the root (/). So let's add these command:

define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             Partition home
        check_command                   check_local_disk!20%!10%!/home
        }
 
 
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             Partition var
        check_command                   check_local_disk!20%!10%!/var
        }
 
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             Partition tmp
        check_command                   check_local_disk!20%!10%!/tmp

 

 once it's done, you must restart nagios and you should have something like this when you click on "services" :

 

 

 

 4. client configuration

To use these command on your client you must:

- install nrpe plugins on the client (required to allow the server to remote execute command on the host)

- install the general nagios plugins on the client

 

 

4.1 Install required package

 

On your client/host AND server you must install these package:

apt-get update
apt-get upgrade
apt-get install nagios-plugins nagios-nrpe-server nagios-nrpe-plugin

nrpe allowed to execute remote command on hosts.

 

4.2 configure nrpe

for your first install i highly recommand to disable some feature in nrpe configuration such as allowed_host (restrict which host are allowed to connect) and server_address (restrict the interface to listen on). indeed at first it could give you some hard time. Once everything is working, you can enforced it if you want. the config file can be found here : /etc/nagios/nrpe.cfg

 

on the host/client you must then declare the command you want to use. In /etc/nagios/nrpe.cfg, you can add for example:

command[check_root]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /
command[check_var]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /var
command[check_home]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /home
command[check_tmp]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /tmp

 

 you can test it like this (after a restart of nrpe):

/usr/lib/nagios/plugins/check_nrpe -H 127.0.0.1 -c check_root
DISK OK - free space: / 3436 MB (66% inode=75%);| /=1692MB;4341;4884;0;5427

 

if it's working, you must then try it on your nagios server using your host ip.

if it's working you must then declare the "nagios command" to be able to use it directly into your configfile:

vim /usr/local/nagios/etc/objects/commands.cfg
# this command runs a program $ARG1$ with arguments $ARG2$
define command {
        command_name    check_nrpe
        command_line    /usr/lib/nagios/plugins/check_nrpe -H $HOSTADDRESS-u -c $ARG1-a $ARG2$ARG3$ARG4$ARG5$ARG6$
}
 
# this command runs a program $ARG1$ with no arguments
define command {
        command_name    check_nrpe_noarg
        command_line    /usr/lib/nagios/plugins/check_nrpe -H $HOSTADDRESS-u -c $ARG1$
}

 

 once it's done you have to declare your host. to do so, you can edit the file : /usr/local/nagios/etc/nagios.cfg and remove the "#" in front of this line

cfg_dir=/usr/local/nagios/etc/servers

then you must create this directory like this

 

mkdir /usr/local/nagios/etc/servers
chown nagios:nagios /usr/local/nagios/etc/servers

 then you create a file and add your configuration into it:

 

define host{
        use                     linux-server            ; Name of host template to use
                                                        ; This host definition will inherit all variables that are defined
                                                        ; in (or inherited by) the linux-server host template definition.
        host_name               elasticsearch
        alias                   elasticsearch
        address                 192.168.0.12
        }
 
 
 
# Define a service to "ping" the local machine
 
define service{
        use                             generic-service         ; Name of service template to use
        host_name                       elasticsearch
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60%
        }
 
 
# Define a service to check the disk space of the root partition
# on the local machine.  Warning if < 20% free, critical if
# < 10% free space on partition.
 
define service{
        use                             generic-service         ; Name of service template to use
        host_name                       elasticsearch
        service_description             Partition Root
        check_command                   check_nrpe_noarg!check_root
        }
 
define service{
        use                             generic-service         ; Name of service template to use
        host_name                       elasticsearch
        service_description             Partition home
        check_command                   check_nrpe_noarg!check_home
        }

 Once it's done, restart nagios and voilà:

 

 

 

 

 

Add comment


Security code
Refresh

Go to Top
Template by JoomlaShine