1. 程式人生 > >Zabbix 監控 Nginx 狀態

Zabbix 監控 Nginx 狀態

nginx zabbix

1、獲取 Nginx 狀態條件( 需要nginx安裝模塊)

[localhost]#/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.8.0built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module

2、配置單獨的虛擬主機配置文件

vim nginx_status.conf

server {
    listen       80;
    server_name 127.0.0.1;
    index  index.php  index.html index.htm;
    location ~ /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    allow 67.134.57.88;  #zabbix_server端的ip
    deny all;
    }


    location ~ .*\.(php|php5)?$
        {
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$
                include fastcgi.conf;
        }
}

3、名詞解釋

curl http://127.0.0.1/nginx_statusActive connections: 1server accepts handled requests1 1 1Reading: 0 Writing: 1 Waiting: 0

## Active connections: 對後端發起的活動連接數
## Server accepts handled requests: Nginx 總共處理了 1 個連接,成功創建了 1 次握手(沒有失敗次數),總共處理了 1 個請求
## Reading: Nginx 讀取到客戶端的 Header 信息數
## Writing: Nginx 返回給客戶端的 Header 信息數

## Waiting: 開啟 keep-alive 的情況下,這個值等於 active - ( reading + writing ), 意思是 Nginx 已經處理完成,正在等待下一次請求指令的駐留連接
## 在訪問效率很高,請求很快被處理完畢的情況下,Waiting 數比較多是正常的。如果 reading + writing 數較多,則說明並發訪問量很大,正在處理過程中

4、編寫腳本獲取上面的 key 值

vim /script/nginx_status.sh
#!/bin/bashcase $1 in
    active)
        curl -s http://127.0.0.1/nginx_status | awk ‘/Active/ {print $3}‘ ;;    accepts)
        curl -s http://127.0.0.1/nginx_status | awk ‘NR==3 {print $1}‘ ;;    handled)
        curl -s http://127.0.0.1/nginx_status | awk ‘NR==3 {print $2}‘ ;;    requests)
        curl -s http://127.0.0.1/nginx_status | awk ‘NR==3 {print $3}‘ ;;    reading)
        curl -s http://127.0.0.1/nginx_status | awk ‘/Reading/ {print $2}‘ ;;    writing)
        curl -s http://127.0.0.1/nginx_status | awk ‘/Writing/ {print $4}‘ ;;    waiting)
        curl -s http://127.0.0.1/nginx_status | awk ‘/Waiting/ {print $6}‘ ;;
    *)        echo "Usage: $0 { active | accepts | handled | requests | reading | writing | waiting }" ;;esac

## 腳本使用 curl 結合 awk 來獲取 key 的值。
## -s 靜默模式,如果不加 -s 參數,則獲取到的結果是不正確的。

chmod a+x /script/nginx_status.sh

5、添加自定義 key 配置文件

vim /usr/local/zabbix/etc/zabbix_agentd.conf.d/nginx_status.conf
## Nginx_status
UserParameter=nginx.active,/script/nginx_status.sh active
UserParameter=nginx.accepts,/script/nginx_status.sh accepts
UserParameter=nginx.handled,/script/nginx_status.sh handled
UserParameter=nginx.requests,/script/nginx_status.sh requests
UserParameter=nginx.reading,/script/nginx_status.sh reading
UserParameter=nginx.writing,/script/nginx_status.sh writing
UserParameter=nginx.waiting,/script/nginx_status.sh waiting

## 也可以直接加到 /usr/local/zabbix/etc/zabbix_agentd.conf 末尾

vim /usr/local/zabbix/etc/zabbix_agentd.conf

Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf

UnsafeUserParameters=1 # 允許自定義 Key

5、重啟 Zabbix_agentd

/etc/init.d/zabbix_agent restart

## 註意,上面全部操作都是在被監控端

6、Zabbix 服務端測試能否拿到 Key

/usr/local/zabbix/bin/zabbix_get -s 122.78.90.88 -k nginx.active 
108

技術分享

## 可以獲取到 key 的值,說明配置沒有問題了
## 被監控端記得要開啟 10050 端口

7、Zabbix 監控 Nginx 狀態

## 接下來就是在 web 界面,創建模板、添加監控項了

> 創建模板

## 一起跟著我點點點 Configuration -> Templates -> Create template

Template name : Template App Nginx Service

Groups In groups : Templates

Update

> 創建應用分組

## 現在又回到了模板頁,跟我一起點 Template App Nginx Service 模板後面的 Applications -> Create application

Name : Nginx status

Update

> 創建監控項

## 現在的位置是 Template App Nginx Service 模板中的 Applications 頁面,跟我一起點擊剛創建的 Nginx status 後面的 Items -> Create item

Name : nginx active ## 監控項名稱

Type : Zabbix agent ## 監控類型,默認被動模式

Key : nginx.active ## 由於是自定義 Key ,所以要自己寫上去。如果使用自帶的 Key ,點擊 Select 選擇就可以了

Type of information : Numeric( unsiqned ) ## 數據在進行類型轉化之後存入數據庫

Numeric( unsiqned ) : 64 位無符號整數
Numeric( float ) : 浮點數類型
Character : 字符串類型數據,限制為 255B
Log : 日誌文件,必須使用的 Key 為 log[]
Text : 文本,不限制大小

Data type : Decimal ## 數據類型用於存儲 Items 中 Key 所獲取的數值值,存儲在不同的表中,如:history 、history_str 等表

Boolean : 在數據存儲時將原本的值替換為 0 或 1,TRUE 存儲為 1 ,FALSE 存儲為 0 ,所有的值都區分大小寫( 任何非零都為 TRUE ,0 為 FALSE )
Octal : 八進制數的數值格式
Decimal : 十進制數的數值格式
Hexadecimal : 十六進制數的數值格式

## Zabbix 自動執行數據類型的格式轉換

Units : 留空 ## 單位符號,Zabbix 會自動處理接收到的數據,並且把數據轉換為需要顯示的格式

Use custom multiplier : 不勾選 ## 如果開啟,所接收到的數據會被乘以整數或浮點數。該選項用於單位換算,KB 、MBps 換為 B 、Bps 等

Update interval( in sec ) : 30 ## 間隔多長時間通過 Items 收集數據,單位 秒

Flexible intervals : ## 用於設置不同時間段收集數據的間隔不同,如果設置,則此時間段按這個間隔,其余的按上面參數的間隔

New flexible interval Interval( in sec ) : 50 Period 1-7,00:00-24:00 Add : ## 通過這裏來添加一個新的時間段

History storage period( in days ) : 90 ## 歷史數據保留時間

Trend storage period( in days ) : 365 ## 趨勢數據保留時間

Store value : As is ## 數據存儲時的類型

As is : 無預處理

Delta( speed per second ) : 顯示每秒速率的值( 例如網卡流量 )

Delta( simple change ) : 顯示本次數值與上次數值的差值

Show value : As is ## 無預處理( 數據顯示時的類型,值映射:將接收到的值映射為一個狀態,不改變原值,只改變顯示結果,例如將 1 映射為 Nginx service states )

New application : 留空 ## 創建一個新的應用分組

Applications : Nginx status ## 選擇一個已有的應用分組

Populates host inventory field : None ## 將此 Item 分配給某個資產管理組

Description : 留空 ## 對 Item 的描述信息

Enabled : 勾選 ## 開啟此 Item

Add

## 分別定義 active | accepts | handled | requests | reading | writing | waiting 即可

8、將模板應用到主機

> 如果沒有主機,跟著我點點點 Configuration -> Hosts -> Create host

Host name : 123.121.211.52 ## 被監控端 IP( 這裏要寫被監控端 zabbix_agentd.conf 文件中 Hostname 的值 )

Visible name : Shuma_1 ## 定義一個別名( 要顯示的名字 )

Groups In groups : Shuma ## 選擇加入到一個主機組中

New group : 留空 ## 自定義一個主機分組

Agent interfaces : ## 接口協議

IP address : 123.121.211.52 ## 被監控端的 IP

DNS name : 留空 ## 監控主機能夠解析的名稱

Connect to : IP ## 選擇什麽方式

Port : 10050 ## 被監控端要開放此端口

Description : 留空 ## 對主機的描述信息

Monitored by proxy : no proxy ## 不使用代理

Enabled : 勾選 ## 開啟監控 ( Monitored 代表主機可用,已被監控中;Not monitored 代表主機不可用,不被監控 )

Add ## 代表主機添加完成( 這裏先不點的啦,一並把模板鏈接上 )選擇當前頁面中的 Templates

> 現在的位置是主機頁中的模板選項卡中,跟著我點點點 Select

新頁面中選擇 Template App Nginx Service 之後又返回來了,點擊 Add 這樣就成功鏈接模板了

Add ## 最後來一次就可以啦

9、看一下勞動成果咯

> 跟著我點點點 Configuration -> Hosts

## 你會看到剛添加的主機,它有一個 Applications ,7 個 Items ,看到鏈接了模板,狀態為 Enabled 。

> 跟著我點點點 Monitoring -> Latest data

Hosts : Select 一下剛創建的主機,註意:它在分組裏面,點擊 Filter

## 現在,你有沒有看到 7 個監控項,並且都獲取到了數據喲

10、給主機添加一個 Graph

> 跟著我點點點 Configuration -> Hosts 之後,點擊剛創建的主機後面的 Graphs 就可以創建圖表了

## 我臨時改變主意了,直接去模板裏創建比較好,這樣就不用給每臺主機創建了

> 跟著我點點點 Configuration -> Templates 之後,點擊剛創建的模板後面的 Graphs -> Create graph

Name : Nginx status ## 圖表名稱

width : 900 ## 寬度

height : 200 ## 高度

Graph type : Normal ## 圖表類型

Normal : 常規圖表,值用線條表示

Stacked : 疊圖

pie : 餅圖

Exploded : 分解餅圖

Show legend : 勾選 ## 顯示圖表說明

Show working time : 勾選 ## 非 "工作時間" 用灰色表示,不有用於餅圖跟分解餅圖。

Show triggers : 勾選 ## 觸發達到閾值會用紅色線條顯示,不能用於餅圖跟分解餅圖,只有部分觸發器函數支持,如 min 、max

Percentile line( left ) : 不勾選 ## 左邊的 Y 軸用來顯示百分比,僅對常規圖表適用

Percentile line( right ) : 不勾選 ## 右邊的 Y 軸用來顯示百分比,僅對常規圖表適用

Y axix MIN value : Calculated ## 自動計算 Y 軸最小值

Y axis MAX value : Calculated ## 自動計算 Y 軸最大值

Items ## 點擊 Add 將之前的 7 個 item 都添加到一張圖中

Name : Item 的名稱顯示的數據

Function : 當一個 Item 存在不止一個值時,顯示哪一個數據( all 全部 ,min 僅最小值 ,avg 僅平均值 ,max 僅最大值 )

Draw style : Line ## 僅對常規圖表適用,對疊圖填充區域適用

Line :畫線條

Filled region : 畫填充區域

Bold line : 畫粗線

Dot : 畫圓點

Dashed line : 畫虛線

Y axis side : Left ## Y 軸的左側分給元素

Colour : 每種元素代表的顏色

Add ## 完成 Graph 創建,如果在主機中,可以通過第二選項卡 Preview 查看

11、查看 Graph

## 由於之前已經引用了模板,所以現在主機中自動就有了此 Graph

> 跟著我點點點 Monitoring -> Graphs

技術分享

到此,zabbix監控nginx狀態完成。

本文出自 “zpp” 博客,請務必保留此出處http://1439337369.blog.51cto.com/10270624/1949480

Zabbix 監控 Nginx 狀態