Nagios3 Raspberry Pi Installation and Configuration with nginx

We will install the package nagios3 from the raspbian repository, by default nagios3 uses the Apache web server but we will use the nginx web server in this article.


$ sudo apt install nagios3 php php-fpm fcgiwrap nginx

You will be asked for the nagiosadmin password after installation.

Setting up nginx configuration: (/etc/nginx/sites-enabled/nagios.conf)


server {
    server_name     localhost;
    root            /usr/share/nagios3/htdocs;
    listen          8080;
    index           index.php index.html index.htm;
    access_log      /var/log/nginx/nagios.access.log;
    error_log       /var/log/nginx/nagios.error.log;

    auth_basic            "Nagios Admin Access";
    auth_basic_user_file  /etc/nagios3/htpasswd.users;

    # Fixes frames not working
    add_header X-Frame-Options "ALLOW";

	location /stylesheets {
                alias /etc/nagios3/stylesheets;
	}

	location /nagios3/stylesheets {
                alias /etc/nagios3/stylesheets;
	}

	location /nagios3/images {
                alias /usr/share/nagios3/htdocs/images;
	}

    location ~ \.php$ {
        try_files       $uri = 404;
        fastcgi_index   index.php;
        fastcgi_pass    unix:/var/run/php/php7.0-fpm.sock;
        include         fastcgi.conf;
    }

    location ~ \.cgi$ {
         rewrite ^/cgi-bin/nagios3/(.*)$ /$1;
         include /etc/nginx/fastcgi_params;
         
         fastcgi_param AUTH_USER $remote_user;
         fastcgi_param REMOTE_USER $remote_user;
	     fastcgi_param SCRIPT_FILENAME /usr/lib/cgi-bin/nagios3$fastcgi_script_name;

         fastcgi_pass    unix:/var/run/fcgiwrap.socket;
    }

    # Fixes the fact some links are expected to resolve to /nagios, see here.
    location /nagios3 {
        alias /usr/share/nagios3/htdocs;
    }
}

Restart nginx, fcgiwrap and php-fpm service.


$ sudo systemctl restart nginx
$ sudo systemctl restart fcgiwrap
$ sudo systemctl restart php7.0-fpm

This configuration will allow you to access nagios on http://localhost:8080

Monitoring MySQL/MariaDB

Create a user for nagios (be sure to change user and password):


$ sudo mysql -u root
> CREATE USER 'nagios'@'localhost'  IDENTIFIED BY 'password';

Sample output:


Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.1.38-MariaDB-0+deb9u1 Raspbian 9.0

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CREATE USER 'nagios'@'localhost'  IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.01 sec)

Create and edit the file: /etc/nagios3/conf.d/services_nagios2.cfg
be sure to change the user: nagios and password: password


define service {
        hostgroup_name                  mysql-servers
        service_description             MYSQL
        check_command                   check_mysql_cmdlinecred!nagios!password
        use                             generic-service
        notification_interval           0 ; set > 0 if you want to be renotified
}

Create and edit the file: /etc/nagios3/conf.d/hostgroups_nagios2.cfg


define hostgroup {
        hostgroup_name  mysql-servers
        alias           MySQL Servers
        members         localhost
   }
   

Test config
$ sudo nagios3 -v /etc/nagios3/nagios.cfg

Sample output:


...
Total Errors:   0

Things look okay - No serious problems were detected during the pre-flight check

Checking DNS server:
Edit the file: /etc/nagios3/conf.d/services_nagios2.cfg


define service{
    use                 generic-service
    host_name           localhost
    service_description DNS
    check_command       check_dns
}

TO BE UPDATED FOR OTHER SERVICES.

References:
https://gist.github.com/mrbichel/1754118
https://wiki.archlinux.org/index.php/Nagios#Nginx_configuration

Nagios Monitoring MySQL Servers

How to Enable check_dns on Nagios Monitoring System