Zabbix监控nginx状态

1. lnmp搭建


#1.1 安装后配置

# 修改/usr/local/nginx/conf/nginx.conf配置文件
修改/usr/local/nginx/conf/nginx.conf配置文件
[root@nginx ~]# cd /usr/local/nginx/conf/
[root@nginx conf]# vim nginx.conf
http {
    server {
        listen       80;
        server_name  localhost;		##注:若想用域名访问,请修改localhost
            location / {
            root   html;
            index  index.php index.html index.htm;		##注:添加index.php
            }
    }
        location ~ \.php$ {
            root           html;		##注:网页文件存放目录,在本机的/usr/local/nginx/html/目录下
            fastcgi_pass   192.168.40.134:9000;		##注:修改为php服务器地址
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /www/$fastcgi_script_name;		##注:将$/scripts修改为根目录(192.168.91.130服务器的存放网页文件的目录)
            include        fastcgi_params;
        }
}

检查配置文件是正确
[root@nginx conf]# 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

重启nginx
[root@nginx ~]# nginx -s reload
[root@nginx ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128      *:80                   *:*                  
LISTEN     0      128      *:22                   *:*                  
LISTEN     0      100    127.0.0.1:25                   *:*                  
LISTEN     0      128     :::22                  :::*                  
LISTEN     0      100    ::1:25                  :::*     


在存放网页的目录中创建index.php文件(与配置文件中的“root”项对应)
[root@nginx ~]# vim /usr/local/nginx/html/index.php
[root@nginx ~]# cat > /usr/local/nginx/html/index.php <<EOF
> <?php
>     phpinfo();
> ?>
> EOF

1.2 访问php网页

2. Zabbix部署

环境说明:

环境IP要安装的应用
服务器192.168.30.130lnmp架构
zabbix
serverzabbix agent
客户端192.168.30.129zabbix agent

2.1 zabbix服务端安装

# 安装依赖包
[root@swk ~]# yum -y install net-snmp-devel libevent-devel
安装过程略....

# 下载zabbix
[root@swk src]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.0.3/zabbix-4.0.3.tar.gz

# 解压
[root@swk src]# tar xf zabbix-4.0.3.tar.gz

# 创建zabbix用户和组
[root@swk ~]# groupadd -r zabbix
[root@swk ~]# useradd -r -M -s /sbin/nologin -g zabbix zabbix

# 配置zabbix数据库
[root@swk ~]# mysql -uroot -psuwenkang123
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 2
Server version: 5.7.22

Copyright (c) 2000, 2018, 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> alter user 'root'@'localhost' identified by 'swk666';
Query OK, 0 rows affected (0.00 sec)

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

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

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

mysql> quit
Bye

[root@swk ~]# cd /usr/src/zabbix-4.0.3/database/mysql/
[root@swk mysql]# ls
data.sql  images.sql  Makefile.am  Makefile.in  schema.sql
[root@swk mysql]# mysql -uzabbix -pzabbix123! zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@swk mysql]# mysql -uzabbix -pzabbix123! zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@swk mysql]# mysql -uzabbix -pzabbix123! zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.


# 编译安装zabbix
[root@swk mysql]# cd /usr/src/zabbix-4.0.3
[root@swk zabbix-4.0.3]# ./configure --enable-server \
> --enable-agent \
> --with-mysql \
> --with-net-snmp \
> --with-libcurl \
> --with-libxml2
root@swk zabbix-4.0.3]# make install

2.2 zabbix服务端配置

# 修改服务端配置文件
# 设置数据库信息
[root@swk ~]# ls /usr/local/etc/
zabbix_agentd.conf  zabbix_agentd.conf.d  zabbix_server.conf  zabbix_server.conf.d
[root@swk ~]# vim /usr/local/etc/zabbix_server.conf
....
DBPassword=zabbix123!       //设置zabbix数据库连接密码

# 启动zabbix_server和zabbix_agentd
[root@swk ~]# zabbix_server
[root@swk ~]# zabbix_agentd 
[root@swk ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128              *:80                           *:*                  
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             :::22                          :::*                  
LISTEN     0      100            ::1:25                          :::*                  
LISTEN     0      80              :::3306                        :::*       

2.3 zabbix web界面安装前配置
# 修改/etc/php.ini的配置并重启php-fpm
[root@swk ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[root@swk ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini
[root@swk ~]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[root@swk ~]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[root@swk ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@swk ~]# cd /usr/src/zabbix-4.0.3
[root@swk zabbix-4.0.3]# ls
aclocal.m4  compile        config.sub    depcomp     m4           misc     src
AUTHORS     conf           configure     frontends   Makefile     missing
bin         config.guess   configure.ac  include     Makefile.am  NEWS
build       config.log     COPYING       INSTALL     Makefile.in  README
ChangeLog   config.status  database      install-sh  man          sass
[root@swk zabbix-4.0.3]# mkdir /usr/local/nginx/html/zabbix
[root@swk zabbix-4.0.3]# cp -a frontends/php/* /usr/local/nginx/html/zabbix/
[root@swk zabbix-4.0.3]# chown -R nginx.nginx /usr/local/nginx/html

# 修改nginx配置文件
 #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index zabbix index.php index.html index.htm;		##此处添加zabbix
        }


        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  zabbix;			##此处为zabbix
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;

# 启动nginx status,在nginx.conf配置文件中添加以下内容
[root@swk zabbix-4.0.3]# vim /usr/local/nginx/conf/nginx.conf		# 
        location /nginx_status {
            stub_status on;
            access_log off;
            allow 192.168.100.100;	## 定义仅客户端能访问
            deny all;
        }

[root@swk zabbix-4.0.3]# 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@100 zabbix-4.0.3]# nginx -s reload

# 客户端使用curl命令获取nginx status,并了解其参数值
[root@zabbix ~]# curl http://192.168.100.100/nginx_status
Active connections: 1 
server accepts handled requests
 8 8 4 
Reading: 0 Writing: 1 Waiting: 0

## Active connections – 活跃的连接数量
## server accepts handled requests — 总共处理了1个连接 , 成功创建1次握手, 总共处理了1个请求
## Reading — 读取客户端的连接数
## Writing — 响应数据到客户端的数量
## Waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接.

# 设置zabbix/conf目录的权限,让zabbix有权限生成配置文件zabbix.conf.php
[root@swk ~]# chmod 777 /usr/local/nginx/html/zabbix/conf
[root@swk ~]# ll -d /usr/local/nginx/html/zabbix/conf
drwxrwxrwx 2 nginx nginx 81 12月 20 2018 /usr/local/nginx/html/zabbix/conf

# 重启nginx
[root@swk ~]# nginx -s stop
[root@swk ~]# nginx 
[root@swk ~]# ss -antl
State      Recv-Q Send-Q    Local Address:Port                   Peer Address:Port              
LISTEN     0      128                   *:80                                *:*                  
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                  :::22                               :::*                  
LISTEN     0      100                 ::1:25                               :::*                  
LISTEN     0      80                   :::3306                             :::* 

登录进去zabbix网页
在这里插入图片描述

# 恢复zabbix/conf目录的权限为755
[root@100 ~]# chmod 755 /usr/local/nginx/html/zabbix/conf


2.4 zabbix 客户端配置

# 安装依赖包
[root@zabbix ~]# yum -y install net-snmp-devel libevent-devel libxml2-devel curl-devel pcre*

# 下载zabbix并解压
[root@zabbix ~]# cd /usr/src/
[root@zabbix src]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.0.3/zabbix-4.0.3.tar.gz
[root@zabbix src]# tar xf zabbix-4.0.3.tar.gz

# 创建zabbix用户和组
[root@zabbix ~]# groupadd -r zabbix
[root@zabbix ~]# useradd -r -M -s /sbin/nologin -g zabbix zabbix

# 编译安装zabbix
[root@zabbix zabbix-4.0.3]# ./configure --enable-agent
编译过程略。。
[root@zabbix zabbix-4.0.3]# make install
[root@zabbix zabbix-4.0.3]# ls /usr/local/etc/
zabbix_agentd.conf  zabbix_agentd.conf.d

# 修改服务端配置文件
# 设置数据库信息
[root@zabbix ~]# vim /usr/local/etc/zabbix_agentd.conf #找到以下三行并修改为:
Server=192.168.30.130
ServerActive=192.168.30.130
Hostname=swk       # 可自定义

# 启动zabbix_agentd
[root@zabbix ~]# zabbix_agentd
[root@zabbix ~]# 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             :::22                          :::*                  
LISTEN     0      100            ::1:25       
:::*            


# 在客户端创建脚本存放目录
[root@zabbix ~]# mkdir /scripts

# 写脚本
[root@zabbix ~]# vim /scripts/ngx_status.sh
#!/bin/bash

case $1 in
    accepts)
        curl -s http://192.168.30.130/status 2>/dev/null | awk 'NR==3 {print $1}'
    ;;
    handled)
        curl -s http://192.168.30.130/status 2>/dev/null |awk 'NR==3 {print $2}'
    ;;
    requests)
        curl -s http://192.168.30.130/status 2>/dev/null |awk 'NR==3 {print $3}'
    ;;
    *)
        echo "Usage: $0 { accepts | handled | requests }"
esac

# 给脚本执行权限
[root@zabbix ~]# chmod +x /scripts/ngx_status.sh
[root@zabbix ~]# ll /scripts/ngx_status.sh 
-rwxr-xr-x. 1 root root 399 8月  30 02:13 /scripts/ngx_status.sh

# 改脚本存放目录的属主
[root@zabbix ~]# chown -R zabbix.zabbix /scripts/
[root@zabbix ~]# ll -d /scripts/
drwxr-xr-x. 2 zabbix zabbix 27 8月  30 02:13 /scripts/

进行访问测试检测:
[root@zabbix ~]# curl http://192.168.30.130/nginx_status
Active connections: 2 
server accepts handled requests
 39 39 312 
Reading: 0 Writing: 1 Waiting: 1 

[root@zabbix ~]# bash /scripts/ngx_status.sh accepts
40		## 这里因为每次数字都会变化

 在服务端手动测试
[root@zabbix ~]# zabbix_get -s 192.168.30.129 -k nginx[requests]
358

3. 配置网页界面

创建新主机:

  • 主机名必须和刚刚自定义的主机名一致: Hostname=runtime
    在这里插入图片描述
    添加监控项
  • 配置——主机——监控项
  • 配置以下配置其他保持默认
    在这里插入图片描述
    添加触发器
  • 配置——主机——触发器
    在这里插入图片描述

4. 验证

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值