zabbix ☞ nginx

阅读量: zyh 2020-05-14 09:34:44
Categories: > Tags:

确保相关目录地址如下:

/etc/zabbix/shell

/etc/zabbix/zabbix_agentd.d

/usr/local/nginx/conf/server

cat > /usr/local/nginx/conf/server/server_status.server << 'EOF'
server {
    listen       80;
    server_name  127.0.0.1;
    #charset koi8-r;
    access_log  off;
    location /server_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }
}
EOF
/usr/local/nginx/sbin/nginx -t && /usr/local/nginx/sbin/nginx -s reload

mkdir /etc/zabbix/shell -p;
cat > /etc/zabbix/shell/nginx.sh << 'EOF'
#!/bin/bash  

function active {
/usr/bin/curl "http://127.0.0.1/server_status" 2> /dev/null| grep 'Active' | awk '{print $NF}'
}

function reading {
/usr/bin/curl "http://127.0.0.1/server_status" 2> /dev/null| grep 'Reading' | awk '{print $2}'
}

function writing {
/usr/bin/curl "http://127.0.0.1/server_status" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}

function waiting {
/usr/bin/curl "http://127.0.0.1/server_status" 2> /dev/null| grep 'Waiting' | awk '{print $6}'
}

function accepts {
/usr/bin/curl "http://127.0.0.1/server_status" 2> /dev/null| awk NR==3 | awk '{print $1}'
}

function handled {
/usr/bin/curl "http://127.0.0.1/server_status" 2> /dev/null| awk NR==3 | awk '{print $2}'
}

function requests {
/usr/bin/curl "http://127.0.0.1/server_status" 2> /dev/null| awk NR==3 | awk '{print $3}'
}

function qps {
        NGINX_STATUS_URL="http://127.0.0.1/server_status"
        #若是tnginx,则最后应输出d[length(d)-1]
        requestold=`/usr/bin/curl -s ${NGINX_STATUS_URL} | /usr/bin/awk '/server accepts handled requests/{getline a;split(a,d);print d[length(d)]}'`
        TimeWait=1
        sleep $TimeWait
        requestnew=`/usr/bin/curl -s ${NGINX_STATUS_URL} | /usr/bin/awk '/server accepts handled requests/{getline a;split(a,d);print d[length(d)]}'`
        if [ $requestnew -gt 0 ];then
                QPS=`echo "( $requestnew - $requestold ) / $TimeWait" | /usr/bin/bc`
        fi
        echo $QPS
}

# Run the requested function  
$1
EOF
chmod a+x /etc/zabbix/shell/nginx.sh

cat > /etc/zabbix/zabbix_agentd.d/nginx.conf << 'EOF'
#monitor nginx  
UserParameter=nginx.accepts,/etc/zabbix/shell/nginx.sh accepts
UserParameter=nginx.handled,/etc/zabbix/shell/nginx.sh handled
UserParameter=nginx.requests,/etc/zabbix/shell/nginx.sh requests
UserParameter=nginx.connections.active,/etc/zabbix/shell/nginx.sh active
UserParameter=nginx.connections.reading,/etc/zabbix/shell/nginx.sh reading
UserParameter=nginx.connections.writing,/etc/zabbix/shell/nginx.sh writing
UserParameter=nginx.connections.waiting,/etc/zabbix/shell/nginx.sh waiting
UserParameter=nginx.connections.qps,/etc/zabbix/shell/nginx.sh qps
EOF