Ah, monit, my favorite tool for monitoring services. Monit is checking in a period of time (default is every 120 seconds, I am setting it every 30 seconds) your services and if something is down, monit will start it automatically. It also provides a cool web interface to quickly check what is up and when it started (so you can check if something died and restarted by monit). In this article I will show some simple and basic configuration files, some monit configurations and how I am using it. You can do much more advanced things, like sending email when some service die, kill some service if there is much CPU/RAM usage etc. I'll touch the surface and if you want more, go and read the official documentation.
The way monit is working is that it checks a txt file which contains the pid of your service. For example nginx will store it's pid under
/var/run/nginx.pid on ubuntu so you have to define it. If your service doesn't create a pid file, you can create it by your own, check the documentation and the FAQ of monit of how to do it.
Ok first, you need to install monit. On ubuntu you can just type
apt-get install monit and everything will be install. The folder that we are interested in, is on
/etc/monit, in this folder we have the monitrc file which is the basic configuration file and the conf.d folder has the custom configuration for each service.
Here is a basic monitrc file: It's pretty straightforward, it enables to 2812 port the web interface of monit with http configuration (change username and password). If you don't want to let 2812 port open, you can do a ssh reverse port locally with the following command:
ssh -Nf -L 2812:localhost:2812 your_username@your_domain.com, then you can access the web interface of monit through your local browser by going to
http://localhost:2812 . Of course if you have static ip or specific ips you can do more tricks with monitrc, but the above are working fine with me.
Here are some additional configuration files for ubuntu 14.04 with default installations. Before using them, be sure they are working and the pids are in the folder of the config. After that, just create a file inside conf.d folder like redis.conf and add the following:Redis Nginx ssh unicorn (create a config for unicorn specific of your project) sidekiq (create a config and yml for sidekiq specific of your project) I may post some sidekiq and unicorn default config files and guides in the future.