说明:
使用zabbix监控nginx,首先nginx需要配置ngx_status
在编译安装nginx时需要使用--with-http_stub_status_module参数
在nginx的配置文件nginx.conf里添加如下:
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; }
注意:
自定义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.status[*],/root/scripts/ngx_status.sh $1 /etc/init.d/zabbix_agentd restart
##服务端: ##zabbix_get测试: [[email protected] ~]# /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.status[*],/root/scripts/ngx_status.sh $1 /etc/init.d/zabbix_agentd restart
#服务端: ##zabbix_get测试: [[email protected] ~]# /opt/zabbix/bin/zabbix_get -s 192.168.1.92 -k nginx[requests] 43232
最后在zabbix的web界面操作:
创建模板--->创建分组--->创建监控项--->创建触发器--->创建图形
注:
附件有一个模板
时间: 2024-10-06 19:25:45