Monit is a comparitively lightweight server monitor. I'd say its biggest sellig point is th ease ans sped of setup compared to the more enterprise-grade monnitoring services like Nagios, Icinga, Munnin, Zabbix, etc. Those are likely overkill unless in an enterprise situation.
Although I say comparitively lightweight you'll find as you dig into Monit it has a great depth of features. I came to use it recently because a problem I've recently run into with PHP-FPM dying for no apparent reason leaving me with a bad gateway timeout for nginx.
Monit to the rescue which will detect the dead service, restart it, log it and continue monitoring.
This assumes you're running Ubuntu/Debian in all the following but you should be able to adapt it for your own distro.
sudo apt-get update
After Monit is installed go ahead and use your fave editor (Vim obviously) and edit monits config file:
# Basic Setup
sudo vim /etc/monit/monitrc
There's a few key things you'll want to change in here and a few that are optional but the config file is great with loads of explanation on each variabe you can change.
# Service Monitoring
Service monitoring is just one of the many core facets of Monit but its the one I'm focussing on in this blog. Even within basic service monitoring there's a lot extra options you can use but I'm keeping it simple for now. If you want to go into more detail such as file checking, space, network monitoring, etc check out the full manual at monit HQ.
Heres a very basic example for checking 3 services on yoru box:
Afer doing any changes to the monitrc file you must run:
sudo monit reload
# Web Interface
If receiving notifications and allowing Mnit to do its thing isn't enough then you can satisfy your inner voyeur by setting up the Monit httpd service. In the monitc file find the commented out section below:
The comments make it pretty self explanatory but to give you an idea I've included a simple example here:
set httpd port 2500 and # The port to allow access from
With those settings you can visit
http://126.96.36.199:2500 and after putting in the username and password above you will be presented with something like this:
# Going Further
This really only scractes the surface of what Monit can do as you may have noticed with the huge number of examples and sub-rules to checks in the monitrc file. I'd strongly recommend reading through the config file and checking out the manual too for some of Monits more powerful features.
# Problem 1 - Missing PID files
If you find your MySQL server doesn't have a pid file (one of my servers didn't) then its as simple as adding it to your config file (in MySQL's case: /etc/mysql/my.cnf) like so:
pid-file = /var/run/mysqld/mysqld.pid
Then set the location of that pid file in your server monitoring configs as above.
# Problem 2 - Alert emails not working
This is pretty common especially if you're using the localhost. Its most likely they are going to spam or your server is simply picking them up due to a misconfigured Postfix install. Check your spam folder and try a different alert email on a different domain to narrow down the issue.