1. 程式人生 > >分散式監控系統之Zabbix proxy

分散式監控系統之Zabbix proxy

  前文我們瞭解了zabbix 使用snmp和jmx通道採集資料的相關使用配置,回顧請參考https://www.cnblogs.com/qiuhom-1874/p/14029864.html;今天我們來說zabbix proxy;

  1、zabbix proxy是什麼?為什麼要使用zabbix proxy?

  簡單講zabbix proxy就是zabbix server的代理服務,它能幫助zabbix server採集資料,然後再統一的傳送給zabbix server ;它的功能和zabbix server 很類似,zabbix server 能做到它幾乎都能做;主要作用就是代理zabbix server ;我們知道監控一個主機或者服務,通常監控項有很多,每個監控項的資料採集工作都會消耗zabbix server 一個socket;這樣一來監控一個主機少則十幾個socket連線,多則幾十個上百個;我們試想一個場景,我們要監控一個叢集,這個叢集有100臺物理主機,每個物理主機都要監控cpu,記憶體,磁碟等等,一臺伺服器平均監控項為20個,那麼100臺伺服器就要2000個socket連線;這意味著zabbix server要有2000個socket連線需要維持;這樣一來無疑對zabbix server效能有很大的影響;為了降低zabbix server連線socket數量過大而帶來的效能消耗,此時zabbix server就應該委託其他主機來代理收集資料;這個代理就是zabbix proxy;除了以上場景,比如跨機房的場景我們也需要用zabbix proxy,每個zabbix proxy只負責採集本地同一機房裡的主機的資料,然後統一發送給zabbix server;這樣一來可以減少zabbix server的socket連線數量,從而降低zabbix server的壓力;

  2、zabbix proxy內部元件

  zabbix proxy主要由zabbix proxy和資料庫組成;它這個資料庫不是永久儲存採集資料的,它可以將採集的資料臨時存放在資料庫中,當它把對應的資料傳送給zabbix server以後,本地臨時儲存的資料就會清除;除了快取採集的資料意外,它還可以用來儲存在zabbix server上拉取的監控配置資訊;

  3、zabbix proxy 部署

  準備yum倉庫檔案

[root@node03 ~]# scp /etc/yum.repos.d/zabbix.repo node06:/etc/yum.repos.d/
zabbix.repo                                                                   100%  242   164.4KB/s   00:00    
[root@node03 ~]# scp /etc/yum.repos.d/mariadb.repo node06:/etc/yum.repos.d/
mariadb.repo                                                                  100%  129   116.6KB/s   00:00    
[root@node03 ~]# 

  安裝MariaDB-server

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

  配置資料庫

[root@node06 ~]# cat /etc/my.cnf.d/zabbix_proxy.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@node06 ~]# 

  啟動mariadb

[root@node06 ~]# /etc/init.d/mysql start
Starting MariaDB.201125 22:12:03 mysqld_safe Logging to '/var/lib/mysql/node06.test.org.err'.
201125 22:12:03 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
 SUCCESS! 
[root@node06 ~]# 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@node06 ~]# 

  設定root密碼

[root@node06 ~]# 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

  建立資料庫和使用者授權

[root@node06 ~]# mysql -uroot -padmin123.com
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
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)]> grant all privileges on zabbix.* to zabbix_proxy@'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 proxy

[root@node06 ~]# yum install -y zabbix-proxy-mysql

  檢視zabbix proxy 安裝的檔案

[root@node06 ~]# rpm -ql zabbix-proxy-mysql
/etc/logrotate.d/zabbix-proxy
/etc/zabbix/zabbix_proxy.conf
/usr/lib/systemd/system/zabbix-proxy.service
/usr/lib/tmpfiles.d/zabbix-proxy.conf
/usr/lib/zabbix/externalscripts
/usr/sbin/zabbix_proxy_mysql
/usr/share/doc/zabbix-proxy-mysql-4.0.26
/usr/share/doc/zabbix-proxy-mysql-4.0.26/AUTHORS
/usr/share/doc/zabbix-proxy-mysql-4.0.26/COPYING
/usr/share/doc/zabbix-proxy-mysql-4.0.26/ChangeLog
/usr/share/doc/zabbix-proxy-mysql-4.0.26/NEWS
/usr/share/doc/zabbix-proxy-mysql-4.0.26/README
/usr/share/doc/zabbix-proxy-mysql-4.0.26/schema.sql.gz
/usr/share/man/man8/zabbix_proxy.8.gz
/var/log/zabbix
/var/run/zabbix
[root@node06 ~]# 

  匯入表

[root@node06 ~]# zcat /usr/share/doc/zabbix-proxy-mysql-4.0.26/schema.sql.gz |mysql -uzabbix_proxy -padmin123.com -h192.168.0.46 zabbix
[root@node06 ~]#

  驗證:檢視對應資料庫是否有表生成?

[root@node06 ~]# mysql -uzabbix_proxy -padmin123.com -h192.168.0.46 zabbix -e 'show tables;' |wc -l
145
[root@node06 ~]#

  提示:能夠統計到對應表的數量,說明我們匯入表的操作沒有問題;

  配置zabbix proxy

[root@node06 ~]# grep -Ei "^[a-z]" /etc/zabbix/zabbix_proxy.conf
ProxyMode=0
Server=192.168.0.43
ServerPort=10051
HostnameItem=system.hostname
ListenPort=10051
LogFile=/var/log/zabbix/zabbix_proxy.log
LogFileSize=0
DebugLevel=3
PidFile=/var/run/zabbix/zabbix_proxy.pid
SocketDir=/var/run/zabbix
DBHost=192.168.0.46
DBName=zabbix
DBUser=zabbix_proxy
DBPassword=admin123.com
HeartbeatFrequency=60
ConfigFrequency=60
DataSenderFrequency=1
StartPollers=5
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
[root@node06 ~]# 

  提示:主要配置zabbix server的地址以及zabbix proxy的hostname和資料庫相關的幾個選項以及後面的心跳頻率和配置檔案更新頻率以及資料傳送頻率;其他保持預設即可;這裡需要提醒下,預設情況zabbix proxy 工作為主動模式,所謂主動模式是指zabbix proxy主動到zabbix server上獲取監控配置和推送採集的資料;如果需要配置為被動,這需要將ProxyMode更改為1即可;Server是配置zabbix server的地址,意思是告訴proxy到到哪裡獲取配置以及將採集的資料推送到哪裡;後面的HeartbeatFrequency是用來指定多長時間傳送一次心跳資訊給zabbix server,預設是60秒;ConfigFrequency是用來指定更新監控配置資訊的頻率,意思是多久去zabbix server 上同步一下監控配置;預設是3600;這個可以根據實際情況更改;DataSenderFrequency是用於指定多長時間發一次資料給zabbix server,預設為1秒;

  啟動zabbix proxy

[root@node06 ~]# systemctl start zabbix-proxy.service 
[root@node06 ~]# 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                          *:3306                                     *:*                  
LISTEN     0      128                         :::22                                      :::*                  
LISTEN     0      100                        ::1:25                                      :::*                  
LISTEN     0      128                         :::10051                                   :::*                  
[root@node06 ~]# 

  提示:zabbix proxy 預設監聽在10051,啟動後請確保該埠正常監聽;

  安裝zabbix agent用於監控zabbix proxy自身

[root@node06 ~]# yum install -y zabbix-agent

  配置zabbix agent

[root@node06 ~]# grep -Ei "^[a-z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.0.46
ListenPort=10050
ListenIP=0.0.0.0
ServerActive=192.168.0.46
HostnameItem=system.hostname
Include=/etc/zabbix/zabbix_agentd.d/*.conf
[root@node06 ~]# 

  提示:這裡的配置和我們前邊配置zabbix agent幾乎一樣,不同的是我們需要將Server指向zabbix proxy的地址和埠;如果其他agent需要用到proxy來代理,需要配置執行proxy來採集資料;這個Server的值可以是多個,分別用逗號隔開即可;同樣ServerActive也應該指向zabbix proxy的地址;

  啟動zabbix agent

[root@node06 ~]# systemctl start zabbix-agent.service 
[root@node06 ~]# 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                          *:3306                                     *:*                  
LISTEN     0      128                         :::22                                      :::*                  
LISTEN     0      100                        ::1:25                                      :::*                  
LISTEN     0      128                         :::10051                                   :::*                  
[root@node06 ~]#

  在zabbix web介面配置新增zabbix proxy

 

  提示:在administration--->proxIes---->create proxy點選進入新建proxy的頁面;

  提示:這裡的proxy name必須和本地執行zabbix proxy服務主機名稱相同;填寫好proxy name以後點選新增即可;

 

  新增主機,使用我們剛才新增到proxy來代理採集資料

  提示:新增主機一定要選擇對應要使用的zabbix proxy;

  連結模板

  提示:選擇好模板以後,最後點選add將主機新增到主機列表;

  提示:可以看到現在新增到主機,前邊都有對應proxy的名稱;過一會我們就可以看到proxy將模板上的監控資料採集後,傳送給zabbix server,此時就可以看到對應的主機上的監控資料;

  驗證:檢視對應主機是否有監控資料?

  提示:可以看到node06連結的模板監控項也陸續有資料,說明proxy採集到資料已經發送給zabbix server ;

  檢視zabbix proxy狀態

  提示:可以看到現在node06.test.org這個proxy有一個host,34個item,vps為0.39;如果其他主機需要用該proxy代理採集資料,我們需要將對應的agent配置允許該proxy來採集資料;

  到此,zabbix proxy的安裝,測試就完成