zabbix監控nginx伺服器狀態

2020-08-12 11:17:42


環境說明:

伺服器端IP 要安裝的應用
192.168.116.180 lnmp架構 zabbix server

因爲zabbix是用php語言開發的,所以必須先部署lamp架構,使其能夠支援執行php網頁

這裏已經安裝好了lnmp環境

1. 部署zabbix

1.1 zabbix伺服器端安裝

//安裝依賴包
[root@xian ~]# yum -y install net-snmp-devel libevent-devel
 
//下載zabbix
[root@xian ~]# wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.2.tar.gz
 
[root@xian ~]# tar xf zabbix-5.0.2.tar.gz 
[root@xian ~]# cd zabbix-5.0.2

//建立zabbix使用者
[root@xian zabbix-5.0.2]# useradd -r -M -s /sbin/nologin zabbix
[root@xian ~]# id zabbix
uid=304(zabbix) gid=304(zabbix) groups=304(zabbix)
 
//設定zabbix數據庫
[root@xian zabbix-5.0.2]# mysql -uroot -p
Enter password: 

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@xian zabbix-5.0.2]# cd database/mysql/
[root@xian mysql]# ls
data.sql    images.sql  Makefile.am  schema.sql
double.sql  Makefile    Makefile.in
[root@xian mysql]# mysql -uzabbix -pzabbix123! zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@xian mysql]# mysql -uzabbix -pzabbix123! zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@xian mysql]# mysql -uzabbix -pzabbix123! zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
 
//編譯安裝zabbix
[root@xian zabbix-5.0.2]# ./configure --enable-server  --enable-agent  --with-mysql  --with-net-snmp  --with-libcurl  --with-libxml2
[root@xian zabbix-5.0.2]# make install

1.2 zabbix伺服器端設定

[root@xian zabbix-5.0.2]# ls /usr/local/etc/
zabbix_agentd.conf  zabbix_agentd.conf.d  zabbix_server.conf  zabbix_server.conf.d
 
//修改伺服器端組態檔
//設定數據庫資訊
 
[root@xian zabbix-5.0.2]# vim /usr/local/etc/zabbix_server.conf

DBPassword=zabbix123!       //設定zabbix數據庫連線密碼

//啓動zabbix_server和zabbix_agentd
[root@xian zabbix-5.0.2]# zabbix_server 
[root@xian zabbix-5.0.2]# zabbix_agentd 
[root@xian mysql]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128     *:10050               *:*                  
LISTEN     0      128     *:10051               *:*                  
LISTEN     0      128     *:9000                *:*                  
LISTEN     0      128     *:80                  *:*                  
LISTEN     0      128     *:22                  *:*                  
LISTEN     0      100    127.0.0.1:25                  *:*                  
LISTEN     0      80     :::3306               :::*                  
LISTEN     0      128    :::22                 :::*                  
LISTEN     0      100       ::1:25                 :::*                  

//按zabbix部署要求修改/etc/php.ini的設定並重新啓動php-fpm
[root@xian ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[root@xian ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini
[root@xian ~]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[root@xian ~]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[root@xian ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
 
 
 //設定web介面
[root@xian ~]# cd zabbix-5.0.2
[root@xian zabbix-5.0.2]# cp -r ui /usr/local/nginx/html/zabbix
[root@xian zabbix-5.0.2]# chown -R nginx.nginx /usr/local/nginx/html/zabbix/

//設定nginx虛擬主機
[root@xian ~]# 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@xian ~]# chmod 777 /usr/local/nginx/html/zabbix/conf

//過載組態檔
[root@xian ~]# nginx -s reload

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

在这里插入图片描述

2. 開啓狀態頁面

[root@xian mysql]# vim /usr/local/nginx/conf/nginx.conf
在內部插入下面 下麪的內容
        location /status {
            stub_status on;
            allow 192.168.116.0/24;
            deny all;
        }

[root@xian mysql]# nginx -s reload

在这里插入图片描述

3. 設定監控指令碼

[root@xian ~]# mkdir /scripts
[root@xian ~]# cd /scripts/
[root@xian scripts]# vim handled.sh
#!/bin/bash

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

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

echo $status
[root@xian scripts]# vim Writing.sh
#!/bin/bash

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

echo $status
[root@xian scripts]# chmod +x *.sh
[root@xian scripts]# ls
handled.sh  Reading.sh  Writing.sh

4. 編輯zabbix_agent組態檔

[root@xian ~]# 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@xian ~]# pkill zabbix
[root@xian ~]# zabbix_server 
[root@xian ~]# zabbix_agentd


//測試key是否可用
[root@xian ~]# zabbix_get -s 127.0.0.1 -k check_handled
326
[root@xian ~]# zabbix_get -s 127.0.0.1 -k check_Reading
0
[root@xian ~]# zabbix_get -s 127.0.0.1 -k check_Writing
1

5. web介面設定

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

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