Linux運(yùn)維必備:命令行中實(shí)時(shí)監(jiān)控 Nginx 神器
下面的指南將教你如何安裝和使用 ngxtop。本軟件是一款免費(fèi)、開源、靈活、實(shí)時(shí)的 Nginx 網(wǎng)絡(luò)服務(wù)器監(jiān)控工具。它的工作原理是程序解析 Nginx 訪問日志并打印有關(guān)請求計(jì)數(shù)、URI 和狀態(tài)代碼等頂級功能的信息。這是系統(tǒng)管理員或高級用戶的最愛,他們想要一個(gè)輕量級工具來監(jiān)控對他們的 Nginx Web 服務(wù)器的請求。
必要條件
- Ubuntu Server 20.04或以上版本。
- Nginx服務(wù)器更新到穩(wěn)定或主線。
- Root訪問權(quán)限或具有sudo權(quán)限的帳戶。
- 更新到最新的系統(tǒng)。
- 已安裝Python 3和PIP 3。
- linuxmi@linuxmi:~/www.linuxmi.com$ sudo apt update && sudo apt upgrade -y
- linuxmi@linuxmi:~/www.linuxmi.com$ sudo apt install python3 python3-pip
ngxtop 的安裝
安裝 python 和 pip 后,我們現(xiàn)在可以繼續(xù)安裝 ngxtop。輸入以下命令。
- linuxmi@linuxmi:~/www.linuxmi.com$ pip3 install ngxtop
- Collecting ngxtop
- Downloading ngxtop-0.0.3-py2.py3-none-any.whl (11 kB)
- Collecting docopt
- Downloading docopt-0.6.2.tar.gz (25 kB)
- Requirement already satisfied: pyparsing in /usr/lib/python3/dist-packages (from ngxtop) (2.4.6)
- Collecting tabulate
- Downloading tabulate-0.8.9-py3-none-any.whl (25 kB)
- Building wheels for collected packages: docopt
- Building wheel for docopt (setup.py) ... done
- Created wheel for docopt: filename=docopt-0.6.2-py2.py3-none-any.whl size=13704 sha256=e4e951599acce8f67d071eb659a1f5e65c2c016a4e3dce3b0f62038b63f74a70
- Stored in directory: /home/linuxmi/.cache/pip/wheels/56/ea/58/ead137b087d9e326852a851351d1debf4ada529b6ac0ec4e8c
- Successfully built docopt
- Installing collected packages: docopt, tabulate, ngxtop
- Successfully installed docopt-0.6.2 ngxtop-0.0.3 tabulate-0.8.9
安裝后,確認(rèn)版本。
- linuxmi@linuxmi:~/www.linuxmi.com$ ngxtop --version
示例輸出:
xstat 0.1
如何使用 ngxtop
現(xiàn)在,不帶任何參數(shù)運(yùn)行 ngxtop 命令將顯示請求計(jì)數(shù)的摘要、請求的 URI、狀態(tài)代碼的請求數(shù)。
- linuxmi@linuxmi:~/www.linuxmi.com$ ngxtop
輸出如下:
接下來,如果您想查看到 Nginx 服務(wù)器的top遠(yuǎn)程連接列表,請鍵入以下內(nèi)容。
- linuxmi@linuxmi:~/www.linuxmi.com$ ngxtop top remote_addr
輸出如下:
如果你想看到4xx或5xx響應(yīng)和HTTP參考附件,你可以輸入以下命令:
- linuxmi@linuxmi:~/www.linuxmi.com$ ngxtop -i 'status >= 400' print request status http_referer
輸出如下:
這些只是一些例子??梢栽谙旅嬲业酵暾慕M合列表。
用法:
ngxtop 選項(xiàng)
ngxtop 信息
選項(xiàng):
-l , --access-log 要解析的訪問日志文件。
-f , --log-format 日志格式,如 log_format 指令中指定的那樣。
--no-follow ngxtop 默認(rèn)行為是忽略日志中的當(dāng)前行
并且只在新行寫入訪問日志時(shí)注意它們。
使用此標(biāo)志告訴 ngxtop 處理訪問日志的當(dāng)前內(nèi)容。
-t , --interval 在跟隨模式下運(yùn)行時(shí)報(bào)告間隔 [默認(rèn)值:2.0]
-g , --group-by 按變量分組 [默認(rèn):request_path]
-w ,--have 子句 [默認(rèn):1]
-o , --order-by 默認(rèn)查詢的輸出順序 [默認(rèn):計(jì)數(shù)]
-n , --limit 限制 top 命令的報(bào)告中包含的記錄數(shù) [默認(rèn)值:10]
-a ..., --a ... 將 exp(必須是聚合 exp:sum、avg、min、max 等)添加到輸出中
-v, --verbose 更詳細(xì)的輸出
-d, --debug 打印每一行和解析的記錄
-h, --help 打印此幫助消息。
--version 打印版本信息。
高級/實(shí)驗(yàn)選項(xiàng):
-c , --config 允許 ngxtop 解析 nginx 配置文件的日志格式和位置。
-i , --filter filter in, 滿足給定表達(dá)式的記錄被處理。
-p , --pre-filter in-filter 表達(dá)式以檢查預(yù)解析階段。
總結(jié)
您已經(jīng)學(xué)習(xí)了如何在 Ubuntu(一個(gè)操作系統(tǒng))上為您的 Nginx 服務(wù)器安裝 ngxtop。請注意,這是用于從日志中搜索特定統(tǒng)計(jì)信息的輕量級、快速和干凈的基本監(jiān)控。