mkdir /etc/zabbix/scripts
vim /etc/zabbix/scripts/nginx-check.sh
#!/bin/bash
#Zabbix requested parameter
ZBX_REQ_DATA=“$1”
ZBX_REQ_DATA_URL=“$2”
#Nginx defaults
#设置Nginx监控URL
NGINX_STATUS_DEFAULT_URL=“http://localhost:10080/nginx_status”
#定义监控工具
WGET_BIN=“/usr/bin/wget”
#设置错误返回值
ERROR_NO_ACCESS_FILE=“-0.9900”
ERROR_NO_ACCESS=“-0.9901”
ERROR_WRONG_PARAM=“-0.9902”
#either can not connect /bad host /bad port
ERROR_DATA=“-0.9903”
#如果没有设定URL则取默认值
if [ ! -z “
Z
B
X
R
E
Q
D
A
T
A
U
R
L
"
]
;
t
h
e
n
U
R
L
=
"
ZBX_REQ_DATA_URL" ];then URL="
ZBXR?EQD?ATAU?RL"];thenURL="ZBX_REQ_DATA_URL”
else
URL=“$NGINX_STATUS_DEFAULT_URL”
fi
#将nginx状态数据存入变量中
NGINX_STATS=
(
(
(WGET_BIN -q $URL -O - 2>/dev/null)
#处理访问过程中的错误
if [
?
?
n
e
0
?
o
?
z
"
? -ne 0 -o -z "
??ne0?o?z"NGINX_STATS" ]; then
echo $ERROR_DATA
exit 1
fi
#获取nginx状态数据中的特定数值(即zabbix模板中添加的监控项)
case
Z
B
X
R
E
Q
D
A
T
A
i
n
a
c
t
i
v
e
c
o
n
n
e
c
t
i
o
n
s
)
e
c
h
o
"
ZBX_REQ_DATA in active_connections) echo "
ZBXR?EQD?ATAinactivec?onnections)echo"NGINX_STATS" | head -1 | cut -f3 -d’ ’
;;
accepted_connections)
echo “KaTeX parse error: Double superscript at position 50: … | cut -f2 -d' '? ;; handled_c…NGINX_STATS” | grep -Ev ‘[a-zA-Z]’ | cut -f3 -d’ ’
;;
handled_requests)
echo “KaTeX parse error: Double superscript at position 50: … | cut -f4 -d' '? ;; reading) …NGINX_STATS” | tail -1 | cut -f2 -d’ ’
;;
writing)
echo “KaTeX parse error: Double superscript at position 38: … | cut -f4 -d' '? ;; waiting) …NGINX_STATS” | tail -1 | cut -f6 -d’ ’
;;
*)
echo $ERROR_WRONG_PARAM;
exit 1
;;
esac
exit 0