zabbix监控nginx状态
文章目录
服务端 | 客户端 | |
---|---|---|
主机名 | C1 | C2 |
ip | 192.168.207.143 | 192.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
编写脚本
[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上添加监控项
继续改参数