zabbix管理七之監控nginx性能python
說明:
nginx
使用zabbix監控nginx,首先nginx須要配置ngx_statusweb
在編譯安裝nginx時須要使用--with-http_stub_status_module參數shell
在nginx的配置文件nginx.conf裏添加以下:
vim
location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; allow 27.115.xxx.xxx; allow 211.95.xxx.xxx; deny all; }
注意1:bash
訪問:服務器
http://localhost/nginx_status
會看到這樣的信息:併發
Active connections: 291 server accepts handled requests 16630948 16630948 31070465 Reading: 6 Writing: 179 Waiting: 106
其含義很容易理解:curl
第一行ide
當前的活躍鏈接數:291
第二行
服務器已接受的鏈接數:16630948(accepted connection #)
服務器已處理的鏈接數:16630948(handled connection #)
服務器已處理的請求:31070465(能夠算出,平均每一個鏈接有 1.8 個請求)(handled connection #)
第三行
Reading – Nginx 讀取的請求頭次數爲 6;
Writting – Nginx 讀取請求體、處理請求併發送響應給客戶端的次數爲 179;
Waiting – 當前活動的長鏈接數:106。
Nginx 官方的解釋以下:
active connections
– number of all open connections
server accepts handled requests
– nginx accepted 16630948 connections, handled 16630948 connections (no one was closed just it was accepted), and handles 31070465 requests (1.8 requests per connection)
reading
– nginx reads request header
writing
– nginx reads request body, processes request, or writes response to a client
waiting
– keep-alive connections, actually it is active - (reading + writing)
注意2:
自定義key(鍵值)參考:http://www.ttlsa.com/zabbix/zabbix-user-parameters/
方法一:使用shell實現:
zabbix客戶端:
cd /root/scripts vim ngx_status.sh #!/bin/bash # Description:zabbix監控nginx性能以及進程狀態 # Note:此腳本須要配置在被監控端 HOST="192.168.1.92" PORT="80" # 檢測nginx性能 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}' } # 執行function $1 chmod +x /root/scripts/ngx_status.sh
##將自定義的UserParameter加入配置文件,而後重啓agentd vim /opt/zabbix/etc/zabbix_agentd.conf 添加: UserParameter=nginx[*],/root/scripts/ngx_status.sh $1 /etc/init.d/zabbix_agentd restart
##服務端: ##zabbix_get測試: [root@localhost ~]# /opt/zabbix/bin/zabbix_get -s 192.168.1.92 -k nginx[requests] 43232
方法二:使用python實現:
zabbix客戶端:
cd /root/scripts vim ngx_status.py #!/usr/bin/env python #encoding:utf-8 import sys import requests r = requests.get('http://192.168.1.92/nginx_status') statusList = r.text.split() def connections(): connections = statusList[2] print connections def accepts(): accepts = statusList[7] print accepts def handled(): handled = statusList[8] print handled def requests(): requests = statusList[9] print requests def Reading(): Reading = statusList[11] print Reading def Writing(): Writing = statusList[13] print Writing def Waiting(): Waiting = statusList[15] print Waiting fun_name = eval(sys.argv[1]) #將字符串類型轉換爲函數類型 fun_name() chmod +x /root/scripts/ngx_status.sh
##將自定義的UserParameter加入配置文件,而後重啓agentd vim /opt/zabbix/etc/zabbix_agentd.conf 添加: UserParameter=nginx[*],/root/scripts/ngx_status.sh $1 /etc/init.d/zabbix_agentd restart
#服務端: ##zabbix_get測試: [root@localhost ~]# /opt/zabbix/bin/zabbix_get -s 192.168.1.92 -k nginx[requests] 43232
最後在zabbix的web界面操做:
建立模板--->建立分組--->建立監控項--->建立觸發器--->建立圖形
注:
附件有一個模板