编译nginx status模块以及zabbix监控nginx状态

背景原因:
因刚换工作,突然发现nginx status状态没有被监控,然后在准备添加nginx status的时候,发现之前同事在编译nginx的时候没有编译nginx status模块,可恶,常用的模块都不编译.主要两部分,
第一部分:编译nginx模块
第二部分:zabbix 监控nginx status,其实之前写过,稍微把监控方式优化了下。
一、nginx 编译模块
1.下载nginx安装包
root@gcs:/home/steven# wget http://nginx.org/download/nginx-1.6.3.tar.gz
--2015-09-09 17:27:40--  http://nginx.org/download/nginx-1.6.3.tar.gz
Resolving nginx.org (nginx.org)... 206.251.255.63, 2606:7100:1:69::3f
Connecting to nginx.org (nginx.org)|206.251.255.63|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 805253 (786K) [application/octet-stream]
Saving to: ‘nginx-1.6.3.tar.gz’

100%[============================================================================================================>] 805,253      569KB/s   in 1.4s

2015-09-09 17:27:42 (569 KB/s) - ‘nginx-1.6.3.tar.gz’ saved [805253/805253]

root@gcs:/home/steven# ls
nginx-1.6.3.tar.gz
root@gcs:/home/steven#
2.解压
root@gcs:/home/steven# tar xf nginx-1.6.3.tar.gz
root@gcs:/home/steven# ls
nginx-1.6.3  nginx-1.6.3.tar.gz
root@gcs:/home/steven# cd nginx-1.6.3/
root@gcs:/home/steven/nginx-1.6.3# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
root@gcs:/home/steven/nginx-1.6.3#

3.查看之前nginx编译的参数
root@gcs:/home/steven/nginx-1.6.3# /opt/nginx/sbin/nginx -V
nginx version: nginx/1.6.3
built by gcc 4.8.2 (Ubuntu 4.8.2-19ubuntu1)
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx --with-http_ssl_module --with-http_spdy_module --with-http_gzip_static_module --with-ld-opt=-Wl,-rpath,/usr/local/lib --add-module=/home/gitcafe/src/ngx_devel_kit-0.2.19 --add-module=/home/gitcafe/src/lua-nginx-module-0.9.13 --add-module=/usr/local/lib/ruby/gems/2.0.0/gems/passenger-5.0.8/ext/nginx --with-http_realip_module --with-http_gunzip_module
root@gcs:/home/steven/nginx-1.6.3#

4.二次编译。。。切记在编译之前备份原来的nginx目录文件
root@gcs:/home/steven/nginx-1.6.3#./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_spdy_module --with-http_gzip_static_module --with-ld-opt=-Wl,-rpath,/usr/local/lib --add-module=/home/gitcafe/src/ngx_devel_kit-0.2.19 --add-module=/home/gitcafe/src/lua-nginx-module-0.9.13 --add-module=/usr/local/lib/ruby/gems/2.0.0/gems/passenger-5.0.8/ext/nginx --with-http_realip_module --with-http_gunzip_module --with-http_stub_status_module
root@gcs:/home/steven/nginx-1.6.3# make

5.检查备份之前的nginx文件
root@gcs:/home/steven/nginx-1.6.3# cd /opt/nginx/sbin/
root@gcs:/home/steven/nginx-1.6.3# cp -a /opt/nginx/sbin/nginx /opt/nginx/sbin/nginx.20150909

6.检查文件
root@gcs:/home/steven/nginx-1.6.3#/opt/nginx/sbin/nginx -t 
7.停止nginx服务
root@gcs:/home/steven/nginx-1.6.3#/opt/nginx/sbin/nginx -s stop 

8.覆盖nginx老的文件
root@gcs:/home/steven/nginx-1.6.3#cp -a objs/nginx /opt/nginx/sbin/nginx 备份编译之前的nginx配置文件然后覆盖

9.重启nginx
root@gcs:/home/steven/nginx-1.6.3#/opt/nginx/sbin/nginx 

10.查看编译是否完成
root@gcs:/home/steven/nginx-1.6.3#/opt/nginx/sbin/nginx -V 
二、配置nginx虚拟目录
1.查看nginx status模块是否加载
root@gcs:/home/ops/script# /opt/nginx/sbin/nginx -V
nginx version: nginx/1.6.3
built by gcc 4.8.2 (Ubuntu 4.8.2-19ubuntu1)
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx --with-http_ssl_module --with-http_spdy_module --with-http_gzip_static_module --with-ld-opt=-Wl,-rpath,/usr/local/lib --add-module=/home/gitcafe/src/ngx_devel_kit-0.2.19 --add-module=/home/gitcafe/src/lua-nginx-module-0.9.13 --add-module=/usr/local/lib/ruby/gems/2.0.0/gems/passenger-5.0.8/ext/nginx --with-http_realip_module --with-http_gunzip_module --with-http_stub_status_module
root@gcs:/home/ops/script#

2.在nginx目录中conf.d中添加虚拟站点
   server {
    listen 868;
    server_name 127.0.0.1;
    location /nginx_status {
        stub_status on;
        allow 127.0.0.1;
        deny all;
}
}

3.reload nginx服务
/opt/nginx/sbin/nginx -t
/opt/nginx/sbin/nginx -s reload
三、zabbix配置,参考我之前写过的优化脚本开做的。
1.取值nginx status的脚本如下:
root@gcs:/home/ops/script# cat /home/ops/script/nginx_status.sh
# Set Variables
BKUP_DATE=`/bin/date +%Y%m%d`
LOG="/var/log/nginx_status.log"
#HOST=`/sbin/ifconfig eth1 | sed -n '/inet /{s/.*addr://;s/ .*//;p}'`
HOST='127.0.0.1'
PORT="868"

case $1 in
  active)
    /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| grep 'Active' | awk '{print $NF}' ;;
  reading)
    /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| grep 'Reading' | awk '{print $2}' ;;
  writing)
    /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| grep 'Writing' | awk '{print $4}'  ;;
  waiting)
    /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| grep 'Waiting' | awk '{print $6}' ;;
  accepts)
    /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| awk NR==3 | awk '{print $1}'  ;;
  handled)
    /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| awk NR==3 | awk '{print $2}'  ;;
  requests)
    /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| awk NR==3 | awk '{print $3}' ;;
  *)
   echo "Usage: %0 {active | reading | writing| waiting| accepts | handled | requests}";;
esac
root@gcs:/home/ops/script#

2.zabbix agent监控脚本配置
root@gcs:/etc/zabbix/zabbix_agentd.conf.d# cat /etc/zabbix/zabbix_agentd.conf.d/nginx_status
UnsafeUserParameters=1
UserParameter=nginx.active,/home/ops/script/nginx_status.sh active
UserParameter=nginx.accepts,/home/ops/script/nginx_status.sh accepts
UserParameter=nginx.handled,/home/ops/script/nginx_status.sh handled
UserParameter=nginx.requests,/home/ops/script/nginx_status.sh requests
UserParameter=nginx.reading,/home/ops/script/nginx_status.sh  reading
UserParameter=nginx.writing,/home/ops/script/nginx_status.sh  writing
UserParameter=nginx.waiting,/home/ops/script/nginx_status.sh  waiting
root@gcs:/etc/zabbix/zabbix_agentd.conf.d#

3.重启zabbix agent
root@gcs:/etc/zabbix/zabbix_agentd.conf.d# service zabbix-agent restart
zabbix-agent stop/waiting
zabbix-agent start/running, process 8239
root@gcs:/etc/zabbix/zabbix_agentd.conf.d#

4.在zabbix services 中添加即可。
    关于在zabbix serivces中配置参考我以前写的文章,
    http://opsnotes.net/2014/05/15/install_zabbix_09/
OpsNotes wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!