← All posts

Debian. Installing and Configuring Nagios Monitoring

Nagios logo

In my previous article, I explained how to install and configure Nagios 3 on FreeBSD, but since there are a few small differences for Debian, I decided to describe them. They mostly concern installing and configuring php/cgi rather than configuring Nagios itself.

This assumes you already have git, nginx, and wget installed.
You will also need a text editor, such as emacs.

The version discussed here is Nagios 3.2.1.

Installation

We need to install “Nagios 3”, “php5-fpm”, and “fcgiwrap”:

sudo apt-get update && apt-get upgrade
sudo apt-get install nagios3 php5-fpm

Not long ago, I explained how to install a recent version of fcgiwrap for Debian that has the features we need:

sudo apt-get install libfcgi-dev autoconf2.13
cd ~/Downloads
git clone git://github.com/gnosek/fcgiwrap.git fcgiwrap
cd fcgiwrap
autoreconf -i
./configure 
make
sudo make install

Configuring fcgiwrap

After installing fcgiwrap, copy the init script and create a directory for the Unix socket:

wget -O /etc/init.d/fcgiwrap http://adw0rd.com/files/fcgiwrap
mkdir /var/run/fcgiwrap
chown www-data:www-data /var/run/fcgiwrap/

Open /etc/init.d/fcgiwrap for editing, find FCGI_SOCKET, and edit it:

FCGI_SOCKET="/var/run/$NAME/$NAME.socket"
#FCGI_PORT="8088"
#FCGI_ADDR="127.0.0.1"

Start it:

sudo /etc/init.d/fcgiwrap start

Configuring php5-fpm

My /etc/php5/fpm/pool.d/www.conf configuration looked like this:


[www]
user = www-data
group = www-data
listen = /var/run/php5/fpm.sock;
listen.owner = www-data
listen.group = www-data
listen.mode = 0666
...

Start it:

sudo /etc/init.d/php5-fpm start

Configuring Nagios

Open the file /etc/nagios3/cgi.cfg for editing, find the line containing “url_html_path”, and replace it with:

url_html_path=/

Otherwise, all the configuration files are identical to those I wrote about in my previous article. The main thing to remember is that commands are stored in the /etc/nagios-plugins/config/ directory, and to add your own commands, you can edit the existing *.cfg files or create your own.


/etc/nagios-plugins/config
├── apt.cfg
├── breeze.cfg
├── dhcp.cfg
├── disk.cfg
├── disk-smb.cfg
├── dns.cfg
├── dummy.cfg
├── flexlm.cfg
├── fping.cfg
├── ftp.cfg
├── games.cfg
├── hppjd.cfg
├── http.cfg
├── ifstatus.cfg
├── ldap.cfg
├── load.cfg
├── mail.cfg
├── mailq.cfg
├── mrtg.cfg
├── mysql.cfg
├── netware.cfg
├── news.cfg
├── nt.cfg
├── ntp.cfg
├── pgsql.cfg
├── ping.cfg
├── procs.cfg
├── radius.cfg
├── real.cfg
├── rpc-nfs.cfg
├── snmp.cfg
├── ssh.cfg
├── tcp_udp.cfg
├── telnet.cfg
└── users.cfg

Notification commands (for example, for SMS) need to be defined in the file /etc/nagios3/commands.cfg.


/etc/nagios3
├── cgi.cfg
├── commands.cfg
├── conf.d
│   ├── contacts_nagios2.cfg
│   ├── extinfo_nagios2.cfg
│   ├── generic-host_nagios2.cfg
│   ├── generic-service_nagios2.cfg
│   ├── hostgroups_nagios2.cfg
│   ├── localhost_nagios2.cfg
│   ├── services_nagios2.cfg
│   └── timeperiods_nagios2.cfg
├── htpasswd.users
├── nagios.cfg
└── resource.cfg

To configure contacts, services, hosts, and groups for all of these, use the /etc/nagios3/conf.d directory, but I think you can figure that out yourselves.

Now start Nagios:

sudo /etc/init.d/nagios3 start

Configuring Nginx

To proxy the Nagios web interface through Nginx, we need to create a new configuration file, /etc/nginx/sites-available/nagios.conf:


server {
    listen 80;
    server_name nagios.example.org;
    auth_basic "Password, please!";
    auth_basic_user_file /path/to/htpasswd;
    root /usr/share/nagios3/htdocs;
    index index.php index.html;
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php5/fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location /stylesheets/ {
        alias /etc/nagios3/stylesheets/;
    }
    location ~ ^/cgi-bin/nagios3/.+\.cgi$ {
        rewrite ^/cgi-bin/nagios3/(.+\.cgi)$ /$1 break;
        root /usr/lib/cgi-bin/nagios3;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
        # У нас свой htpasswd файл, в котором нет пользователя "nagiosadmin", но так как мы доверяем своему файлу,
        # то подменяем $remote_user с нашим пользователем на "nagiosadmin" чтобы Nagios не паниковал:
        fastcgi_param AUTH_USER "nagiosadmin";
        fastcgi_param REMOTE_USER "nagiosadmin";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Don’t forget to generate htpasswd:

# Создаем новый
htpasswd -cmb /path/to/htpasswd <username> <password>

# Или обновляем существующий
htpasswd -mb /path/to/htpasswd <username> <password>

Create a symlink to the configuration file and reload Nginx:

sudo ln -s /etc/nginx/sites-available/nagios.conf /etc/nginx/sites-enabled/nagios.conf
sudo nginx -s reload

Further Reading

Comments are closed. Read the original discussion in Russian →