zabbix监控nginx服务器状态

zabbix监控nginx服务器状态

环境说明:

服务端IP 要安装的应用
192.168.32.125 lnmp架构 zabbix server

因为zabbix是用php语言开发的,所以必须先部署lamp架构,使其能够支持运行php网页

zabbix5.0官方安装文档:https://www.zabbix.com/documentation/current/manual/installation/install

这里已经安装好了lnmp环境,参考文档[lnmp环境搭建](

1. 确保安装相关模块

这个ngx_http_stub_status_module模块提供对基本状态信息的访问。

默认情况下不生成此模块,应使用--with-http_stub_status_module配置参数

#这里已经配置了这个模块
[root@www ~]# nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=/root/echo-nginx-module-0.61

2. 修改配置文件

Syntax: **stub_status**;
Default:
Context: server, location

配置实例:

location = /status {
stub_status;
access_log off;
}

此配置创建了一个简单的网页,其基本状态数据如下所示:

Active connections: 291 
server accepts handled requests
 16630948 16630948 31070465 
Reading: 6 Writing: 179 Waiting: 106 

在1.7.5之前的版本中的配置为“stub_status on”.

    server {
        listen       80;
        listen       443 ssl;
        server_name  www.test.com;

        ssl_certificate      /usr/local/nginx/ssl/www.test.com.crt;
        ssl_certificate_key  /usr/local/nginx/ssl/www.test.com.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location = /status {
            stub_status;
        }
    }

3. 访问状态页面


状态页面信息详解:

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

4. 部署zabbix

4.1 zabbix服务端安装

#安装依赖包
[root@www ~]# yum -y install net-snmp-devel libevent-devel

#下载zabbix
[root@www ~]# wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.2.tar.gz

[root@www ~]# tar xf zabbix-5.0.2.tar.gz 
[root@www ~]# cd zabbix-5.0.2
[root@www zabbix-5.0.2]# ls
aclocal.m4  build      conf          configure     database  INSTALL     Makefile.am  misc     README  ui
AUTHORS     ChangeLog  config.guess  configure.ac  depcomp   install-sh  Makefile.in  missing  sass
bin         compile    config.sub    COPYING       include   m4          man          NEWS     src

#创建zabbix用户
[root@www zabbix-5.0.2]# useradd -r -M -s /sbin/nologin zabbix
[root@www zabbix-5.0.2]# id zabbix
uid=994(zabbix) gid=992(zabbix) groups=992(zabbix)

#配置zabbix数据库
[root@www zabbix-5.0.2]# mysql -uroot -p
Enter password: 
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 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.

#utf-8是Zabbix支持的唯一编码,要使Zabbix服务器/代理与MySQL数据库正常工作,需要字符集UTF 8和UTF 8_bin排序规则。
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.00 sec)

mysql> quit
Bye


[root@www zabbix-5.0.2]# ls
aclocal.m4  build      conf          configure     database  INSTALL     Makefile.am  misc     README  ui
AUTHORS     ChangeLog  config.guess  configure.ac  depcomp   install-sh  Makefile.in  missing  sass
bin         compile    config.sub    COPYING       include   m4          man          NEWS     src
[root@www zabbix-5.0.2]# cd database/mysql/
[root@www mysql]# ls
data.sql  double.sql  images.sql  Makefile.am  Makefile.in  schema.sql
[root@www mysql]# mysql -uzabbix -pzabbix123! zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@www mysql]# mysql -uzabbix -pzabbix123! zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@www mysql]# mysql -uzabbix -pzabbix123! zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.



#编译安装zabbix
[root@www zabbix-5.0.2]# ./configure --enable-server  --enable-agent  --with-mysql  --with-net-snmp  --with-libcurl  --with-libxml2
[root@www zabbix-5.0.2]# make install

4.2 zabbix服务端配置

[root@www zabbix-5.0.2]# ls /usr/local/etc/
zabbix_agentd.conf  zabbix_agentd.conf.d  zabbix_server.conf  zabbix_server.conf.d

#修改服务端配置文件
#设置数据库信息

[root@www ~]# vim /usr/local/etc/zabbix_server.conf
....
DBPassword=zabbix123!       //设置zabbix数据库连接密码
....

#启动zabbix_server和zabbix_agentd
[root@www zabbix-5.0.2]# zabbix_server 
[root@www zabbix-5.0.2]# zabbix_agentd 
[root@www zabbix-5.0.2]# ss -tanl
State       Recv-Q Send-Q              Local Address:Port                             Peer Address:Port              
LISTEN      0      100                     127.0.0.1:25                                          *:*                  
LISTEN      0      128                             *:443                                         *:*                  
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                                     [::]:*                  
LISTEN      0      128                          [::]:22                                       [::]:*           



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



[root@www ~]# cd zabbix-5.0.2
[root@www zabbix-5.0.2]# ls
aclocal.m4  ChangeLog     config.log     configure.ac  include     Makefile     misc     sass
AUTHORS     compile       config.status  COPYING       INSTALL     Makefile.am  missing  src
bin         conf          config.sub     database      install-sh  Makefile.in  NEWS     ui
build       config.guess  configure      depcomp       m4          man          README
[root@www zabbix-5.0.2]# cd ui/
[root@www ui]# mkdir /usr/local/nginx/html/zabbix
[root@www ui]# cp -a . /usr/local/nginx/html/zabbix/
[root@www ui]# chown -R nginx.nginx /usr/local/nginx/html/zabbix/
[root@www ui]# ls  /usr/local/nginx/html/zabbix/
actionconf.php      conf                         httpconf.php     map.import.php     slides.php
api_jsonrpc.php     conf.import.php              httpdetails.php  map.php            srv_status.php
app                 correlation.php              image.php        modules            sysmap.php
applications.php    discoveryconf.php            imgstore.php     overview.php       sysmaps.php
assets              disc_prototypes.php          include          queue.php          templates.php
audio               favicon.ico                  index_http.php   report2.php        toptriggers.php
auditacts.php       graphs.php                   index.php        report4.php        tr_events.php
browserwarning.php  history.php                  index_sso.php    robots.txt         trigger_prototypes.php
chart2.php          host_discovery.php           items.php        screenconf.php     triggers.php
chart3.php          hostgroups.php               js               screenedit.php     vendor
chart4.php          hostinventoriesoverview.php  jsLoader.php     screen.import.php  zabbix.php
chart5.php          hostinventories.php          jsrpc.php        screens.php
chart6.php          host_prototypes.php          local            services.php
chart7.php          host_screen.php              locale           setup.php
chart.php           hosts.php                    maintenance.php  slideconf.php



#配置nginx
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
......

    server {
        listen       80;
        listen       443 ssl;
        server_name  www.test.com;

        ssl_certificate      /usr/local/nginx/ssl/www.test.com.crt;
        ssl_certificate_key  /usr/local/nginx/ssl/www.test.com.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   html/zabbix;
            index  index.php;
        }
        location = /status {
            stub_status;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ \.php$ {
            root           html/zabbix;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

}

......


#重新加载nginx配置文件
[root@www ~]# nginx -s reload



#设置zabbix/conf目录的权限,让zabbix有权限生成配置文件zabbix.conf.php
[root@www ~]# chmod 777 /usr/local/nginx/html/zabbix/conf
[root@www ~]# ll -d /usr/local/nginx/html/zabbix/conf
drwxrwxrwx 3 nginx nginx 94 Jul  6 05:54 /usr/local/nginx/html/zabbix/conf



#配置zabbix开机自启
[root@www core]# pwd
/root/zabbix-5.0.2/misc/init.d/fedora/core
[root@www core]# ls
zabbix_agentd  zabbix_server
[root@www core]# cp -a . /etc/init.d/

[root@www core]# chkconfig --add zabbix_server 
[root@www core]# chkconfig --add zabbix_agentd 
[root@www core]# chkconfig zabbix_server on
[root@www core]# chkconfig zabbix_agentd on




4.3 web界面安装

初始账户密码是Admin,zabbix


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

  1. 配置自定义监控来监控nginx状态

5.1 编写脚本,取出需要的值

#编写脚本
[root@www ~]# mkdir /scripts
[root@www ~]# cd /scripts/
[root@www scripts]# cat nginx_status.sh 
#!/bin/bash
host=192.168.32.125


Reading () {
value=`curl -k -s ${host}/status | awk 'NR==4{print $2}'`
}

requests () {
value=`curl -k -s ${host}/status | awk 'NR==3{print $3}'`
}

Writing () {
value=`curl -k -s 192.168.32.125/status | awk 'NR==4{print $4}'`
}
$1
echo $value



5.2 修改配置文件,添加自定义key

[root@www scripts]# vim /usr/local/etc/zabbix_agentd.conf

......
Server=192.168.32.125 #改为服务端ip
ServerActive=192.168.32.125	#改为服务端ip
......

### Option: UnsafeUserParameters
#       Allow all characters to be passed in arguments to user-defined parameters.
#       The following characters are not allowed:
#       \ ' " ` * ? [ ] { } ~ $ ! & ; ( ) < > | # @
#       Additionally, newline characters are not allowed.
#       0 - do not allow
#       1 - allow
#
# Mandatory: no
# Range: 0-1
# Default:
# UnsafeUserParameters=0
UnsafeUserParameters=1

### Option: UserParameter
#       User-defined parameter to monitor. There can be several user-defined parameters.
#       Format: UserParameter=<key>,<shell command>
#       See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
# UserParameter=
UserParameter=nginx_status[*],/bin/bash /scripts/nginx_status.sh





[root@www scripts]# service zabbix_agentd restart
Restarting zabbix_agentd (via systemctl):                  [  OK  ]



#测试配置的key
[root@www scripts]# ./nginx_status.sh Reading
0
[root@www scripts]# ./nginx_status.sh requests
568
[root@www scripts]# ./nginx_status.sh Writing
1


5.3 配置监控项




5.4 查看监控


posted @ 2020-08-11 12:35  EverEternity  阅读(728)  评论(0编辑  收藏  举报