zabbix監控nginx狀態

2020-08-12 11:54:24

部署zabbix

lnmp環境

#安裝依賴包
[root@node01-Linux ~]# yum -y install wget vim net-snmp-devel libevent-devel

#下載zabbix
[root@node01-Linux ~]# cd /usr/src/
[root@node01-Linux src]# wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.2.tar.gz

#解壓
[root@node01-Linux src]# tar xf zabbix-5.0.2.tar.gz

#建立zabbix使用者和組
[root@node01-Linux src]# useradd -r -M -s /sbin/nologin zabbix

#設定zabbix數據庫
[root@node01-Linux src]# mysql -uroot -p10063607
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 8
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.

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 zabbix@localhost identified by 'zabbix123!';
Query OK, 0 rows affected, 2 warnings (0.01 sec)

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

mysql> exit
Bye


[root@node01-Linux src]# cd zabbix-5.0.2/database/mysql/
[root@node01-Linux mysql]# mysql -uzabbix -pzabbix123! zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@node01-Linux mysql]# mysql -uzabbix -pzabbix123! zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@node01-Linux mysql]# mysql -uzabbix -pzabbix123! zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

#編譯安裝zabbix
[root@node01-Linux mysql]# cd /usr/src/zabbix-5.0.2
[root@node01-Linux zabbix-5.0.2]# ./configure --enable-server \
--enable-agent \
--with-mysql \
--with-net-snmp \
--with-libcurl \
--with-libxml2

[root@node01-Linux zabbix-5.0.2]# make install

# zabbix伺服器端設定
#修改伺服器端組態檔、設定數據庫資訊
[root@node01-Linux zabbix-5.0.2]# cd /usr/local/etc/
[root@node01-Linux etc]# vim zabbix_server.conf
...
DBPassword=zabbix123!		//設定zabbix數據庫連線密碼
...

#啓動zabbix_server和zabbix_agentd
[root@node01-Linux etc]# zabbix_server 
[root@node01-Linux etc]# zabbix_agentd
[root@node01-Linux etc]# ss -antl|grep -E '10050|10051'
LISTEN     0      128          *:10050                    *:*                  
LISTEN     0      128          *:10051                    *:*   

設定zabbix服務web介面

#修改/etc/php.ini的設定並重新啓動php-fpm
[root@node01-Linux ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[root@node01-Linux ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini
[root@node01-Linux ~]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[root@node01-Linux ~]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[root@node01-Linux ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done

[root@node01-Linux ~]# cd /usr/src/zabbix-5.0.2
[root@node01-Linux zabbix-5.0.2]# cp -r ui /usr/local/nginx/html/zabbix
[root@node01-Linux zabbix-5.0.2]# chown -R nginx.nginx /usr/local/nginx/html/zabbix/

#設定nginx虛擬主機
[root@node01-Linux ~]# vim /usr/local/nginx/conf/nginx.conf
...
location / {
            root   html/zabbix;   //這裏修改爲zabbix的web介面目錄
            index  index.php index.html index.htm;
        }
...
location ~ \.php$ {
            root           html/zabbix;  //這裏修改爲zabbix的web介面目錄
            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
[root@node01-Linux ~]# chmod 777 /usr/local/nginx/html/zabbix/conf

[root@node01-Linux ~]# nginx -s reload

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2 . 設定nginx服務狀態頁面

[root@node01-Linux ~]# vim /usr/local/nginx/conf/nginx.conf
...
        location /status {
            stub_status on;
            allow 192.168.25.0/24;
            deny all;
        }
...
[root@node01-Linux ~]# nginx -s reload

#檢視
[root@node01-Linux ~]# curl http://192.168.25.131/status
Active connections: 3 
server accepts handled requests
 16 16 85 
Reading: 0 Writing: 1 Waiting: 2 

設定監控指令碼

[root@node01-Linux ~]# mkdir /scripts
[root@node01-Linux ~]# vim /scripts/handled.sh
#!/bin/bash

status=$(curl -s http://192.168.25.131/status |awk 'NR==3{print $3}')
echo $status
[root@node01-Linux ~]# vim /scripts/Reading.sh
#!/bin/bash

status=$(curl -s http://192.168.25.131/status |awk 'NR==4{print $2}')
echo $status
[root@node01-Linux ~]# vim /scripts/Writing.sh
#!/bin/bash

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

echo $status
[root@node01-Linux ~]# chmod +x /scripts/*

設定zabbix_agent檔案

[root@node01-Linux ~]# vim /usr/local/etc/zabbix_agentd.conf
...
# Default:
UnsafeUserParameters=1  //將此行取消註釋,並將0改爲1
...
# Default:
UserParameter=check_handled,/bin/bash /scripts/handled.sh        //將此行取消註釋,並修改後面的內容
UserParameter=check_Reading,/bin/bash /scripts/Reading.sh
UserParameter=check_Writing,/bin/bash /scripts/Writing.sh
...

[root@node01-Linux ~]# pkill zabbix;zabbix_server;zabbix_agentd

#檢視key
[root@node01-Linux ~]# zabbix_get -s 127.0.0.1 -k check_handled
433
[root@node01-Linux ~]# zabbix_get -s 127.0.0.1 -k check_Reading
0
[root@node01-Linux ~]# zabbix_get -s 127.0.0.1 -k check_Writing
1

web介面設定

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

檢視

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述