001、事先准备

没有装好 Elasticsearch(以下简称 es)的可以参考<;,里面有讲到 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 创建的过程

  

zabbix版本对应操作系统 zabbix 6.0_数据

 

 

zabbix版本对应操作系统 zabbix 6.0_json_02

 

 

zabbix版本对应操作系统 zabbix 6.0_数据_03

 

 

zabbix版本对应操作系统 zabbix 6.0_数据_04