首页 > 系统相关 >Zabbix监控Nginx

Zabbix监控Nginx

时间:2022-12-07 14:07:29浏览次数:57  
标签:status http nginx -- zabbix Nginx Zabbix 监控 root

  在实际生产中会经常需要自定义一些各种中间件的监控项,自己一个个添加的话太繁琐了,之所以在zabbix官网和第三方网站都会提供大量的监控模板,有的写的非常可以,我们在生产中的话是可以去下载一些模板,并进行修改成自己适合的监控模版使用到生产环境中,这样的话也简便和高效。这里我还是在Linux主机上操作。

1、部署nginx服务

如果是使用编译安装nginx的话,需要添加--with-http_stub_status_module这个编译的参数,这里我就演示一下编译安装nginx了。

#安装nginx的时候也是要同时安装zabbix agent的,这里我就不安装zabbix agent了,我使用的这个主机在监控Linux主机是安装了,没安装的话可以执行下面的命令安装,安装了的话就直接执行apt -y install nginx就可以了
root@Linux:~# wget https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.0-1%2Bfocal_all.deb
root@Linux:~# dpkg -i zabbix-release_5.0-1+focal_all.deb
root@Linux:~# apt update
root@Linux:~# apt -y install zabbix-agent

#编译安装nginx
root@Linux:~# cat install_all_nginx.sh	#使用脚本来一键编译安装nginx
#!/bin/bash

SRC_DIR=/usr/local/src
NGINX_URL=http://nginx.org/download/
NGINX_FILE=nginx-1.20.2
TAR=.tar.gz
NGINX_INSTALL_DIR=/apps/nginx
CPUS=`lscpu | awk '/^CPU\(s\)/{print $2}'`

color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "    
    elif [ $2 = "failure" -o $2 = "1"  ] ;then 
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo 
}

os_type () {
   awk -F'[ "]' '/^NAME/{print $2}' /etc/os-release
}

os_version () {
   awk -F'"' '/^VERSION_ID/{print $2}' /etc/os-release
}

check () {
    [ -e ${NGINX_INSTALL_DIR} ] && { color "nginx 已安装,请卸载后再安装" 1; exit; }
    cd  ${SRC_DIR}
    if [  -e ${NGINX_FILE}${TAR} ];then
        color "相关文件已准备好" 0
    else
        color '开始下载 nginx 源码包' 0
        wget ${NGINX_URL}${NGINX_FILE}${TAR} 
        [ $? -ne 0 ] && { color "下载 ${NGINX_FILE}${TAR}文件失败" 1; exit; } 
    fi
} 

install () {
    color "开始安装 nginx" 0
    if id nginx  &> /dev/null;then
        color "nginx 用户已存在" 1 
    else
        useradd -s /sbin/nologin -r  nginx
        color "创建 nginx 用户" 0 
    fi
    color "开始安装 nginx 依赖包" 0
    if [ `os_type` == "CentOS" -a `os_version` == '8' ] ;then
        yum -y -q install make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed 
    elif [ `os_type` == "CentOS" -a `os_version` == '7' ];then
        yum -y -q  install make gcc pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed
    else
        apt update &> /dev/null
        apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev &> /dev/null
    fi
    cd $SRC_DIR
    tar xf ${NGINX_FILE}${TAR}
    NGINX_DIR=`echo ${NGINX_FILE}${TAR}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd ${NGINX_DIR}
    ./configure --prefix=${NGINX_INSTALL_DIR} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module 
    make -j $CPUS && make install 
    [ $? -eq 0 ] && color "nginx 编译安装成功" 0 ||  { color "nginx 编译安装失败,退出!" 1 ;exit; }
    echo "PATH=${NGINX_INSTALL_DIR}/sbin:${PATH}" > /etc/profile.d/nginx.sh
    cat > /lib/systemd/system/nginx.service <<EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=/bin/rm -f ${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=${NGINX_INSTALL_DIR}/sbin/nginx -t
ExecStart=${NGINX_INSTALL_DIR}/sbin/nginx
ExecReload=/bin/kill -s HUP \$MAINPID
KillSignal=SIGQUIT
LimitNOFILE=100000
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl enable --now nginx &> /dev/null 
    systemctl is-active nginx &> /dev/null ||  { color "nginx 启动失败,退出!" 1 ; exit; }
    color "nginx 安装完成" 0
}

check
install
root@Linux:~# bash install_all_nginx.sh
root@Linux:~# ln -s /apps/nginx/sbin/nginx /usr/sbin/
root@Linux:~# nginx -V
nginx version: nginx/1.20.2
built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1) 
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

root@Linux:~# vi /apps/nginx/conf/nginx.conf	#在server语句块中加上以下语句块
#配置nginx的状态页
location /nginx_status {
    stub_status;
    allow 10.0.0.0/24;
    allow 127.0.0.1;
    deny all;
}
root@Linux:~# nginx -t	#检查nginx配置文件的语法
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
root@Linux:~# nginx -s reload	#重新加载nginx配置文件,使其刚刚修改的配置生效
root@Linux:~# lsof -i:80
COMMAND    PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   184102  root    6u  IPv4 385155      0t0  TCP *:http (LISTEN)
nginx   194036 nginx    6u  IPv4 385155      0t0  TCP *:http (LISTEN)
nginx   194037 nginx    6u  IPv4 385155      0t0  TCP *:http (LISTEN)

在另一个主机上检测状态页是否可以访问
root@zabbix-server:~# curl http://10.0.0.102/nginx_status	#这里我在浏览器上也打开了浏览状态页
Active connections: 3 
server accepts handled requests
 4 4 4 
Reading: 0 Writing: 1 Waiting: 2

名词解释
Active connections:当前出于活动状态的客户端连接数,包括连接等待空闲连接数
accepts:统计总值,Nginx自启动后已经接受的客户端请求的总数
handled:统计总值,Nginx自启动后已经处理完成的客户端请求的总数,通常此值等于accepts的值,除非因worker_coonections限制等被拒绝的连接
requests:统计总值,Nginx自启动后客户端发来的总的请求数
Reading: 当前状态,正在读取客户端请求报文首部的连接的连接数
Writing: 当前状态,正在向客户端发送响应报文过程中的连接数
Waiting: 当前状态,正在等待客户端发出请求的空闲连接数,开启keep-alive的情况下,这个值等于active - (Reading+Writing)

image.png

2、编辑监控获取数据的脚本

#在生产中的连接数是不止这么多的,这数值是我刚刚用了循环语句造的
root@zabbix-server:~# curl http://10.0.0.102/nginx_status
Active connections: 1 
server accepts handled requests
 583 583 599 
Reading: 0 Writing: 1 Waiting: 0

root@Linux:~# cd /etc/zabbix/zabbix_agentd.d/
root@Linux:/etc/zabbix/zabbix_agentd.d# vi nginx_monitor.sh
#!/bin/bash

nginx_status_fun(){
    NGINX_PORT=$1
    NGINX_COMMAND=$2
    nginx_active(){
        /usr/bin/curl http://10.0.0.102:"${NGINX_PORT}"/nginx_status 2> /dev/null | grep 'Active' | awk '{print $NF}'
    }
    nginx_reading(){
        /usr/bin/curl http://10.0.0.102:"${NGINX_PORT}"/nginx_status 2> /dev/null | grep 'Reading' | awk '{print $2}'
    }
    nginx_writing(){
        /usr/bin/curl http://10.0.0.102:"${NGINX_PORT}"/nginx_status 2> /dev/null | grep 'Writing' | awk '{print $4}'
    }
    nginx_waiting(){
        /usr/bin/curl http://10.0.0.102:"${NGINX_PORT}"/nginx_status 2> /dev/null | grep 'Waiting' | awk '{print $6}'
    }
    nginx_accepts(){
        /usr/bin/curl http://10.0.0.102:"${NGINX_PORT}"/nginx_status 2> /dev/null | awk NR==3 | awk '{print $1}'
    }
    nginx_handled(){
        /usr/bin/curl http://10.0.0.102:"${NGINX_PORT}"/nginx_status 2> /dev/null | awk NR==3 | awk '{print $2}'
    }
    nginx_requests(){
        /usr/bin/curl http://10.0.0.102:"${NGINX_PORT}"/nginx_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
        nginx_status)
            nginx_status_fun $2 $3;
            ;;
        *)
            echo $"Usage: $0 {nginx_status key}"
    esac
}

main $1 $2 $3
root@Linux:/etc/zabbix/zabbix_agentd.d# chmod a+x nginx_monitor.sh
root@Linux:/etc/zabbix/zabbix_agentd.d# bash nginx_monitor.sh nginx_status 80 active
1
root@Linux:/etc/zabbix/zabbix_agentd.d# bash nginx_monitor.sh nginx_status 80 reading
0

3、Zabbix agent添加自定义监控项

root@Linux:~# vim /etc/zabbix/zabbix_agentd.conf
root@Linux:~# grep -Ev "#|^$" /etc/zabbix/zabbix_agentd.conf
PidFile=/tmp/zabbix_agentd.pid
LogFile=/tmp/zabbix_agentd.log
LogFileSize=0
Server=10.0.0.100,10.0.0.104
ListenPort=10050
ListenIP=0.0.0.0
StartAgents=3
ServerActive=10.0.0.104
Hostname=10.0.0.102
Timeout=30
AllowRoot=1
User=root
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UserParameter=linux_status[*],/etc/zabbix/zabbix_agentd.d/tcp_conn_plugin.sh "$1" "$2"
UserParameter=memcache_status[*],/etc/zabbix/zabbix_agentd.d/memcache_monitor.sh "$1" "$2" "$3"
UserParameter=redis_status[*],/etc/zabbix/zabbix_agentd.d/redis_monitor.sh "$1" "$2" "$3"
UserParameter=nginx_status[*],/etc/zabbix/zabbix_agentd.d/nginx_monitor.sh "$1" "$2" "$3"
root@Linux:~# systemctl restart zabbix-agent

4、在Zabbix server上测试是否可以获取数据

root@zabbix-server:~# zabbix_get -s 10.0.0.102 -p 10050 -k "nginx_status["nginx_status",80,"active"]"
1
root@zabbix-server:~# zabbix_get -s 10.0.0.102 -p 10050 -k "nginx_status["nginx_status",80,"reading"]"
0

5、导入之前做好的nginx监控模板

监控模板可以到zabbix官网上和第三方网站上下载,上面有很多的模板可以下载,不过有些模板下载下来后需要修改一下,修改完后就可以导入模板进行使用了。 image.png image.png image.png image.png

6、关联主机的监控模板并验证数据

image.png image.png image.png

标签:status,http,nginx,--,zabbix,Nginx,Zabbix,监控,root
From: https://blog.51cto.com/u_15105742/5916683

相关文章

  • Nginx常用命令
    查看版本号./nginx-v启动./nginx查看状态ps-ef|grepnginx停止./nginx-sstop重启./nginx-sreload ......
  • Zabbix监控Redis
      监控redis服务可以使用redis自带的客户端命令来连接redis的服务,redis-cli连接redis服务在使用info命令来查询redis状态信息,在通过脚本利用此命令并结合其他命令将数据抽......
  • Python如何动态监控跟踪文件内容?
    需求:Python如何动态监控跟踪文件内容?写个小工具模仿linux中的tail来监控文件更新的内容?解答:利用文件的指针f.seek(0,2)importtimewithopen("a.txt",mode="r......
  • nginx: [emerg] no "events" section in configuration
    添加events,如下图http{includemime.types;default_typeapplication/octet-stream;keepalive_timeout65;server{listen80......
  • CentOS下nginx版本平稳升级记录
    起因:系统漏洞扫描出高危漏洞:CVE-2019-9513/CVE-2019-9511/CVE-2019-9516,需升级nginx。下载地址:https://nginx.org/en/download.html,选择了稳定版本1.22.1。将下载......
  • 在windows下导入react项目并且打包编译后部署到nginx上
     在windows下导入react项目并且打包编译后部署到nginx上一、安装npm二、创建react项目三、安装nginx四、总结最近接手了公司的一个django项目,这是应该前后端分......
  • 数据中台预警监控功能简要设计
    需求:针对产品主要流程中一些异常情况,数据中台接收各个业务系统,其他中台系统发送的异常事件。满足触发阀值之后进行短信告警功能。 设计:1、配置监控器:......
  • nginx解决vue跨域问题
    location/epayapi{proxy_passhttp://127.0.0.1:7011;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX......
  • centos 7.6 部署spring自动化运维环境一nginx安装
    首先安装所需的环境一.安装PCREpcre-devel#####yuminstall-ypcrepcre-devel二.安装gcc#####yum-yinstallgccgcc-c++kernel-devel三.安装zlib#####yumins......
  • Kubernetes(K8S) 监控 Prometheus + Grafana
    监控指标集群监控节点资源利用率节点数运行PodsPod监控容器指标应用程序Prometheus开源的监控、报警、数据库以HTTP协议周期性抓取被监控组件状态不需要......