Raja's Exocortex

Monit

Monit is an open source system monitoring and error recovery utility.

Filename: 00-local

# File: /etc/monit/conf.d/00-local
# Monit base config overrides
set httpd port 2812
  allow admin:monit

set daemon 30    # check services at 30 second interface

Filename: system

# Monit config to monitor CPU, RAM and disk usage
# File: /etc/monit/conf.d/system
# Note: modify the file system alert values based on requirement

check system $HOST
    if loadavg (5min)  > 4 for 4 cycles
        then exec /usr/local/sbin/monit-alerts.sh
        as uid "nobody" and gid "nogroup"
    else if succeeded
    then exec
        /usr/local/sbin/monit-alerts.sh
        as uid "nobody" and gid "nogroup"

    if loadavg (15min) > 4 for 4 cycles
        then exec /usr/local/sbin/monit-alerts.sh as uid "nobody" and gid "nogroup"
    else if succeeded
    then exec
        /usr/local/sbin/monit-alerts.sh
        as uid "nobody" and gid "nogroup"

    if memory usage > 80% for 4 cycles
        then exec /usr/local/sbin/monit-alerts.sh as uid "nobody" and gid "nogroup"
    else if succeeded
    then exec
        /usr/local/sbin/monit-alerts.sh
        as uid "nobody" and gid "nogroup"

# Alert if total number of processes exceeds 500, prevents zombies
check program process_count_500 with path "/bin/sh -c 'count=$(ps -e --no-headers | wc -l); [ $count -gt 500 ] && exit 1 || exit 0'"
    if status != 0
       then exec /usr/local/sbin/monit-alerts.sh as uid "nobody" and gid "nogroup"
    else if succeeded
       then exec /usr/local/sbin/monit-alerts.sh as uid "nobody" and gid "nogroup"

check filesystem "root" with path /
    if space usage > 10 GB for 8 cycles
        then exec /usr/local/sbin/monit-alerts.sh as uid "nobody" and gid "nogroup"
    else if succeeded
    then exec
        /usr/local/sbin/monit-alerts.sh
        as uid "nobody" and gid "nogroup"

check filesystem "app" with path /app
    if space usage > 5 GB for 8 cycles
       then exec /usr/local/sbin/monit-alerts.sh as uid "nobody" and gid "nogroup"
    else if succeeded
    then exec
        /usr/local/sbin/monit-alerts.sh
        as uid "nobody" and gid "nogroup"

Filename: laravel

# Filename: /etc/monit/conf.d/laravel
# Monit config fragment to health check a laravel app and
# send alerts to Google Spaces
check host app.example.com with address web1
    if failed
        port 443 protocol https
        with http headers [Host: app.example.com]
        and request /up with content == "Application up"
        for 3 cycles
    then exec
    /usr/local/sbin/monit-alerts.sh
        as uid "nobody" and gid "nogroup"
    else if succeeded
    then exec
        /usr/local/sbin/monit-alerts.sh
        as uid "nobody" and gid "nogroup"

Filename: monit-alerts.sh

#!/bin/bash
# File: /usr/local/sbin/monit-alerts.sh
# Send Notification Alerts to Google-Space

# set -x

# Google-Space URL
BASE_URL="https://chat.googleapis.com/v1/spaces/..."

curl -X POST ${BASE_URL} \
    -H 'Content-Type: application/json' \
    -d "{ \"text\": \"MONIT_HOST: ${MONIT_HOST}\nMONIT_SERVICE: ${MONIT_SERVICE}\nMONIT_EVENT: ${MONIT_EVENT}\nMONIT_DESCRIPTION: ${MONIT_DESCRIPTION}\nMONIT_DATE: ${MONIT_DATE}\" }"