步骤
1:在agent端安装nginx
2:配置nginx status页面
3:agent端编写脚本监控nginx性能以及进程状态
4:修改agent端zabbix配置文件
5:server端测试
6:web界面 创建监控模板 创建应用集 创建监控项 创建图像
安装nginx
[root@agent ~]# rpm -e httpd --nodeps
[root@agent ~]# yum -y install pcre-devel zlib-devel
[root@agent ~]# useradd -M -s /sbin/nologin nginx
[root@agent ~]# tar zxvf nginx-1.12.2.tar.gz -C /usr/src/
[root@agent ~]# cd /usr/src/nginx-1.12.2/
[root@agent nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@agent nginx-1.12.2]# make &&make install
[root@agent nginx-1.12.2]# ls /usr/local/nginx/
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@agent nginx-1.12.2]# cd
优化nginx服务并配置status页面
[root@agent ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##优化命令执行路径
[root@ agent ~]# vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Server Control Script
NP="/usr/local/nginx/sbin/nginx"
NPF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$NP;
if [ $? -eq 0 ]
then
echo "nginx is starting!! "
fi
;;
stop)
kill -s QUIT $(cat $NPF)
if [ $? -eq 0 ]
then
echo "nginx is stopping!! "
fi
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $NPF)
if [ $? -eq 0 ]
then
echo "nginx config file is reload! "
fi
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@ agent ~]# chmod +x /etc/init.d/nginx
[root@ agent ~]# chkconfig --add nginx
[root@ agent ~]# chkconfig nginx on
[root@ agent ~]# vi /usr/local/nginx/conf/nginx.conf
添加location区域
location /status {
stub_status on;
allow 127.0.0.1;
# deny all;
access_log off;
}
[root@ agent ~]# /etc/init.d/nginx start
nginx is starting!!
[root@ agent ~]# netstat -utpln |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3713/nginx
agent端编写脚本监控nginx性能以及进程状态
[root@agent ~]# vi /tmp/ngx_status.sh
#! /bin/bash
#
# 检测nginx进程是否存在
function ping {
/sbin/pidof nginx | wc -l
}
# 检测nginx性能
function active {
/usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
/usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
/usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
/usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
/usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
/usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
/usr/bin/curl "http://$HOST:$PORT/status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
# 执行function
$1
[root@agent ~]# chmod +x /tmp/ngx_status.sh
[root@agent ~]# vi /usr/local/zabbix/etc/zabbix_agentd.conf
LogFile=/tmp/zabbix_agentd.log
Server=192.168.100.200
ServerActive=192.168.100.200
Hostname=agent.zabbix.com
UnsafeUserParameters=1 要使用脚本监控需要将此项开启
UserParameter=memory_userd,free -m|grep Mem|awk '{print $3}'
UserParameter=nginx.status[*],/tmp/ngx_status.sh $1 在脚本中添加定义以下各项的键值
[root@agent ~]# /usr/local/zabbix/sbin/zabbix_agentd restart
server端测试
[root@server ~]# zabbix_get -s 192.168.100.151 -k nginx.status[active]
1
[root@server ~]# zabbix_get -s 192.168.100.151 -k nginx.status[writing]
1
web界面 创建监控模板 创建应用集 创建监控项 创建图像