Zabbix 監控 Nginx 狀態,zabbixnginx

來源:互聯網
上載者:User

Zabbix 監控 Nginx 狀態,zabbixnginx


簡介:

如何使用 Zabbix 監控 Nginx 狀態 ?

1、擷取 Nginx 狀態( HTTP Stub Status )

shell > /usr/local/nginx/sbin/nginx -Vnginx 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、配置 nginx.conf

shell > vim /usr/local/nginx/conf/nginx.conf    location ~ /nginx_status {    stub_status on;    access_log off;    allow 127.0.0.1;    allow 121.142.111.210;    deny all;    }

## 在虛擬機器主機 server {} 中加入上面配置,也可以單獨定義一個專門用於監控的虛擬機器主機。
## deny all , 拒絕除 allow 中的主機之外所有主機訪問此 URL ,實現過程中如果遇到 403 ,有可能是你把自己測試的機器拒絕了!

3、Nginx 監控項解釋

shell > 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 值

shell > 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 參數,則擷取到的結果是不正確的。

shell > chmod a+x /script/nginx_status.sh

5、添加自訂 key 設定檔

shell > vim /usr/local/zabbix/etc/zabbix_agentd.conf.d/nginx_status.conf## Nginx_statusUserParameter=nginx.active,/script/nginx_status.sh activeUserParameter=nginx.accepts,/script/nginx_status.sh acceptsUserParameter=nginx.handled,/script/nginx_status.sh handledUserParameter=nginx.requests,/script/nginx_status.sh requestsUserParameter=nginx.reading,/script/nginx_status.sh readingUserParameter=nginx.writing,/script/nginx_status.sh writingUserParameter=nginx.waiting,/script/nginx_status.sh waiting

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

shell > vim /usr/local/zabbix/etc/zabbix_agentd.confInclude=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.confUnsafeUserParameters=1 # 允許自訂 Key

## 添加上面配置

5、重啟 Zabbix_agentd

shell > service zabbix_agentd restart

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

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

shell > /usr/local/zabbix/bin/zabbix_get -s 123.57.79.52 -k nginx.active1

## 可以擷取到 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.