监控是VPS运维的核心工作。没有监控就无法了解服务器的运行状态,也无法在问题发生前预警。本文介绍从命令行工具到图形化监控平台的多种方案,根据VPS配置和需求选择合适的方案。
htop - 增强版进程管理器
# 安装htop
apt install htop -y
# 运行
htop
htop比top更直观,支持鼠标操作,可以实时查看CPU、内存、Swap使用率和每个进程的资源占用。常用快捷键:F6排序、F9杀进程、F5树形视图。
其他实用的命令行工具:
# iotop - 磁盘I/O监控
apt install iotop -y
iotop -o # 只显示有I/O活动的进程
# iftop - 网络流量监控
apt install iftop -y
iftop -i eth0 # 监控eth0网卡流量
# nethogs - 按进程显示网络流量
apt install nethogs -y
nethogs eth0
# dstat - 综合资源监控
apt install dstat -y
dstat -cdnmg # CPU、磁盘、网络、内存、分页
# nload - 实时网络带宽
apt install nload -y
nload eth0
# glances - 综合监控工具(推荐)
apt install glances -y
glances
Netdata是一款轻量级的实时监控工具,提供美观的Web仪表盘,资源占用极低,适合搬瓦工VPS使用。
# 一键安装
curl https://get.netdata.cloud/kickstart.sh > /tmp/netdata-kickstart.sh
bash /tmp/netdata-kickstart.sh --stable-channel
# 安装后自动启动,默认监听19999端口
systemctl status netdata
# 查看Web界面
# 浏览器访问 http://your_vps_ip:19999
Netdata安全配置(仅允许本地访问,通过Nginx反向代理对外):
# 编辑Netdata配置
nano /etc/netdata/netdata.conf
# 修改监听地址为仅本地
[web]
bind to = 127.0.0.1
# Nginx反向代理配置
# server {
# listen 443 ssl;
# server_name monitor.example.com;
#
# location / {
# proxy_pass http://127.0.0.1:19999;
# proxy_set_header Host $host;
# auth_basic "Monitoring";
# auth_basic_user_file /etc/nginx/.htpasswd;
# }
# }
systemctl restart netdata
Netdata监控的内容包括:CPU使用率、内存和Swap、磁盘I/O、网络流量、系统负载、Nginx连接数、MySQL查询统计、PHP-FPM状态等。全部实时更新,精度到秒。
Prometheus是业界标准的监控解决方案,配合Grafana可视化,适合专业运维需求。由于资源消耗较大,适合2GB以上内存的VPS。
# 安装Node Exporter(采集主机指标)
wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz
tar xzf node_exporter-1.7.0.linux-amd64.tar.gz
cp node_exporter-1.7.0.linux-amd64/node_exporter /usr/local/bin/
# 创建systemd服务
cat > /etc/systemd/system/node_exporter.service << 'EOF'
[Unit]
Description=Node Exporter
After=network.target
[Service]
Type=simple
User=nobody
ExecStart=/usr/local/bin/node_exporter
Restart=always
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start node_exporter
systemctl enable node_exporter
# Node Exporter默认端口9100
如果只有一台VPS,Prometheus + Grafana可以考虑使用Docker部署以简化管理。对于单台 VPS 的场景,Netdata 通常是更轻量的选择。
以上工具都是VPS内部监控,如果VPS宕机则监控也会失效。需要配合外部监控服务:
UptimeRobot配置要点:
Uptime Kuma是功能强大的开源监控工具,可以自建在另一台VPS上(不要与被监控的VPS相同):
# 使用Docker安装(推荐)
docker run -d --restart=always -p 3001:3001 \
-v uptime-kuma:/app/data \
--name uptime-kuma \
louislam/uptime-kuma
# 或使用Node.js安装
apt install nodejs npm -y
git clone https://github.com/louislam/uptime-kuma.git
cd uptime-kuma
npm run setup
npm start
Uptime Kuma支持HTTP(S)、TCP端口、Ping、DNS等多种监控类型,以及丰富的通知渠道。
编写简单的监控脚本,定时检查关键服务状态:
cat > /usr/local/bin/check-services.sh << 'SCRIPT'
#!/bin/bash
LOG="/var/log/service-check.log"
ALERT_EMAIL="admin@example.com"
check_service() {
if ! systemctl is-active --quiet $1; then
echo "[$(date)] $1 已停止,尝试重启..." >> $LOG
systemctl restart $1
if systemctl is-active --quiet $1; then
echo "[$(date)] $1 重启成功" >> $LOG
else
echo "[$(date)] $1 重启失败!" >> $LOG
echo "$1 service failed on $(hostname)" | mail -s "Service Alert" $ALERT_EMAIL
fi
fi
}
# 检查关键服务
check_service nginx
check_service mysql
check_service php8.2-fpm
check_service redis-server
# 检查磁盘空间(使用率超过90%告警)
DISK_USAGE=$(df / | tail -1 | awk '{print $5}' | tr -d '%')
if [ $DISK_USAGE -gt 90 ]; then
echo "[$(date)] 磁盘使用率: ${DISK_USAGE}%" >> $LOG
echo "Disk usage ${DISK_USAGE}% on $(hostname)" | mail -s "Disk Alert" $ALERT_EMAIL
fi
# 检查内存使用
MEM_USAGE=$(free | grep Mem | awk '{printf "%.0f", $3/$2 * 100}')
if [ $MEM_USAGE -gt 90 ]; then
echo "[$(date)] 内存使用率: ${MEM_USAGE}%" >> $LOG
fi
SCRIPT
chmod +x /usr/local/bin/check-services.sh
# 每5分钟执行一次
crontab -e
# */5 * * * * /usr/local/bin/check-services.sh
| 方案 | 适用场景 | 资源占用 | 难度 |
|---|---|---|---|
| htop + 自定义脚本 | 512MB内存的小VPS | 极低 | 简单 |
| Netdata | 1GB+内存,需要图形化监控 | 低(约50MB) | 简单 |
| Prometheus + Grafana | 2GB+内存,专业运维需求 | 较高(约300MB) | 中等 |
| UptimeRobot(外部) | 所有VPS,必备 | 无 | 简单 |
建议组合使用:Netdata(内部实时监控)+ UptimeRobot(外部可用性监控)+ 自定义脚本(服务自动重启),这是性价比最高的监控方案。
Tip: 更多教程请查看新手教程。