用zabbix监控nginx
实验环境
服务端 | 客户端 | |
---|---|---|
ip | 192.168.11.136 | 192.168.11.120 |
在客户端部署nginx
创建nginx系统用户
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
安装依赖环境
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget make
[root@localhost ~]# yum -y groups mark install 'Development Tools'
创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
下载nginx
[root@localhost ~]# wget https://nginx.org/download/nginx-1.20.1.tar.gz
编译安装
[root@localhost ~]# tar xf nginx-1.20.1.tar.gz
[root@localhost ~]# cd nginx-1.20.1/
[root@localhost 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@localhost nginx-1.20.1]# make && make install
配置环境变量
[root@localhost nginx-1.20.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost nginx-1.20.1]# . /etc/profile.d/nginx.sh
启动
[root@localhost ~]# nginx
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
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 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# systemctl restart firewalld.service
[root@localhost ~]# setenforce 0
配置nginx
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow 192.168.11.1;
deny all;
}
[root@localhost ~]# nginx -s reload
编写脚本
[root@localhost ~]# mkdir /scripts
[root@localhost ~]# vim /scripts/check_nginx.sh
#!/bin/bash
#Script to fetch nginx statuses for monitoring systems
HOST="127.0.0.1"
PORT="80"
function ping {
/sbin/pidof nginx | wc -l
}
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}'
}
$1
[root@localhost ~]# chmod +x /scripts/check_nginx.sh
zabbix-agent配置文件开启自定义功能
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1 #取消注释并改为1
在最后的空白处添加
UserParameter=check_nginx[*],/bin/bash /scripts/check_nginx.sh $1
[root@localhost ~]# pkill zabbix
[root@localhost ~]# zabbix_agentd
测试server端是否可以收到数据
[root@localhost etc]# zabbix_get -s 192.168.11.120 -p 10050 -k check_nginx[accepts]
9
[root@localhost etc]# zabbix_get -s 192.168.11.120 -k check_nginx[active]
在web上配置
全选然后创建图像