Python 實(shí)時獲取Linux服務(wù)器信息
性能測試中需要關(guān)注Linux服務(wù)器哪些數(shù)據(jù)?
在進(jìn)行 Linux 服務(wù)器的性能測試時,需要關(guān)注多個關(guān)鍵指標(biāo)以確保系統(tǒng)能夠高效、穩(wěn)定地運(yùn)行。以下是一些常見的性能測試指標(biāo)及其重要性:
1. CPU 使用率
用戶時間 (User Time): 應(yīng)用程序?qū)嶋H使用 CPU 的時間。
系統(tǒng)時間 (System Time): 內(nèi)核和系統(tǒng)調(diào)用使用 CPU 的時間。
空閑時間 (Idle Time): CPU 空閑的時間。
等待 I/O 時間 (I/O Wait Time): CPU 等待 I/O 操作完成的時間。
2. 內(nèi)存使用情況
總內(nèi)存 (Total Memory): 系統(tǒng)總的物理內(nèi)存。
已用內(nèi)存 (Used Memory): 當(dāng)前正在使用的內(nèi)存。
可用內(nèi)存 (Available Memory): 可供新進(jìn)程使用的內(nèi)存。
緩存 (Cache): 用于文件系統(tǒng)的緩存。
緩沖區(qū) (Buffers): 用于塊設(shè)備 I/O 的緩沖區(qū)。
交換空間 (Swap Space): 用于虛擬內(nèi)存的磁盤空間。
交換使用 (Swap Usage): 當(dāng)前正在使用的交換空間。
3. 磁盤 I/O
讀取速率 (Read Rate): 每秒從磁盤讀取的數(shù)據(jù)量。
寫入速率 (Write Rate): 每秒寫入磁盤的數(shù)據(jù)量。
IOPS (Input/Output Operations Per Second): 每秒的 I/O 操作次數(shù)。
等待隊(duì)列長度 (Average Queue Length): 平均等待 I/O 操作的請求數(shù)。
4. 網(wǎng)絡(luò)帶寬
發(fā)送速率 (Send Rate): 每秒發(fā)送的數(shù)據(jù)量。
接收速率 (Receive Rate): 每秒接收的數(shù)據(jù)量。
網(wǎng)絡(luò)錯誤 (Network Errors): 網(wǎng)絡(luò)傳輸中的錯誤數(shù)。
丟包率 (Packet Loss): 數(shù)據(jù)包丟失的比例。
5. 進(jìn)程信息
進(jìn)程數(shù) (Number of Processes): 系統(tǒng)中運(yùn)行的進(jìn)程數(shù)量。
僵尸進(jìn)程 (Zombie Processes): 已經(jīng)終止但其父進(jìn)程尚未回收其資源的進(jìn)程。
負(fù)載平均值 (Load Average): 過去 1 分鐘、5 分鐘和 15 分鐘內(nèi)的系統(tǒng)負(fù)載平均值。
6. 文件系統(tǒng)
掛載點(diǎn) (Mount Points): 文件系統(tǒng)的掛載點(diǎn)。
使用率 (Usage): 每個掛載點(diǎn)的使用百分比。
剩余空間 (Free Space): 每個掛載點(diǎn)的剩余空間。
7. 系統(tǒng)日志
系統(tǒng)日志 (System Logs): 如 /var/log 目錄下的日志文件,記錄系統(tǒng)事件和錯誤信息。
8. 系統(tǒng)啟動時間和響應(yīng)時間
啟動時間 (Boot Time): 系統(tǒng)啟動所需的時間。
響應(yīng)時間 (Response Time): 系統(tǒng)對請求的響應(yīng)時間。
9. 上下文切換 (Context Switches)
上下文切換次數(shù) (Context Switches): 每秒發(fā)生的上下文切換次數(shù)。
10. 中斷 (Interrupts)
中斷次數(shù) (Interrupts): 每秒處理的硬件中斷次數(shù)。
性能測試工具
為了收集這些數(shù)據(jù),可以使用多種工具,包括但不限于:
top, htop: 實(shí)時查看系統(tǒng)資源使用情況。
vmstat, iostat, mpstat: 統(tǒng)計(jì)系統(tǒng)性能數(shù)據(jù)。
dstat: 多合一系統(tǒng)資源統(tǒng)計(jì)工具。
sar (System Activity Reporter): 收集、報(bào)告和保存系統(tǒng)活動信息。
nmon (Nigel's Monitor): 實(shí)時監(jiān)控系統(tǒng)性能。
netstat, ss, iptraf, iftop: 網(wǎng)絡(luò)流量監(jiān)控。
tcpdump, Wireshark: 網(wǎng)絡(luò)抓包分析。
iotop, iostat: 磁盤 I/O 監(jiān)控。
sysdig, strace, ltrace: 系統(tǒng)調(diào)用跟蹤。
示例命令
以下是一些常用的命令示例,可以幫助你獲取上述指標(biāo):
# 查看 CPU 使用情況
top -b -n 1 | grep "Cpu(s)"
# 查看內(nèi)存使用情況
free -h
# 查看磁盤 I/O
iostat -x 1 1
# 查看網(wǎng)絡(luò)帶寬
ifstat 1 1
# 查看進(jìn)程信息
ps aux --sort=-%cpu
# 查看文件系統(tǒng)使用情況
df -h
# 查看系統(tǒng)日志
tail -f /var/log/syslog
# 查看上下文切換和中斷
vmstat 1 1
以下代碼作為輔助,大家如果有興趣寫平臺的話,可以用到
python 每1秒獲取一次mac電腦的cup、內(nèi)存、磁盤,并每分鐘記錄一次數(shù)據(jù)到指定excel文件中
import psutil
import time
from openpyxl import Workbook
from openpyxl.utils import get_column_letter
# 初始化 Excel 工作簿
wb = Workbook()
ws = wb.active
ws.title = "System Monitor"
ws.append(["Timestamp", "CPU (%)", "Memory (%)", "Disk (%)"])
# 記錄數(shù)據(jù)的時間間隔(秒)
record_interval = 60
data_points = []
def get_system_info():
# 獲取 CPU 使用率
cpu_percent = psutil.cpu_percent(interval=1)
# 獲取內(nèi)存使用情況
memory_info = psutil.virtual_memory()
memory_percent = memory_info.percent
# 獲取磁盤使用情況
disk_info = psutil.disk_usage('/')
disk_percent = disk_info.percent
return cpu_percent, memory_percent, disk_percent
def record_data_to_excel(data_points):
for data_point in data_points:
ws.append(data_point)
# 計(jì)算平均值
if data_points:
avg_cpu = sum(point[1] for point in data_points) / len(data_points)
avg_memory = sum(point[2] for point in data_points) / len(data_points)
avg_disk = sum(point[3] for point in data_points) / len(data_points)
# 將平均值寫入 Excel
ws.append(["Average", avg_cpu, avg_memory, avg_disk])
# 保存 Excel 文件
wb.save("system_monitor.xlsx")
try:
while True:
# 獲取當(dāng)前時間戳
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
# 獲取系統(tǒng)信息
cpu_percent, memory_percent, disk_percent = get_system_info()
# 打印當(dāng)前系統(tǒng)信息
print(f"Timestamp: {timestamp}, CPU: {cpu_percent}%, Memory: {memory_percent}%, Disk: {disk_percent}%")
# 將數(shù)據(jù)點(diǎn)添加到列表中
data_points.append([timestamp, cpu_percent, memory_percent, disk_percent])
# 每分鐘記錄一次數(shù)據(jù)到 Excel 文件
if len(data_points) >= record_interval:
record_data_to_excel(data_points)
data_points = [] # 清空數(shù)據(jù)點(diǎn)列表
# 等待 1 秒
time.sleep(1)
except KeyboardInterrupt:
# 如果用戶中斷程序(例如按 Ctrl+C),保存剩余的數(shù)據(jù)點(diǎn)
if data_points:
record_data_to_excel(data_points)
print("Monitoring stopped. Data saved to system_monitor.xlsx")
代碼說明
初始化 Excel 工作簿:
創(chuàng)建一個新的 Excel 工作簿,并設(shè)置工作表名稱為 "System Monitor"。
在第一行添加列標(biāo)題。
定義 get_system_info 函數(shù):
使用 psutil 庫獲取 CPU 使用率、內(nèi)存使用率和磁盤使用率。
定義 record_data_to_excel 函數(shù):
將數(shù)據(jù)點(diǎn)列表中的數(shù)據(jù)寫入 Excel 文件。
計(jì)算 CPU、內(nèi)存和磁盤使用率的平均值,并將這些平均值寫入 Excel 文件。
保存 Excel 文件。
主循環(huán):
每秒獲取一次系統(tǒng)信息并打印。
將獲取的數(shù)據(jù)添加到 data_points 列表中。
當(dāng) data_points 列表中的數(shù)據(jù)點(diǎn)數(shù)量達(dá)到 60 個時(即每分鐘),調(diào)用 record_data_to_excel 函數(shù)將數(shù)據(jù)寫入 Excel 文件,并清空 data_points 列表。
使用 time.sleep(1) 使程序暫停 1 秒,以實(shí)現(xiàn)每秒獲取一次數(shù)據(jù)。
異常處理:
如果用戶通過按 Ctrl+C 中斷程序,保存剩余的數(shù)據(jù)點(diǎn)并退出程序。
運(yùn)行腳本
將上述代碼保存到一個 Python 文件中(例如 system_monitor.py),然后在 Linux 終端中運(yùn)行它:
python3 system_monitor.py
這將開始每秒獲取一次系統(tǒng)信息,并每分鐘將數(shù)據(jù)記錄到 system_monitor.xlsx 文件中。每分鐘結(jié)束時,還會計(jì)算并記錄 CPU、內(nèi)存和磁盤使用率的平均值。如果需要停止監(jiān)控,可以按 Ctrl+C。
請確保你有適當(dāng)?shù)臋?quán)限來執(zhí)行腳本,并且安裝了所有必需的庫。如果遇到任何問題,請告訴我!