zabbix使用模版监控nginx服务
开启监控页面:
插曲:mysql -s “show status” 有很多值 就可以监控了
修改配置文件:
vim nginx.conf location /nginx_status { stub_status on; access_log off; } systemctl restart nginx 访问 curl http://127.0.0.1/nginx_status
[root@agentd2 ~]# curl http://127.0.0.1/nginx_status Active connections: 1 #总用有多少个客户端在访问 server accepts handled requests 2 2 2 # 接收2 正在处理2 总共接收到2 Reading: 0 Writing: 1 Waiting: 0
取值 总用有多少个 :curl http://192.168.123.26/nginx_status 2>/dev/null |awk 'NR==1{print $NF}'
模版的使用:
1、导入模版
配置-模版-导入
选择文件-导入
配置-模版-名称:nginx -应用
2、创建zabbix-agent自定义配置文件,把脚本放在指定目录
[root@agentd2 zabbix_agentd.d]# cat user_def.conf UserParameter=nginx.status[*],/bin/bash /etc/zabbix/scripts/nginx_status.sh $1 #黄色是key UserParameter=staut, netstat -ant |grep ESTABLISHED |wc -l UserParameter=ESTABLISHED,netstat -ant |grep -c ESTABLISHED UserParameter=SYN_SENT,netstat -ant |grep -c SYN_SENT UserParameter=SYN_RECV,netstat -ant |grep -c SYN_RECV UserParameter=FIN_WAIT1,netstat -ant |grep -c FIN_WAIT1 UserParameter=FIN_WAIT2,netstat -ant |grep -c FIN_WAIT2 UserParameter=TIME_WAIT,netstat -ant |grep -c TIME_WAIT UserParameter=CLOSE,netstat -ant |grep -c CLOSE UserParameter=CLOSE_WAIT,netstat -ant |grep -c CLOSE_WAIT UserParameter=LAST_ACK,netstat -ant |grep -c LAST_ACK UserParameter=LISTEN,netstat -ant |grep -c LISTEN UserParameter=CLOSING,netstat -ant |grep -c CLOSING
确定/etc/zabbix/scripts/nginx_status.sh 脚本存在 并执行无报错
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/bin/bash HOST="127.0.0.1" 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}' } # 执行function $1
3、zabbix_get测试取值
[root@localhost zabbix]# zabbix_get -s 192.168.123.26 -k nginx.status[accepts] 172 [root@localhost zabbix]# zabbix_get -s 192.168.123.26 -k nginx.status[reading] 0 [root@localhost zabbix]# zabbix_get -s 192.168.123.26 -k nginx.status[ping] 1 随机测试参数,测试没问题,可以创建主机,或者关联模版等操作
4创建主机(已存在直接)关联模版