# Linux 主機效能監控(sysstat) ## 安裝常用套件 `apt install sysstat btop htop` `btop` 跟 `htop` 基本比較單純執行後就是直接顯示目前主機運行的效能狀況,簡單把他們當作 Windwos 工作管理員的文字版就好,至於選 btop 或 htop 就看個人喜好。 ## 長時間的效能監控 除了即時的效能監控,做主機維運例行的效能紀錄一樣很重要,因為正常不會整天 24小時開著,工作管理員去看效能,其他事情都不用做了。所以就需要 `sysstat` 這套件去幫忙紀錄平常效能使用狀況,需要時隨時可以去調用特定時段的資源使用情況。 ### 啟用 sysstat 服務(安裝後預設是沒在自動紀錄的) `systemctl enable --now sysstat` 設定啟用 `systemctl status sysstat` 查看服務狀態 ### 調整效能取樣頻率(預設10分鐘取樣一次) 下面範例將採樣頻率調整成3分鐘一次 `systemctl edit sysstat-collect.timer` 原本內容 ``` ### Editing /etc/systemd/system/sysstat-collect.timer.d/override.conf ### Anything between here and the comment below will become the new contents of the file ### Lines below this comment will be discarded ............略 # [Unit] # Description=Run system activity accounting tool every 10 minutes # # [Timer] # OnCalendar=*:00/10 # .............略 ``` 調整成 ``` ### Editing /etc/systemd/system/sysstat-collect.timer.d/override.conf ### Anything between here and the comment below will become the new contents of the file [Unit] Description=Run system activity accounting tool every 3 minutes [Timer] OnCalendar= OnCalendar=*:00/3 ### Lines below this comment will be discarded ............略 # [Unit] # Description=Run system activity accounting tool every 10 minutes # # [Timer] # OnCalendar=*:00/10 # .............略 ``` `systemctl restart sysstat-collect.timer`重新載入設定,後續透過 `systemctl status sysstat-collect.timer` 可以觀察下次採樣的時間點. ``` ● sysstat-collect.timer - Run system activity accounting tool every 3 minutes Loaded: loaded (/lib/systemd/system/sysstat-collect.timer; enabled; preset: enabled) Drop-In: /etc/systemd/system/sysstat-collect.timer.d └─override.conf Active: active (waiting) since Wed 2024-06-12 14:11:58 CST; 1s ago Trigger: Wed 2024-06-12 14:12:00 CST; 114ms left Triggers: ● sysstat-collect.service Jun 12 14:11:58 ai-lab01 systemd[1]: Stopped sysstat-collect.timer - Run system activity accounting tool every 3 minutes. Jun 12 14:11:58 ai-lab01 systemd[1]: Stopping sysstat-collect.timer - Run system activity accounting tool every 3 minutes... Jun 12 14:11:58 ai-lab01 systemd[1]: Started sysstat-collect.timer - Run system activity accounting tool every 3 minutes. ``` ### 調整效能紀錄保留天數(預設保留七天) 編輯 `/etc/sysstat/sysstat` 找到 `HISTORY=7` 假設要保留30天就改成 `HISTORY=30`,改完後 `systemctl restart sysstat` 重啟啟動服務讓設定生效。 sysstat 效能紀錄的資料預設會存放在 `/var/log/sysstat` 一天一個檔案。 ### sar效能紀錄調閱 * `sar -u` 查看 cpu 效能紀錄 * `sar -r` 查看 記憶體 效能紀錄 * `sar -d` 查看 磁碟IO 效能紀錄 * `sar -n ALL` 查看 網路 效能紀錄 * `sar -n ALL --iface=enp3s0` 查看 網路(指定介面卡enp3s0) 效能紀錄