zabbix4.2配置監控nginx服務


1.監控原理

  通過status模塊監控(--with-http_stub_status_module)

 

 

 2.修改nginx配置(/etc/nginx/conf.d/default.conf)

  在被監控的主機上修改nginx配置文件,在配置文件中添加(一個location)下面的內容,修改后記得重啟nginx服務:

 location /nginx_status {
                stub_status on;
                allow 192.168.10.100;    #表示允許該主機訪問url:http://192.168.10.100/nginx_status
                allow 192.168.10.2;
                access_log off;
        }

  

3.添加監控腳本

  在nginx服務器主機上添加監控nginx腳本(/etc/zabbix/zabbix_agent.d/nginx_status.sh,同時賦予該腳本可執行權限chmod a+x nginx_status.sh

#!/bin/bash
#Script to fetch nginx statuses for monitoring systems 
    HOST="192.168.10.100"   #IP地址為裝有nginx服務的主機地址
    PORT="80"

    function ping {
        /sbin/pidof nginx | wc -l
    }

    function active {
        /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
    }
    function reading {
        /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
    }
    function writing {
        /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
    }
    function waiting {
        /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
    }
    function accepts {
        /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
    }
    function handled {
        /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
    }
    function requests {
        /usr/bin/curl "http://$HOST:$PORT/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
    }
    $1

  

4.進行訪問測試檢測

[root@zabbix-server-center alertscripts]# curl http://192.168.10.100/nginx_status   #IP地址為nginx服務器的主機地址
Active connections: 1 
server accepts handled requests
 1 1 1 
Reading: 0 Writing: 1 Waiting: 0 

  

5.檢測腳本測試

  關於nginx狀態的一些概念:

Active connections  Nginx正處理的活動鏈接數個數;重要
server              Nginx啟動到現在共處理了多少個連接。
accepts             Nginx啟動到現在共成功創建幾次握手。 
handled requests    Nginx總共處理了幾次請求。
Reading             Nginx讀取到客戶端的 Header 信息數。
Writing             Nginx返回給客戶端的 Header 信息數。
Waiting             Nginx已經處理完正在等候下一次請求指令的駐留鏈接,開啟。
Keep-alive的情況下,Waiting這個值等於active-(reading + writing)。
請求丟失數=(握手數-連接數)可以看出,本次狀態顯示沒有丟失請求。
[root@zabbix-server-center alertscripts]# sh nginx_status.sh active
1
[root@zabbix-server-center alertscripts]# sh nginx_status.sh reading
0
[root@zabbix-server-center alertscripts]# sh nginx_status.sh writing
1
[root@zabbix-server-center alertscripts]# sh nginx_status.sh accepts
20

 

6.在裝有nginx服務的主機上修改zabbix_agentd.conf配置文件(/etc/zabbix/zabbix_agentd.conf) 

修改:UnsafeUserParameters 等於 1 :UnsafeUserParameters=1

 

 

 

7.添加zabbix配置文件,放於 /etc/zabbix/zabbix_agentd.d/目錄下(agent的配置文件 /etc/zabbix/zabbix_agentd.conf 中定義了其他key的包含目錄)創建配置文件nginx_status.conf

添加:UserParameter=nginx[*], /etc/zabbix/zabbix_agentd.d/nginx_status.sh $1    
                                                    #后面路徑是監控腳本的位置

 

 

 

8.重啟zabbix-agent服務

[root@ansible-control zabbix_agentd.d]# systemctl restart zabbix-agent.service

  

9.在zabbix server服務器上測試,是否能正常獲取數據  

[root@zabbix-server-center /]# zabbix_get -s 192.168.10.100 -p 10050 -k nginx.status[ping]
1
[root@zabbix-server-center /]# zabbix_get -s 192.168.10.100 -p 10050 -k nginx.status[requests]
2304
[root@zabbix-server-center /]# zabbix_get -s 192.168.10.100 -p 10050 -k nginx.status[accepts]
6285
[root@zabbix-server-center /]# zabbix_get -s 192.168.10.100 -p 10050 -k nginx.status[handled]
6286

 

7.進入zabbix界面配置監控模板  

  (1)創建模板:

 

 

   (2)添加應用集:

 

 

   (3)創建監控項:

 

 

   (4)創建多個監控項:

 

 

   (5)創建觸發器:觸發器實時監控 Nginx 的存活狀態,Nginx 一旦 Dump 機,就會發生報警。

 

 

   (6)創建圖形:

 

 

 

8.將模板應用在裝有Nginx服務的主機

 

 

 

9.查看監控圖形

 

 

 10.如果剛開始數值比較小,不利於查看圖形,可以使用ab進行簡單的壓力測試(主要是requests),再查看數值變化

 

        -n:在測試會話中所執行的請求個數。默認時,僅執行一個請求。

 

        -c:一次產生的請求個數。默認是一次一個。

 

[root@zabbix-server-center /]# ab -n 1000 -c 1000 http://192.168.10.100:80/nginx_status
[root@zabbix-server-center /]# ab -n 1000 -c 1000 http://192.168.10.100:80/nginx_status
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.10.100 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx/1.17.1
Server Hostname:        192.168.10.100
Server Port:            80

Document Path:          /nginx_status
Document Length:        106 bytes

Concurrency Level:      1000
Time taken for tests:   0.331 seconds
Complete requests:      1000
Failed requests:        819
   (Connect: 0, Receive: 0, Length: 819, Exceptions: 0)
Write errors:           0
Total transferred:      253092 bytes
HTML transferred:       109092 bytes
Requests per second:    3016.63 [#/sec] (mean)
Time per request:       331.496 [ms] (mean)
Time per request:       0.331 [ms] (mean, across all concurrent requests)
Transfer rate:          745.59 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        3   31  15.1     26      69
Processing:    26   90  68.1     62     253
Waiting:        1   90  68.2     62     253
Total:         72  121  64.4     91     282

Percentage of the requests served within a certain time (ms)
  50%     91
  66%    103
  75%    109
  80%    113
  90%    239
  95%    280
  98%    282
  99%    282
 100%    282 (longest request)

  

 值得參考的帖子:https://cloud.tencent.com/developer/article/1400917

 

11.關於Nginx的一個告警測試

  進入模板選擇nginx-status模板,添加觸發器,表示requests大於500就發送郵件報警。

 

 在zabbix服務器上使用ab命令進行瘋狂壓力測試(發送requests請求),不一會requests請求就超過500,隨后就會收到報警郵件。

[root@zabbix-server-center /]# ab -n 1000 -c 1000 http://192.168.10.100:80/nginx_status

 

 

 

  

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM