簡介
nginx和很多軟體一樣(php-fpm、docker、apache等)内建了一個狀态頁,對于nginx的狀态檢視以及監控提供了很大幫助。本文主要介紹通過日志服務logtail采集nginx status資訊,并對采集的status資訊進行查詢、統計、搭建儀表盤、建立自定義報警,對您的nginx叢集進行全方位的監控。
環境準備
開啟nginx status插件
确認nginx具備 status功能
輸入以下指令檢視nginx是否具備status功能
nginx -V 2>&1 | grep -o with-http_stub_status_module
with-http_stub_status_module
如果輸出
with-http_stub_status_module
代表支援status功能。
配置 nginx status
在nginx的配置檔案(預設為/etc/nginx/nginx.conf)中開啟status功能,樣例配置如下:
location /private/nginx_status {
stub_status on;
access_log off;
allow 11.132.232.238;
deny all;
}
注意: 該配置隻允許ip為
11.132.232.238
的機器通路
nginx status
功能
驗證Logtail安裝的機器具有 nginx status
通路權限
nginx status
可通過如下指令測試
$curl http://11.132.232.59/private/nginx_status
Active connections: 1
server accepts handled requests
2507455 2507455 2512972
Reading: 0 Writing: 1 Waiting: 0
資料采集
安裝logtail
根據
文檔安裝logtail,确認版本号在0.16.0及以上。若低于0.16.0版本請根據文檔提示更新到最新版本。
采集配置
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5iMyUjYyQDM4ITZzEGM2gTO3Y2M0gzM1MWM1cTZiRWZy8CX5d2bs92Yl1iclB3bsVmdlR2LcNWaw9CXt92Yu4GZjlGbh5yYjV3Lc9CX6MHc0RHaiojIsJye.png)
- 在日志服務控制台建立一個新的Logstore,采集向導中選擇自建軟體中的Nginx監控
- 根據提示配置Nginx監控的url以及相關參數(基于 http采集
功能實作),例如
樣例配置如下:
{
"inputs": [
{
"type": "metric_http",
"detail": {
"IntervalMs": 60000,
"Addresses": [
"http://11.132.232.59/private/nginx_status",
"http://11.132.232.60/private/nginx_status",
"http://11.132.232.62/private/nginx_status"
],
"IncludeBody": true
}
}
],
"processors": [
{
"type": "processor_regex",
"detail": {
"SourceKey": "content",
"Regex": "Active connections: (\\d+)\\s+server accepts handled requests\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+Reading: (\\d+) Writing: (\\d+) Waiting: (\\d+)[\\s\\S]*",
"Keys": [
"connection",
"accepts",
"handled",
"requests",
"reading",
"writing",
"waiting"
],
"FullMatch": true,
"NoKeyError": true,
"NoMatchError": true,
"KeepSource": false
}
}
]
}
- 将樣例配置中
字段内容修改為您需要監控的url清單Addresses
- 如果您的nginx status傳回的資訊和預設的不同,請修改
用以支援http的body解析,具體文檔參見 資料處理配置processors
資料預覽
應用配置1分鐘後,點選預覽可以看到狀态資料已經采集上來(logtail的http采集除了将body解析上傳,還會将url、狀态碼、方法名、響應時間、是否請求成功一并上傳):
_address_:http://11.132.232.59/private/nginx_status
_http_response_code_:200
_method_:GET
_response_time_ms_:1.83716261897
_result_:success
accepts:33591200
connection:450
handled:33599550
reading:626
requests:39149290
waiting:68
writing:145
- 注意: 若無資料,請檢查配置是否為合法json;若配置正常,請參考 資料采集異常排查文檔 自助排查
查詢分析
自定義查詢
查詢相關幫助文檔參見
日志服務查詢- 查詢某一ip的status資訊:
_address_ : 10.168.0.0
- 查詢響應時間超過100ms的請求:
_response_time_ms_ > 100
- 檢視狀态碼非200的請求:
not _http_response_code_ : 200
統計分析
統計分析文法參見
日志服務統計文法- 每5分鐘統計 waiting reading writing connectio 平均值
*| select avg(waiting) as waiting, avg(reading) as reading, avg(writing) as writing, avg(connection) as connection, from_unixtime( __time__ - __time__ % 300) as time group by __time__ - __time__ % 300 order by time limit 1440
- 統計top 10的 waiting
*| select max(waiting) as max_waiting, address, from_unixtime(max(__time__)) as time group by address order by max_waiting desc limit 10
- 目前nginx總數以及invalid數量
* | select count(distinct(address)) as total
not _result_ : success | select count(distinct(address))
- 最近 top 10 失敗的請求
not _result_ : success | select _address_ as address, from_unixtime(__time__) as time order by __time__ desc limit 10
- 每5分鐘統計統計請求處理總數
*| select avg(handled) * count(distinct(address)) as total_handled, avg(requests) * count(distinct(address)) as total_requests, from_unixtime( __time__ - __time__ % 300) as time group by __time__ - __time__ % 300 order by time limit 1440
- 每5分鐘統計平均請求延遲
*| select avg(_response_time_ms_) as avg_delay, from_unixtime( __time__ - __time__ % 300) as time group by __time__ - __time__ % 300 order by time limit 1440
- 請求有效數/無效數
not _http_response_code_ : 200 | select count(1)
_http_response_code_ : 200 | select count(1)
儀表盤
日志服務預設對于Nginx監控資料提供了儀表盤,您可以在nginx status的儀表盤,儀表盤搭建參見
日志服務儀表盤設定。
設定報警
- 将以下查詢另存為快速查詢,名稱為
:invalid_nginx_status
not _http_response_code_ : 200 | select count(1) as invalid_count
- 根據該快速查詢 建立報警規則 ,樣例如下:
選項 | 值 |
報警規則名稱 | |
快速查詢名稱 | |
資料查詢時間(分鐘) | |
檢查間隔(分鐘) | |
觸發次數 | |
字段名稱 | |
比較符 | |
檢查門檻值 | |
通知類型 | |
通知内容 | |
- nginx 日志分析可以參考 https://help.aliyun.com/document_detail/56728.html
若有收獲,就點個贊吧