Zabbix监控案例(六)Nginx监控
- 编译安装nginx,监控nginx_stats页面,实现故障自治愈
- 安装zabbix-agent(下面是apt安装,如果是编译安装需要注意路径)
编译安装nginx
- 准备nginx编译包nginx-1.10.3.tar.gz,解压
tar xf nginx-1.10.3.tar.gz
cd nginx-1.10.3/
- 编译安装
安装环境
apt install gcc libpcre3-dev libpcre3 openssl libssl-dev zlib1g-dev make iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server nfs-common tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev
./configure --prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make && make install
如果出错
src/core/ngx_murmurhash.c: In function ‘ngx_murmur_hash2’:
src/core/ngx_murmurhash.c:37:11: error: this statement may fall through [-Werror=implicit-fallthrough=]
h ^= data[2] << 16;
~~^~~~~~~~~~~~~~~~
src/core/ngx_murmurhash.c:38:5: note: here
case 2:
^~~~
src/core/ngx_murmurhash.c:39:11: error: this statement may fall through [-Werror=implicit-fallthrough=]
h ^= data[1] << 8;
~~^~~~~~~~~~~~~~~
src/core/ngx_murmurhash.c:40:5: note: here
case 1:
^~~~
cc1: all warnings being treated as errors
objs/Makefile:460: recipe for target 'objs/src/core/ngx_murmurhash.o' failed
make[1]: *** [objs/src/core/ngx_murmurhash.o] Error 1
make[1]: Leaving directory '/root/nginx-1.10.3'
Makefile:8: recipe for target 'build' failed
make: *** [build] Error 2
解决:objs/Makefile文件,将第三行的-Werror去掉就可以
- 在server下加段
cd /usr/local/nginx/
vim conf/nginx.conf
location /nginx_status {
stub_status on;
}
- 启动
/usr/local/nginx/sbin/nginx
- 测试nginx状态面
开启zabbix sudu权限
vim /etc/sudoers
zabbix ALL =(ALL) NOPASSWD: ALL
nginx脚本
cd /etc/zabbix/zabbix_agentd.conf.d/
vim nginx_check.sh
#!/bin/bash
nginx_status_fun(){ #函数内容
NGINX_PORT=$1 #端口,函数的第一个参数是脚本的第二个参数,即脚本的第二个参数是段端口号
NGINX_COMMAND=$2 #命令,函数的第二个参数是脚本的第三个参数,即脚本的第三个参数是命令
nginx_active(){ #获取nginx_active数量,以下相同,这是开启了nginx状态但是只能从本机看到
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
nginx_reading(){ #获取nginx_reading状态的数量
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
nginx_writing(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
nginx_waiting(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
nginx_accepts(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
nginx_handled(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
nginx_requests(){
/usr/bin/curl "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
case $NGINX_COMMAND in
active)
nginx_active;
;;
reading)
nginx_reading;
;;
writing)
nginx_writing;
;;
waiting)
nginx_waiting;
;;
accepts)
nginx_accepts;
;;
handled)
nginx_handled;
;;
requests)
nginx_requests;
esac
}
main(){
case $1 in
nginx_status)
nginx_status_fun $2 $3;
;;
*)
echo $"Usage: $0 {nginx_status key}"
esac
}
main $1 $2 $3
- 测试下
bash nginx_check.sh nginx_status 80 accepts
- 权限
chown zabbix.zabbix nginx_check.sh
chmod +x nginx_check.sh
配置agent
vim /etc/zabbix/zabbix_agentd.conf.d/check.conf
UserParameter=nginx_check[*],/etc/zabbix/zabbix_agentd.conf.d/nginx_check.sh "$1" "$2" "$3"
- 重启服务
systemctl restart zabbix-agent
- 在server端测试
# ./zabbix_get -s 192.168.99.23 -p 10050 -k "nginx_check[nginx_status,80,accepts]"
7
将nginx添加到zabbix监控
- 创建模板
- 添加监控项
- 可以添加多个监控项
- 添加图形
- 添加触发器
- 把模板添加到主机
- 稍等一会,查看图形是否有数据
实现故障自治愈
修改配置文件支持运程命令
vim /etc/zabbix/zabbix_agentd.conf
EnableRemoteCommands=1
UnsafeUserParameters=1
重启服务
systemctl restart zabbix-agent
- 先在nginx模板中创建一个监控项来监控80端口
- 监控80端口
- 检查下有没有数据
- 创建触发器
- 创建动作
- 添加条件
- 添加新的操作,传送命令
sudo su - root -c /usr/local/nginx/sbin/nginx
- 将nginx服务停止
pkill nginx
- 检查是否报警
- 检查自治愈
- 报警关闭