nginx状态监控
IP | 主机 |
---|
192.168.218.132 | lnmp架构、zabbix_server | 192.168.218.133 | nginx、zabbix_agent |
zabbix_agent端部署nginx
//安装依赖环境
[root@agent ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@agent ~]# yum -y groups mark install 'Development Tools'
//创建系统用户nginx
[root@agent ~]# useradd -r -M -s /sbin/nologin nginx
//创建日志存放目录
[root@agent ~]# mkdir -p /var/log/nginx
[root@agent ~]# chown -R nginx.nginx /var/log/nginx
//下载nginx
[root@agent src]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
//编译安装
[root@agent nginx-1.20.1]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@agent nginx-1.20.1]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//配置环境变量
[root@agent ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@agent ~]# source /etc/profile.d/nginx.sh
[root@agent ~]# which nginx
/usr/local/nginx/sbin/nginx
//设置开机自启
[root@agent ~]# vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx start
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP
[Install]
WantedBy=multi-user.target
[root@agent ~]# systemctl daemon-reload
//启动并开机自启nginx
[root@agent ~]# systemctl enable --now nginx
[root@agent ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
//开启状态页面
[root@agent ~]# vim /usr/local/nginx/conf/nginx.conf
......
location /status {
stub_status;
}
......
[root@agent ~]# nginx -s reload
zabbix_agent配置
//编写脚本
[root@agent scripts]# pwd
/scripts
//监控waiting值
[root@agent scripts]# vim check_wait.sh
#!/bin/bash
waiting=$(curl -s http://192.168.218.133/status|awk 'NR==4{print $6}')
if [ $waiting -gt 40 ];then
echo "1"
else
echo "0"
fi
//监控reading值
[root@agent scripts]# vim check_read.sh
#!/bin/bash
reading=$(curl -s http://192.168.218.133/status | awk 'NR==4{print $2}')
if [ $reading -gt 80 ];then
echo "1"
else
echo "0"
fi
//监控wrinting值
[root@agent scripts]# vim check_write.sh
#!/bin/bash
writing=$(curl -s http://192.168.218.133/status | awk 'NR==4{print $4}')
if [ $writing -gt 40 ];then
echo "1"
else
echo "0"
fi
[root@agent scripts]# chmod +x check_wait.sh check_read.sh check_write.sh
[root@agent etc]# pwd
/usr/local/etc
[root@agent etc]# vim zabbix_agentd.conf
......
UnsafeUserParameters=1
......
//在结尾添加以下三行
UserParameter=check_read,/scripts/check_read.sh
UserParameter=check_write,/scripts/check_write.sh
UserParameter=check_wait,/scripts/check_wait.sh
[root@agent ~]# pkill zabbix
[root@agent ~]# zabbix_agentd
[root@server ~]# zabbix_get -s 192.168.218.133 -k check_read
0
[root@server ~]# zabbix_get -s 192.168.218.133 -k check_write
0
[root@server ~]# zabbix_get -s 192.168.218.133 -k check_wait
0
zabbix页面部署
- 添加监控项
Reading监控项 Writing监控项 Waiting监控项 - 添加触发器
添加Reading触发器 添加Writing触发器 添加Waiting触发器 - 查看实时是否正常
- 测试
//修改脚本check_wait.sh中的比较值,主动触发报警
[root@agent scripts]# vim check_wait.sh
#!/bin/bash
waiting=$(curl -s http://192.168.218.133/status|awk 'NR==4{print $6}')
if [ $waiting -gt 0 ];then //改为0,主动触发
echo "1"
else
echo "0"
fi
//修改完成以后无需重启服务就会立即报警
优化脚本
- 合并脚本
以上自定义监控nginx状态时,写了三个脚本,虽然功能可以实现,但是编辑配置文件时需要写三条UserParameter,如果数量太大就相对来讲比较麻烦,以下我们把三个脚本合并到一个脚本中
[root@agent ~]# cat /scripts/check_nginx_status.sh
#!/bin/bash
case $1 in
'Reading')
if [ `curl -s http://192.168.218.133/status | awk 'NR==4{print $2}'` -gt 80 ];then
echo "1"
else
echo "0"
fi
;;
'Writing')
if [ `curl -s http://192.168.218.133/status | awk 'NR==4{print $4}'` -gt 40 ];then
echo "1"
else
echo "0"
fi
;;
'Waiting')
if [ `curl -s http://192.168.218.133/status|awk 'NR==4{print $6}'` -gt 40 ];then
echo "1"
else
echo "0"
fi
;;
esac
[root@agent etc]# pwd
/usr/local/etc
[root@agent etc]# vim zabbix_agentd.conf
......
UnsafeUserParameters=1
......
//在结尾添加以下内容
UserParameter=check_nginx_status[*],/scripts/check_nginx_status.sh $1
|