zabbix监控nginx
Posted 三岁半的胖啊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zabbix监控nginx相关的知识,希望对你有一定的参考价值。
一、准备环境
主机 | 名称 |
192.168.200.113 | agent.zabbix.com |
1、关闭防火墙和安全机制
[root@agent ~]# systemctl stop firewalld [root@agent ~]# setenforce 0 [root@agent ~]# iptables -F
二、安装nginx(已有自己组合的包,直接安装)
1、安装
[root@agent ~]# rpm -ivh nginx-1.15.9-1.x86_64.rpm 准备中... ################################# [100%] 正在升级/安装... 1:nginx-1.15.9-1 ################################# [100%]
[root@agent ~]# nginx -V nginx version: nginx/1.15.9 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module
2、修改配置文件
[root@agent ~]# vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; #stub_status on; #access_log off; } location /status { stub_status on; access_log off; } #error_page 404
[root@agent ~]# killall -HUP nginx //平滑重启
3、网页测试
4、编写zabbix监控nginx的脚本
[root@agent ~]# vim /usr/local/zabbix/scripts/nginx-check.sh
#!/bin/bash
###########################
#zabbix monitoring script
#
# nginx:
# - anything available via nginx stub-status module
#
##################################
# Contact:
# vincent.viallet@gmail.com
# Zabbix requested parameter
ZBX_REQ_DATA="$1"
ZBX_REQ_DATA_URL="$2"
# Nginx defaults
NGINX_STATUS_DEFAULT_URL="192.168.200.113/status" #(这里写网站的域名)
WGET_BIN="/usr/bin/wget"
#
# Error handling:
# - need to be displayable in Zabbix (avoid NOT_SUPPORTED)
# - items need to be of type "float" (allow negative + float)
#
ERROR_NO_ACCESS_FILE="-0.9900"
ERROR_NO_ACCESS="-0.9901"
ERROR_WRONG_PARAM="-0.9902"
ERROR_DATA="-0.9903" # either can not connect / bad host / bad port
# Handle host and port if non-default
if [ ! -z "$ZBX_REQ_DATA_URL" ]; then
URL="$ZBX_REQ_DATA_URL"
else
URL="$NGINX_STATUS_DEFAULT_URL"
fi# save the nginx stats in a variable for future parsing
NGINX_STATS=$($WGET_BIN -q $URL -O - 2> /dev/null)
# error during retrieve
if [ $? -ne 0 -o -z "$NGINX_STATS" ]; then
echo $ERROR_DATA
exit 1
fi
#
# Extract data from nginx stats
#
case $ZBX_REQ_DATA in
active_connections) echo "$NGINX_STATS" | head -1 | cut -f3 -d\' \';;
accepted_connections) echo "$NGINX_STATS" | grep -Ev \'[a-zA-Z]\' | cut -f2 -d\' \';;
handled_connections) echo "$NGINX_STATS" | grep -Ev \'[a-zA-Z]\' | cut -f3 -d\' \';;
handled_requests) echo "$NGINX_STATS" | grep -Ev \'[a-zA-Z]\' | cut -f4 -d\' \';;
reading) echo "$NGINX_STATS" | tail -1 | cut -f2 -d\' \';;
writing) echo "$NGINX_STATS" | tail -1 | cut -f4 -d\' \';;
waiting) echo "$NGINX_STATS" | tail -1 | cut -f6 -d\' \';;
*) echo $ERROR_WRONG_PARAM; exit 1;;
esac
exit 0
[root@agent ~]# chmod +x /usr/local/zabbix/scripts/nginx-check.sh
5、配置agent.conf文件
[root@agent ~]# vim /usr/local/zabbix/etc/zabbix_agentd.conf PidFile=/tmp/zabbix_agentd.pid Server=192.168.200.112 ServerActive=192.168.200.112 Hostname=agent.zabbix.com LogFile=/usr/local/zabbix/logs/zabbix_agentd.log Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf UnsafeUserParameters=1 UserParameter=mysql.version,mysql -V UserParameter=mysql.status[*],/usr/local/zabbix/etc/chk_mysql.sh $1 UserParameter=mysql.ping,mysqladmin -uroot -p123123 -P3306 -h192.168.200.113 ping | grep -c alive UnsafeUserParameters=1 UserParameter=nginx[*],/usr/local/zabbix/scripts/nginx-check.sh "$1"
6、重新启动
[root@agent ~]# /etc/init.d/zabbix_agentd restart Zabbix agent terminated. Zabbix agent started. [root@agent ~]# netstat -anpt | grep 10050 tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 27812/zabbix_agentd
7、server端(192.168.200.112)测试获取数据
在获取数据之前server端必须安装wget
[root@agent ~]# yum -y install wget
agent端访问
[root@server ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.200.113 -p 10050 -k "nginx[reading]" 0 [root@server ~]# /usr/local/zabbix/bin/zabbix_get -s 192.168.200.113 -p 10050 -k "nginx[writing]" 1
三、创建监控测试
选择模板文件导入,导入后就可以使用了
有图像说明成功了
以上是关于zabbix监控nginx的主要内容,如果未能解决你的问题,请参考以下文章