Linux 效能監控:用 htop 同 iostat 快速排查伺服器瓶頸

# Linux 效能監控:用 htop 同 iostat 快速排查伺服器瓶頸 做 IT 嘅你一定試過呢個場景:伺服器突然慢到嘔,CPU 飆到 100%,但係你連邊個 process 搞鬼都唔知。今日就同大家分享兩個 Linux 效能監控神器 — **htop** 同 **iostat**,教你點樣喺 5 分鐘內搵出瓶頸所在。 ## 點解要用 htop 而唔係 top? 老實講,top 係經典,但 ...

# Linux 效能監控:用 htop 同 iostat 快速排查伺服器瓶頸

做 IT 嘅你一定試過呢個場景:伺服器突然慢到嘔,CPU 飆到 100%,但係你連邊個 process 搞鬼都唔知。今日就同大家分享兩個 Linux 效能監控神器 — **htop** 同 **iostat**,教你點樣喺 5 分鐘內搵出瓶頸所在。

## 點解要用 htop 而唔係 top?

老實講,top 係經典,但 htop 先係真正好用嘅 Linux 效能監控工具。htop 支援滑鼠操作、彩色顯示、直觀嘅 CPU/記憶體使用率 bar chart,仲可以一鍵 kill process,唔使先記 PID 再打指令。

### 安裝 htop

Ubuntu/Debian:

sudo apt update && sudo apt install htop -y

RHEL/CentOS/AlmaLinux:

sudo dnf install htop -y

### htop 實戰技巧

行 `htop` 之後你會見到一個好豐富嘅畫面。最頂係 CPU 使用率(每個 core 獨立顯示),下面係記憶體同 swap。中間係 process list,預設按 CPU 使用率排序。

**常用快速鍵:**
– `F6` → 揀排序方式(按 CPU、MEM、TIME 等)
– `F9` → 直接 kill process(可以揀 SIGTERM 或 SIGKILL)
– `F5` → Tree view,睇 parent-child process 關係
– `F4` → Filter,打 keyword 只顯示相關 process
– `F2` → Setup,自訂顯示欄位

### 實戰案例:MySQL 食晒 CPU

假設你發現伺服器好慢,`htop` 一開見到 `mysqld` 食咗 800% CPU(8 core 嘅機)。你可以:

1. 按 `F2` → Display options → 勾選「Show custom thread names」
2. 按 `F5` 轉 tree view,睇下係咪某個 connection 嘅 query 出事
3. 或者直接用 `SHOW PROCESSLIST` 喺 MySQL 入面排查

## iostat:磁碟 I/O 瓶頸排查神器

CPU 同 RAM 睇完,好多時瓶頸其實喺 storage。**iostat** 係 sysstat 套件嘅一部分,專門睇 disk I/O 效能。

### 安裝 sysstat

sudo apt install sysstat -y   # Debian/Ubuntu
sudo dnf install sysstat -y   # RHEL/CentOS

### iostat 常用指令

基本用法(每秒更新一次,顯示 5 次):

iostat -x 1 5

`-x` 參數顯示 extended statistics,你會見到以下關鍵指標:

– **%util** → 磁碟忙碌時間百分比。超過 80% 就代表 I/O 瓶頸
– **await** → 平均 I/O 等待時間(ms)。超過 20ms 就要留意
– **svctm** → 平均 service time。如果 await >> svctm,代表 I/O queue 太長
– **r/s, w/s** → 每秒讀寫次數
– **rkB/s, wkB/s** → 每秒讀寫數據量

### 實戰案例:log 寫入拖慢成部機

有次我遇到部 web server 無啦啦好慢,CPU 同 RAM 都正常。一行 `iostat -x 1` 就見到 `/var` partition 嘅 **%util 長期 100%**,await 超過 200ms。

原來係某個 debug log 瘋狂寫入,每秒幾百 MB。解決方法好簡單:rotate 咗個 log、調整 log level 去 `warn`,I/O 即刻回落正常。

## 進階組合技:htop + iostat + dstat

想一次過睇晒 CPU、RAM、Disk、Network?用 **dstat**:

dstat -cdngy 1

呢個指令會同時顯示 CPU、Disk、Network、Paging、System stats,每秒更新。係快速 overview 嘅最佳選擇。

## 總結

Linux 效能監控唔使好複雜。記住呢個 troubleshooting flow:

1. **htop** → 睇 CPU/RAM,搵出食資源嘅 process
2. **iostat -x** → 睇 disk I/O,排查 storage 瓶頸
3. **dstat** → 全面 overview,確認 network 同 system 層面

三個工具夾埋,5 分鐘內搵出 90% 嘅效能問題。下次伺服器出事,唔使再靠估啦!

—

🔗 參考資料:NVD NIST 漏洞資料庫

_想學更多 Linux 管理技巧?記得 bookmark 我哋嘅「知識分享」專欄,每星期都有新教學!_

#SSL #Linux #監控 #技術分享 #IT教學

Linux 效能監控:用 htop 同 iostat 快速排查伺服器瓶頸 - 文章重點速覽 infographic