Nov 1, 2018

How To Install and Configure Monit

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. 

Install Monit

sudo apt-get install monit 
yum -y install monit (You need to enable EPEL)
Configuration file:
sudo vi /etc/monit/monitrc
sudo vi /etc/monit.conf
Configure Monit

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”.

Web Service

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 and
use address localhost
allow localhost
Once this is configured, monit should reload and reread the configuration file, and the web interface will be available:
monit reload

Typing monit status displays monit’s details:
# monit status
The Monit daemon 5.4 uptime: 6m

Process 'mysqld'
  status                            Does not exist
  monitoring status                 Monitored
  data collected                    Thu, 31 Oct 2013 03:54:17

System 'system_wp'
  status                            Running
  monitoring status                 Monitored
  load average                      [0.02] [0.04] [0.05]
  cpu                               3.2%us 0.7%sy 0.1%wa
  memory usage                      217700 kB [43.2%]
  swap usage                        0 kB [0.0%]
  data collected                    Thu, 31 Oct 2013 03:54:17
Configuring Programs

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/
    start program = "/etc/init.d/apache2 start" with timeout 60 seconds
    stop program  = "/etc/init.d/apache2 stop"
check process mysqld with pidfile /var/run/mysqld/
    start program = "/etc/init.d/mysql start"
    stop program = "/etc/init.d/mysql 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, 

Monit can be started up with a command that then keeps it running in the background

you can start running all of the monitored programs.
monit start all

See also:

How to alert disk space using Monit