zh

VPS性能监控:工具选择和最佳实践

了解服务器的运行状态是保障稳定性的前提。没有监控就无法在问题变严重前发现它们。本指南介绍从基础命令行工具到完整监控系统(Prometheus+Grafana)的全套监控方案,帮助您随时掌握VPS的健康状况。

Need this done for your project?

We implement, you ship. Async, documented, done in days.

Start a Brief

基础监控命令行工具

掌握这些命令是服务器监控的基础: 实时系统状态: top # 查看CPU和内存使用情况 htop # 更友好的交互式top(需安装:apt install htop) iotop # 磁盘I/O监控(需安装:apt install iotop) 内存使用: free -h # 显示内存使用概况 vmstat 1 # 每秒刷新的详细内存和CPU统计 磁盘状态: df -h # 磁盘空间使用情况 du -sh /var/log/* # 查看日志目录各文件大小 iostat -x 1 # 磁盘I/O详情(需安装sysstat) 网络状态: ss -tuln # 查看所有监听的端口和服务 iftop # 实时网络流量监控(需安装) nethogs # 按进程显示网络使用(需安装) 系统负载: uptime # 系统运行时间和负载均值 dmesg | tail -50 # 查看内核最近消息 这些命令无需额外安装(除括号标注外),是快速诊断问题的第一选择。

安装Netdata实现实时监控仪表盘

Netdata是一个轻量级的实时性能监控工具,提供美观的Web仪表盘,安装极其简单。 一键安装: bash <(curl -Ss https://my-netdata.io/kickstart.sh) 安装完成后,通过 http://服务器IP:19999 访问仪表盘。 Netdata自动监控数百个指标,包括: - CPU使用率和负载 - 内存和Swap使用 - 磁盘I/O和空间 - 网络流量和连接数 - 运行的进程和服务 - Nginx/MySQL/Redis等服务的专项指标 安全配置(不建议直接暴露19999端口): 通过Nginx反向代理并添加基本认证: apt install apache2-utils -y htpasswd -c /etc/nginx/.htpasswd admin # 在Nginx配置中添加auth_basic指令 Netdata还支持告警通知,当指标超过阈值时发送邮件、Slack或Telegram通知,配置文件位于 /etc/netdata/health.d/。

配置Prometheus + Grafana专业监控栈

对于需要长期历史数据和自定义仪表盘的场景,Prometheus+Grafana是业界标准方案。 使用Docker Compose快速部署: 创建 /opt/monitoring/docker-compose.yml: version: '3' services: prometheus: image: prom/prometheus volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml ports: - 127.0.0.1:9090:9090 grafana: image: grafana/grafana ports: - 127.0.0.1:3000:3000 environment: - GF_SECURITY_ADMIN_PASSWORD=强密码 node-exporter: image: prom/node-exporter volumes: - /proc:/host/proc:ro - /sys:/host/sys:ro command: - '--path.procfs=/host/proc' - '--path.sysfs=/host/sys' ports: - 127.0.0.1:9100:9100 prometheus.yml 配置: scrape_configs: - job_name: 'node' static_configs: - targets: ['node-exporter:9100'] 启动:docker compose up -d 在Grafana中添加Prometheus数据源,导入Node Exporter Full仪表盘(ID: 1860)即可获得专业监控视图。

配置告警和通知

监控数据只有在异常时能及时通知您才有价值。以下是配置Telegram告警通知的方案(适合个人项目): 创建简单的资源监控脚本 /usr/local/bin/check_resources.sh: #!/bin/bash BOT_TOKEN=您的Telegram机器人Token CHAT_ID=您的ChatID THRESHOLD_CPU=80 THRESHOLD_MEM=85 THRESHOLD_DISK=90 CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d. -f1) MEM=$(free | grep Mem | awk '{printf "%.0f", $3/$2 * 100}') DISK=$(df / | tail -1 | awk '{print $5}' | tr -d %) send_alert() { curl -s -X POST https://api.telegram.org/bot$BOT_TOKEN/sendMessage -d chat_id=$CHAT_ID -d text="$1" > /dev/null } [ "$CPU" -gt "$THRESHOLD_CPU" ] && send_alert "CPU告警:当前使用率${CPU}%" [ "$MEM" -gt "$THRESHOLD_MEM" ] && send_alert "内存告警:当前使用率${MEM}%" [ "$DISK" -gt "$THRESHOLD_DISK" ] && send_alert "磁盘告警:当前使用率${DISK}%" 每5分钟检查一次: chmod +x /usr/local/bin/check_resources.sh */5 * * * * /usr/local/bin/check_resources.sh AnubizHost离岸VPS提供稳定的网络环境,配合完善的监控体系,帮助您在问题影响业务前提前发现和处理。

Why Anubiz Host

100% async — no calls, no meetings
Delivered in days, not weeks
Full documentation included
Production-grade from day one
Security-first approach
Post-delivery support included

Ready to get started?

Skip the research. Tell us what you need, and we'll scope it, implement it, and hand it back — fully documented and production-ready.

Anubiz Chat AI

Online