版权声明:本文为博主原创文章,转载请注明出处:https://twocups.cn/index.php/2021/06/28/42/

Zabbix 官方配置:https://www.zabbix.com/documentation/5.0/zh/manual/appendix/install/elastic_search_setup

事先准备

没有装好 Elasticsearch(以下简称 es)的可以去看下我之前的Elastic Stack系列,里面有讲到 es 是如何安装并部署的。es 的默认端口是9200,但是我是在公司的测试环境里部署的,并且又想能在办公桌面直接访问到 es,所以把 es 的端口改成了8080。不同公司的内部测试云环境都是不同的,阿里云叫孤岛,腾讯叫 DevCloud。这类环境是专门用于测试的,和正式的线上环境很像,不过会放更多的权限给开发者用于调试。这些环境都不能直接访问,都需要先登陆代理,然后通过代理去访问。这就导致了在办公桌面是无法直接访问到测试环境里的端口,但有几个端口是被放出来的,比如我将 es 部署到的8080端口。es 修改端口的方法我在 es 部署那一章提到过,如果 es 还没有部署好的,可以移步去看一下。

Eticsearch 版本:7.13.1,端口:8080

Zabbix 版本:5.0 LTS,端口:80/zabbix

Elasticsearch中的数据存储

原来我们 Zabbix 的数据是存储在 MariaDB 中的,按照数据格式的不同分别存储的五个表中:history、history_uint、history_str、history_log、history_text。这五个表和 es 中相对应的索引关系如下。

数据类型zabbix数据库表es索引类型
数字(无符号)history_uintuint
数字(浮点型)historydbl
字符history_strstr
日志history_loglog
文本history_texttext

简单解释一下,字符就是短的词语,文本就是长的句子,日志是数据本身有不同属性,可以被直接一列一列展示出来的。

当我们把 Zabbix 的数据存储到 es 之后,原来的 MariaDB 中的这五个数据库表就不再写入新的数据了。

在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:8080/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:8080/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:8080/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:8080/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"
        }
    }
  }
}'

配置Zabbix

es 那边配置好了,我们再来修改 Zabbix 的配置文件。

vim /etc/zabbix/zabbix_server.conf

HistoryStorageURL=127.0.0.1:8080
HistoryStorageTypes=uint,dbl,str,log,text

由于我 Zabbix 服务端和 es 是部署在同一台机器上的,所以可以填127.0.0.1。如果不在同一台机器上,这里填 es 所在机器的 ip 地址。后面跟的 8080 是因为我 es 部署在 8080 端口上,如果你 es 部署在其他端口上,这里也要改一下。

之后,我们再配置 Zabbix 前端文件。

vim /etc/zabbix/web/zabbix.conf.php

首先在文件的开头将该配置文件中的“$DB”和“$HISTORY”设置为全局参数。

<?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:8080';
// Value types stored in Elasticsearch.
$HISTORY['types'] = ['uint', 'text', 'log', 'str', 'dbl'];

如果想在 Kibana 上看,那么还需要在 Kibana 上创建相应的索引(Configure an index pattern),时间过滤字段(Time Filter field name)填写“clock”。这些 Kibana 都会提示你的。

到这里,配置就全部完成了。我们需要重启 zabbix-server 和 Zabbix Web(httpd)。

systemctl restart zabbix-server httpd

稍等一会儿之后,Kibana 和 Zabbix Web 上的数据和图像就显示正常了。

林皓伟

发表回复

您的电子邮箱地址不会被公开。