编写脚本实现Zabbix监控Nginx状态
1-配置nginx# cat ../nginx.conf
worker_processes1;
events {
worker_connections1024;
}
http {
include mime.types;
default_typeapplication/octet-stream;
sendfile on;
keepalive_timeout65;
log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
include extra/status.conf;
}
# cat status.conf
server {
listen 80;
server_namestatus.etiantian.org;
location / {
stub_status on;
access_logoff;
}
}
# ../../sbin/nginx -t #→检查配置文件语法
# ../../sbin/nginx -s reload #→重新加载nginx配置生效 2-编写脚本
# cat /server/scripts/nginx_status.sh
#!/bin/bash
. /etc/init.d/functions
. /etc/profile
# Script to fetch nginx statuses for tribily monitoring systems
# Author: yongzhen
# Email: 1041282946@qq.com
# Date: 2015-11-22
# Version 1.0
# Set Variables
BKUP_DATE=`/bin/date +%Y%m%d`
LOG="/data/log/zabbix/webstatus.log"
HOST="status.etiantian.org"
PORT="80"
# Functions to return nginx stats
function active {
/usr/bin/curl -s "http://$HOST:$PORT" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
/usr/bin/curl -s "http://$HOST:$PORT" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
/usr/bin/curl -s "http://$HOST:$PORT" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
/usr/bin/curl -s "http://$HOST:$PORT" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
/usr/bin/curl -s "http://$HOST:$PORT" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
/usr/bin/curl -s "http://$HOST:$PORT" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
/usr/bin/curl -s "http://$HOST:$PORT" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
# Run the requested function
$1 3-配置zabbix-agent
# cat -n /etc/zabbix/zabbix_agentd.conf
### Option: UserParameter
# User-defined parameter to monitor. There can be several user-defined parameters.
# Format: UserParameter=,
# See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
UserParameter=nginx.active,/bin/sh /server/scripts/nginx_status.sh active
UserParameter=nginx.accepts,/bin/sh /server/scripts/nginx_status.sh accepts
UserParameter=nginx.handled,/bin/sh /server/scripts/nginx_status.sh handled
UserParameter=nginx.requests,/bin/sh /server/scripts/nginx_status.sh requests
UserParameter=nginx.reading,/bin/sh /server/scripts/nginx_status.sh reading
UserParameter=nginx.writing,/bin/sh /server/scripts/nginx_status.sh writing
UserParameter=nginx.waiting,/bin/sh /server/scripts/nginx_status.sh waiting
# /etc/init.d/zabbix-agent restart#→重启zabbix-agent 4-配置zabbix-server
# zabbix_get -s 10.0.0.8 -p 10050 -k "nginx.active"
1
# zabbix_get -s 10.0.0.8 -p 10050 -k "nginx.accepts"
354
# zabbix_get -s 10.0.0.8 -p 10050 -k "nginx.handled"
357
# zabbix_get -s 10.0.0.8 -p 10050 -k "nginx.requests"
402
# zabbix_get -s 10.0.0.8 -p 10050 -k "nginx.reading"
0
# zabbix_get -s 10.0.0.8 -p 10050 -k "nginx.writing"
1
# zabbix_get -s 10.0.0.8 -p 10050 -k "nginx.waiting"
0 添加Items:
http://s4.运维网.com/wyfs02/M00/76/AB/wKioL1ZYfeHx4GDpAACSmR8xB7I412.png
http://s3.运维网.com/wyfs02/M01/76/AB/wKioL1ZYfjODRcZSAAC2vTuR3uA758.png
添加Graph:
http://s2.运维网.com/wyfs02/M02/76/AB/wKioL1ZYfnvjGBAqAACTuklcZnE606.png
http://s4.运维网.com/wyfs02/M00/76/AB/wKioL1ZYfrigf9hdAACNb0ufLfs519.png
查看nginx_status图表:
http://s1.运维网.com/wyfs02/M00/76/AC/wKiom1ZYfpGBVKWXAAKZfxFGdJY794.jpg
页:
[1]