Giám Sát VPS: Công Cụ và Phương Pháp Tốt Nhất
Giám sát VPS giúp bạn phát hiện vấn đề trước khi chúng gây ra downtime. Biết khi nào CPU quá tải, disk sắp đầy, hay có tấn công DDoS giúp bạn hành động kịp thời. Hướng dẫn này trình bày các công cụ monitoring tốt nhất cho VPS offshore AnubizHost.
Need this done for your project?
We implement, you ship. Async, documented, done in days.
Giám Sát Thời Gian Thực với Netdata
Netdata cung cấp dashboard real-time đẹp và dễ cài đặt:
wget -O /tmp/netdata-kickstart.sh https://my-netdata.io/kickstart.sh
bash /tmp/netdata-kickstart.sh --non-interactiveNetdata chạy trên port 19999. Bảo mật bằng cách giới hạn truy cập:
# Chỉ cho phép truy cập từ IP của bạn
ufw allow from YOUR_IP to any port 19999Hoặc cấu hình Nginx reverse proxy với xác thực:
location /netdata/ {
proxy_pass http://127.0.0.1:19999/;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}Tạo password: htpasswd -c /etc/nginx/.htpasswd admin
Cài Đặt Prometheus và Node Exporter
Prometheus thu thập metrics từ các exporters. Node Exporter cung cấp metrics hệ thống:
# Tải Node Exporter
wget https://github.com/prometheus/node_exporter/releases/latest/download/node_exporter-1.7.0.linux-amd64.tar.gz
tar xvf node_exporter-*.tar.gz
mv node_exporter-*/node_exporter /usr/local/bin/
# Tạo systemd service
cat > /etc/systemd/system/node_exporter.service << EOF
[Unit]
Description=Node Exporter
[Service]
User=node_exporter
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=multi-user.target
EOF
systemctl enable node_exporter
systemctl start node_exporterThiết Lập Cảnh Báo Email và Telegram
Script cảnh báo khi tài nguyên vượt ngưỡng:
nano /usr/local/bin/check-resources.sh#!/bin/bash
TELEGRAM_TOKEN="your_bot_token"
CHAT_ID="your_chat_id"
THRESHOLD_CPU=85
THRESHOLD_MEM=90
THRESHOLD_DISK=85
# Kiểm tra CPU
CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1 | cut -d',' -f1 | xargs printf "%.0f")
# Kiểm tra Memory
MEM=$(free | grep Mem | awk '{print $3/$2 * 100.0}' | xargs printf "%.0f")
# Kiểm tra Disk
DISK=$(df / | tail -1 | awk '{print $5}' | tr -d '%')
send_alert() {
curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendMessage" -d chat_id="$CHAT_ID" -d text="$1"
}
[ "$CPU" -gt "$THRESHOLD_CPU" ] && send_alert "ALERT: CPU ${CPU}% on $(hostname)"
[ "$MEM" -gt "$THRESHOLD_MEM" ] && send_alert "ALERT: Memory ${MEM}% on $(hostname)"
[ "$DISK" -gt "$THRESHOLD_DISK" ] && send_alert "ALERT: Disk ${DISK}% on $(hostname)"Log Management với Journald
Quản lý logs hệ thống hiệu quả:
# Xem logs theo thời gian thực
journalctl -f
# Xem logs của service cụ thể
journalctl -u nginx -f
# Xem logs từ 1 giờ qua
journalctl --since "1 hour ago"
# Lọc theo mức độ nghiêm trọng
journalctl -p err -xb
# Giới hạn dung lượng log
nano /etc/systemd/journald.conf
# Thêm:
SystemMaxUse=500M
MaxRetentionSec=30dayCài đặt logrotate cho log tùy chỉnh:
nano /etc/logrotate.d/myapp/var/log/myapp/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
}Related Services
Why Anubiz Host
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.