分布式監控系統之Zabbix部署


  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頁面上做監控配置和管理以及監控數據的展示;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM