zabbix监控nginx状态

Posted 码出未来_远

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zabbix监控nginx状态相关的知识,希望对你有一定的参考价值。

zabbix监控nginx状态

服务端客户端
主机名C1C2
ip192.168.207.143192.168.207.131

启动nginx

[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:22                0.0.0.0:*                  
LISTEN    0         128                0.0.0.0:80                0.0.0.0:*                  
LISTEN    0         128                   [::]:22                   [::]:*  

配置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.207.1;
            deny all;
        }
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx -s reload

image-20210627131532505

编写脚本

[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

在要监控的主机上安装agent

[root@localhost ~]# ls
anaconda-ks.cfg  zabbix-5.2.6.tar.gz
[root@localhost ~]# useradd -r -M -s /sbin/nologin zabbix
[root@localhost ~]# tar xf zabbix-5.2.6.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# mv zabbix-5.2.6/ zabbix
[root@localhost local]# yum -y install gcc gc++ make pcre*
[root@localhost local]# cd zabbix/
[root@localhost zabbix]# ./configure --enable-agent
[root@localhost zabbix]# make install
[root@localhost zabbix]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port         Peer Address:Port    Process    
LISTEN    0         128                0.0.0.0:22                0.0.0.0:*                  
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                   [::]:22                   [::]:*   

在agentd. conf文件中配置server地址和hostname启动agent

[root@localhost zabbix]# vim /usr/local/etc/zabbix_agentd.conf
Server=192.168.207.143    
ServerActive=192.168.207.143
Hostname=Zabbix123

重启服务

[root@localhost zabbix]# pkill zabbix_agentd 
[root@localhost zabbix]# zabbix_agentd 

zabbix-agent配置文件开启自定义功能

[root@localhost zabbix]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1   #取消注释并改为1
UserParameter=check_nginx[*],/bin/bash /scripts/check_nginx.sh $1 添加在最后的空白处

测试server端是否可以收到数据

[root@localhost etc]# zabbix_get -s 192.168.207.131 -p 10050 -k check_nginx[accepts]
9
[root@localhost etc]# zabbix_get -s 192.168.207.131 -k check_nginx[active]
1

在web上添加监控项

image-20210627134755959

image-20210627134848514

image-20210627134912995

image-20210627134934923

image-20210627135536925

image-20210627135519636

image-20210627135653153

继续改参数

image-20210627135913592

image-20210627135932003

image-20210627135945806

image-20210627140001090

image-20210627140652991

image-20210627140347712

image-20210627140457212

image-20210627141223580

以上是关于zabbix监控nginx状态的主要内容,如果未能解决你的问题,请参考以下文章

zabbix3.0.4监控nginx服务

Zabbix监控之Nginx的状态监控

zabbix 监控nginx状态

用zabbix监控nginx_status状态

Zabbix监控Nginx状态

Zabbix监控nginx服务进程状态