Skip to content

HOWTO devops monit

steveoro edited this page Apr 14, 2021 · 2 revisions

HOWTO: install & configure Monit on Ubuntu

Install & configure Monit for checking MongoDB/web server status.

  • see: D.O. tutorial on Monit
  • see: Monit documentation
  • the Monit embedded http server is needed especially if you want to issue monit commands from the command line (it uses HTTP to talk to the demon): for instance, when lanunching: $ monit status or $ monit summary

Install:

Install vim, if you haven't, in order to edit config files (otherwise, use the nano editor):

$> sudo apt-get install vim

Install sendmail or postfix to send out email notifies on process events:

$> sudo apt-get install sendmail

Install monit:

$> sudo apt-get install monit

Add monit to the startup scripts:

$> sudo update-rc.d monit defaults

Edit config file:

To fine tune the configuration file and in order to setup any additional services that are in need of monitoring (any other web server, additional DB server, whatever...), use:

$> sudo vi /etc/monit/monitrc

See below for some configuration examples.


Commands:

Some misc command examples.

Launch manually:

$> monit

Check status:

$> monit status

Relaunch service:

$>  monit reload

Quit service:

$>  monit quit

Setup for resource monitoring

Configuration setup for resource monitoring:

----8<---- [> vi /etc/monit/monitrc]

# [...]
check system localhost
  group system
  if memory usage > 85% then alert
  if cpu usage (user) > 95% then alert
  if cpu usage (system) > 50% then alert
# [...]

----8<----

Setup for Apache2 monitoring

Configuration setup for Apache2 monitoring:

----8<---- [> vi /etc/monit/monitrc]

# [...]
check process apache2 with pidfile /run/apache2/apache2.pid
  group server
  start program = "/etc/init.d/apache2 restart" with timeout 30 seconds
  stop program  = "/etc/init.d/apache2 stop"
# [...]

----8<----

Setup for NGinx monitoring

Configuration setup for NGinx monitoring:

----8<---- [> vi /etc/monit/monitrc]

    # [...]
    check process nginx with pidfile /var/run/nginx.pid
        start program = "/etc/init.d/nginx start"
        stop program = "/etc/init.d/nginx stop"
    # [...]

----8<----

Testing configuration file syntax

Useful after each modify to the config file:

$> monit -t

Start all monitored programs:

$> monit start all

Monit config file example

Taken directly from Goggles, legacy vers. 5:

----8<---- [> vi /etc/monit/monitrc]

# [...]
  set daemon 180            # check services at 2-minute intervals
    with start delay 120    # optional: delay the first check by 2-minutes
# [...]
  set logfile /var/log/monit.log
# [...]
  set mailserver localhost
# [...]
  set eventqueue
      basedir /var/lib/monit/events # set the base directory where events will be stored
      slots 100                     # optionally limit the queue size
# [...]
  set mail-format { from: monit@<SITE_NAME.DOMAIN> }
# [...]
  set alert webmaster@<SITE_NAME.DOMAIN> not on { instance, action }

# Local httpd server for status reporting:
 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'

# [...]
  check system localhost
    group system
    if memory usage > 85% then alert
    if cpu usage (user) > 95% then alert
    if cpu usage (system) > 50% then alert

# [...]

# [Steve, 20150521] Apache 2:
  check process apache2 with pidfile /run/apache2/apache2.pid
    group server
    start program = "/etc/init.d/apache2 restart" with timeout 30 seconds
    stop program  = "/etc/init.d/apache2 stop"

# [...]

# [Steve, 20150521] MySql:
  check process mysql with pidfile /run/mysqld/mysqld.pid
    group database
    start program = "/etc/init.d/mysql restart"
    stop program = "/etc/init.d/mysql stop"

# [...]

----8<----

  • On OpenSuse installations, AppArmor must not block MySql instances upon periodic check:see this
Clone this wiki locally