Monit alerts on Ubuntu + PHP7.2-FPM + NGINX + MYSQLD

 In Servers

monit-php7.2-fpm

Monit is an excellent tool to check your server’s services uptime, Monit is “Pro-active Monitoring” used on VPS or dedicated server. Monit will make sure php7.2-fpm and any server web services are always running in case it halts or freezes up.

Monit will check the php7.2-fpm, nginx and mysqld processes by looking at the PID they are listening on and if the PID can not be found or detected Monit will find the services and restart them.

Install
apt-based:
apt-get install monit

Config
Monit Config File
nano /etc/monit/monitrc

Use Monit to Monitor + Restart PHP7.2-FPM Service Automatically

Monit needs some information to check to see if php7.2-fpm is healthy.
We will use the socket php7-fpm is using and its pid file.

Find php7.2-fpm Unix or TCP Socket

php7-fpm can listen on either unix sockets or TCP sockets. To find out use this command
grep "listen =" /etc/php/7.2/fpm/pool.d/www.conf

If you see this output then php7.2-fpm is using unix sockets
listen = /run/php/php7.2-fpm.sock

If you see this output then php7.2-fpm is using TCP sockets, the loopback interface 127.0.0.1 on port 9090
listen = 127.0.0.1:9090

Find php7-fpm pid file

Monit needs the pid file as well
find /run -iname php*.pid

You should see some output like this
/run/php/php7.2-fpm.pid

Now we have the information necessary to configure Monit for php7-fpm monitoring.

Configure Monit to Monitor php7.2-fpm

Create a Monit php7-fpm configuration, you may use the conf-enabled folder instead of conf.d, check your /etc/monit/monitrc file to verify.
nano /etc/monit/conf.d/php7.2-fpm

This Monit php7.2-fpm configuration is for unix sockets.
It checks for the php7.2-fpm.pid file and if it doesn’t exist Monit will try to restart it.

Monit is also going to check that the php7.2-fpm unix socket exists: /var/run/php7.2-fpm.sock

check process php7.2-fpm with pidfile /run/php/php7.2-fpm.pid
start program = "/usr/sbin/service php7.2-fpm start" with timeout 60 seconds
stop program = "/usr/sbin/service php7.2-fpm stop"
if failed unixsocket /var/run/php7.2-fpm.sock then restart

If you are using TCP sockets then this will be your Monit configuration.

Monit looks for a daemon listening on 127.0.0.1 on port 9090, if not found Monit will restart php7-fpm after 3 CPU cycles.

Change 127.0.0.1 to match your interface and 9090 if it was different when you checked earlier.

check process php7.2-fpm with pidfile /run/php/php7.2-fpm.pid
start program = "/usr/sbin/service php7.2-fpm start" with timeout 60 seconds
stop program = "/usr/sbin/service php7.2-fpm stop"
if failed host 127.0.0.1 port 9090 type tcp for 3 cycles then restart

System Monitoring
#System Monitoring

Process 'mysqld'
status Running
monitoring status Monitored
pid 1619
parent pid 1
uid 112
effective uid 112
gid 116
uptime 1h 29m
threads 37
children 0
memory 148.0 MB
memory total 148.0 MB
memory percent 14.9%
memory percent total 14.9%
cpu percent 0.0%
cpu percent total 0.0%
data collected Tue, 20 Nov 2018 12:29:01

Process 'nginx'
status Running
monitoring status Monitored
pid 1570
parent pid 1
uid 0
effective uid 0
gid 0
uptime 1h 29m
threads 1
children 2
memory 10.4 MB
memory total 30.1 MB
memory percent 1.1%
memory percent total 3.0%
cpu percent 0.0%
cpu percent total 0.0%
data collected Tue, 20 Nov 2018 12:29:01

System 'nuno-sarmento.com'
status Running
monitoring status Monitored
load average [0.02] [0.10] [0.20] cpu 1.2%us 0.3%sy 0.1%wa
memory usage 445.4 MB [45.0%] swap usage 0 B [0.0%] data collected Tue, 20 Nov 2018 12:29:01

Process 'php7.2-fpm'
status Running
monitoring status Monitored
pid 1241
parent pid 1
uid 0
effective uid 0
gid 0
uptime 1h 29m
threads 1
children 0
memory 34.5 MB
memory total 34.5 MB
memory percent 3.5%
memory percent total 3.5%
cpu percent 0.0%
cpu percent total 0.0%
port response time 0.077 ms to [127.0.0.1]:9090 type TCP/IP protocol DEFAULT
data collected Tue, 20 Nov 2018 12:29:01

Nginx

#Nginx
check process nginx with pidfile "/var/run/nginx.pid"
if cpu > 80% for 2 cycles then alert

MySqld

#MySQL Monitoring
check process mysqld with pidfile "/var/run/mysqld/mysqld.pid"
if cpu > 80% for 2 cycles then alert

Check the Monit configuration has valid syntax
monit -t

You should see this message indicating the Monit syntax is OK.
Control file syntax OK

Then reload Monit to activate the configuration
sudo monit reload

Verify
Run command monit status, it should something like:

The Monit daemon 5.13 uptime: 2m
System 'example.com'
status Running
monitoring status Monitored
load average [0.48] [0.40] [0.81] cpu 0.0%us 0.0%sy 0.0%wa
memory usage 8276900 kB [25.1%] swap usage 1192 kB [0.0%] data collected Thu, 03 Apr 2014 10:41:45
#many more lines...

You may also open example.com:2182 in your browser to use web-based interface.

Monit-Graph
You can use commercial version M/Monit for a lot many features or free script monit-graph

Install monit-graph

cd /var/www/example.com/htdocs
git clone https://github.com/danschultzer/monit-graph mg
cd mg
chmod -R 0777 data
chmod 0644 data/index.php

Config File
Open config.php and adjust following values:

"url" => "localhost:2812",
"url_ssl" => false,
"http_username" => "youruser",
"http_password" => "yourpassword",

Setup Cron
Add following line to cron:

* * * * * php /var/www/example.com/htdocs/mg/cron.php >> /var/log/monit-graph.log

View Graphs
You can open http://example.com/mg in browser to see visual graphs!

Recent Posts

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Start typing and press Enter to search

Let's work together

I'd love to work with you! Please fill out my project form to tell me more about your project/work. I reply to all enquiries within 48 hours.

Availability

  • November - Booked
  • December - Booked
  • January - Available

Rates

  • My day rate is £300.
  • I can provide a fixed price for your work.

EN - Header Enquiry Form

  • This form collects your name, email address and telephone number so that I can respond to the enquiry(ies) that you submit via this website. Please check my privacy policy for the full story on how I use, protect and manage your submitted data.