Linux下Zabbix监控之Nginx监控添加

nginx_status配置

  • conf配置开启nginx status监控
#由于使用的是vhost虚拟主机,故新建一个conf文件
server {
    listen  80;
    server name localhost;

    location /ngx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }
  • 重启nginx
systemctl restart nginx.service
  • 测试status
curl http://127.0.0.1/ngx_status
Active connections: 1
server accepts handled requests request_time
 22884 22884 11890 71698
Reading: 0 Writing: 1 Waiting: 0
  • 数据介绍
Active connections:1    #活跃的连接数量 
server accepts handled requests request_time  #总共处理了22884个请求,成功建立22884次握手,总共处理11890个请求;request_time指的就是从接受用户请求的第一个字节到发送完响应数据的时间,即包括接收请求数据时间、程序响应时间、输出响应数据时间。
            22884    22884     11890     71698
reading  #读取客户端的连接数
writing  #响应数据到客户端的数量
waiting  #开启 keep-alive 的情况下,这个值等于active-(reading+writing),意思就是Nginx已经处理完正在等候下一次请求指令的驻留连接

zabbix-agent添加nginx监控

  • zabbix-agent上nginx数据获取脚本
#!/bin/bash
HOST="127.0.0.1"
PORT="80"
KEYWORD="ngx_status"
URL="http://$HOST:$PORT/$KEYWORD/"
 
#参数是否正确
if [ $# -ne "1" ];then
    echo "arg error!"
fi

#获取数据
case $1 in
    active)
        result=`/usr/bin/curl $URL 2>/dev/null| grep 'Active' | awk '{print $NF}'`
        echo $result
        ;;      
    reading)
        result=`/usr/bin/curl $URL 2>/dev/null| grep 'Reading' | awk '{print $2}'`
        echo $result
        ;;      
    writing)
        result=`/usr/bin/curl $URL 2>/dev/null| grep 'Writing' | awk '{print $4}'`
        echo $result
        ;;      
    waiting)
        result=`/usr/bin/curl $URL 2>/dev/null| grep 'Waiting' | awk '{print $6}'`
        echo $result
        ;;      
    accepts)
        result=`/usr/bin/curl $URL 2>/dev/null| awk NR==3 | awk '{print $1}'`
        echo $result
        ;;      
    handled)
        result=`/usr/bin/curl $URL 2>/dev/null| awk NR==3 | awk '{print $2}'`
        echo $result
        ;;      
    requests)
        result=`/usr/bin/curl $URL 2>/dev/null| awk NR==3 | awk '{print $3}'`
        echo $result
        ;;      
    ping)
        result=`/sbin/pidof nginx | wc -l`
        echo $result
        ;;
    request_time)
        result=`/usr/bin/curl $URL 2> /dev/null|awk NR==3|awk '{print $4}'`
        echo $result
        ;;
    *)  
    echo "Usage:$0(ping|active|reading|writing|waiting|accepts|handled|requests|request_time)"  
    ;;
esac
  • userparameter_nginx.conf配置
vim /etc/zabbix/zabbix_agentd.d/userparameter_nginx.conf
UserParameter=nginx.status[*],/etc/zabbix/scripts/ngx_status.sh $1
  • 重启zabbix-agent服务
systemctl restart zabbix-agent.service

nginx监控模板



    3.4
    2018-06-25T06:12:07Z
    
        
            Templates
        
    
    
        
    
    
        
            {Template App NGINX:nginx.status[ping].last()}=0
            0
            
            nginx was down!
            0
            
            
            0
            4
            NGINX进程数:0,请注意
            0
            0
            
            
        
    
    
        
            nginx status connections
            900
            200
            0.0000
            100.0000
            1
            1
            0
            1
            0
            0.0000
            0.0000
            0
            0
            0
            0
            
                
                    0
                    0
                    00C800
                    0
                    2
                    0
                    
                        Template App NGINX
                        nginx.status[active]
                    
                
                
                    1
                    0
                    C80000
                    0
                    2
                    0
                    
                        Template App NGINX
                        nginx.status[reading]
                    
                
                
                    2
                    0
                    0000C8
                    0
                    2
                    0
                    
                        Template App NGINX
                        nginx.status[waiting]
                    
                
                
                    3
                    0
                    C800C8
                    0
                    2
                    0
                    
                        Template App NGINX
                        nginx.status[writing]
                    
                
            
        
        
            nginx status server
            900
            200
            0.0000
            100.0000
            1
            1
            0
            1
            0
            0.0000
            0.0000
            0
            0
            0
            0
            
                
                    0
                    0
                    00C800
                    0
                    2
                    0
                    
                        Template App NGINX
                        nginx.status[accepts]
                    
                
                
                    1
                    0
                    C80000
                    0
                    2
                    0
                    
                        Template App NGINX
                        nginx.status[handled]
                    
                
                
                    2
                    0
                    0000C8
                    0
                    2
                    0
                    
                        Template App NGINX
                        nginx.status[requests]
                    
                
                
                    3
                    0
                    A54F10
                    0
                    2
                    0
                    
                        Template App NGINX
                        nginx.status[request_time]
                    
                
            
        
    
    
        
            Service state
            
                
                    0
                    Down
                
                
                    1
                    Up
                
            
        
    


你可能感兴趣的:(Linux下Zabbix监控之Nginx监控添加)