zoukankan      html  css  js  c++  java
  • zabbix系列(四)Zabbix3.0.4添加对Nginx服务的监控

    Zabbix3.0.4添加对Nginx服务的监控


    通过Nginx的http_stub_status_module模块提供的状态信息来监控,所以在Agent端需要配置Nginx状态获取的脚本,和添加key信息等,然后在Server端配置Nginx的监控模板等。请根据自己情况调整,这里只做简单的参照。


    主要是使用Github这个项目的代码 zabbix-templates


    zabbix-server端:192.168.3.108
    系统是 centos7.2 zabbix-server是3.0.4版本


    Agent端:192.168.386
    系统是Centos6.x, Zabbix-agent是3.0版本, Nginx1.11.3 官方最新版本


    1.检查Nginx是否安装了 http_stub_status_module 模块,通过下面的命令可以看到编译参数。


    nginx -V 
    nginx version: yaya
    built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) 
    built with OpenSSL 1.0.1e-fips 11 Feb 2013
    TLS SNI support enabled
    configure arguments: --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --with-debug --pid-path=/var/run/nginx/nginx.pid --with-pcre --with-http_gzip_static_module --with-http_ssl_module --with-http_realip_module --with-http_geoip_module --add-module=../nginx_upstream_check_module-master --add-module=../ngx_cache_purge-2.3 --add-module=../ngx_devel_kit-master/ --add-module=../lua-nginx-module-master/ --with-http_stub_status_module


    如果没有这个模块,还需要重新编译Nginx.


    2.配置Nginx


    Nginx 80端口的server配置增加如下的片段
    /etc/nginx/nginx.conf
    server{
    listen       *:80 default_server;
    location /nginx_status {
                stub_status on;
                access_log off;
                allow 127.0.0.1;
                allow 192.168.3.108;  # zabbix 服务端IP
                deny all;
            }
    }

    配置完成之后,redload nginx,然后用简单测试下


    curl http://127.0.0.1/nginx_status
    Active connections: 2 
    server accepts handled requests
     528924 528924 528953 
    Reading: 0 Writing: 1 Waiting: 1


    3.zabbix-agent 配置


    有3个步骤,首先是编写获取Nginx信息脚本,接着配置中增加key信息,然后重启agent 服务。


    ①编写Nginx监控脚本,记住路径,后面配置需要用到,注意脚本权限问题,agent运行用户要能执行。
    mkdir -p /usr/local/zabbix-agent/scripts
    cd /usr/local/zabbix-agent/scripts


    vim nginx-check.sh


    cat 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="http://127.0.0.1/nginx_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


    赋予脚本执行权限
    chmod o+x /usr/local/zabbix-agent/scripts/nginx-check.sh


    ②agent的配置文件 /etc/zabbix/zabbix_agentd.conf 中定义了其他key的包含目录 Include=/etc/zabbix/zabbix_agentd.d/, 如果没有这个配置请自己添加下。接着在 /etc/zabbix/zabbix_agentd.d/ 目录新建一个文件 nginx-params.conf, 内容如下
    cat /etc/zabbix/zabbix_agentd.d/nginx-params.conf


    UserParameter=nginx[*],/usr/local/zabbix-agent/scripts/nginx-check.sh "$1" 


    ③重启agent
    service zabbix-agent restart


    Server 的Web端


    首先命令行测试下刚才agent好使不,确认好用之后在web端导入模板,之后就可以给对应主机添加监控喽。


    zabbix_get -s 192.168.3.86 -p 10050 -k "nginx[reading]"
    0

    登录Zabbix3.0 的web界面,一次选择 Configuration > Templates , 在主界面的右上角有个 Import 按钮,用来导入模板。



    模板文件比较长留一个下载地址https://github.com/jizhang/zabbix-templates/blob/master/nginx/nginx-template.xml

    导入之后就可以给主机添加监控啦。


    为了能快速出图,可以配合压力测试
    ab -c 10 -n 100000 http://192.168.3.86:80/


  • 相关阅读:
    我很喜欢玩游戏,那么我就适合做游戏程序员吗?
    宁可多花1000元租房,也绝不要去挤半小时地铁
    996 盛行的年代,互联网人如何平衡工作和生活 ?
    互联网公司里都有哪些潜规则?
    那些拼命加班的程序员们,后来都怎么样了?
    MongoDB更需要好的模式设计 及 案例赏析
    MongoDB 提升性能的18原则(开发设计阶段)
    关于MongoDB数据库的日志解析
    实现MongoDB读写分离的“读偏好”介绍
    MongoDB分片 在部署和维护管理 中常见事项的总结
  • 原文地址:https://www.cnblogs.com/reblue520/p/6239747.html
Copyright © 2011-2022 走看看