Zabbix监控nginx状态

nginx,mysql,php的安装请参照lnmp分离部署

注意:本次将lnmp搭建在一台服务器上做演示

1. lnmp搭建

1.1 安装后配置

# 修改nginx的主配置文件
[root@100 ~]# vim /usr/local/nginx/conf/nginx.conf
.....
error_log  logs/error.log;
#error_log  logs/error.log  notice;		##开启错误日志存放目录
#error_log  logs/error.log  info;
.....

# 开启以匹配php结尾的网站模板
		......
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;		##此处修改/scripts为$document_root
            include        fastcgi_params;
        }
		......

# 修改server段内容为
        #access_log  logs/host.access.log  main;

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

# 在网页目录站点创建index.php的文件,写入php网页信息
[root@100 ~]# cat > /usr/local/nginx/html/index.php <<EOF
> <?php
>     phpinfo();
> ?>
> EOF

# 检查配置文件语法是否有误,并重新加载配置文件
[root@100 ~]# 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 ~]# nginx -s reload

到此lnmp搭建完成可以取访问验证。。。。

1.2 访问php网页

在这里插入图片描述

2. Zabbix部署

环境说明:

环境IP要安装的应用
服务器192.168.100.100lnmp架构
zabbix server
zabbix agentd
客户端192.168.100.96zabbix agentd

2.1 zabbix服务端安装

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

# 下载zabbix
[root@100 ~]# cd /usr/src/
[root@100 ~]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.0.3/zabbix-4.0.3.tar.gz
下载过程略....

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

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

# 配置zabbix数据库
[root@100 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
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 'scl666';		#重置密码
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.07 sec)

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

mysql> quit
Bye

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

[root@100 mysql]# mysql -uzabbix -pzabbix123! zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

[root@100 mysql]# mysql -uzabbix -pzabbix123! zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

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

[root@100 zabbix-4.0.3]# make install


2.2 zabbix服务端配置

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

# 启动zabbix_server和zabbix_agentd
[root@100 ~]# zabbix_server 
[root@100 ~]# zabbix_agentd 
[root@100 ~]# 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                             :::* 



2.3 zabbix web界面安装前配置

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

[root@100 ~]# cd /usr/src/zabbix-4.0.3
[root@100 zabbix-4.0.3]# ls
aclocal.m4  ChangeLog     config.log     configure.ac  frontends   m4           man      README
AUTHORS     compile       config.status  COPYING       include     Makefile     misc     sass
bin         conf          config.sub     database      INSTALL     Makefile.am  missing  src
build       config.guess  configure      depcomp       install-sh  Makefile.in  NEWS

[root@100 zabbix-4.0.3]# mkdir /usr/local/nginx/html/zabbix
[root@100 zabbix-4.0.3]# cp -a frontends/php/* /usr/local/nginx/html/zabbix/
[root@100 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@100 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@100 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@96 ~]# 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@100 ~]# chmod 777 /usr/local/nginx/html/zabbix/conf
[root@100 ~]# ll -d /usr/local/nginx/html/zabbix/conf
drwxrwxrwx 2 nginx nginx 81 12月 20 2018 /usr/local/nginx/html/zabbix/conf

# 重启nginx
[root@100 ~]# nginx -s stop
[root@100 ~]# nginx 
[root@100 ~]# 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@96 ~]# yum -y install net-snmp-devel libevent-devel libxml2-devel curl-devel pcre*

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

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

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

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

# 启动zabbix_agentd
[root@96 zabbix-4.0.3]# zabbix_agentd
[root@96 zabbix-4.0.3]# ss -antl
[root@96 ~]# ss -antl
State       Recv-Q Send-Q                Local Address:Port                               Peer Address:Port              
LISTEN      0      128                               *:111                                           *:*                  
LISTEN      0      128                               *:22                                            *:*                  
LISTEN      0      128                               *:10050                                         *:*                  
LISTEN      0      128                              :::111                                          :::*                  
LISTEN      0      128                              :::22                                           :::*   

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

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

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

# 给脚本执行权限
[root@96 ~]# chmod +x /scripts/ngx_status.sh
[root@96 ~]# ll /scripts/ngx_status.sh 
-rwxr-xr-x 1 root root 441 8月  29 00:04 /scripts/ngx_status.s

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

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

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


# 改客户端配置文件agentd.conf
[root@96 ~]# vim /usr/local/etc/zabbix_agentd.conf
修改:UnsafeUserParameters 等于 1 :UnsafeUserParameters=1
UserParameter=nginx[*],/bin/bash /scripts/ngx_status.sh $1		##末行加入此行

# 重启zabbix_agentd
[root@96 ~]# pkill zabbix
[root@96 ~]# zabbix_agentd

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


3. 配置网页界面

创建新主机:

主机名必须和刚刚自定义的主机名一致: Hostname=runtime

  • 配置——主机——创建主机
    在这里插入图片描述

添加监控项

  • 配置——主机——监控项
  • 配置以下配置其他保持默认
    在这里插入图片描述

添加触发器

  • 配置——主机——触发器
    在这里插入图片描述

4. 验证

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值