分散式監控系統之Zabbix基礎

1874發表於2020-11-18

  1、為什麼要使用監控系統?

  我們知道一個系統不管怎麼講它都會出故障,我們為了保證線上業務的最大化的可用性,通常我們要給關鍵業務做高可用;做高可用的目的是為了讓故障發生時,能夠有一個備用的解決方案,將故障轉移,從而實現服務的高可用性;那麼問題來了,我們怎麼知道系統發生了故障或者將要發生故障呢?怎麼去把將要發生的故障扼殺在搖籃裡呢?這個時候我們就需要用到監控系統;監控系統本質上不是業務系統,沒有監控系統,線上業務系統也是可以正常執行的,它的存在主要是方便我們對業務系統的重要指標資料做採集、分析,使得我們能夠更加清楚的瞭解線上系統正處於什麼狀態,cpu,記憶體,io等等一系列我們需要關心的點,都可以通過監控系統幫我們監控著,一旦被監控的主機或裝置對應指標資料出現異常,能夠及時的反饋並通知相關人員,使得我們能夠及時的發現問題,從而解決問題;簡單講,監控系統就是輔助我們時時瞭解線上系統各指標資料,當被監控的主機或裝置或服務出現異常時或即將出現異常時,它能夠通過即時通訊的方式通知管理員(比如發郵件、簡訊等等),從而使得管理員能夠提前知道線上系統處於什麼狀態,從而針對特定的異常排查原因,修復異常,對即將發生的故障扼殺在搖籃裡;

  2、zabbix是什麼?zabbix元件以及其各元件的作用

  zabbix是一個開源的分散式監控系統,它主要有zabbix-server 、zabbix-database、zabbix-web GUI 、zabbix-agent、zabbix-proxy五大元件組成;其中zabbix-server主要提供收集資料、處理資料,並將資料儲存在zabbix-database中;zabbix-database主要就是提供儲存zabbix系統所需資料的資料儲存服務;zabbix-web GUI主要作用是提供配置、展示、管理zabbix監控系統的一個web前端工具,它能將管理員的管理、配置操作通過web介面儲存到zabbix-database中,並將zabbix-database中儲存的指標資料通過web介面進行展示;zabbix-agent是zabbix的一個專有客戶端代理,它主要執行在各個被監控的主機之上,其作用是接受zabbix-server傳送的各種採集資料指令,並將採集到的資料通過專有代理zabbix-agent傳送或響應給zabbix-server;zabbix-proxy是zabbix的一個服務端代理,主要作用是代理zabbix-server接收各個zabbix-agent傳送或響應的指標資料;

  3、zabbix架構圖

  上圖主要描述了zabbix監控系統的元件間的工作過程;首先zabbix的配置、管理以及展示都是通過zabbix web GUI這個元件進行的,管理員通過zabbix web GUI把要監控的主機、監控項、觸發器等等一系列配置寫進zabbix-database,然後zabbix-server到資料庫中拿到對應的配置,進行應用;zabbix-server通過配置資訊定義的各個通道,去採集對應主機或裝置上要監控的指標資料,將採集到的資料進行處理以後存放到資料庫,最後通過web GUI到資料庫取資料進行展示;

  4、zabbix監控系統部署

  環境說明

主機名 角色 ip地址:埠
node01 zabbix web GUI 192.168.0.41:80
node02 zabbix database 192.168.0.42:3306
node03 zabbix-server/zabbix-agent 192.168.0.43:10051/10050

 

  

 

 

 

 

 

  zabbix-database部署

  zabbix-databse本質上就是一個資料庫服務,zabbix主要支援mysql(或者mariadb)和pgsql,兩種資料庫系統,我們部署zabbix-database就是部署一個mysql(或mariadb)或pgsql即可;

  準備mariadb yum源

[root@node02 ~]# cat /etc/yum.repos.d/mariadb.repo 
[mariadb]
name=mariadb repo
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mariadb/mariadb-10.0.38/yum/centos/7/x86_64/
gpgcheck=0
[root@node02 ~]# 

  安裝MariaDB-server

[root@node02 ~]# yum install -y MariaDB-server

  新增zabbix-databse.cnf配置到/etc/my.cnf.d/目錄下

[root@node02 ~]# cat /etc/my.cnf.d/zabbix-database.cnf
[mysqld]
bind-address = 0.0.0.0
default-storage-engine = innodb
innodb_file_per_table = on
max_connections = 4096
collation-server = utf8_general_ci
character-set-server = utf8
skip_name_resolve
[root@node02 ~]# 

  啟動maridb

[root@node02 ~]# systemctl start mariadb
Failed to start mariadb.service: Unit not found.
[root@node02 ~]# /etc/init.d/mysql start
Starting MariaDB.201117 23:15:28 mysqld_safe Logging to '/var/lib/mysql/node02.test.org.err'.
201117 23:15:28 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
 SUCCESS! 
[root@node02 ~]# ss -tnl
State      Recv-Q Send-Q           Local Address:Port                          Peer Address:Port              
LISTEN     0      128                          *:22                                       *:*                  
LISTEN     0      100                  127.0.0.1:25                                       *:*                  
LISTEN     0      128                          *:3306                                     *:*                  
LISTEN     0      128                         :::22                                      :::*                  
LISTEN     0      100                        ::1:25                                      :::*                  
[root@node02 ~]# 

  提示:清華源裝的MariaDB-server沒有提供unit file,只有一個啟動指令碼,所以啟動時不能用systemctl 方式啟動;當然也可以寫一個unit file,使用systemctl方式啟動;

  設定mysql開機啟動

[root@node02 ~]# chkconfig --level 3 mysql on
[root@node02 ~]# chkconfig --list mysql

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysql           0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@node02 ~]# 

  設定root密碼,清除test庫和相關賬號資訊

[root@node02 ~]# 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
New password: 
Re-enter new password: 
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 
 ... 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
 - 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@node02 ~]# 

  建立zabbix資料庫,並設定預設字符集為utf8

[root@node02 ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.0.38-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)]> create database zabbix character set utf8 collate utf8_bin;                          
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]>

  建立zabbix使用者,授權允許從192.168.的網路連入管理zabbix資料庫,並設定其密碼為admin123.com

MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@'192.168.%.%' identified by 'admin123.com';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> 

  到此zabbix-database就準備好了

  在node03上安裝zabbix-server、zabbix-agent

  配置yum源

[root@node03 ~]# cat /etc/yum.repos.d/zabbix.repo 
[zabbix-server]
name=zabbix-server repo
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/
gpgcheck=0

[non-supported]
name=non-supported repo
baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/x86_64/
gpgcheck=0
[root@node03 ~]# 

  將zabbix.reop複製到node01

[root@node03 ~]# scp /etc/yum.repos.d/zabbix.repo node01:/etc/yum.repos.d/
zabbix.repo                                                                   100%  242   165.8KB/s   00:00       
[root@node03 ~]# 

  在node03上安裝zabbix-server和zabbix-agent

[root@node03 ~]# yum install -y zabbix-server-mysql zabbix-agent

  提示:如果資料庫用的是pgsql就安裝zabbix-server-pgsql;在zabbix-server上安裝agent的主要原因是可以監控zabbix-server自身的一些指標;

  使用zabbix使用者匯入表到zabbix庫

[root@node03 ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -h192.168.0.42 -uzabbix -p zabbix 
Enter password: 
[root@node03 ~]# 

  驗證:檢視zabbix庫是否有表生成?

[root@node03 ~]# mysql -h192.168.0.42 -uzabbix -padmin123.com zabbix -e 'show tables;'|wc -l
145
[root@node03 ~]# 

  提示:只要能夠統計到對應庫下有表的數量,說明我們匯入表達操作就沒有什麼問題,通常這個匯入表,會匯入很多張表;

  配置zabbix-server

[root@node03 ~]# grep -Ei ^[^#] /etc/zabbix/zabbix_server.conf
ListenPort=10051
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBHost=192.168.0.42
DBName=zabbix
DBUser=zabbix
DBPassword=admin123.com
DBPort=3306
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
[root@node03 ~]# 

  提示:配置zabbix-server主要配置連線資料庫相關的配置,其他配置可以保持預設即可;

  配置zabbix-agent

[root@node03 ~]# grep -Ei ^[^#] /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=127.0.0.1
ListenPort=10050
ListenIP=0.0.0.0
StartAgents=3
ServerActive=127.0.0.1
Hostname=Zabbix server
HostnameItem=system.hostname
Include=/etc/zabbix/zabbix_agentd.d/*.conf
[root@node03 ~]# 

  提示:zabbix-server主機上的zabbix-agent幾乎不用修改配置,保持預設即可;

  啟動zabbix-server,並將其設定為開機啟動

[root@node03 ~]# systemctl start zabbix-server.service
[root@node03 ~]# systemctl enable zabbix-server.service
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-server.service to /usr/lib/systemd/system/zabbix-server.service.
[root@node03 ~]# ss -tnl
State      Recv-Q Send-Q           Local Address:Port                          Peer Address:Port              
LISTEN     0      128                          *:22                                       *:*                  
LISTEN     0      100                  127.0.0.1:25                                       *:*                  
LISTEN     0      128                          *:10051                                    *:*                  
LISTEN     0      128                         :::22                                      :::*                  
LISTEN     0      100                        ::1:25                                      :::*                  
LISTEN     0      128                         :::10051                                   :::*                  
[root@node03 ~]# 

  提示:zabbix-server預設監聽10051,請確保10051埠處於正常監聽狀態即可;到此zabbix-server就配置啟動成功;

  啟動zabbix-agent,並設定開機自啟動

[root@node03 ~]# systemctl start zabbix-agent.service 
[root@node03 ~]# systemctl enable zabbix-agent.service
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
[root@node03 ~]# ss -tnl
State      Recv-Q Send-Q           Local Address:Port                          Peer Address:Port              
LISTEN     0      128                          *:22                                       *:*                  
LISTEN     0      100                  127.0.0.1:25                                       *:*                  
LISTEN     0      128                          *:10050                                    *:*                  
LISTEN     0      128                          *:10051                                    *:*                  
LISTEN     0      128                         :::22                                      :::*                  
LISTEN     0      100                        ::1:25                                      :::*                  
LISTEN     0      128                         :::10051                                   :::*                  
[root@node03 ~]# 

  提示:請確保10050埠正常監聽;

  在node01上安裝zabbix-web-mysql

[root@node01 ~]# yum install -y zabbix-web-mysql

  修改時區資訊

  提示:除了修改以上配置可以修改時區以外,我們也可以編輯/etc/php.ini檔案,找到date.timezone將其註釋去掉,寫上對應的時區資訊儲存退出即可;這兩種方式選一種修改就行;

  啟動httpd,並將其設定為開機自動啟動

[root@node01 ~]# systemctl start httpd
[root@node01 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@node01 ~]# ss -tnl
State      Recv-Q Send-Q           Local Address:Port                          Peer Address:Port              
LISTEN     0      128                          *:22                                       *:*                  
LISTEN     0      100                  127.0.0.1:25                                       *:*                  
LISTEN     0      128                         :::80                                      :::*                  
LISTEN     0      128                         :::22                                      :::*                  
LISTEN     0      100                        ::1:25                                      :::*                  
[root@node01 ~]# 

  提示:請確保80埠正常監聽即可;到此zabbix-web元件就安裝配置好了;接下來我們就可以使用瀏覽器訪問zabbix-web服務;

  用瀏覽器訪問zabbix-web,進行zabbix安裝

  提示:這裡主要是驗證環境,要全部是ok狀態才可以;

  提示:這裡是配置資料庫連線相關資訊,填寫對應資料庫相關資訊,點選下一步即可;

  提示:這裡是填寫zabbix-server相關資訊;

  提示:預設使用者名稱是Admin密碼是zabbix;

  到此zabbix監控系統基礎環境就搭建好了;後續我們就可以在這個web頁面上做監控配置和管理以及監控資料的展示;

相關文章