#
#zabbix自定义监控项监控Nginx
#
#
#系统版本:
[root@zabbix_2 ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core)
主机名:zabbix_1 | 角色:zabbix-server | IP地址:192.168.174.136 |
主机名:zabbix_2 | 角色:zabbix-agent | IP地址:192.168.174.137 |
# 均关闭防火墙和selinux
#编译安装Nginx
#Nginx 下载地址:http://mirrors.sohu.com/nginx/ pcre :http://down.linuxprobe.com/Tools/pcre-8.35.tar.gz
#上传nginx 和pcre 到zabbix_2主机上
#编译安装依赖包
[root@zabbix_2 ~]# ls anaconda-ks.cfg nginx-1.8.1.tar.gz pcre-8.35.tar.gz [root@zabbix_2 ~]# yum install -y gcc-c++ openssl-devel zlib-devel [root@zabbix_2 ~]# tar -xf pcre-8.35.tar.gz [root@zabbix_2 ~]# cd pcre-8.35/ [root@zabbix_2 pcre-8.35]# ./configure --prefix=/usr/local/pcre/ [root@zabbix_2 pcre-8.35]# make && make install
#编译安装 Nginx
[root@zabbix_2 nginx-1.8.1]# useradd -s /sbin/nologin -M nginx [root@zabbix_2 nginx-1.8.1]# cd nginx-1.8.1/ [root@zabbix_2 nginx-1.8.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module [root@zabbix_2 nginx-1.8.1]# make && make install #./configure --help 查看参数 #启动nginx [root@zabbix_2 ~]# /usr/local/nginx/sbin/nginx
#启动成功,如图所示
#修改 nginx.conf
[root@zabbix_2 ~]# vim /usr/local/nginx/conf/nginx.conf #添加如下内容 [root@zabbix_2 ~]# sed -n '47,52p' /usr/local/nginx/conf/nginx.conf location /status { stub_status on; access_log off; allow 192.168.174.0/24; deny all; }
#测试配置文件是否正确
[root@zabbix_2 ~]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#重启nginx
[root@zabbix_2 ~]# /usr/local/nginx/sbin/nginx -s reload
#访问
#zabbix自定义监控项
#编写脚本
[root@zabbix_2 ~]# cd /etc/zabbix/zabbix_agentd.d/ [root@zabbix_2 zabbix_agentd.d]# vim nginx_status.conf [root@zabbix_2 zabbix_agentd.d]# cat nginx_status.conf UserParameter=nginx.active, /usr/bin/curl -s http://192.168.174.137/status | grep -i Active | awk -F" " '{print $3}' #UserParameter 为关键字 /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf里面有案例 #naginx.active 为key ,后面为获取数据脚本 [root@zabbix_2 zabbix_agentd.d]# /usr/bin/curl -s http://192.168.174.137/status | grep -i Active | awk -F" " '{print $3}' 1 [root@zabbix_2 zabbix_agentd.d]# systemctl restart zabbix-agent.service
#在zabbix_1上测试
[root@zabbix_1 ~]# yum install -y zabbix-get [root@zabbix_1 ~]# zabbix_get -s 192.168.174.137 -p 10050 -k "nginx.active"
1
#找到zabbix_2
#创建监控项
#添加监控项内容(注意此处的键值至关重要,千万不能错)
#监控项添加成功
#创建图形
#选择所要表示的监控项
#图形添加成功
#测试监控是否成功(在zabbix_1上使用ab 测试工具)
[root@zabbix_1 ~]# ab -c 1000 -n 100000 http://192.168.174.137/status
#可以看到图形发生了变化,自定义监控项添加成功
#自定义监控项 监控nginx状态 监控TCP状态
#编写脚本内容如下
[root@zabbix_2 ~]# cat /opt/nginx_TCP_status.sh #!/bin/bash tcp_status_fun(){ TCP_STAT=$1 #netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in state) print key,state[key]}' > /tmp/netstat.tmp ss -ant | awk 'NR>1 {++s[$1]} END {for(k in s) print k,s[k]}' > /tmp/netstat.tmp TCP_STAT_VALUE=$(grep "$TCP_STAT" /tmp/netstat.tmp | cut -d ' ' -f2) if [ -z $TCP_STAT_VALUE ];then TCP_STAT_VALUE=0 fi echo $TCP_STAT_VALUE } nginx_status_fun(){ NGINX_PORT=$1 NGINX_COMMAND=$2 nginx_active(){ /usr/bin/curl "http://192.168.174.137:"$NGINX_PORT"/status/" 2>/dev/null| grep 'Active' | awk '{print $NF}' } nginx_reading(){ /usr/bin/curl "http://192.168.174.137:"$NGINX_PORT"/status/" 2>/dev/null| grep 'Reading' | awk '{print $2}' } nginx_writing(){ /usr/bin/curl "http://192.168.174.137:"$NGINX_PORT"/status/" 2>/dev/null| grep 'Writing' | awk '{print $4}' } nginx_waiting(){ /usr/bin/curl "http://192.168.174.137:"$NGINX_PORT"/status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}' } nginx_accepts(){ /usr/bin/curl "http://192.168.174.137:"$NGINX_PORT"/status/" 2>/dev/null| awk NR==3 | awk '{print $1}' } nginx_handled(){ /usr/bin/curl "http://192.168.174.137:"$NGINX_PORT"/status/" 2>/dev/null| awk NR==3 | awk '{print $2}' } nginx_requests(){ /usr/bin/curl "http://192.168.174.137:"$NGINX_PORT"/status/" 2>/dev/null| awk NR==3 | awk '{print $3}' } case $NGINX_COMMAND in active) nginx_active; ;; reading) nginx_reading; ;; writing) nginx_writing; ;; waiting) nginx_waiting; ;; accepts) nginx_accepts; ;; handled) nginx_handled; ;; requests) nginx_requests; esac } main(){ case $1 in tcp_status) tcp_status_fun $2; ;; nginx_status) nginx_status_fun $2 $3; ;; *) echo $"Usage: $0 {tcp_status key|nginx_status key}" esac } main $1 $2 $3
[root@zabbix_2 ~]# cd /etc/zabbix/zabbix_agentd.d/ #修改配置文件 [root@zabbix_2 zabbix_agentd.d]# cat nginx_status.conf UserParameter=nginx.active, /usr/bin/curl -s http://192.168.174.137/status | grep -i Active | awk -F" " '{print $3}' UserParameter=nginx.status[*],/opt/nginx_TCP_status.sh "$1" "$2" "$3"
#重启zabbix-agent服务
[root@zabbix_2 zabbix_agentd.d]# systemctl restart zabbix-agent.service
#测试key
[root@zabbix_1 ~]# zabbix_get -s 192.168.174.137 -p 10050 -k nginx.status[nginx_status,80,handled] 44
#添加监控项
#将所有的都添加好后
#创建图形
#添加监控项
#查看最新数据
#查看图形
(注意:由于我们把所以的监控行全放在一个图形里面,而每个监控项的数据大小差距太大,导致数据很小的监控项已经与X轴重合了,)
#导入TCP模板(模板由abcdocker提供)
#模板下载地址:http://pan.baidu.com/s/1i54ULjJ 密码:25lh
#修改配置文件
[root@zabbix_2 zabbix_agentd.d]# vim nginx_status.conf
#添加一行
[root@zabbix_2 zabbix_agentd.d]# cat nginx_status.conf UserParameter=nginx.active, /usr/bin/curl -s http://192.168.174.137/status | grep -i Active | awk -F" " '{print $3}' UserParameter=nginx.status[*],/opt/nginx_TCP_status.sh "$1" "$2" "$3" UserParameter=linux_status[*],/opt/nginx_TCP_status.sh "$1" "$2" "$3"
#重启zabbix-agent服务
[root@zabbix_2 zabbix_agentd.d]# systemctl restart zabbix-agent.service
#测试key
[root@zabbix_1 ~]# zabbix_get -s 192.168.174.137 -p 10050 -k linux_status[tcp_status,LISTEN] 7
#导入模板
#选择文件(文件从上面的链接地址下载)
#选择主机中添加模板
#添加模板
#查看最新数据
#查看图形