Set monit to monitor mariadb on a RunCloud instance on Ubuntu 22.04

Get up and running with Monit on Ubuntu 22.04

Install Monit

SSH as root into your VM instance and install Monit

apt update
apt install monit

Start/stop/restart and see the status of monit

systemctl status monit
systemctl start monit
systemctl stop monit
systemctl restart monit

Status will tell if it’s running

root@mel1:~# systemctl status monit
● monit.service - LSB: service and resource monitoring daemon
Loaded: loaded (/etc/init.d/monit; generated)
Active: active (running) since Sat 2023-06-17 16:23:58 AEST; 42s ago

Get the monit version

monit --version

View and tail the monit log

tail -f /var/log/monit.log

Configure Monit

Back up the original monit config file

cp /etc/monit/monitrc /etc/monit/monitrc.bak

Web Access

In the config file, look for httpd port and uncomment the lines and set your password.

set httpd port 2812 and
use address 0.0.0.0 # only accept connection from localhost (drop if you use M/Monit)
allow 0.0.0.0/0 # allow localhost to connect to the server and
allow admin:password # require user 'admin' with password 'password'

Restart monit:

systemctl restart monit

Now you can view via a browser, use your server IP and 2812 port number – use http protocol or configure https for use.

You can now also view the monit stats via the command line with:

monit status

Email Alerts

Email alerts for Monit can also be configured in the monitrc file.

I am using postfix for sending server emails, search for ‘set alert‘ and ‘set mailserver‘ and uncomment and set your email config, there are more email options that can be set in the config file such as sending email for a certain monitoring service.

set alert [email protected]            # receive all alerts
set mailserver 127.0.0.1,               # primary mailserver
               localhost                # fallback relay

Check monit config syntax

Any changes you do to the monit config file can be syntax checked with:

monit -t

Add MariaDB service to be watched by monit

That’s monit up and running, now lets add a monitoring process on mariadb database which will restart it if it is terminated due to lack of resources.

Change the content of the mysql config file:

/etc/monit/conf-available/mysql

To the following which basically changes the content from mysql to mariadb

 check process mysqld with pidfile /var/run/mysqld/mysqld.pid
   group database
   group mysql
   start program = "/etc/init.d/mariadb start"
   stop  program = "/etc/init.d/mariadb stop"
   if failed host localhost port 3306 protocol mysql with timeout 15 seconds for 3 times within 4 cycles then restart
   if failed unixsocket /var/run/mysqld/mysqld.sock protocol mysql for 3 times within 4 cycles then restart
   if 5 restarts with 5 cycles then timeout
   depend mysql_bin
   depend mysql_rc

 check file mysql_bin with path /usr/sbin/mysqld
   group mysql
   include /etc/monit/templates/rootbin

 check file mysql_rc with path /etc/init.d/mariadb
   group mysql
   include /etc/monit/templates/rootbin

Make a soft link to the mysql config from the config available directory to the config enabled directory.

ln -s /etc/monit/conf-available/mysql  /etc/monit/conf-enabled/

Restart monit

systemctl restart monit

That’s it, test the monitoring by intentionally stopping MariaDB and waiting for 2 minutes’ish

systemctl stop mariadb

Fix MySQL logging

If you have your MySQL error logging enabled you will get a warning output about unauthenticated users which is monit checking the MariaDB service.

Warning] Aborted connection to db: 'unconnected' user: 'unauthenticated' host: 'localhost' (This connection closed normally without authentication)

Fix this by adding a variable in:

/etc/mysql/mariadb.conf.d/50-server.cnf

Add in the logging section:

log_warnings = 1

Restart MariaDB

systemctl restart mariadb

Leave all Comment