服务器资源监控告警脚本:CPU、内存、磁盘使用率邮件通知
#!/bin/bash
This script is used to monitor the server resources, including CPU, Memory and Disk, and then send alert emails.
Monitor the server resources
resource_usage=top -n 1 | grep -E 'Cpu|Mem|/dev'
Get the server info
host_name=hostname
host_ip=hostname -i
Define the alert threshold
cpu_usage_threshold=80 mem_usage_threshold=80 disk_usage_threshold=80
Get the current usage
cpu_usage=echo $resource_usage | awk '{print $2}' | sed -e 's/[^0-9]//g'
mem_usage=echo $resource_usage | awk '{print $4}' | sed -e 's/[^0-9]//g'
disk_usage=echo $resource_usage | awk '{print $7}' | sed -e 's/[^0-9]//g'
Check the usage
if [ $cpu_usage -gt $cpu_usage_threshold ]; then echo 'CPU usage has exceeded the threshold of $cpu_usage_threshold%, the current usage is $cpu_usage%.' | mail -s 'Server Resource Alert: $host_name($host_ip)' admin@example.com fi
if [ $mem_usage -gt $mem_usage_threshold ]; then echo 'Memory usage has exceeded the threshold of $mem_usage_threshold%, the current usage is $mem_usage%.' | mail -s 'Server Resource Alert: $host_name($host_ip)' admin@example.com fi
if [ $disk_usage -gt $disk_usage_threshold ]; then echo 'Disk usage has exceeded the threshold of $disk_usage_threshold%, the current usage is $disk_usage%.' | mail -s 'Server Resource Alert: $host_name($host_ip)' admin@example.com fi
原文地址: https://www.cveoy.top/t/topic/lh0Y 著作权归作者所有。请勿转载和采集!