# cd /usr/local/src # wget http://nginx.org/download/nginx-1.16.1.tar.gz # tar xvf nginx-1.16.1.tar.gz # cd nginx-1.16.1/ # ./configure --prefix=/apps/nginx --with-http_stub_status_module --without-http_rewrite_module # make -j 4 && make install
# vim /apps/nginx/conf/nginx.conf ##添加location location /nginx_status { stub_status; allow 10.0.0.0/16 allow 127.0.0.1; deny all; }
# /appt/nginx/sbin/ningx -t ##检查格式无误,启动 nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok nginx: configuration file /apps/nginx/conf/nginx.conf test is successful # /appt/nginx/sbin/ningx -s reload nginx: [error] invalid PID number "" in "/apps/nginx/logs/nginx.pid" ##出来些许差错,查看目录/apps/nginx/logs/发现nginx.pid存在的,看了些pid文件本身出现语法错误了,只能将其删除删掉了 # rm -f /apps/nginx/logs/nginx.pid ##重新读取一下配置文件 # /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf # /appt/nginx/sbin/ningx -s reload ##顺利启动成功
# curl http://127.0.0.1/nginx_status Active connections: 1 server accepts handled requests 3 3 4 Reading: 0 Writing: 1 Waiting: 0
参考文章:https://www.cnblogs.com/hzgy/p/15890879.html
##安装agent # wget https://repo.zabbix.com/zabbix/4.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.0-3+bionic_all.deb # sudo dpkg -i zabbix-release_4.0-3+bionic_all.deb # sudo apt update # sudo apt install zabbix-agent -y ##配置agent #vim /etc/zabbix/zabbix_agentd.conf Server=10.10.0.101 #指向当前zabbix server ListenPort=10050 #监听端口 ListenIP=0.0.0.0 startAgents=5 #被动状态时默认启动的实例数(进程数),为0不监听任何端口 Hostname=10.0.0.106 ##启动agent # systemctl start zabbix-agent
# cat nginx_status.sh nginx_status_fun(){ #函数内容 NGINX_PORT=$1 #端口,函数的第一个参数是脚本的第二个参数,即脚本的第二个参数是段端口号 NGINX_COMMAND=$2 #命令,函数的第二个参数是脚本的第三个参数,即脚本的第三个参数是命令 nginx_active(){ #获取nginx_active数量,以下相同,这是开启了nginx状态但是只能从本机看到 /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}' } nginx_reading(){ #获取nginx_reading状态的数量 /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}' } nginx_writing(){ /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}' } nginx_waiting(){ /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}' } nginx_accepts(){ /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}' } nginx_handled(){ /usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}' } nginx_requests(){ /usr/bin/curl "http://127.0.0.1:"$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就调用nginx_status_fun,并传递第二和第三个参数 nginx_status_fun $2 $3; ;; *) #其他的输入打印帮助信息 echo $"Usage: $0 {nginx_status key}" esac #分支结束符 } main $1 $2 $3 ##将脚本放到自定义目录下 # mv nginx_status.sh /etc/zabbix/zabbix_agentd.d/ # chmod a+x /etc/zabbix/zabbix_agentd.d/nginx_status.sh
# vim /etc/zabbix/zabbix_agentd.conf UserParameter=nginx_status[*],/etc/zabbix/zabbix_agentd.d/nginx_status.sh "$1" "$2" "$3" # systemctl restart zabbix-agent
# apt install zabbix-get # zabbix_get -s 10.0.0.106 -p 10050 -k "nginx_status["nginx_status",80,"active"]" 1
最终如下:
当前已经创建好的模板如下
关联至主机如下: