A Guide to Monitoring Servers/Services with Nagios — Part 3

Kelom
8 min readSep 4, 2020

--

(ADDING A REMOTE LINUX BOX AND MONITORING MYSQL)

In part 1 of this series, we installed and configure the Nagios server (read it here)
In part 2, we went a step further and added a Windows host to the Nagios Core server for monitoring (read it here).

In this article, we are going to monitor a linux box and mysql 5.7 database on that linux box. You can monitor a number of services (http, ssh, vpn, etc) with Nagios

Prerequisite:

What we will be doing:

· Installing NRPE on the linux box.
· Installing mysql 5.7 databse
· Nagios configuration to monitor the linux box and the mysql database

1. Installing NRPE

Step 1: download the necessary packages required by Nagios:

yum install -y wget httpd php gcc glibc glibc-common gd gd-devel make net-snmp perl perl-devel openssl openssl-devel

Step 2: Installing NRPE, download the NRPE file, i am downloading it to the tmp folder:

wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-3.2.1/nrpe-3.2.1.tar.gz -P /tmp/

Step 3: Extract the NRPE Source File downloaded in step 2

cd /tmp
tar zxvf nrpe-3.2.1.tar.gz

Step 4: Configure NRPE by running the command:

cd /tmp/nrpe-3.2.1/
./configure — enable-command-args — with-nrpe-user=nagios — with-nrpe-group=naggrp

Step 5: still in the /nrpe-3.2.1/ directory, run the following commands:

make all
make install
make install-config
make install-init

2. Installing mysql 5.7 database

Step 1: I change directory to my downloads folder, and download MySQL 5.7 repo file :

wget https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

Step 2: still in the downloads folder, Install MySQL 5.7 repo file:

sudo rpm -ivh mysql57-community-release-el7–8.noarch.rpm

Step 3: We now install the mysql server database:

sudo yum install mysql-server –y

Step 4: Start the MySQL server:

sudo systemctl start mysqld

Step 5: Enable Mysql to auto start at boot/reboot:

sudo systemctl enable mysqld

Step 6:

During the MYSQL installation, initial password for the root is created can be found in the error log file. We get the password by issuing the following command:

sudo grep ‘temporary password’ /var/log/mysqld.log

We will run the mysql_secure_installation script to perform the following MYSQL security recommendations:

· Changing the MySQL root password
· Removing anonymous user accounts
· Disabling root logins outside of localhost
· Removing test databases.

sudo mysql_secure_installation

we have successfully installed mysql on the linux box.

We can now connect to the mysql database:

mysql –u root –p

Step 7:

With mysql installed, we configure the firewall to allow access to port 3306 (default port for mysql)

sudo firewall-cmd — permanent — add-port=3306/tcp

Step 8:

We start the firewall:

firewall-cmd –reload

Step 9:

We now create a user for Nagios:
note: 192.168.20.132 is the ip of my Nagios box (enter the ip of your Nagios box). read about how to install nagios here.

create user ‘nagios’@’192.168.20.132' identified by ‘Pas1234’;grant usage on *.* to ‘nagios’@’192.168.20.132';flush privileges;

3. Nagios configuration to monitor the linux box and the mysql database

Step 1:

We go to the nagios box and download check_mysql_health

wget https://labs.consol.de/assets/downloads/nagios/check_mysql_health-2.2.2.tar.gz

Step 2:

Change directory to the download location and unzip the file

tar zxvf check_mysql_health-2.2.2.tar.gz

Step 3:

Change directory to the extracted folder:

cd check_mysql_health-2.2.2/
./configure
Make

Step 4:

Still in check_mysql_health-2.2.2 directory, we change directory to plugins-scripts:

cd plugins-scripts

we now test the check_mysql_health:
note: 192.168.20.131 is the ip of the mysql box

./check_mysql_health -H 192.168.20.131 — user nagios — password Pa_s1234 — mode uptime

We should see: OK — database is up since xxxxxxx minutes | uptime=xxxxxxxxxxxs

When the above command returns OK, we are good to continue

Step 5:

Still on the Nagios server, we will add an entry to the command file. change directory to /usr/local/nagios/etc/objects:

cd /usr/local/nagios/etc/objects

Step 6:

vi commands.cfg

add the following:

define command{
command_name check_mysql_health
command_line $USER1$/check_mysql_health -H $ARG1$ — port $ARG2$ — username $ARG3$ — password $ARG4$ — mode $ARG5$
}

Step 7:

Ideally, we should have separate config files for the servers/host and the services, but for the purposes of this tutorials, we will do everything in one file.
We will add an entry for the linux box (the myusql box) change directory to: /usr/local/nagios/etc/objects

cd /usr/local/nagios/etc/objects

Step 8:

vi localhost.cfg

Add the following (host definition):

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 mysql_server
alias mysql_server
address 192.168.20.131
notification_interval 1
notification_options d,u,r,f,s
check_interval 1
retry_interval 1
contact_groups admins
notifications_enabled 1
notification_period 24x7
}

These parameters are very important for our notification/alerts:

notification_interval 1
notification_options d,u,r,f,s
check_interval 1
retry_interval 1
contact_groups admins
notifications_enabled 1
notification_period 24x7

For more info about the parameters, read the doc: https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/3/en/objectdefinitions.html

Step 9:

We can monitor a lot of things with Nagios, but we will only monitor the current users, the root partition and ping

define service {
use local-service ; Name of service template to use
host_name localhost, mysql_server
service_description Current Users
check_command check_local_users!20!50
}
define service {
use local-service ; Name of service template to use
host_name localhost, mysql_server
service_description Root Partition
check_command check_local_disk!20%!10%!/
}
define service {
use local-service ; Name of service template to use
host_name localhost, mysql_server
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}

one entry here is the hostname.
For the MYSQL box, we defined as: mysql_server

Step 10:

a number of things can be monitored, like table-lock-contention, threads-connected, slow-queries, long-running-procs, etc. But we will keep it simple and monitor the number of MYSQL connections and MYSQL uptime.

read more on check_mysql_health:
https://labs.consol.de/nagios/check_mysql_health/index.html

Add the following (service definition):

define service { 
host_name mysql_server
service_description MySQL Connections
check_command check_mysql_health!192.168.20.131!3306!nagios!Pa_s1234!threads-connected
notification_interval 1
notification_options w,u,c,r,f,s
check_interval 1
retry_interval 1
contact_groups admins
notifications_enabled 1
notification_period 24x7
max_check_attempts 3
check_period 24x7
}
define service {
host_name mysql_server
service_description MySQL Uptime
check_command check_mysql_health!192.168.20.131!3306!nagios!Pa_s1234!uptime
check_interval 1
retry_interval 1
contact_groups admins
notifications_enabled 1
notification_period 24x7
notification_options w,u,c,r,f,s
notification_interval 1
max_check_attempts 3
check_period 24x7
}

Step 11:

Now, restart the Nagios service (on the Nagios box).

systemctl restart nagios.service

After some few minutes, you should see the status of the linux box and the mysql on Nagios.

login to the nagios web interface by using the ip address of the nagios server followed by /nagios, mine is: http://192.168.20.131/nagios

Read: Part 4 - Nagios notification (email alert) configuration.

--

--