Linux下Zabbix5.0 LTS監控基礎原理及安裝部署(圖文教程)
- 2021 年 10 月 29 日
- 筆記
- Linux Zabbix, Linux 實例
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 //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 瀏覽器訪問 //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 //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設置。