1. 程式人生 > 其它 >Zabbix 6.0 使用Elasticsearch作為 後端資料庫

Zabbix 6.0 使用Elasticsearch作為 後端資料庫

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

001、事先準備

沒有裝好 Elasticsearch(以下簡稱 es)的可以去看下我之前的Elastic Stack系列,裡面有講到 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 前端檔案