Linux下Zabbix5.0 LTS監控基礎原理及安裝部署(圖文教程)

上古南城發表於2021-10-29

Zabbix 是什麼?

  • zabbix 是一個基於 Web 介面的提供分散式系統監視以及網路監視功能的企業級的開源解決方案。通過 C/S 模式採集資料,通過 B/S 模式在 Web 端展示和配置,能監視各種網路引數,保證伺服器系統的安全運營;並提供靈活的通知機制以讓系統管理員快速定位/解決存在的各種問題。
  • zabbix 由2部分構成,zabbix server 與可選元件 zabbix agent;zabbix server 可以通過 SNMP,zabbix agent,ping,埠監視等方法提供對遠端伺服器/網路狀態的監視,資料收集等功能。zabbix agent 需要安裝在被監視的目標伺服器上,它主要完成對硬體資訊或與作業系統有關的記憶體,CPU 等資訊的收集。

Zabbix監控系統執行大概流程:

  Zabbix由zabbix server與可選元件zabbix agent兩部分組成。

  • Zabbix Server 可以通過 SNMP,Zabbix agent,Ping,埠監視等方法提供對遠端伺服器/網路狀態的監視,資料收集功能,可以在執行 Linux,Solaris,HP-UX,AIX,FreeBSD,OpenBSD,OS X,Windows多平臺。
  • Zabbix Agent 安裝在需要被監控的目標伺服器上,主要完成對硬體資訊與作業系統有關的記憶體,CPU 等資訊收集。
  • Zabbix Server可以單獨監視遠端伺服器的服務狀態,同時也可以與Zabbix Agent結合。可以輪詢Zabbix Agent主動接收監視資料(trapping傳遞 方式),同時還可以被動接收Zabbix Agent傳送的資料。
    • 主動:agent請求server獲取主動的監控項列表,並主動將監控項內需要檢測的資料提交給server/proxy。
    • 被動:server向agent請求獲取監控項的資料,agent返回資料。

  那實際監控中是用主動的還是被動的呢?這裡主要涉及兩個地方:

  • (1) 新建監控專案時,選擇的是zabbix代理還是zabbix端點代理程式(主動式),前者是被動模式,後者是主動模式。
  • (2) agentd配置檔案中StartAgents引數的設定,如果為0,表示禁止被動模式,否則開啟。一般建議不要設定為0,因為監控專案很多時,可以部分使用主動,部分使用被動模式。

Zabbix監控常用架構

  • (1) server-agentd模式:這個是最簡單的架構了,常用於監控主機比較少的情況下。
  • (2) server-proxy-agentd模式:這個常用於比較多的機器,使用proxy進行分散式監控,有效的減輕server端的壓力。

 

 Zabbix官方站點:【文件原始碼包rpm包】下載提供了兩種版本:LTS版(Long Term Support)和標準版

Zabbix YUM安裝部署過程如下:

屬性 Zabbix Server Zabbix Client
節點 ZabbixServer-01 ZabbixClient-01
系統 CentOS Linux release 7.5.1804 (Minimal) CentOS Linux release 7.5.1804 (Minimal)
核心 3.10.0-862.el7.x86_64 3.10.0-862.el7.x86_64
SELinux setenforce 0 | disabled setenforce 0 | disabled
Firewlld systemctl stop/disable firewalld systemctl stop/disable firewalld
IP地址 172.16.70.37

172.16.70.171

  • 第一部分:ZabbixServer-01 上操作。
# 準備環境
[root@ZabbixServer-01 ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@ZabbixServer-01 ~]# uname -r
3.10.0-862.el7.x86_64
[root@ZabbixServer-01 ~]# setenforce 0
[root@ZabbixServer-01 ~]# sed -i '7s#enforcing#disabled#' /etc/selinux/config
[root@ZabbixServer-01 ~]# systemctl stop firewalld && systemctl disable firewalld
[root@ZabbixServer-01 ~]# yum install -y vim net-tools lsof wget curl lrzsz

# 安裝zabbix noarch.rpm
[root@ZabbixServer-01 ~]# rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm

# 修改/etc/yum.repos.d/zabbix.repo,將[zabbix-frontend]下的enabled改為1【注意項】
[root@ZabbixServer-01 ~]# sed -i.bak '11s#enabled=0#enabled=1#' /etc/yum.repos.d/zabbix.repo
[root@ZabbixServer-01 ~]# yum clean all
[root@ZabbixServer-01 ~]# yum repolist | grep zabbix
zabbix/x86_64               Zabbix Official Repository - x86_64              200
zabbix-frontend/x86_64      Zabbix Official Repository frontend - x86_64     183
zabbix-non-supported/x86_64 Zabbix Official Repository non-supported - x8      5

# 安裝zabbix server和agent
[root@ZabbixServer-01 ~]# yum install -y zabbix-server-mysql zabbix-agent

# 安裝Software Collections便於後續安裝高版本的php
[root@ZabbixServer-01 ~]# yum install -y centos-release-scl

# 安裝zabbix FE和相關環境
[root@ZabbixServer-01 ~]# yum install -y zabbix-web-mysql-scl zabbix-apache-conf-scl

# 安裝centos7預設的mariadb資料庫
[root@ZabbixServer-01 ~]# yum install -y mariadb mariadb-server
[root@ZabbixServer-01 ~]# systemctl start mariadb && systemctl enable mariadb

# 安全初始化mariadb並配置root密碼
[root@ZabbixServer-01 ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):  # 空密碼,直接回車
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y   # 是否設定root密碼
New password:    # root密碼
Re-enter new password:  # 再次輸入root密碼
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y  # 是否刪除匿名賬號
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y  # 是否禁止root遠端登入
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y  # 是否刪除test庫和test庫的訪問許可權
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y  # 是否重新整理授權表使其立即生效
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

# 測試root登入,並授權
[root@ZabbixServer-01 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
# 建立zabbix資料庫
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
# 建立zabbix使用者
MariaDB [(none)]> create user zabbix@localhost identified by 'zabbix@01';
Query OK, 0 rows affected (0.00 sec)
# 授權zabbix許可權
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost;
Query OK, 0 rows affected (0.00 sec)
# 重新整理授權,使其立即生效
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye

# 檢視使用者許可權
MariaDB [(none)]> select user,host,password from mysql.user;
+--------+-----------+-------------------------------------------+
| user   | host      | password                                  |
+--------+-----------+-------------------------------------------+
| root   | localhost | *A35F952D7E492A65F4DB82E1ECBFB4BBBC415BFF |
| root   | 127.0.0.1 | *A35F952D7E492A65F4DB82E1ECBFB4BBBC415BFF |
| root   | ::1       | *A35F952D7E492A65F4DB82E1ECBFB4BBBC415BFF |
| zabbix | localhost | *A35F952D7E492A65F4DB82E1ECBFB4BBBC415BFF |
+--------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> show grants for zabbix@'localhost';
+---------------------------------------------------------------------------------------------------------------+
| Grants for zabbix@localhost                                                                                   |
+---------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'zabbix'@'localhost' IDENTIFIED BY PASSWORD '*A35F952D7E492A65F4DB82E1ECBFB4BBBC415BFF' |
| GRANT ALL PRIVILEGES ON `zabbix`.* TO 'zabbix'@'localhost'                                                    |
+---------------------------------------------------------------------------------------------------------------+
2 rows in set (0.01 sec)

# 測試zabbix使用者登入
[root@ZabbixServer-01 ~]# mysql -u zabbix -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show grants for current_user();
+---------------------------------------------------------------------------------------------------------------+
| Grants for zabbix@localhost                                                                                   |
+---------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'zabbix'@'localhost' IDENTIFIED BY PASSWORD '*A35F952D7E492A65F4DB82E1ECBFB4BBBC415BFF' |
| GRANT ALL PRIVILEGES ON `zabbix`.* TO 'zabbix'@'localhost'                                                    |
+---------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

# 匯入zabbix資料庫,zabbix資料庫使用者為zabbix,密碼為zabbix@01
[root@ZabbixServer-01 ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

# 編輯配置檔案/etc/zabbix/zabbix_server.conf,設定資料庫密碼
[root@ZabbixServer-01 ~]# sed -i.bak '/# DBPassword=/a\DBPassword=zabbix@01' /etc/zabbix/zabbix_server.conf

# 編輯配置檔案/etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf,修改時區
[root@ZabbixServer-01 ~]# sed -i.bak '$c php_value[date.timezone] = Asia/Shanghai' /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf

# 重啟服務並設定開啟自啟動
[root@ZabbixServer-01 ~]# systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
[root@ZabbixServer-01 ~]# systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm

[root@ZabbixServer-01 ~]# netstat -nutpl | grep -E 'zabbix|mysql|php|http'
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      2120/zabbix_agentd
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN      2127/zabbix_server
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      2118/php-fpm: maste
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      1991/mysqld
tcp6       0      0 :::10050                :::*                    LISTEN      2120/zabbix_agentd
tcp6       0      0 :::10051                :::*                    LISTEN      2127/zabbix_server
tcp6       0      0 :::80                   :::*                    LISTEN      2117/httpd

[root@ZabbixServer-01 ~]# hostname -I
172.16.70.37

瀏覽器訪問 http://172.16.70.37/zabbix
  •  首次訪問時需要進行一些初始化的設定,我們按照提示操作即可,點選“Next setp”。

  • 檢查各個元件配置是否正常,全部顯示“OK”,點選“Next setp”。

 

 

  •  預設資料庫埠3306,若在安裝資料庫自定義了埠,則需要在這修改,點選“Next setp”。

  •  可選配置,當有多臺ZabbixServer時,可在這設定“Name”,會顯示在頁面標籤,以便區分,點選“Next setp”。

 

  •  檢查所填的資訊,如有誤點選“Back”返回修改,無誤,點選“Next setp”。

 

  •  點選“Finish”完成配置。

 

  •  登入賬號預設為 Admin,預設密碼:zabbix

 

  •  首頁,儀表盤。

 

  •  設定中文模式。

 

  • 此時,頁面已成為中文模式。

 

  • 語言設定為中文時,頁面亂碼如圖,有小方塊,影響閱讀

  •  Windows10字型路徑,選擇想更換的字型,複製貼上至桌面。

 

  • 再次回到ZabbixServer-01機操作
[root@ZabbixServer-01 ~]# cd /usr/share/zabbix/assets/fonts/
[root@ZabbixServer-01 fonts]# mv graphfont.ttf graphfont.ttf.bak
[root@ZabbixServer-01 fonts]# rz     # 上傳貼上在桌面文字
[root@ZabbixServer-01 fonts]# ls
graphfont.ttf.bak  simsun.ttc
[root@ZabbixServer-01 fonts]# mv simsun.ttc simsun.ttf  # 修改擴充名
[root@ZabbixServer-01 fonts]# ls
graphfont.ttf.bak  simsun.tt

# 修改Zabbix的配置檔案/usr/share/zabbix/include/defines.inc.php
[root@ZabbixServer-01 ~]# sed -i.bak 's/graphfont/simsun/g' /usr/share/zabbix/include/defines.inc.php
解析:將下面兩行graphfont改為simsun
  define('ZBX_GRAPH_FONT_NAME',        'graphfont'); // font file name
  define('ZBX_FONT_NAME', 'graphfont');
[root@ZabbixServer-01 ~]# systemctl restart zabbix-server

 

  • 第二部分:ZabbixClient-01 上操作。
# 準備環境
[root@ZabbixClient-01 ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@ZabbixClient-01 ~]# uname -r
3.10.0-862.el7.x86_64
[root@ZabbixClient-01 ~]# setenforce 0
[root@ZabbixClient-01 ~]# sed -i '7s#enforcing#disabled#' /etc/selinux/config
[root@ZabbixClient-01 ~]# systemctl stop firewalld && systemctl disable firewalld
[root@ZabbixClient-01 ~]# yum install -y vim net-tools lsof wget curl

# 安裝zabbix-agent/sender
[root@ZabbixClient-01 ~]# rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
[root@ZabbixClient-01 ~]# yum install -y zabbix-agent zabbix-sender

# 修改配置檔案/etc/zabbix/zabbix_agentd.conf
[root@ZabbixClient-01 ~]# sed -i.bak '117s/Server=127.0.0.1/Server=172.16.70.37/;158s/ServerActive=127.0.0.1/ServerActive=172.16.70.37/' /etc/zabbix/zabbix_agentd.conf
[root@ZabbixClient-01 ~]#sed -i '169s/Hostname=Zabbix server/Hostname=ZabbixClient-01/' /etc/zabbix/zabbix_agentd.conf

# 解析
  Server=172.16.70.37      # Zabbix Server伺服器IP(被動)
  ServerActive=172.16.70.37  # Zabbix Server伺服器IP(主動)
  Hostname=ZabbixClient-01   # 本機能被server端識別的名稱

# 啟動服務
[root@ZabbixClient-01 ~]# systemctl start zabbix-agent
[root@ZabbixClient-01 ~]# netstat -nuptl | grep zabbix
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      4839/zabbix_agentd
tcp6       0      0 :::10050                :::*                    LISTEN      4839/zabbix_agentd
[root@ZabbixClient-01 ~]# ps -ef |grep zabbix
zabbix     4839      1  0 11:33 ?        00:00:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix     4840   4839  0 11:33 ?        00:00:00 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
zabbix     4841   4839  0 11:33 ?        00:00:00 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
zabbix     4842   4839  0 11:33 ?        00:00:00 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
zabbix     4843   4839  0 11:33 ?        00:00:00 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
zabbix     4844   4839  0 11:33 ?        00:00:00 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
  •  Zabbix Web設定。

 

 

 

 

 

相關文章