Zabbix 6.0 使用Elasticsearch作為 后端數據庫


參考文章:https://twocups.cn/index.php/2021/06/28/42/

001、事先准備

沒有裝好 Elasticsearch(以下簡稱 es)的可以參考<https://www.cnblogs.com/suyj/p/16014533.html>,里面有講到 es 是如何安裝並部署的。es 的默認端口是9200

Eticsearch 版本:7.9.1 ,端口:9200

Zabbix 版本:Zabbix 6.0.0beta3 ,端口:80/zabbix

002、Elasticsearch數據存儲介紹

原來我們 Zabbix 的數據是存儲在 MySQL 中的,按照數據格式的不同分別存儲的五個表中:history、history_uint、history_str、history_log、history_text。這五個表和 es 中相對應的索引關系如下。

數據類型 zabbix數據庫表 es索引類型
數字(無符號) history_uint uint
數字(浮點型) history dbl
字符 history_str str
日志 history_log log
文本 history_text text

簡單解釋一下,字符就是短的詞語,文本就是長的句子,日志是數據本身有不同屬性,可以被直接一列一列展示出來的。

當我們把 Zabbix 的數據存儲到 es 之后,原來的 MySQL 中的這五個數據庫表就不再寫入新的數據了。

003、Elasticsearch中創建索引

  首先,我們需要在 es 中創建 Zabbix 需要的索引用以接受數據,這是必須要第一步做的。否則如果我們先在 Zabbix 那邊設置好連入 es,那么 Zabbix 自然就會發現 es 中沒有相應的索引,就會直接在 es 中創建相應的索引。在這個自動創建的索引中,數據的 clock 是 Unix 時間,我們后續的 Kibana 和 Zabbix Web 是無法正常顯示的。

  所以,我們第一步必須先在 es 中手動創建相應的索引。如果創建索引的時候報錯說索引已經存在了,那可能是 Zabbix 已經先一步創建了。這時候就先停止 Zabbix 服務,然后手動刪除這五個索引,然后再按照下面的 shell 指令添加五個索引,之后設置好 Zabbix 相關配置,再啟動 Zabbix。

  這些 shell 指令直接打進命令行就行,順序無先后,注意是要在部署了 es 的那台機器上操作。

添加數字(無符號)類型的索引

curl -X PUT \ http://localhost:9200/uint \
 -H 'content-type:application/json' \ -d '{
   "settings": { "index": { "number_of_replicas": 1, "number_of_shards": 5 } }, "mappings": { "properties": { "itemid": { "type": "long" }, "clock": { "format": "epoch_second", "type": "date" }, "value": { "type": "long" } } } }'

添加數字(浮點型)類型的索引

curl -X PUT \ http://localhost:9200/dbl \
 -H 'content-type:application/json' \ -d '{
   "settings": { "index": { "number_of_replicas": 1, "number_of_shards": 5 } }, "mappings": { "properties": { "itemid": { "type": "long" }, "clock": { "format": "epoch_second", "type": "date" }, "value": { "type": "double" } } } }'

添加字符類型的索引

curl -X PUT \ http://localhost:9200/str \
 -H 'content-type:application/json' \ -d '{
   "settings": { "index": { "number_of_replicas": 1, "number_of_shards": 5 } }, "mappings": { "properties": { "itemid": { "type": "long" }, "clock": { "format": "epoch_second", "type": "date" }, "value": { "fields": { "analyzed": { "index": true, "type": "text", "analyzer": "standard" } }, "index": false, "type": "text" } } } }'

添加日志類型的索引

curl -X PUT \ http://localhost:9200/log \
 -H 'content-type:application/json' \ -d '{
   "settings": { "index": { "number_of_replicas": 1, "number_of_shards": 5 } }, "mappings": { "properties": { "itemid": { "type": "long" }, "clock": { "format": "epoch_second", "type": "date" }, "value": { "fields": { "analyzed": { "index": true, "type": "text", "analyzer": "standard" } }, "index": false, "type": "text" } } } }'

添加文本類型的索引

curl -X PUT \ http://localhost:8080/text \
 -H 'content-type:application/json' \ -d '{
   "settings": { "index": { "number_of_replicas": 1, "number_of_shards": 5 } }, "mappings": { "properties": { "itemid": { "type": "long" }, "clock": { "format": "epoch_second", "type": "date" }, "value": { "fields": { "analyzed": { "index": true, "type": "text", "analyzer": "standard" } }, "index": false, "type": "text" } } } }'

004、配置zabbix

 修改 Zabbix 的配置文件

es 那邊配置好了,我們再來修改 Zabbix 的配置文件

vim /etc/zabbix/zabbix_server.conf ​ HistoryStorageURL=127.0.0.1:9200 HistoryStorageTypes=uint,dbl,str,log,text

  由於我 Zabbix 服務端和 es 是部署在同一台機器上的,所以可以填127.0.0.1。如果不在同一台機器上,這里填 es 所在機器的 ip 地址。

修改Zabbix 前端文件

首先在文件的開頭將該配置文件中的“$DB”和“$HISTORY”設置為全局參數。 

vim /etc/zabbix/web/zabbix.conf.php
<?php // Zabbix GUI configuration file. global $DB, $HISTORY;


#修改兩個“$HISTORY”的值。
// Elasticsearch url (can be string if same url is used for all types). 
$HISTORY['url'] = 'http://127.0.0.1:9200';
// Value types stored in Elasticsearch.
$HISTORY['types'] = ['uint', 'text', 'log', 'str', 'dbl'];

重啟 zabbix-server 

systemctl restart zabbix-server

稍等一會兒之后,Kibana 和 Zabbix Web 上的數據和圖像就顯示正常了。

 

004、kibana創建索引

如果想在 Kibana 上看,那么還需要在 Kibana 上創建相應的索引(Configure an index pattern),時間過濾字段(Time Filter field name)填寫“clock”。下面簡單過一下kibana 創建的過程

http://ip:5601/app/management/kibana/indexPattern

 

 

 

 

 

 

 

 


免責聲明!

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



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