From a77848fc66d04e47d24d29f45e39df68a724db08 Mon Sep 17 00:00:00 2001 From: netkiller Date: Tue, 22 Nov 2016 16:43:22 +0800 Subject: [PATCH] Zabbix nginx monitor --- nginx/README.md | 125 ++++++++ nginx/nginx.sh | 68 +++++ nginx/userparameter_nginx.conf | 1 + nginx/zbx_export_templates.xml | 523 +++++++++++++++++++++++++++++++++ 4 files changed, 717 insertions(+) create mode 100644 nginx/README.md create mode 100644 nginx/nginx.sh create mode 100644 nginx/userparameter_nginx.conf create mode 100644 nginx/zbx_export_templates.xml diff --git a/nginx/README.md b/nginx/README.md new file mode 100644 index 0000000..4b52816 --- /dev/null +++ b/nginx/README.md @@ -0,0 +1,125 @@ +Nginx status +===== + +nginx +----- + + server { + listen 80; + server_name localhost; + + location /status { + stub_status on; + access_log off; + allow 127.0.0.1; + deny all; + } + } + +zabbix_agentd +----- + cat /etc/zabbix/zabbix_agentd.d/userparameter_nginx.conf + + UserParameter=nginx.status[*],/srv/zabbix/libexec/nginx.sh $1 + +scripts +----- + + mkdir -p /srv/zabbix/libexec + vim /srv/zabbix/libexec/nginx.sh + + ```bash + + #!/bin/bash + ################################################## + # AUTHOR: Neo + # WEBSITE: http://www.netkiller.cn + # Description:zabbix 通过 status 模块监控 nginx + # Note:Zabbix 3.2 + # DateTime: 2016-11-22 + ################################################## + + HOST="localhost" + PORT="80" + + function ping { + /sbin/pidof nginx | wc -l + } + + function active() { + /usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Active' | awk '{print $NF}' + } + function accepts() { + /usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk '{print $1}' + } + function handled() { + /usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk '{print $2}' + } + function requests() { + /usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk '{print $3}' + } + function reading() { + /usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Reading' | awk '{print $2}' + } + function writing() { + /usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Writing' | awk '{print $4}' + } + function waiting() { + /usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}' + } + + case "$1" in + active) + active + ;; + accepts) + accepts + ;; + handled) + handled + ;; + requests) + requests + ;; + reading) + reading + ;; + writing) + writing + ;; + waiting) + waiting + ;; + + *) + echo $"Usage $0 {active|accepts|handled|requests|reading|writing|waiting}" + exit + esac + + ``` + +### Test scripts + + chmod +x /srv/zabbix/libexec/nginx.sh + + # /srv/zabbix/libexec/nginx.sh + Usage /srv/zabbix/libexec/nginx.sh {active|accepts|handled|requests|reading|writing|waiting} + # /srv/zabbix/libexec/nginx.sh accepts + 82 + + # systemctl restart zabbix-agent.service + +### Test Agent + + # yum install -y zabbix-get + + # zabbix_get -s -k 'nginx.status[accepts]' + 109 + +Import Template +----- + Import file: choice xml file + click "import" button + + Imported successfully + \ No newline at end of file diff --git a/nginx/nginx.sh b/nginx/nginx.sh new file mode 100644 index 0000000..e17cbd0 --- /dev/null +++ b/nginx/nginx.sh @@ -0,0 +1,68 @@ +#!/bin/bash +################################################## +# AUTHOR: Neo +# WEBSITE: http://www.netkiller.cn +# Descriptionzabbix ͨ status ģ nginx +# NoteZabbix 3.2 +# DateTime: 2016-11-22 +################################################## + +HOST="localhost" +PORT="80" + +function check { + /sbin/pidof nginx | wc -l +} + +function active() { + /usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Active' | awk '{print $NF}' +} +function accepts() { + /usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk '{print $1}' +} +function handled() { + /usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk '{print $2}' +} +function requests() { + /usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk '{print $3}' +} +function reading() { + /usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Reading' | awk '{print $2}' +} +function writing() { + /usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Writing' | awk '{print $4}' +} +function waiting() { + /usr/bin/curl -s "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}' +} + +case "$1" in + check) + check + ;; + active) + active + ;; + accepts) + accepts + ;; + handled) + handled + ;; + requests) + requests + ;; + reading) + reading + ;; + writing) + writing + ;; + waiting) + waiting + ;; + + *) + echo $"Usage $0 {check|active|accepts|handled|requests|reading|writing|waiting}" + exit +esac \ No newline at end of file diff --git a/nginx/userparameter_nginx.conf b/nginx/userparameter_nginx.conf new file mode 100644 index 0000000..f5902f2 --- /dev/null +++ b/nginx/userparameter_nginx.conf @@ -0,0 +1 @@ +UserParameter=nginx.status[*],/srv/zabbix/scripts/nginx.sh $1 \ No newline at end of file diff --git a/nginx/zbx_export_templates.xml b/nginx/zbx_export_templates.xml new file mode 100644 index 0000000..10c2619 --- /dev/null +++ b/nginx/zbx_export_templates.xml @@ -0,0 +1,523 @@ + + + 3.2 + 2016-11-22T16:30:00Z + + + Templates + + + + + + + + {Template App NGINX:nginx.status[check].last()}=0 + nginx was down! + + 0 + 4 + Nginx process count: 0 + 0 + + 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] + + + + + +