Monit is a utility for managing and monitoring processes, files, directories, filesystems, and hosts on Unix systems. It can perform automatic maintenance and repair and can execute meaningful causal actions in error situations.
| File/Directory | Path | Purpose |
|---|---|---|
| Main config | /etc/monit/monitrc |
Primary configuration file |
| Conf.d directory | /etc/monit/conf.d/ |
Additional configuration files |
| Enabled configs | /etc/monit/monit.d/ |
Symlinked active configs |
| Available configs | /etc/monit/conf-available/ |
Available service configs |
| Log file | /var/log/monit.log |
Monit activity log |
| PID file | /var/run/monit.pid |
Monit process ID |
| State file | /var/lib/monit/state |
Monit state information |
| SSL certificates | /etc/monit/ssl/ |
TLS/SSL certificates |
| Systemd service | /etc/systemd/system/monit.service |
Service definition |
# /etc/monit/monitrc
###############################################################################
## Global Settings
###############################################################################
# Set the location of the monit log file
set log /var/log/monit.log
# Set the list of mail servers for alert delivery
set mailserver smtp.example.com port 587
username "alerts@example.com"
password "MailPassword123!"
using tls
with timeout 30 seconds
# Multiple mail servers for redundancy
set mailserver smtp-primary.example.com port 587
username "alerts@example.com"
password "MailPassword123!"
using tls
with timeout 30 seconds
backup
set mailserver smtp-backup.example.com port 587
username "alerts@example.com"
password "MailPassword123!"
using tls
with timeout 30 seconds
# Set the from address for alert emails
set mail-from "monit@example.com"
# Set the subject for alert emails
set mail-format {
from: monit@example.com
subject: "$SERVICE $EVENT at $DATE"
message: "Monit $ACTION $SERVICE at $DATE on $HOST"
}
# Set alert recipients
set alert admin@example.com
set alert ops-team@example.com only on { timeout, nonexist, resource }
set alert oncall@example.com only on { timeout, nonexist }
# Set HTTP daemon configuration for web interface
set httpd port 2812
address localhost
ssl enable
pemfile /etc/monit/ssl/monit.pem
signature "monit"
allow admin:password123
allow @localhost only
allow 192.168.1.0/24
# Set daemon poll interval (in seconds)
set daemon 60
# Set state file location
set statefile /var/lib/monit/state
# Set event queue configuration
set eventqueue
basedir /var/lib/monit/events
slots 100
filesystemstate
# Set process ID file
set pidfile /var/run/monit.pid
# Set syslog logging
set log syslog
# Set default timeout for service checks
set default timeout 300
###############################################################################
## Global Service Defaults
###############################################################################
# Default HTTP check settings
set httpd
timeout 5
retries 2
# Default process check settings
set process
timeout 300
start program = "/etc/init.d/%s start"
stop program = "/etc/init.d/%s stop"
###############################################################################
## System Resource Monitoring
###############################################################################
# Monitor system resources
check system $HOST
if loadavg (1min) > 4 then alert
if loadavg (5min) > 2 then alert
if loadavg (15min) > 1 then alert
if cpu usage > 95% for 5 cycles then alert
if memory usage > 90% for 5 cycles then alert
if swap usage > 50% for 5 cycles then alert
if uptime < 1 minute then alert
###############################################################################
## Filesystem Monitoring
###############################################################################
# Monitor root filesystem
check filesystem rootfs with path /
if space usage > 80% for 5 cycles then alert
if space usage > 90% for 5 cycles then exec "/usr/local/bin/cleanup.sh"
if space usage > 95% for 3 cycles then alert
if inode usage > 80% for 5 cycles then alert
if inode usage > 90% for 5 cycles then alert
# Monitor /var filesystem
check filesystem var with path /var
if space usage > 85% for 5 cycles then alert
if inode usage > 85% for 5 cycles then alert
# Monitor /home filesystem
check filesystem home with path /home
if space usage > 90% for 5 cycles then alert
###############################################################################
## Directory Monitoring
###############################################################################
# Monitor log directory size
check directory varlog with path /var/log
if size > 1 GB for 5 cycles then alert
if size > 2 GB for 3 cycles then exec "/usr/local/bin/rotate-logs.sh"
# Monitor temp directory
check directory tmp with path /tmp
if size > 500 MB for 5 cycles then alert
###############################################################################
## File Monitoring
###############################################################################
# Monitor important files for changes
check file sshd_config with path /etc/ssh/sshd_config
if failed checksum then alert
if failed permission then alert
if failed uid then alert
if failed gid then alert
# Monitor log files for errors
check file syslog with path /var/log/syslog
if match "error" then alert
if match "failed" then alert
if match "critical" then alert
###############################################################################
## Network Monitoring
###############################################################################
# Monitor network interface
check network eth0 with interface eth0
if failed link then alert
if download > 100 MB/s for 5 cycles then alert
if upload > 100 MB/s for 5 cycles then alert
if total traffic > 1 GB/s for 5 cycles then alert
# Monitor DNS resolution
check host dns with address 8.8.8.8
if failed ping type icmp with timeout 5 seconds for 3 cycles then alert
# Monitor external connectivity
check host google with address www.google.com
if failed ping type icmp with timeout 5 seconds for 3 cycles then alert
###############################################################################
## Include Additional Configurations
###############################################################################
# Include service-specific configurations
include /etc/monit/conf.d/*
include /etc/monit/monit.d/*
# /etc/monit/conf.d/nginx.conf
check process nginx with pidfile /var/run/nginx.pid
start program = "/etc/init.d/nginx start"
stop program = "/etc/init.d/nginx stop"
restart program = "/etc/init.d/nginx restart"
# Resource limits
if cpu > 60% for 5 cycles then alert
if cpu > 80% for 3 cycles then restart
if mem > 500 MB for 5 cycles then alert
if mem > 1 GB for 3 cycles then restart
# Process count
if children > 50 for 5 cycles then alert
if children > 100 for 3 cycles then restart
# File descriptors
if open files > 1000 for 5 cycles then alert
# Uptime monitoring
if uptime < 1 minute then alert
# Network connections
if failed host 127.0.0.1 port 80 protocol http
and request "/"
with timeout 5 seconds
for 3 cycles
then restart
if failed host 127.0.0.1 port 443 protocol https
and request "/"
with timeout 5 seconds
for 3 cycles
then restart
# Alert configuration
alert admin@example.com on { timeout, nonexist, resource }
alert ops@example.com on { start, stop, restart }
# Grouping
group webserver
# /etc/monit/conf.d/mysql.conf
check process mysql with pidfile /var/run/mysqld/mysqld.pid
start program = "/etc/init.d/mysql start"
stop program = "/etc/init.d/mysql stop"
restart program = "/etc/init.d/mysql restart"
# Resource limits
if cpu > 80% for 5 cycles then alert
if cpu > 95% for 3 cycles then restart
if mem > 2 GB for 5 cycles then alert
if mem > 4 GB for 3 cycles then restart
# Connection check
if failed host 127.0.0.1 port 3306 protocol mysql
with timeout 15 seconds
for 3 cycles
then restart
# Alert configuration
alert dba@example.com on { timeout, nonexist, resource }
group database
# /etc/monit/conf.d/php-fpm.conf
check process php-fpm with pidfile /var/run/php/php-fpm.pid
start program = "/etc/init.d/php-fpm start"
stop program = "/etc/init.d/php-fpm stop"
restart program = "/etc/init.d/php-fpm restart"
# Resource limits
if cpu > 70% for 5 cycles then alert
if mem > 1 GB for 5 cycles then alert
# Socket check
if failed unixsocket /var/run/php/php-fpm.sock then restart
# Process pool
if children > 50 for 5 cycles then alert
group webserver
# /etc/monit/conf.d/remote-host.conf
# Monitor remote web server
check host webserver01 with address 192.168.1.10
if failed port 80 protocol http
and request "/"
with timeout 5 seconds
for 3 cycles
then alert
if failed port 443 protocol https
and request "/"
with timeout 5 seconds
for 3 cycles
then alert
if failed ping type icmp with timeout 5 seconds for 3 cycles
then alert
alert ops@example.com on { timeout, nonexist }
# /etc/monit/conf.d/postfix.conf
check process postfix with pidfile /var/spool/postfix/pid/master.pid
start program = "/etc/init.d/postfix start"
stop program = "/etc/init.d/postfix stop"
restart program = "/etc/init.d/postfix restart"
if cpu > 50% for 5 cycles then alert
if mem > 500 MB for 5 cycles then alert
if failed host 127.0.0.1 port 25 protocol smtp
with timeout 10 seconds
for 3 cycles
then restart
# Queue monitoring
if failed unixsocket /var/spool/postfix/private/queue then alert
group mailserver
# /etc/monit/conf.d/redis.conf
check process redis with pidfile /var/run/redis/redis-server.pid
start program = "/etc/init.d/redis-server start"
stop program = "/etc/init.d/redis-server stop"
restart program = "/etc/init.d/redis-server restart"
if cpu > 50% for 5 cycles then alert
if mem > 2 GB for 5 cycles then alert
if failed host 127.0.0.1 port 6379 protocol tcp
with timeout 5 seconds
for 3 cycles
then restart
group database
# Email configuration in monitrc
set mailserver smtp.example.com port 587
username "monit@example.com"
password "SecurePassword123!"
using tls
with timeout 30 seconds
# Custom mail format
set mail-format {
from: monit@example.com
subject: "[$HOST] $SERVICE $EVENT at $DATE"
message: "Monit Alert
Service: $SERVICE
Host: $HOST
Event: $EVENT
Action: $ACTION
Date: $DATE
Description: $DESCRIPTION
--
Monit Monitoring System
https://mmonit.com/monit/"
}
# Alert recipients with event filtering
set alert admin@example.com # All events
set alert ops-team@example.com only on { timeout, nonexist, resource }
set alert oncall@example.com only on { timeout, nonexist }
set alert manager@example.com only on { nonexist }
#!/bin/bash
# /usr/local/bin/monit-slack-alert.sh
WEBHOOK_URL="https://hooks.slack.com/services/XXX/YYY/ZZZ"
CHANNEL="#alerts"
USERNAME="Monit"
ICON=":warning:"
# Parse Monit environment variables
HOST="$MONIT_HOST"
SERVICE="$MONIT_SERVICE"
EVENT="$MONIT_EVENT"
DATE="$MONIT_DATE"
DESCRIPTION="$MONIT_DESCRIPTION"
# Create payload
PAYLOAD=$(cat <<EOF
{
"channel": "$CHANNEL",
"username": "$USERNAME",
"icon_emoji": "$ICON",
"attachments": [
{
"color": "danger",
"title": "Monit Alert: $EVENT",
"fields": [
{"title": "Host", "value": "$HOST", "short": true},
{"title": "Service", "value": "$SERVICE", "short": true},
{"title": "Event", "value": "$EVENT", "short": true},
{"title": "Date", "value": "$DATE", "short": true},
{"title": "Description", "value": "$DESCRIPTION", "short": false}
]
}
]
}
EOF
)
# Send to Slack
curl -X POST -H 'Content-type: application/json' --data "$PAYLOAD" "$WEBHOOK_URL"
# Usage in monitrc
check process nginx
if failed port 80 then exec "/usr/local/bin/monit-slack-alert.sh"
# Test configuration syntax
sudo monit -t
# Check configuration with verbose output
sudo monit -t -v
# Validate specific config file
sudo monit -c /etc/monit/monitrc -t
# Check file permissions
sudo ls -la /etc/monit/monitrc
sudo ls -la /etc/monit/conf.d/
# Restart Monit daemon
sudo systemctl restart monit
# Reload configuration without restart
sudo monit reload
# Check service status
sudo systemctl status monit
# View logs
sudo journalctl -u monit -f
sudo tail -f /var/log/monit.log
# Run in foreground for debugging
sudo monit -Iv
# Monitor all services
sudo monit monitor all
# Monitor specific service
sudo monit monitor nginx
# Unmonitor all services
sudo monit unmonitor all
# Unmonitor specific service
sudo monit unmonitor nginx
# Check monitoring status
sudo monit status
# Show configuration summary
sudo monit status
# Show detailed status
sudo monit status -v
# Show summary in tree format
sudo monit summary
# Show specific service status
sudo monit status nginx
# Run all checks immediately
sudo monit validate
# Run specific check
sudo monit validate nginx
# Force service restart
sudo monit restart nginx
# Force service stop/start
sudo monit stop nginx
sudo monit start nginx
# Access web interface
curl -k https://localhost:2812
# With authentication
curl -k -u admin:password123 https://localhost:2812
# Check SSL certificate
openssl s_client -connect localhost:2812
# View recent log entries
sudo tail -50 /var/log/monit.log
# Search for specific events
sudo grep "nginx" /var/log/monit.log
# View error messages
sudo grep -i "error\|fail" /var/log/monit.log
# Real-time monitoring
sudo tail -f /var/log/monit.log
monit -t before applyingSqueezing every bit of performance from your Monit installation? Our experts help with:
Optimize your setup: office@linux-server-admin.com | Contact Us