monit config for docker containers

Home

This monit and scripts were used in Ubuntu 20.04

/etc/monit/monitrc



set mailserver "email-smtp.us-east-1.amazonaws.com" port 587
    username "XX"
    password "XXY"
    using tlsv12



set mail-format {
  from:    DEV Monit <admin@example.com>
  subject: monit alert --  $EVENT $SERVICE
  message: $EVENT Service $SERVICE
                Date:        $DATE
                Action:      $ACTION
                Host:        $HOST
                Description: $DESCRIPTION

           Your faithful employee,
           Monit
}


set alert admin1@example.com 
set alert admin2@example.com


 set httpd port 2812 and
     use address localhost  # only accept connection from localhost (drop if you use M/Monit)
     allow localhost        # allow localhost to connect to the server and
     allow admin:pass123   # require user 'admin' with password 'pass123'


  check system $HOST
    if loadavg (1min) per core > 2 for 5 cycles then alert
    if loadavg (5min) per core > 1.5 for 10 cycles then alert
    if cpu usage > 95% for 10 cycles then alert
    if memory usage > 75% then alert
    if swap usage > 40% then alert


   include /etc/monit/conf.d/*
   include /etc/monit/conf-enabled/*


/etc/monit/conf.d/magento



#  Docker Compose associates containers with the specific
#  docker-compose.yml file and its location. If you run docker-compose
#  from a different directory (even with the same file content), Docker
#  might not recognize the existing containers because it considers
#  them associated with the docker-compose.yml file from the original
#  directory.

# Monitor Magento

CHECK PROGRAM test_magento WITH PATH /etc/monit/scripts/check_magento.sh
    start program = "/usr/bin/docker-compose -f  /home/ubuntu/HCF/scripts/docker_scripts/bitnami_magento243/docker-compose.yml start"
    stop program = "/usr/bin/docker-compose -f  /home/ubuntu/HCF/scripts/docker_scripts/bitnami_magento243/docker-compose.yml stop"
  IF status != 0 FOR 2 CYCLES THEN RESTART
  IF 4 RESTARTS WITHIN 10 CYCLES THEN UNMONITOR



# bitnami_magento243_mariadb_1

CHECK PROGRAM test_mysql WITH PATH /etc/monit/scripts/check_mysql.sh
    start program = "/usr/bin/docker-compose -f /home/ubuntu/HCF/scripts/docker_scripts/bitnami_magento243/docker-compose.yml start"
    ## stop program = "/usr/bin/docker-compose -f /home/ubuntu/HCF/scripts/docker_scripts/bitnami_magento243/docker-compose.yml stop"
  IF status != 0 FOR 2 CYCLES THEN RESTART
  IF 4 RESTARTS WITHIN 10 CYCLES THEN UNMONITOR


# bitnami_magento243_elasticsearch_1

# On elasticsearch monit config, commented out the stop program which is optional.  We don't want to stop magento and mysql too, and then restart everything.
# For example, to avoid manual restart of jupyter lab inside the magento instance.

CHECK PROGRAM test_elasticsearch WITH PATH /etc/monit/scripts/check_elasticsearch.sh
    start program = "/usr/bin/docker-compose -f /home/ubuntu/HCF/scripts/docker_scripts/bitnami_magento243/docker-compose.yml start"
    ##  stop program = "/usr/bin/docker-compose -f /home/ubuntu/HCF/scripts/docker_scripts/bitnami_magento243/docker-compose.yml stop"
  IF status != 0 FOR 2 CYCLES THEN RESTART  
  IF 4 RESTARTS WITHIN 10 CYCLES THEN UNMONITOR  


/etc/monit/scripts/checkeliasticsearch.sh

#!/bin/bash

container_name="bitnami_magento243_elasticsearch_1"

# Check if the container is running
if [ "$(docker container inspect -f '{{.State.Status}}' $container_name)" = "running" ]; then
    exit 0
else
    # Container not running, print an error and exit with code 1
    echo "Error: $container_name not running." >&2
    exit 1
fi


/etc/monit/scripts/checkmagento.sh

#!/bin/bash

container_name="bitnami_magento243_magento_1"

# Check if the container is running
if [ "$(docker container inspect -f '{{.State.Status}}' $container_name)" = "running" ]; then
    exit 0
else
    # Container not running, print an error and exit with code 1
    echo "Error: $container_name not running." >&2
    exit 1
fi

/etc/monit/scripts/checkmysql.sh

#!/bin/bash

container_name="bitnami_magento243_mariadb_1"

# Check if the container is running
if [ "$(docker container inspect -f '{{.State.Status}}' $container_name)" = "running" ]; then
    exit 0
else
    # Container not running, print an error and exit with code 1
    echo "Error: $container_name not running." >&2
    exit 1
fi


Author: Sebastian Emilio Narvaez

Created: 2024-09-26 jue 19:41

Validate