pnp4nagios and icinga2

pnp4nagios and icinga2

This is not my article, it is copy from this site. It cover install icinga2, icinga2 director and pnp4nagios on Ubuntu 20.04, but is very useful as manual for debian buster and Bullseye.

This guide shows how to install Icinga2 monitoring software on Ubuntu 20.04 – this includes backend, frontend, director and pnp4nagios (adding graphs to icinga). Especially PNP was hard to get working – but if you follow the guide everything is nicely lined up at the end.

Offset: Basic ubuntu 20.04 server with openssh package (used 2 vcpu, 3GB ram and 100GB disk)

# My setup:
Hostname: icinga2.skau.dk
IP address: 192.168.112.189

Icinga2 specific decissions:
Icing2 database
DB type mariadb
DB name: icinga2
DB User: icinga2
DB Passwd: icingaPW

icinga2web database:
DB type mariadb
DB name: icinga2_web
DB User: icinga2
DB Passwd: icingaPW

Director database:
DB type mariadb
DB name: icinga2_director
DB User: icinga2
DB Passwd: icingaPW

Let’s start out by disabling the firewall

systemctl stop ufw
systemctl disable ufw

Install and configure mysql

Install mariadb and create user and databases

apt-get install mariadb-server mariadb-client
mysql -u root -p

CREATE USER 'icinga2'@'localhost' IDENTIFIED BY 'icingaPW';
CREATE DATABASE icinga2 CHARACTER SET 'utf8';
CREATE DATABASE icinga2_web CHARACTER SET 'utf8';
CREATE DATABASE icinga2_director CHARACTER SET 'utf8';

GRANT ALL ON *.* to 'icinga2'@'localhost' IDENTIFIED BY 'icingaPW'

quit

Now your databases are ready for the install

Install icinga2 backend

Install all the packages needed using apt (so they will be updated using package manager)

apt-get update
apt-get -y install apt-transport-https wget gnupg

wget -O - https://packages.icinga.com/icinga.key | apt-key add -

. /etc/os-release; if [ ! -z ${UBUNTU_CODENAME+x} ]; then DIST="${UBUNTU_CODENAME}"; else DIST="$(lsb_release -c| awk '{print $2}')"; fi; \
 echo "deb https://packages.icinga.com/ubuntu icinga-${DIST} main" > \
 /etc/apt/sources.list.d/${DIST}-icinga.list
 echo "deb-src https://packages.icinga.com/ubuntu icinga-${DIST} main" >> \
 /etc/apt/sources.list.d/${DIST}-icinga.list

apt-get update
apt-get install icinga2
apt-get install monitoring-plugins
apt-get install icinga2-ido-mysql
# (answer yes to install and no to configure)

Debian Bullseye has packages in repository:

apt-get update
apt-get install apt-transport-https wget gnupg
apt-get install icinga2 monitoring-plugins icinga2-ido-mysql

Setup db connection in /etc/icinga2/features-available/ido-mysql.conf

vi /etc/icinga2/features-available/ido-mysql.conf

/**
 * The db_ido_mysql library implements IDO functionality
 * for MySQL.
 */

library "db_ido_mysql"

object IdoMysqlConnection "ido-mysql" {
  user = "icinga2",
  password = "icingaPW",
  host = "localhost",
  database = "icinga2"
}

Enable icinga2 to use database backend and load database schema

icinga2 feature enable ido-mysql
mysql -uicinga2 -picingaPW icinga2 < /usr/share/icinga2-ido-mysql/schema/mysql.sql
systemctl restart icinga2

Install Icinga2-web (frontend)

apt-get install apache2 php-gd php-json
icinga2 api setup

Now add a user to icinga2 backend by adding to /etc/icinga2/conf.d/api-users.conf, create a user object for icingaweb2 beneath the configured root user

vi /etc/icinga2/conf.d/api-users.conf

object ApiUser "icingaweb2" {
  password = "Wijsn8Z9eRs5E25d"
  permissions = [ "status/query", "actions/*", "objects/modify/*", "objects/query/*" ]
}

Restart incinga2 daemon

systemctl restart icinga2

Install the icingaweb2 frontend itself

note: (memorizing the token at the end (which can be displayed using the last command if you forget)

apt-get install icingaweb2 libapache2-mod-php icingacli
icingacli setup token create
icingacli setup token show

Browse to your new installation using IP (http://192.168.112.189/icingaweb2/setup)

  • insert the token click next
  • enable doc and monitoring and click next
  • accept that PDO-Postgress is missing and click next
  • set authentication to database and click next
  • Name your connection, fill in the new incingadb names and remember to set caracter set to utf8 click next
  • accept backend name of icingaweb2 click next
  • setup first webuser (the master admin) – click next
  • accept application config and click next
  • accept the summary and click next
  • Click next to start config
  • Accept default backend definitions, click next
  • Set the connection details for the icinga2 database (remember to specify utf8 as Char set) – click next
  • Command transport is set to Icinga2 API, use the username password generated earlier in the config file (icingaweb2 / Wijsn8Z9eRs5E25d) – click next
  • Accept the default protected variables – click next
  • Accept the backend config and click finish
  • click the login to Icinga Web 2 button in the upper left corner
  • Log in with the administrative user created in step 8

Adding icingaweb2 addons

Adding Director first – this module gives you the power to configure icinga through the webinterface of icinga2-web.

First lets install all modules from git.

git clone "https://github.com/icinga/icingaweb2-module-director" "/usr/share/icingaweb2/modules/director" --branch v1.8.0
cd /usr/share/icingaweb2/modules/director
git checkout "v1.8.0"

git clone "https://github.com/Icinga/icingaweb2-module-ipl" "/usr/share/icingaweb2/modules/ipl" --branch "v0.5.0"
git clone "https://github.com/Icinga/icingaweb2-module-incubator" "/usr/share/icingaweb2/modules/incubator" --branch "v0.6.0"
git clone "https://github.com/Icinga/icingaweb2-module-reactbundle" "/usr/share/icingaweb2/modules/reactbundle" --branch "v0.9.0"

icingacli module enable ipl
icingacli module enable incubator
icingacli module enable reactbundle
icingacli module enable director

Now we need to add another user to /etc/icinga2/conf.d/api-users.conf underneath the icingaweb2 user definition

vi /etc/icinga2/conf.d/api-users.conf

object ApiUser "icingadirector" {
  password = "Wijsn8Z9eRs5E25d"
  permissions = [ "*" ]
}

Now you need to configure your first zone and first endpoint in zones.conf

vi /etc/icinga2/zones.conf

object Endpoint "icinga2.skau.dk" {
  host = "192.168.112.189"
}

object Zone "master" {
 endpoints = [ "icinga2.skau.dk" ]
}

Reboot to ensure all comes up autoatically

shutdown -r now

From the webfrontend create a new database resource pointing to your new director db

http://192.168.112.189/icingaweb2

  • goto Configuration -> Application -> Resources

  • create a new database object pointing to your director db – remember to set charset to utf8

  • goto module config: modules -> Director -> configuration

  • Select icinga2_director as database backend

  • click create database scheme This will start the kickstart engine

  • endpoint name: icinga2.skau.dk

  • icinga host: icinga2.skau.dk

  • icinga API user: icingadirector

  • icinga API password: Wijsn8Z9eRs5E25d

  • click store config

Deploy outstanding configurations in the webpage by:

Icinga Director -> Deployments -> click on Render Config

Now you need to setup the daemon for director to pickup changes

useradd -r -g icingaweb2 -d /var/lib/icingadirector -s /bin/false icingadirector
install -d -o icingadirector -g icingaweb2 -m 0750 /var/lib/icingadirector
cp /usr/share/icingaweb2/modules/director/contrib/systemd/icinga-director.service /etc/systemd/system/
systemctl daemon-reload
systemctl enable icinga-director
systemctl start icinga-director

Setup PNP and pnp4nagios

This part is the hardest to get right – but the follow receipt worked for me.

install the graphing modules

apt-get install icingaweb2-module-pnp 
apt-get install rrdtool librrd-dev librrds-perl librrdp-perl php-xml php-rrd gcc make

mkdir -p /files/pnp
cd /files/pnp
wget -O pnp4nagios.tar.gz https://github.com/lingej/pnp4nagios/archive/0.6.26.tar.gz
tar -xzf pnp4nagios.tar.gz

cd pnp4nagios-0.6.26

./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-rrdtool=/usr/bin/rrdtool --with-perfdata-dir=/var/spool/icinga2/perfdata --with-httpd-conf=/etc/apache2/sites-available

make all
make full-install

Remove login to pnp4nagios (by changing apache conf file (all chages before mod_rewrite section)

vi /etc/apache2/sites-available/pnp4nagios.conf

# SAMPLE CONFIG SNIPPETS FOR APACHE WEB SERVER

Alias /pnp4nagios "/usr/local/pnp4nagios/share"

<Directory "/usr/local/pnp4nagios/share">
        AllowOverride None
#       Order allow,deny
#       Allow from all
        require all granted
        #
        # Use the same value as defined in nagios.conf
        #
#       AuthName "Nagios Access"
#       AuthType Basic
#       AuthUserFile /usr/local/nagios/etc/htpasswd.users
#       Require valid-user
        <IfModule mod_rewrite.c>
                # Turn on URL rewriting
                RewriteEngine On
                Options symLinksIfOwnerMatch
                # Installation directory
                RewriteBase /pnp4nagios/
                # Protect application and system files from being viewed
                RewriteRule "^(?:application|modules|system)/" - [F]
                # Allow any files or directories that exist to be displayed directly
                RewriteCond "%{REQUEST_FILENAME}" !-f
                RewriteCond "%{REQUEST_FILENAME}" !-d
                # Rewrite all other URLs to index.php/URL
                RewriteRule "^.*$" "index.php/$0" [PT]
        </IfModule>
</Directory>

Enable the config and reload apache

a2ensite pnp4nagios.conf
systemctl reload apache2

Add www-data to nagios group so it can read data

vi /etc/group add to the existing nagios line

nagios:x:118:www-data

Hit the pnp4nagios webpage – all should be green : http://192.168.112.189/pnp4nagios

Enable pnp module and start creating perfdata

icingacli module enable pnp
icinga2 feature enable perfdata
systemctl restart icinga2
mv /usr/local/pnp4nagios/share/install.php /usr/local/pnp4nagios/share/install.php.orig
cd /usr/local/pnp4nagios/etc
mv misccommands.cfg-sample misccommands.cfg
mv nagios.cfg-sample nagios.cfg
systemctl enable npcd
systemctl start npcd

get_magic_quotes_runtime() is deprecated

When you run pnp4 nagios, you get this error:

Please check the documentation for information about the following error.
Function get_magic_quotes_runtime() is deprecated

file [line]:
/usr/local/pnp4nagios/lib/kohana/system/libraries/Input.php [57]:

Pnp4nagios is build for PHP5, as we are using PHP7 there is 1 deprecated function in Input.php that needs to be bypassed.

vi /usr/local/pnp4nagios/lib/kohana/system/libraries/Input.php

Find on row 57 this code:

if (get_magic_quotes_runtime())
{
        set_magic_quotes_runtime(0);
        Kohana::log('debug', 'Disable magic_quotes_runtime! It is evil and deprecated: http://php.net/magic_quotes');
}

// magic_quotes_gpc is enabled
if (get_magic_quotes_gpc())
{
        $this->magic_quotes_gpc = TRUE;
        Kohana::log('debug', 'Disable magic_quotes_gpc! It is evil and deprecated: http://php.net/magic_quotes');
}

And replace with this code:

if (version_compare(PHP_VERSION, '5.3.0', '<')) 
{
        // magic_quotes_runtime is enabled
        if (get_magic_quotes_runtime())
        {
                set_magic_quotes_runtime(0);
                Kohana::log('debug', 'Disable magic_quotes_runtime! It is evil and deprecated: http://php.net/magic_quotes');
        }

        // magic_quotes_gpc is enabled
        if (get_magic_quotes_gpc())
        {
                 $this->magic_quotes_gpc = TRUE;
                 Kohana::log('debug', 'Disable magic_quotes_gpc! It is evil and deprecated: http://php.net/magic_quotes');
        }
}

Then you need to change line 979 in the file: /usr/local/pnp4nagios/share/application/models/data.php

(use :979 to jump to that line)

vi /usr/local/pnp4nagios/share/application/models/data.php

# From:
if(sizeof($pages) > 0 ){
# To:
if(is_array($pages) && sizeof($pages) > 0 ){

Correct perfdata_spool_dir line in npcd config

vi /usr/local/pnp4nagios/etc/npcd.cfg

perfdata_spool_dir = /var/spool/icinga2/perfdata

restart npcd to pickup changes

systemctl restart npcd

Last go in icinga2 webpage and configure pnp to the correct config dir:

Log into icinga2 and goto configuration -> modules -> pnp -> configuration

  • set pnp4nagios configuration to: /usr/local/pnp4nagios/etc

At this point I like to reboot to ensure everything works after reboot

Shutdown -r now

Icingaweb2 service page not showing pnp thumbnails

After install pnp4nagios, I don't see any of thumbnails under the icingaweb2 service page.

pnp4nagios not showing thumbnails pnp4nagios not showing thumbnails

Problem occurs when there are two files (config.php and config_local.php) in PNP4Nagios etc's-directory which define identical views. The solution is to remove the views from one of the files.

~] ls -alFh
drwxr-xr-x 5 nagios nagios 4.0K Sep  2 12:37 ./
drwxr-xr-x 9 root   root   4.0K Sep  1 20:44 ../
-rw-r--r-- 1 nagios nagios  29K Sep  1 20:44 background.pdf
drwxr-xr-x 2 nagios nagios 4.0K Sep  1 20:44 check_commands/
drwxr-xr-x 2 nagios nagios 4.0K Sep  1 20:44 config.d/
-rw-r--r-- 1 nagios nagios 9.4K Sep  2 12:27 config_local.php
-rw-r--r-- 1 nagios nagios 9.4K Sep  1 20:44 config.php
-rw-r--r-- 1 nagios nagios 9.4K Sep  1 20:44 config.php.0.6.26
-rw-r--r-- 1 nagios nagios 1.4K Sep  1 20:44 misccommands.cfg
-rw-r--r-- 1 nagios nagios 1.7K Sep  1 20:44 nagios.cfg
-rw-r--r-- 1 nagios nagios 4.1K Sep  1 20:52 npcd.cfg
drwxr-xr-x 2 nagios nagios 4.0K Sep  1 20:44 pages/
-rw-r--r-- 1 nagios nagios  302 Sep  1 20:44 pnp4nagios_release
-rw-r--r-- 1 nagios nagios 2.5K Sep  1 20:44 process_perfdata.cfg
-rw-r--r-- 1 nagios nagios  651 Sep  1 20:44 rra.cfg-sample

Solution:

You have to you comment out the view statements in the config_local.php file, this fixes the double thumbnails.

vi /usr/local/pnp4nagios/etc/config_local.php

# Define default views with title and start timerange in seconds
#
# remarks: required escape on " with backslash
#
#$views[] = array('title' => 'One Hour',  'start' => (60*60) );
#$views[] = array('title' => '4 Hours',   'start' => (60*60*4) );
#$views[] = array('title' => '25 Hours',  'start' => (60*60*25) );
#$views[] = array('title' => 'One Week',  'start' => (60*60*25*7) );
#$views[] = array('title' => 'One Month', 'start' => (60*60*24*32) );
#$views[] = array('title' => 'One Year',  'start' => (60*60*24*380) );

pnp4nagios thumbnails problem resolved:

Sources