zabbix监控nginx

用zabbix监控nginx

实验环境

服务端客户端
ip192.168.11.136192.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上配置
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
全选然后创建图像
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值