Monit is a helpful program that automatically monitors and manages server programs to ensure that they not only stay online consistently, but that the file size, checksum, or permissions are always correct. Additionally monit comes with a basic web interface through which all of the processes can be set up. This tutorial will cover the most basic setup and configuration.
Monit is easiest to install through apt-get:
sudo apt-get install monit
Once monit downloads, you can add programs and processes to the configuration file:
sudo nano /etc/monit/monitrc
Monit can be started up with a command that then keeps it running in the background
monit
Typing monit status
displays monit’s details:
The Monit daemon 5.3.2 uptime: 1h 25m System 'myhost.mydomain.tld' status Running monitoring status Monitored load average [0.03] [0.14] [0.20] cpu 3.5%us 5.9%sy 0.0%wa memory usage 26100 kB [10.4%] swap usage 0 kB [0.0%] data collected Thu, 30 Aug 2012 18:35:00
Monit is very easy to use nearly out of the box. By default, it is set up to check that services are running every 2 minutes and stores its log file in “/var/log/monit.log”.
These settings can be altered at the beginning of the configuration file in the set daemon
and set logfile
lines respectively.
Monit comes with it’s own web server running on port 2812. To configure the web interface, find and uncomment the section that begins with set httpd port 2812
. Once the section is uncommented, write in your server’s IP or domain name as the address, allow anyone to connect, and then create a monit user and password
set httpd port 2812 use address 12.34.56.789 # only accept connection from localhost allow 0.0.0.0/0.0.0.0 # allow localhost to connect to the server and allow admin:monit # require user 'admin' with password 'monit'
Once this is configured, monit should reload and reread the configuration file, and the web interface will be available:
monit reload
You will then be able to access the monit web interface by going to “example.com:2812”
Login with your chosen username and password. Your screen should look something like this.
Once the web services are set up, you can begin to input the programs that you want monitored and protected into the "/etc/monit/monitrc" configuration file. To simply ensure that programs stay online, you can use the /etc/init.d commands to stop or start a program.
Here are some example configurations:
Apache:
check process apache with pidfile /run/apache2.pid start program = "/etc/init.d/apache2 start" with timeout 60 seconds stop program = "/etc/init.d/apache2 stop"
MySQL
check process mysqld with pidfile /var/run/mysqld/mysqld.pid start program = "/etc/init.d/mysql start" stop program = "/etc/init.d/mysql stop"
Nginx
check process nginx with pidfile /var/run/nginx.pid start program = "/etc/init.d/nginx start" stop program = "/etc/init.d/nginx stop"
Once you have configured all of the programs that you want to run, they will be automatically tracked and restarted should they turn off.
You can control the programs through both the web interface or the command line.
Once you have set up the configuration, check the syntax:
monit -t
After resolving any possible syntax errors, you can start running all of the monitored programs.
monit start all
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
I’ve following this article but I’m getting an “Execution failed” status for apache. It doesn’t look like the PID file exists in: /usr/local/apache/logs/httpd.pid
(Ubuntu 12.04)
There was a typo in the configuration. I have corrected it, please let me know if you continue having issues.
Cheers, that’s done it!
Hi.
I installed monit as above on 12.04 & it’s running but I get this (names changed to protect the innocent):
`$ ps aux | grep monit root 1193 0.0 0.0 5492 832 ? S Sep12 1:27 /usr/bin/monit -c /etc/monit/monitrc fubar 12127 0.0 0.0 4372 812 pts/1 S+ 02:05 0:00 grep --color=auto monit
$ sudo monit status [sudo] password for fubar: monit: error connecting to the monit daemon`
I’ve checked
/etc/monit/monitrc
& it looks good enough to get things going & as seen above I think it’s running…Any help appreciated
sorry to answer my own question but in order to get rid of that error message & see monit’s status, I had to enable the httpd section in
/et/monit/monitrc
even though I wasn’t using the web interface (apparently you have to set the monit httpd process to allow connections from localhost).Hope this helps someone else late at night while searching Google (it’s 02:42 in London ;-)
I have followed this tutorial and I keep getting this message:
monit: activation failed – There is no service by that name monit: activation failed – There is no service by that name
I copied and pasted the congfig for apache and mysql and tried again and still got the same messages.
do you have any ideas about what I have done wrong.
Excellent article, got a test forum running on Digital Ocean in around 1h and used this command to keep thin running after the terminal window closes which I found useful:
thin start -d
macrus.hann6: after your make changes to the config file, don’t forget to run $ monit reload. Also make sure the actual pid files exist at that location.
When I login as a user and run Monit I get “monit: The control file ‘/etc/monit/monitrc’ must be owned by you.”
The file is owned by root, and when I login as root and run monit nothing happens.
So then I change the file to be owned by the user and then get “monit: Error opening the log file ‘/var/log/monit.log’ for writing – Permission denied”
After changing the owners on a lot of files eventually I don’t get any errors, and I also don’t see Monit.
Running monit status gives me “monit: error connecting to the monit daemon”
I am puzzled.
I’m experiencing some troubles with firewall configuration for the web access. i’ve set firewall as described here: https://www.digitalocean.com/community/articles/how-to-set-up-a-firewall-using-ip-tables-on-ubuntu-12-04
if i try to add sudo iptables -A INPUT -p tcp --dport 2812 -j ACCEPT (output too)
i’m unable to access. if i flush the firewall and remove all rules i’m able to access.
any idea? what i’m doing wrong?