Zabbix监控Nginxstatus

2023年 7月 15日 77.7k 0

1,编译安装nginx

wget http://nginx.org/download/nginx-1.9.15.tar.gz
yum -y install pcre-devel pcre openssl openssl-devel
groupadd -r nginx
useradd -g nginx -r nginx

tar xf http://nginx.org/download/nginx-1.9.15.tar.gz
ln -sv nginx-1.9.15 nginx
cd nginx 
./configure 
--prefix=/usr/local/nginx 
--conf-path=/etc/nginx/nginx.conf 
--user=nginx --group=nginx 
--error-log-path=/var/log/nginx/error.log 
--http-log-path=/var/log/nginx/access.log 
--pid-path=/var/run/nginx/nginx.pid 
--lock-path=/var/lock/nginx.lock 
--with-http_ssl_module 
--with-http_stub_status_module 
--with-http_gzip_static_module 
--with-http_flv_module 
--with-http_mp4_module 
--http-client-body-temp-path=/var/tmp/nginx/client 
--http-proxy-temp-path=/var/tmp/nginx/proxy 
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi 
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
make && make install
mkdir -pv /var/tmp/nginx/{client,fastcgi,proxy,uwsgi}

配置文件:

[root@mysql-master nginx]# cat /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;
worker_rlimit_nofile 51200;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    sendfile        on;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    '$host "$request_uri" $status'
    '"$http_referer" "$http_user_agent"';
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htmapplication/xml;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
include /etc/nginx/conf.d/status.conf;
}

status状态

[root@mysql-master nginx]# mkdir /etc/nginx/conf.d/
[root@mysql-master nginx]# cat /etc/nginx/conf.d/status.conf 
server {
        listen          81;
    server_name localhost;
    location /nginx_status {
    stub_status on;
#    auth_basic "Only for VIPs";
#    auth_basic_user_file /etc/nginx/users/.htpasswd;
    access_log off;
#    allow 127.0.0.0/24;
#    allow 192.168.0.0/24;
#    deny all;
    }
    }
[root@mysql-master nginx]# 

nginx启动脚本

[root@mysql-master nginx]# cat /etc/init.d/nginx 
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/etc/nginx/nginx.conf"
NGINX_PID="/var/run/nginx/nginx.pid"
RETVAL=0
prog="Nginx"

start() {
        echo -n $"Starting $prog: "
        mkdir -p /dev/shm/nginx_temp
        daemon $NGINX_SBIN -c $NGINX_CONF
        RETVAL=$?
        echo
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -TERM
        rm -rf /dev/shm/nginx_temp
        RETVAL=$?
        echo
        return $RETVAL
}

reload(){
        echo -n $"Reloading $prog: "
        killproc -p $NGINX_PID $NGINX_SBIN -HUP
        RETVAL=$?
        echo
        return $RETVAL
}

restart(){
        stop
        start
}

configtest(){
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac

exit $RETVAL
[root@mysql-master nginx]# /etc/init.d/nginx restart
Stopping Nginx:                                            [  OK  ]
Starting Nginx:                                            [  OK  ]
[root@mysql-master nginx]# ss -tlnp|grep nginx
LISTEN     0      128                       *:80                       *:*      users:(("nginx",74903,6),("nginx",74904,6))
LISTEN     0      128                       *:81                       *:*      users:(("nginx",74903,7),("nginx",74904,7))
[root@mysql-master nginx]# mkdir /etc/zabbix/scripts/

测试:

[root@mysql-master local]# curl 192.168.158.130:81/nginx_status
Active connections: 1 
server accepts handled requests
 9 9 19 
Reading: 0 Writing: 1 Waiting: 0 
[root@mysql-master local]# 

nginx获取key脚本

[root@mysql-master nginx]# mkdir /etc/zabbix/scripts/
[root@mysql-master zabbix_agentd.d]# vim /etc/zabbix/scripts/nginx-status.sh
#!/bin/bash
dBKUP_DATE=`date +%Y%m%d`
LOG="/var/log/zabbix/nginx-status.log"
HOST=`ifconfig eth0 | sed -n '/inet /{s/.*addr://;s/ .*//;p}'`
PORT="80"
function active {
 /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null|
     grep 'Active' | awk '{print $NF}'
        }
function reading {
 /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| 
     grep 'Reading' | awk '{print $2}'
       }
function writing {
 /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| 
      grep 'Writing' | awk '{print $4}'
       }
function waiting {
  /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| 
       grep 'Waiting' | awk '{print $6}'
       }
function accepts {
  /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| 
       awk NR==3 | awk '{print $1}'
       }
function handled {
 /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| 
       awk NR==3 | awk '{print $2}'
       }
function requests {
  /usr/bin/curl "http://$HOST:$PORT/nginx_status" 2>/dev/null| 
       awk NR==3 | awk '{print $3}'
       }
# Run the requested function
$1

chmod +x nginx-status.sh 

添加到/etc/zabbix/zabbix_agentd.d/nginx-status.conf

vim /etc/zabbix/zabbix_agentd.d/nginx-status.conf
UserParameter=nginx.accepts,/etc/zabbix/scripts/nginx-status.sh accepts
UserParameter=nginx.handled,/etc/zabbix/scripts/nginx-status.sh handled
UserParameter=nginx.requests,/etc/zabbix/scripts/nginx-status.sh requests
UserParameter=nginx.connections.active,/etc/zabbix/scripts/nginx-status.sh active
UserParameter=nginx.connections.reading,/etc/zabbix/scripts/nginx-status.sh reading
UserParameter=nginx.connections.writing,/etc/scripts/nginx-status.sh writing
UserParameter=nginx.connections.waiting,/etc/zabbix/scripts/nginx-status.sh waiting

nginx-reading.pngnginx-server.png

模板下载密码:uwmd

相关文章

对接alertmanager创建钉钉卡片(1)
手把手教你搭建OpenFalcon监控系统
无需任何魔法即可使用 Ansible 的神奇变量“hostvars”
openobseve HA本地单集群模式
基于k8s上loggie/vector/openobserve日志收集
openobseve单节点和查询语法

发布评论