以下是 Shell 系统监控 的完整指南,涵盖资源监控、日志分析、性能调优及自动化实践,帮助你构建高效、可靠的服务器监控方案!
Shell 系统监控 的完整指南
一、Shell 监控脚本核心命令
1. 基础监控命令
命令 | 功能 | 示例 |
---|---|---|
top /htop | 实时查看 CPU、内存、进程负载 | top -o %CPU,%MEM |
vmstat | 统计内存、进程、IO 等系统状态 | vmstat 1 5 (每秒刷新,共5次) |
iostat | 监控磁盘 I/O(读写速度、利用率) | iostat -dx 2 (每2秒刷新) |
iftop /nload | 实时网络流量监控(按端口/协议统计) | iftop -i eth0 |
free | 查看内存使用情况(总内存、可用内存) | free -m (以 MB 为单位) |
df | 监控磁盘空间(已用/剩余空间) | df -h (人类可读格式) |
二、Shell 监控脚本示例
1. 实时 CPU 监控脚本
#!/bin/bash
# 监控 CPU 使用率(超过 70% 触发报警)
while true; do
cpu=$(top -bn1 | grep "%Cpu(s)" | awk '{print $2 + $4}')
if [ $(echo "$cpu > 70" | bc -l) -eq 1 ]; then
echo "$(date): CPU 使用率过高(${cpu}%)" | mail -s "CPU Alert" admin@example.com
fi
sleep 5
done
2. 内存监控脚本
#!/bin/bash
# 监控内存使用(可用内存 < 1GB 触发报警)
while true; do
mem=$(free -m | awk '/Mem:/ {print $7}')
if [ $mem -lt 1024 ]; then
echo "$(date): 可用内存不足(${mem}MB)" | mail -s "Memory Alert" admin@example.com
fi
sleep 5
done
3. 磁盘空间监控
#!/bin/bash
# 监控 /data 目录磁盘空间(剩余空间 < 10GB)
while true; do
disk=$(df -h /data | awk 'NR==2 {print $4}')
if [ $(echo "$disk < 10G" | bc -l) -eq 1 ]; then
echo "$(date): /data 磁盘空间不足(${disk})" | mail -s "Disk Alert" admin@example.com
fi
sleep 60
done
三、高级监控脚本
1. 综合性能监控脚本
#!/bin/bash
# 监控 CPU、内存、磁盘、网络(每5秒输出一次)
monitor() {
echo "===== $(date +%T) ====="
echo "CPU: $(top -bn1 | grep "%Cpu(s)" | awk '{print $2 + $4}')%"
echo "MEM: $(free -m | awk '/Mem:/ {print $7}')MB"
echo "DISK: $(df -h / | awk 'NR==2 {print $4}')"
echo "NETWORK: $(iftop -i eth0 -n 1 | awk '/Total:/ {print $2}')bps"
}
while true; do
monitor
sleep 5
done
2. 日志文件监控(按大小自动清理)
#!/bin/bash
# 清理 /var/log/nginx 日志(超过 100MB 的文件)
find /var/log/nginx -type f -name "*.log" -size +100M -exec rm -f {} \;
四、报警与自动化
1. 邮件报警配置
# 安装 sendmail(若未安装)
sudo apt install sendmail-core
# 配置邮件模板(/etc/mail.rc)
set from=your_email@example.com
set smtp=smtp.example.com:587
set auth=login
set user=your_email@example.com
set password=your_password
2. 结合 Cron 自动运行
# 每小时运行 CPU 监控脚本
0 * * * * /path/to/cpu_monitor.sh
# 每日清理日志
0 2 * * * /path/to/log_cleanup.sh
五、可视化监控(Shell + Gnuplot)
1. 实时绘制 CPU 使用率图表
#!/bin/bash
monitor_cpu() {
while true; do
cpu=$(top -bn1 | grep "%Cpu(s)" | awk '{print $2 + $4}')
echo -n "$cpu " >> cpu_data.txt
sleep 1
done
}
# 启动监控并生成图表
monitor_cpu &
gnuplot -persist -e "plot 'cpu_data.txt' with lines title 'CPU Usage'"
六、安全与权限
1. 脚本权限控制
# 仅允许 root 执行
chmod 700 /path/to/monitor.sh
2. 防止暴力破解(Fail2Ban)
# 安装 Fail2Ban
sudo apt install fail2ban
# 配置 SSH 防护
sudo nano /etc/fail2ban/jail.local
[sshd]
bantime = 600
maxretry = 3
七、云原生扩展
1. 监控 Docker 容器
# 查看容器资源使用
docker stats --no-stream
# 监控特定容器(CPU > 50%)
while true; do
stats=$(docker stats --format "{{.CPUPerc}} {{.MemPerc}}" my-container)
cpu=$(echo $stats | awk '{print $1}')
if [ $(echo "$cpu > 50" | bc -l) -eq 1 ]; then
echo "Container CPU Alert!" | mail -s "Docker Alert" admin@example.com
fi
sleep 5
done
2. Kubernetes 集群监控
# 获取 Pod 资源使用
kubectl top pods --namespace=default
# 监控特定 Pod
while true; do
metrics=$(kubectl top pod my-pod --namespace=default --output=json)
cpu=$(echo $metrics | jq '.items[0].metrics.cpu.usage')
if [ $(echo "$cpu > 70" | bc -l) -eq 1 ]; then
echo "Pod CPU Alert!" | mail -s "K8s Alert" admin@example.com
fi
sleep 5
done
八、常见问题与解决方案
1. 脚本执行权限错误
• 解决:chmod +x script.sh
并确保 shebang 正确(如 #!/bin/bash
)。
2. 邮件报警未触发
• 检查:
sendmail
服务是否运行:systemctl status sendmail
- 邮箱地址和密码是否正确。
- 防火墙是否允许 SMTP 端口(25/587)。
3. 资源监控不准确
• 优化:
• 使用 vmstat
或 mpstat
替代 top
(更精确)。
• 增加采样间隔(如 sleep 10
减少负载)。
九、工具链推荐
- 监控脚本框架:
• Bash scripting(基础工具)
• SHELLcheck(脚本语法检查):shellcheck script.sh
- 可视化:
• Gnuplot(生成图表)
• Graphite(长期存储历史数据) - 高级监控:
• Prometheus + Node Exporter(容器化环境)
• Datadog(云原生监控)
通过以上指南,你可以用 Shell 脚本构建一个轻量级、高可用的系统监控方案,适用于本地服务器或小型云环境!如果有具体需求(如 GPU 监控),欢迎进一步讨论。 🐚