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
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
monit -t
systemctl enable --now monit
curl -v http://admin:[email protected]:2812
https://www.webfoobar.com/node/49
https://cloudraya.com/knowledge-base/simple-monitoring-and-alerting-with-monit-on-ubuntu-22-04-lts/
https://gist.github.com/jcdarwin/fa9235dd48276f348cc7
https://mmonit.com/monit/documentation/monit.html
https://serverfault.com/questions/528509/how-to-set-up-monit-to-monitor-disk-space