Zabbix自动化监控——监控Nginx

3 篇文章 1 订阅
3 篇文章 0 订阅

Zabbix自动化监控——Nginx

前面我们已经把监控端和被监控端都安装了zabbix3.4,今天我们进行个实战,监控网站服务器Nginx的运行。

一、被监控端准备好Nginx

1、首先在被监控端安装Nginx服务,设置yum存储库,请创建名为/etc/yum.repos.d/nginx.repo的文件,其内容如下:

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

2、安装nginx

[root@zabbix-agent-none1 ~]#yum  -y  install nginx
[root@zabbix-agent-none1 ~]#systemctl start nginx
[root@zabbix-agent-none1 ~]#systemctl enable nginx

3、Nginx 提供了 ngx_http_stub_status_module,这个模块提供了基本的监控功能,所以我们需要检查该模块是否存在(使用yum下载会有该 模块,保险起见 ,检查一下)

[root@zabbix-agent-none1 ~]# nginx -V
nginx version: nginx/1.20.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'    //慢慢找别急,我已经找到了

4、确定有 ngx_http_stub_status_module之后,就可以放心的往下进行了,写一个nginx子配置文件。

[root@zabbix-agent-none1 ~]# vim /etc/nginx/conf.d/ngx.conf 
编写内容如下:
server {
        listen 80;
        server_name localhost;
        location /ngx_status {
                stub_status     on;
                access_log      on;
                }
}

重启nginx,重新加载nginx的配置文件

[root@zabbix-agent-none1 ~]# systemctl restart nginx

5、浏览器访问测试 http://被监控端ip/ngx_status (关闭防火墙和seLinux)

Active connections: 2 
server accepts handled requests
 10196 10196 10196 
Reading: 0 Writing: 1 Waiting: 1 

6、此时网站服务器准备完成。

二、自定义监控参数

1、在被监控端上操作

[root@zabbix-agent-none1 ~]# vim /etc/zabbix/zabbix_agentd.d/nginx_status.conf 
写入以下内容:
#监控Nginx活动连接数
UserParameter=Nginx.Active.Connections,/usr/bin/curl -s http://10.8.165.27/ngx_status 2>/dev/null |grep 'Active connections:'|awk '{print $NF}'

2、在监控端测试我们定义的参数是否可用

[root@zabbix-server ~]# zabbix_get -s 10.8.165.27 -p 10050 -k "Nginx.Active.Connections"
1        //此时取到值了,说明自定义的监控参数可用

注:zabbix_get命令是在server端用来检查agent端的一个命令,在添加完主机或者触发器后,不能正常获得数据,可以用zabbix_get来检查能否采集到数据,以便判断问题症结所在。

安装zabbix-get

[root@zabbix-server ~]#yum -y install epel-release
[root@zabbix-server ~]#yum -y install zabbix-get
三、WEB监控界面的配置

1、在监控上,创建一个监控模板,方便我们使用
在这里插入图片描述

2、设置一个item监控项,步骤:配置–>模板–>找到我们刚创建的nginx模板

在这里插入图片描述

3、点击监控项–>创建监控项

在这里插入图片描述

4、设置好键值,点击添加
在这里插入图片描述

5、将创建好的模板关联被监控端的主机。点击配置–>主机–>找到被监控端的主机

在这里插入图片描述

6、选择模版

在这里插入图片描述

7、找到我们创建的监控模板nginx_status,点击小的添加,在点大的添加即可。

在这里插入图片描述

8、来到检测中,最新数据,就可以看到我们刚添加的监控nginx的参数了。

在这里插入图片描述

9、我们将以下的参数都定义出来并监控

#监控Nginx活动连接数
UserParameter=Nginx.Active.Connections,/usr/bin/curl -s http://127.0.0.1/ngx_status 2>/dev/null |grep 'Active connections:'|awk '{print $NF}'
#监控Nginx处理连接总数
UserParameter=Nginx.Accepts.Connections,/usr/bin/curl -s http://127.0.0.1/ngx_status 2>/dev/null|sed -n '3p'|awk '{print $1}'
#监控Nginx处理连接失败数
UserParameter=Nginx.Handled.Connections,/usr/bin/curl -s http://127.0.0.1/ngx_status 2>/dev/null|sed -n '3p'|awk '{print $2}'
#监控Nginx处理请求总数
UserParameter=Nginx.requests.Connections,/usr/bin/curl -s http://127.0.0.1/ngx_status 2>/dev/null|sed -n '3p'|awk '{print $3}'
#Nginx读取到客户端的Header信息数
UserParameter=Nginx.Reading,/usr/bin/curl -s http://127.0.0.1/ngx_status 2>/dev/null|sed -n '4p'|awk '{print $2}'
#Nginx返回给客户端的Header信息数
UserParameter=Nginx.Writing,/usr/bin/curl -s http://127.0.0.1/ngx_status 2>/dev/null|sed -n '4p'|awk '{print $4}'
#Nginx处理完并等候状态的驻留连接
UserParameter=Nginx.Waiting,/usr/bin/curl -s http://127.0.0.1/ngx_status 2>/dev/null|sed -n '4p'|awk '{print $6}'

达到如下效果,并且都能监测到数据。

在这里插入图片描述

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值