What are we doing?
Installing Monit on Ubuntu
Monit is a lightweight program that can monitor processes, restart them if they fail and send email alerts if there are problems.
Update repositories and install Monit
sudo apt-get update
sudo apt-get install monit
Enable the built in webserver and add a process to monitor
sudo nano /etc/monit/monitrc
Find the following code block
# set httpd port 2812 and # use address localhost # only accept connection from localhost # allow localhost # allow localhost to connect to the server and # allow admin:monit # require user 'admin' with password 'monit' # allow @monit # allow users of group 'monit' to connect (rw) # allow @users readonly # allow users of group 'users' to connect readonly #
Change it to the following
set httpd port 2812 and use address IP_ADDRESS_OF_SERVER # only accept connection from localhost allow IP_OR_SUBNET_YOU_CONNECT_FROM # allow localhost to connect to the server and allow admin:monit # require user 'admin' with password 'monit' # allow @monit # allow users of group 'monit' to connect (rw) # allow @users readonly # allow users of group 'users' to connect readonly #
Add a service to monitor
In this case I will demonstrate how to configure and verify monitoring of Nginx but the principle is the same for most processes
In the same file, add the following block of code
check process nginx with pidfile /var/run/nginx.pid start program = "/etc/init.d/nginx start" stop program = "/etc/init.d/nginx stop" group www-data
ctrl + x then y to save and exit nano
Monit can be reloaded, if required, with the following
Tail the Monit log file
tail -f /var/log/monit.log
Now visit YOUR_SERVER_IP_ADDRESS:2812 in a web browser, log in with the credentials admin – monit and restart Nginx. If successful, the log tail will show something similar to the following
[UTC Dec 26 00:56:26] info : 'nginx' stop: /etc/init.d/nginx [UTC Dec 26 00:56:27] info : 'nginx' start: /etc/init.d/nginx [UTC Dec 26 00:56:28] info : 'nginx' restart action done
Monit can watch just about any process. Head over to the monit website for more examples.