欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

zabbix监控nginx状态

程序员文章站 2022-07-12 19:47:36
...

状态页面信息详解:

状态码 表示的意义
Active connections 2 当前所有处于打开状态的连接数
accepts 总共处理了多少个连接
handled 成功创建多少握手
requests 总共处理了多少个请求
Reading nginx读取到客户端的Header信息数,表示正处于接收请求状态的连接数
Writing nginx返回给客户端的Header信息数,表示请求已经接收完成,且正处于处理请求或发送响应的过程中的连接数
Waiting 开启keep-alive的情况下,这个值等于active - (reading + writing),意思就是Nginx已处理完正在等候下一次请求指令的驻留连接

部署

主机名 IP 服务
wyt1 192.168.179.128 lnmp,zabbix

zabbix搭建

先安装lnmp详见文章lnmp部署

安装依赖包

[aaa@qq.com ~]# yum -y install net-snmp-devel libevent-devel

下载zabbix与解压

[aaa@qq.com ~]# wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.1.tar.gz
[aaa@qq.com ~]# ls
anaconda-ks.cfg  lamp  zabbix-5.0.1.tar.gz
[aaa@qq.com ~]# tar xf zabbix-5.0.1.tar.gz

创建zabbix用户和组

[aaa@qq.com ~]# useradd -r -M -s /sbin/nologin zabbix

配置zabbix数据库

[aaa@qq.com ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be inre.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30 MySQL Community Server (GPL)

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

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 state.

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 aaa@qq.com identified by 'zabbix123';
Query OK, 0 rows affected, 2 warnings (0.01 sec)

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

mysql> quit
Bye

[aaa@qq.com ~]# cd zabbix-5.0.1/database/mysql/
[aaa@qq.com mysql]# ls
data.sql  double.sql  images.sql  Makefile.am  Makefile.in  schema.sql
[aaa@qq.com mysql]# mysql -uzabbix -pzabbix123 zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[aaa@qq.com mysql]# mysql -uzabbix -pzabbix123 zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[aaa@qq.com mysql]# mysql -uzabbix -pzabbix123 zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

编译安装zabbix

[aaa@qq.com mysql]# cd zabbix-5.0.1
[aaa@qq.com zabbix-5.0.1]# ./configure --enable-server \
--enable-agent \
--with-mysql \
--with-net-snmp \
--with-libcurl \
--with-libxml2
....
[aaa@qq.com zabbix-5.0.1]# make install

zabbix服务端配置

[aaa@qq.com zabbix-5.0.1]# cd /usr/local/etc/
[aaa@qq.com etc]# ls
zabbix_agentd.conf    zabbix_server.conf
zabbix_agentd.conf.d  zabbix_server.conf.d
[aaa@qq.com etc]# vim zabbix_server.conf

DBPassword=zabbix123      //设置zabbix数据库连接密码

启动zabbix_server和zabbix_agentd
[aaa@qq.com ~]# zabbix_server 
[aaa@qq.com ~]# zabbix_agentd
[aaa@qq.com ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128         *:22                      *:*                  
LISTEN     0      100    127.0.0.1:25                      *:*                  
LISTEN     0      128         *:10050                   *:*                  
LISTEN     0      128         *:10051                   *:*                  
LISTEN     0      128    127.0.0.1:9000                    *:*                  
LISTEN     0      128        :::80                     :::*                  
LISTEN     0      128        :::22                     :::*                  
LISTEN     0      100       ::1:25                     :::*                  
LISTEN     0      80         :::3306                   :::*  

zabbix web界面安装前配置
修改/etc/php.ini的配置并重启php-fpm

[aaa@qq.com ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[aaa@qq.com ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini
[aaa@qq.com ~]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[aaa@qq.com ~]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[aaa@qq.com ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done

创建数据存放目录并修改目录属主和属组

[aaa@qq.com ~]# cd zabbix-5.0.1
[aaa@qq.com zabbix-5.0.1]# cp -a ui /usr/local/nginx/html/zabbix
[aaa@qq.com zabbix-5.0.1]# chown -R nginx.nginx /usr/local/nginx/html/zabbix/

配置apache虚拟主机

[aaa@qq.com ~]# vim /usr/local/nginx/conf/nginx.conf

location / {
            root   html/zabbix; //添加zabbix
            index index.php index.html index.htm;
            
location ~ \.php$ {
            root           html/zabbix; //添加zabbix
            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

[aaa@qq.com ~]# chmod 777 /usr/local/nginx/html/zabbix/conf
[aaa@qq.com ~]# ll -d /usr/local/nginx/html/zabbix/conf
drwxrwxrwx 3 nginx nginx 94 5月  25 20:15 /usr/local/nginx/html/zabbix/conf

重载配置文件

[aaa@qq.com ~]# nginx -s reload
[aaa@qq.com ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      100    127.0.0.1:25                        *:*                  
LISTEN      0      128           *:10050                     *:*                  
LISTEN      0      128           *:10051                     *:*                  
LISTEN      0      128           *:9000                      *:*                  
LISTEN      0      128           *:80                        *:*                  
LISTEN      0      128           *:22                        *:*                  
LISTEN      0      100       [::1]:25                     [::]:*                  
LISTEN      0      80         [::]:3306                   [::]:*                  
LISTEN      0      128        [::]:22                     [::]:*   

访问web界面
zabbix监控nginx状态
zabbix监控nginx状态
zabbix监控nginx状态
zabbix监控nginx状态
zabbix监控nginx状态
恢复zabbix/conf目录的权限为755

[aaa@qq.com ~]# chmod 755 /usr/local/nginx/html/zabbix/conf
[aaa@qq.com ~]# ll -d /usr/local/nginx/html/zabbix/conf
drwxr-xr-x 3 nginx nginx 117 8月  11 11:44 /usr/local/nginx/html/zabbix/conf

开启状态界面

[aaa@qq.com ~]# vim /usr/local/nginx/conf/nginx.conf
        location /status {
            stub_status on;
            allow 192.168.179.0/24;
            deny all;
        }

访问
zabbix监控nginx状态

配置监控脚本取出想要的数据

[aaa@qq.com ~]# mkdir /scripts
[aaa@qq.com scripts]# ls
handled.sh  Reading.sh  Writing.sh
[aaa@qq.com scripts]# cat handled.sh 
#!/bin/bash

status=$(curl -s http://192.168.179.128/status|awk 'NR==3{print $3}')
echo $status
[aaa@qq.com scripts]# cat 
handled.sh  Reading.sh  Writing.sh  
[aaa@qq.com scripts]# cat Reading.sh 
#!/bin/bash

status=$(curl -s http://192.168.179.128/status|awk 'NR==4{print $2}')
echo $status
[aaa@qq.com scripts]# cat Writing.sh 
#!/bin/bash

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

[aaa@qq.com scripts]# chmod +x *

启用自定义脚本监控功能

//添加下列三行
UnsafeUserParameters=1
UserParameter=check_handled,/bin/bash /scripts/handled.sh      
UserParameter=check_Reading,/bin/bash /scripts/Reading.sh
UserParameter=check_Writing,/bin/bash /scripts/Writing.sh

重启服务

[aaa@qq.com ~]# zabbix_server 
[aaa@qq.com ~]# zabbix_agentd
[aaa@qq.com ~]# zabbix_get -s 127.0.0.1 -k check_handled //测试脚本能否取出值
296
[aaa@qq.com ~]# zabbix_get -s 127.0.0.1 -k check_Reading
0
[aaa@qq.com ~]#  zabbix_get -s 127.0.0.1 -k check_Writing
1

web界面配置

添加监控项

zabbix监控nginx状态
zabbix监控nginx状态
zabbix监控nginx状态
zabbix监控nginx状态
zabbix监控nginx状态
zabbix监控nginx状态
查看handled数据
zabbix监控nginx状态
zabbix监控nginx状态
查看Reading数据
zabbix监控nginx状态
zabbix监控nginx状态
查看Writing数据
zabbix监控nginx状态
zabbix监控nginx状态