zabbixを利用して、複数のnginxのアクティブコネクション数を監視する方法
1.システム要件:
lnmp環境 3台nginx
2.nginx serverの設定ファイルにstatusモジュールを配置
location /ngst{
stub_status on;
access_log off;
allow 127.0.0.1;
allow xxx.xxx.xxx.xxx;
deny all;
}
3.nginx-statusの値を取得するスクリプト
#!/bin/bash
#HOST=`ifconfig eth0 | sed -n '/inet /{s/.*addr://;s/ .*//;p}’`
HOST="192.168.103.2″
PORT="8898″
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
4.nginx-statusのテスト
/usr/bin/curl “http://192.168.103.2:8898/nginx-status/"
Active connections: 2
server accepts handled requests
1968 1968 3907
Reading: 0 Writing: 1 Waiting: 1
5.「zabbix_agentd.conf」の設定を変更
/etc/zabbix/zabbix_agentd.conf
UserParameter=nginx.accepts,/home/zabbix/nginx-status.sh accepts
UserParameter=nginx.handled,/home/zabbix/nginx-status.sh handled
UserParameter=nginx.requests,/home/zabbix/nginx-status.sh requests
UserParameter=nginx.connections.active,/home/zabbix/nginx-status.sh active
UserParameter=nginx.connections.reading,/home/zabbix/nginx-status.sh reading
UserParameter=nginx.connections.writing,/home/zabbix/nginx-status.sh writing
UserParameter=nginx.connections.waiting,/home/zabbix/nginx-status.sh waiting