Files
semaphore/resource_monitor.sh
2025-07-28 18:37:53 +02:00

58 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
# Log file path
LOG_FILE="/opt/phx/log/resource_monitor.log"
# Thresholds
CPU_THRESHOLD=85
MEM_THRESHOLD=90
DISK_THRESHOLD=90
# Get the hostname
HOSTNAME=$(hostname)
# Function to log messages with timestamp
log_info() {
echo "$(date '+%Y-%m-%d %H:%M:%S') [INFO] $1" | tee -a "$LOG_FILE"
}
log_warning() {
echo "$(date '+%Y-%m-%d %H:%M:%S') [WARNING] $1" | tee -a "$LOG_FILE"
}
log_error() {
echo "$(date '+%Y-%m-%d %H:%M:%S') [ERROR] $1" | tee -a "$LOG_FILE" >&2
}
# Start monitoring
log_info "========== Resource Monitoring Started =========="
# Check CPU usage (Fixed)
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print 100 - $8}')
CPU_USAGE=${CPU_USAGE%.*}
if (( CPU_USAGE > CPU_THRESHOLD )); then
log_warning "⚠️ CPU usage is high: ${CPU_USAGE}% (Threshold: ${CPU_THRESHOLD}%) on $HOSTNAME."
else
log_info "✅ CPU usage is normal: ${CPU_USAGE}%."
fi
# Check Memory usage
MEM_USAGE=$(free | awk '/Mem/ {printf "%.0f", ($3/$2)*100}')
if (( MEM_USAGE > MEM_THRESHOLD )); then
log_warning "⚠️ Memory usage is high: ${MEM_USAGE}% (Threshold: ${MEM_THRESHOLD}%) on $HOSTNAME."
else
log_info "✅ Memory usage is normal: ${MEM_USAGE}%."
fi
# Check Disk usage
DISK_USAGE=$(df / | awk 'END {print $5}' | sed 's/%//')
if (( DISK_USAGE > DISK_THRESHOLD )); then
log_warning "⚠️ Disk usage is high: ${DISK_USAGE}% (Threshold: ${DISK_THRESHOLD}%) on $HOSTNAME."
else
log_info "✅ Disk usage is normal: ${DISK_USAGE}%."
fi
log_info "========== Resource Monitoring Completed =========="