Monitor Resource VPS ด้วย htop และ glances
ก่อนจะ scale up VPS หรือ debug performance ต้องรู้ก่อนว่า resource ตัวไหนเป็นคอขวด htop ให้ภาพ process แบบ interactive ส่วน glances ให้ภาพรวมทั้ง CPU/RAM/disk/network ในหน้าจอเดียว บทความนี้สอนใช้ทั้งสองเครื่องมือและตีความสิ่งที่เห็น
htop: interactive process viewer
sudo apt install htop -y
htop
Shortcut ที่ใช้บ่อยใน htop:
F6— เรียงตาม CPU/MEM/PID/TIMEF5— tree view (ดู parent-child process)F4— filter ตาม process nameF9— kill process ที่เลือกSpace— tag process หลายตัวพร้อมกันu— กรองตาม user
อ่านค่าบน htop
- Load Average (มุมบนขวา) — ค่าเฉลี่ย 1/5/15 นาที ถ้าสูงกว่าจำนวน CPU core = overload
- MEM bar — สีเขียว = used, สีน้ำเงิน = buffers, สีเหลือง = cache (cache ปกติมากได้ ไม่ใช่ปัญหา)
- Swp bar — ถ้าใช้ swap มาก (สีแดง) = RAM ไม่พอจริงๆ
glances: all-in-one monitor
sudo apt install glances -y
# รันแบบ terminal
glances
# รันแบบ web UI (เข้าผ่าน browser port 61208)
glances -w
glances แสดงข้อมูลครบในหน้าจอเดียว:
- CPU per-core usage + top processes
- Memory + swap
- Disk I/O read/write per second
- Network in/out per interface
- Open files, connections, sensors
glances --export csv --output /tmp/glances.csv บันทึก metrics เป็น CSV ได้ทุก interval เหมาะสำหรับ debug ปัญหา performance ในช่วงเวลาที่กำหนด
คำสั่ง built-in ที่ต้องรู้
# CPU usage ทุก process
top -bn1 | head -20
# RAM ละเอียด
free -h
# Disk I/O real-time
iostat -xz 1
# Network traffic real-time
sar -n DEV 1
# ดู process ที่กิน disk I/O มากที่สุด
sudo iotop -o
ตรวจสอบว่า Monitoring ทำงานถูกต้อง
# ตรวจ load average (ควรต่ำกว่าจำนวน core)
uptime
nproc # จำนวน CPU core
# ตรวจ memory ว่ายังพอ
free -h | awk '/^Mem:/ {print "Available:", $7}'
# ตรวจ disk I/O
iostat -d 1 3 | tail -10
# ตรวจ network bandwidth
ifstat -i eth0 1 5 2>/dev/null || ip -s link show eth0
iostat -x 1 ดูค่า %iowait
แก้ปัญหาที่พบบ่อย
htop แสดง CPU 100% ตลอดเวลา
กด F6 เรียงตาม CPU จะเห็น process ที่กิน CPU มากที่สุด ถ้าเป็น PHP/Python worker อาจเป็น infinite loop หรือ query ช้า:
# ดู process ที่กิน CPU มากที่สุด 5 อันดับ
ps aux --sort=-%cpu | head -6
# ดู stack trace ของ process ที่สงสัย (ต้องติดตั้ง strace)
sudo strace -p PID -c -f 2>&1 | head -20
RAM เต็มแต่ไม่รู้ว่า process ไหนกิน
ค่า "used" ใน free -h รวม cache ซึ่งปกติ kernel ปล่อยให้คืนเองเมื่อต้องการ RAM จริง ดูค่า "available" แทน:
# ดู process ที่กิน RAM มากที่สุด (RSS = physical RAM จริง)
ps aux --sort=-%mem | awk 'NR<=6 {print $2, $4, $11}'
# ดู memory breakdown ของ process (เช่น PID 1234)
cat /proc/1234/status | grep -E "VmRSS|VmSwap|Name"
glances แสดง "CRITICAL" สีแดงตลอด
glances ตั้ง threshold ไว้ใน /etc/glances/glances.conf ค่า default อาจ sensitive เกิน ปรับ threshold ให้เหมาะกับ server:
sudo nano /etc/glances/glances.conf
# เพิ่มหรือแก้ส่วน [cpu]
[cpu]
careful=70
warning=80
critical=90
ไม่มี iotop command
ต้องติดตั้งเพิ่ม และต้องใช้ sudo เพราะ iotop อ่าน kernel counters:
sudo apt install iotop -y
# รันแบบ non-interactive (batch mode)
sudo iotop -b -n 3 -o
คำถามที่พบบ่อย
htop กับ top ต่างกันอย่างไร?
top เป็น built-in tool มีทุก Linux แต่ UI ไม่ friendly htop ต้องติดตั้งเพิ่มแต่ scroll/filter/kill process ได้สะดวกกว่ามาก มี color coding ชัดเจน และดู per-core CPU usage แยกได้ สำหรับการใช้งานประจำวัน htop ดีกว่ามาก
Load Average สูงแต่ CPU ใช้งานไม่มาก เกิดจากอะไร?
Load Average นับ process ที่อยู่ใน run queue ทั้งหมด รวมถึงตัวที่รอ I/O (disk/network) ถ้า %iowait สูงใน iostat แสดงว่า disk เป็น bottleneck ไม่ใช่ CPU ต้องดู disk I/O และพิจารณา upgrade storage หรือใช้ caching
glances web UI เปิดได้ไหมถ้าไม่มี public IP?
ได้ ใช้ SSH tunnel: ssh -L 61208:localhost:61208 user@VPS_IP แล้วเปิด browser ที่ http://localhost:61208 ปลอดภัยกว่าการเปิด port 61208 สาธารณะ