Skip to content

Instantly share code, notes, and snippets.

@budiantoip
Last active December 13, 2024 00:13
Show Gist options
  • Save budiantoip/1adb1e810c2f6af4b24f86e9475287e6 to your computer and use it in GitHub Desktop.
Save budiantoip/1adb1e810c2f6af4b24f86e9475287e6 to your computer and use it in GitHub Desktop.
Monit

How to set up monit on domain.com

vim /etc/monit/monitrc

set daemon  30

set mailserver smtp.gmail.com port 587              # primary mailserver
username "[email protected]" password "my_password"
using tls
with timeout 30 seconds

set alert [email protected]                       # receive all alerts

set httpd port 2812 and
use address 0.0.0.0  # only accept connection from localhost (drop if you use M/Monit)
allow localhost        # allow localhost to connect to the server and
allow 0.0.0.0/0.0.0.0 # allow all IPv4
allow 5.6.7.8   # allow dev's IP to connect to the server
allow admin:myPassword

vim /etc/monit/conf.d/system

## System
check system $HOST
    if memory usage > 80% for 4 cycles then alert
    if swap usage > 20% for 4 cycles then alert
    # Test the user part of CPU usage 
    if cpu usage (user) > 80% for 2 cycles then alert
    # Test the system part of CPU usage 
    if cpu usage (system) > 20% for 2 cycles then alert
    # Test the i/o wait part of CPU usage 
    if cpu usage (wait) > 80% for 2 cycles then alert
    # Test CPU usage including user, system, and wait. Note that 
    # multi-core systems can generate 100% per core
    # so total CPU usage can be more than 100%
    if cpu usage > 100% for 4 cycles then alert

    # Monitor disk usage and alert if it exceeds 80%
check filesystem rootfs with path /
    if space usage > 80% then alert

To send memory and process stats

First, create a bash script to generate the stats:

vim /root/monit_alert.sh

Then put this script:

#!/bin/bash

# Save memory usage
free -h > /tmp/monit_memory_status

# Save top 10 CPU-intensive processes
echo -e "\nTop 10 CPU-intensive processes:" >> /tmp/monit_memory_status
ps aux --sort=-%cpu | head -n 11 >> /tmp/monit_memory_status

# Save top 10 memory-intensive processes
echo -e "\nTop 10 Memory-intensive processes:" >> /tmp/monit_memory_status
ps aux --sort=-%mem | head -n 11 >> /tmp/monit_memory_status

# Output the file
cat /tmp/monit_memory_status

Make it executable:

chmod u+x /root/monit_alert.sh

Modify vim /etc/monit/conf.d/system:

check system $HOST
    # Lines before
    if memory usage > 80% for 4 cycles then exec "/root/monit_alert.sh"
    # Lines after

Validate monit configuration

monit -t
systemctl enable --now monit

curl -v http://admin:[email protected]:2812

References

How to install monit

https://www.webfoobar.com/node/49

Simple monitoring and alerting with Monit on Ubuntu 22.04 LTS

https://cloudraya.com/knowledge-base/simple-monitoring-and-alerting-with-monit-on-ubuntu-22-04-lts/

How to allow monit to use gmail as a smtp relay to send out alert emails

https://gist.github.com/jcdarwin/fa9235dd48276f348cc7

Monit official documentation

https://mmonit.com/monit/documentation/monit.html

Use monit to monitor disk space

https://serverfault.com/questions/528509/how-to-set-up-monit-to-monitor-disk-space

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment