zabbix监控nginx状态

本文详细介绍了如何使用Zabbix监控Nginx服务状态,包括Zabbix的部署、配置Nginx服务状态页面、编写监控脚本、配置Zabbix代理、web界面设置及查看监控数据的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 . 部署zabbix

lnmp环境

#安装依赖包
[root@node01-Linux ~]# yum -y install wget vim net-snmp-devel libevent-devel

#下载zabbix
[root@node01-Linux ~]# cd /usr/src/
[root@node01-Linux src]# wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.2.tar.gz

#解压
[root@node01-Linux src]# tar xf zabbix-5.0.2.tar.gz

#创建zabbix用户和组
[root@node01-Linux src]# useradd -r -M -s /sbin/nologin zabbix

#配置zabbix数据库
[root@node01-Linux src]# mysql -uroot -p10063607
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.01 sec)

mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix123!';
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye


[root@node01-Linux src]# cd zabbix-5.0.2/database/mysql/
[root@node01-Linux mysql]# mysql -uzabbix -pzabbix123! zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@node01-Linux mysql]# mysql -uzabbix -pzabbix123! zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@node01-Linux mysql]# mysql -uzabbix -pzabbix123! zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

#编译安装zabbix
[root@node01-Linux mysql]# cd /usr/src/zabbix-5.0.2
[root@node01-Linux zabbix-5.0.2]# ./configure --enable-server \
--enable-agent \
--with-mysql \
--with-net-snmp \
--with-libcurl \
--with-libxml2

[root@node01-Linux zabbix-5.0.2]# make install

# zabbix服务端配置
#修改服务端配置文件、设置数据库信息
[root@node01-Linux zabbix-5.0.2]# cd /usr/local/etc/
[root@node01-Linux etc]# vim zabbix_server.conf
...
DBPassword=zabbix123!		//设置zabbix数据库连接密码
...

#启动zabbix_server和zabbix_agentd
[root@node01-Linux etc]# zabbix_server 
[root@node01-Linux etc]# zabbix_agentd
[root@node01-Linux etc]# ss -antl|grep -E '10050|10051'
LISTEN     0      128          *:10050                    *:*                  
LISTEN     0      128          *:10051                    *:*   

配置zabbix服务web界面

#修改/etc/php.ini的配置并重启php-fpm
[root@node01-Linux ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[root@node01-Linux ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini
[root@node01-Linux ~]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[root@node01-Linux ~]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[root@node01-Linux ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done

[root@node01-Linux ~]# cd /usr/src/zabbix-5.0.2
[root@node01-Linux zabbix-5.0.2]# cp -r ui /usr/local/nginx/html/zabbix
[root@node01-Linux zabbix-5.0.2]# chown -R nginx.nginx /usr/local/nginx/html/zabbix/

#配置nginx虚拟主机
[root@node01-Linux ~]# vim /usr/local/nginx/conf/nginx.conf
...
location / {
            root   html/zabbix;   //这里修改为zabbix的web界面目录
            index  index.php index.html index.htm;
        }
...
location ~ \.php$ {
            root           html/zabbix;  //这里修改为zabbix的web界面目录
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
...

#设置zabbix/conf目录的权限,让zabbix有权限生成配置文件zabbix.conf.php
[root@node01-Linux ~]# chmod 777 /usr/local/nginx/html/zabbix/conf

[root@node01-Linux ~]# nginx -s reload

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2 . 配置nginx服务状态页面

[root@node01-Linux ~]# vim /usr/local/nginx/conf/nginx.conf
...
        location /status {
            stub_status on;
            allow 192.168.25.0/24;
            deny all;
        }
...
[root@node01-Linux ~]# nginx -s reload

#查看
[root@node01-Linux ~]# curl http://192.168.25.131/status
Active connections: 3 
server accepts handled requests
 16 16 85 
Reading: 0 Writing: 1 Waiting: 2 

3 . 配置监控脚本

[root@node01-Linux ~]# mkdir /scripts
[root@node01-Linux ~]# vim /scripts/handled.sh
#!/bin/bash

status=$(curl -s http://192.168.25.131/status |awk 'NR==3{print $3}')
echo $status
[root@node01-Linux ~]# vim /scripts/Reading.sh
#!/bin/bash

status=$(curl -s http://192.168.25.131/status |awk 'NR==4{print $2}')
echo $status
[root@node01-Linux ~]# vim /scripts/Writing.sh
#!/bin/bash

status=$(curl -s http://192.168.25.131/status |awk 'NR==4{print $4}')

echo $status
[root@node01-Linux ~]# chmod +x /scripts/*

4 . 配置zabbix_agent文件

[root@node01-Linux ~]# vim /usr/local/etc/zabbix_agentd.conf
...
# Default:
UnsafeUserParameters=1  //将此行取消注释,并将0改为1
...
# Default:
UserParameter=check_handled,/bin/bash /scripts/handled.sh        //将此行取消注释,并修改后面的内容
UserParameter=check_Reading,/bin/bash /scripts/Reading.sh
UserParameter=check_Writing,/bin/bash /scripts/Writing.sh
...

[root@node01-Linux ~]# pkill zabbix;zabbix_server;zabbix_agentd

#查看key
[root@node01-Linux ~]# zabbix_get -s 127.0.0.1 -k check_handled
433
[root@node01-Linux ~]# zabbix_get -s 127.0.0.1 -k check_Reading
0
[root@node01-Linux ~]# zabbix_get -s 127.0.0.1 -k check_Writing
1

5 . web界面配置

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

6 . 查看

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值