Linux 系统资源监控脚本编写(完整可直接用)
我给你写一个开箱即用、功能完整、格式清晰的 Linux 监控脚本,支持监控:
- CPU 使用率
- 内存使用率
- 磁盘使用率
- 系统负载
- 网络流量
- 进程 TOP 5
脚本会自动格式化输出,可以定时运行、日志记录、也可配合告警使用。
完整脚本:sys_monitor.sh
bash
运行
#!/bin/bash
# ==============================================
# Linux 系统资源监控脚本
# 功能:CPU / 内存 / 磁盘 / 负载 / 网络 / 进程
# ==============================================
# 日志输出文件(可自定义)
LOG_FILE="/var/log/sys_monitor.log"
# 获取当前时间
CURRENT_TIME=$(date "+%Y-%m-%d %H:%M:%S")
# ==============================================
# 1. 输出标题
# ==============================================
echo -e "\n==================================================" | tee -a $LOG_FILE
echo "系统监控报告 - $CURRENT_TIME" | tee -a $LOG_FILE
echo "==================================================" | tee -a $LOG_FILE
# ==============================================
# 2. 系统版本与主机信息
# ==============================================
echo -e "\n📌 主机信息:" | tee -a $LOG_FILE
hostnamectl | grep "Static hostname" | awk '{print "主机名: " $3}' | tee -a $LOG_FILE
hostnamectl | grep "Operating System" | cut -d: -f2 | tee -a $LOG_FILE
hostnamectl | grep "Kernel" | cut -d: -f2 | tee -a $LOG_FILE
# ==============================================
# 3. CPU 使用率
# ==============================================
echo -e "\n📌 CPU 使用情况:" | tee -a $LOG_FILE
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *([0-9.]*)%* id.*/\1/" | awk '{print 100 - $1}')
echo "CPU 总使用率: $CPU_USAGE%" | tee -a $LOG_FILE
# ==============================================
# 4. 内存使用率
# ==============================================
echo -e "\n📌 内存使用情况:" | tee -a $LOG_FILE
free -h | awk 'NR==2{
printf "总内存: %s\n已用: %s\n空闲: %s\n使用率: %.2f%%\n", $2, $3, $4, $3/$2*100
}' | tee -a $LOG_FILE
# ==============================================
# 5. 系统负载(1/5/15分钟)
# ==============================================
echo -e "\n📌 系统负载:" | tee -a $LOG_FILE
uptime | awk '{print "1分钟负载: " $10 " 5分钟负载: " $11 " 15分钟负载: " $12}' | tee -a $LOG_FILE
# ==============================================
# 6. 磁盘使用率(根分区 /)
# ==============================================
echo -e "\n📌 磁盘使用率:" | tee -a $LOG_FILE
df -h / | awk 'NR==2{
printf "总容量: %s\n已用: %s\n可用: %s\n使用率: %s\n", $2, $3, $4, $5
}' | tee -a $LOG_FILE
# ==============================================
# 7. 网络流量(网卡名可自行修改)
# ==============================================
echo -e "\n📌 网络流量(网卡 eth0):" | tee -a $LOG_FILE
# 若网卡不是 eth0,用 ip addr 查看后替换
sar -n DEV 1 1 | grep eth0 | awk 'NR==3{
printf "接收流量: %.2f MB/s\n发送流量: %.2f MB/s\n", $5/1024, $6/1024
}' | tee -a $LOG_FILE
# ==============================================
# 8. 占用资源最高的 5 个进程
# ==============================================
echo -e "\n📌 TOP5 占用 CPU 最高进程:" | tee -a $LOG_FILE
ps -aux --sort=-%cpu | head -6 | awk '{printf "%-8s %-6s %-6s %s\n", $1, $2, $3, $11}' | tee -a $LOG_FILE
echo -e "\n📌 TOP5 占用内存最高进程:" | tee -a $LOG_FILE
ps -aux --sort=-%mem | head -6 | awk '{printf "%-8s %-6s %-6s %s\n", $1, $2, $4, $11}' | tee -a $LOG_FILE
# ==============================================
# 结束
# ==============================================
echo -e "\n==================================================\n" | tee -a $LOG_FILE
使用方法
1. 创建脚本
bash
运行
vi sys_monitor.sh
把上面代码粘贴进去,保存退出。
2. 赋予执行权限
bash
运行
chmod +x sys_monitor.sh
3. 运行
bash
运行
./sys_monitor.sh
4. 查看日志
bash
运行
cat /var/log/sys_monitor.log
脚本效果预览
plaintext
==================================================
系统监控报告 - 2025-12-17 15:30:00
==================================================
📌 主机信息:
主机名: localhost
Operating System: CentOS Stream 9
Kernel: Linux 5.14.0-xxx
📌 CPU 使用情况:
CPU 总使用率: 12.5%
📌 内存使用情况:
总内存: 7.6Gi
已用: 2.3Gi
空闲: 4.1Gi
使用率: 30.20%
...
可选增强功能(你可以直接加进去)
1. 超过阈值自动告警(示例)
bash
运行
# 磁盘使用率超过 85% 告警
DISK_USAGE=$(df -h / | grep / | awk '{print $5}' | sed 's/%//g')
if [ $DISK_USAGE -gt 85 ]; then
echo "⚠️ 磁盘使用率过高!当前:$DISK_USAGE%" | tee -a $LOG_FILE
# 可以发邮件、企业微信、钉钉告警
fi
2. 定时监控(每分钟执行)
bash
运行
crontab -e
添加:
plaintext
* * * * * /root/sys_monitor.sh
我可以帮你定制
你可以告诉我你想要:
- 只监控某几项(CPU + 内存)
- 输出 JSON 格式
- 输出到网页面板
- 钉钉 / 企业微信 / 邮件告警
- 监控 GPU、端口、TCP 连接数