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.
apt-get install monit
Monit Config File
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
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.
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
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] memory usage 445.4 MB swap usage 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 check process nginx with pidfile "/var/run/nginx.pid" if cpu > 80 for 2 cycles then alert
#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
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
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 memory usage 8276900 kB swap usage 1192 kB 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.
You can use commercial version M/Monit for a lot many features or free script 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
Open config.php and adjust following values:
"url" => "localhost:2812", "url_ssl" => false, "http_username" => "youruser", "http_password" => "yourpassword",
Add following line to cron:
* * * * * php /var/www/example.com/htdocs/mg/cron.php >> /var/log/monit-graph.log
You can open http://example.com/mg in browser to see visual graphs!