MENU

#Linux运维#zabbix监控nginx

October 24, 2018 • Linux阅读设置

1.如何监控Nginx?
需要添加http_stub_status_module 模块

2.安装Nginx需要的模块
(1)安装依赖包

yum -y install vim gcc wget pcre-devel openssl-devel gcc-c++ pcre-dev zlib-devel
(2)安装nginx

wget http://101.96.10.63/nginx.org/download/nginx-1.12.1.tar.gz
解压

tar -zxvf nginx-1.12.1.tar.gz
添加nginx组并添加nginx用户

useradd -M -s /sbin/nologin nginx
(3)编译安装

cd nginx-1.12.1
./configure --prefix=/etc/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
(4)vim nginx.conf

在server {} 添加nginx统计模块

nginx状态监控

    location /nginx_status {
          stub_status on;
          access_log off;
          allow all;        }

(5)重启nginx

3.重启agent
systemctl restart zabbix-agent
4.添加nginx模板
vim /etc/zabbix/zabbix_agentd.d/userparameter_nginx.conf
UserParameter=nginx_status[*],/bin/bash /etc/zabbix/shell/monitor_nginx.sh "$1"
5.编写shell脚本
vim /etc/zabbix/shell/monitor_nginx.sh

!/bin/bash

nginx_port=81
nginx_ip=127.0.0.1
curl_addres="http://$nginx_ip:$nginx_port/nginx_status"
nginx_active() {

curl -s $curl_addres|grep "Active"|awk '{print $3}'

}

nginx_reading() {
curl -s $curl_addres |grep "Reading"| awk '{print $2}'
}
nginx_writing() {
curl -s $curl_addres |grep "Writing"|awk '{print $4}'
}

nginx_waiting() {
curl -s $curl_addres |grep "Waiting"|awk '{print $6}'
}

nginx_accept() {
curl -s $curl_addres |awk 'NR3{print $1}'
}
nginx_handled() {
curl -s $curl_addres |awk 'NR
3{print $2}'
}

nginx_requests() {
curl -s $curl_addres |awk 'NR==3{print $3}'
}

case $1 in

nginx_active)
    nginx_active;
;;
nginx_reading)
    nginx_reading;
;;
nginx_writing)
    nginx_writing;
;;
nginx_waiting)
    nginx_waiting;
;;
nginx_accept)
    nginx_accept;
;;
nginx_handled)
    nginx_handled;
;;
nginx_requests)
    nginx_requests;
;;
*)

esac

Last Modified: October 25, 2018