1. 程式人生 > 實用技巧 >zabbix監控nginx伺服器狀態

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;
}

此配置建立了一個簡單的網頁,其基本狀態資料如下所示:

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]# ls
Reading_value.sh  requests_value.sh  Writing_value.sh
[root@www scripts]# cat requests_value.sh 
#!/bin/bash
value=`curl -k -s 192.168.32.125/status | awk 'NR==3{print $3}'`
echo $value

[root@www scripts]# cat Reading_value.sh 
#!/bin/bash
value=`curl -k -s 192.168.32.125/status | awk 'NR==4{print $2}'`
echo $value

[root@www scripts]# cat Writing_value.sh 
#!/bin/bash
value=`curl -k -s 192.168.32.125/status | awk 'NR==4{print $4}'`
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=requests_value,/bin/bash /scripts/requests_value.sh
UserParameter=Reading_value,/bin/bash /scripts/Reading_value.sh
UserParameter=Writing_value,/bin/bash /scripts/Writing_value.sh




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



#測試配置的key
[root@www scripts]# zabbix_get -s 192.168.32.125 -k requests_value
689
[root@www scripts]# zabbix_get -s 192.168.32.125 -k Writing_value
1
[root@www scripts]# zabbix_get -s 192.168.32.125 -k Reading_value
0

5.3 配置監控項


同樣的方式新增另外兩個監控項

5.4 檢視監控