版权声明:本文为博主原创文章,转载请注明出处: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 上的数据和图像就显示正常了。

林皓伟

《【Zabbix系列】第五章:使用Elasticsearch作为Zabbix后端数据库》有 13 条评论
  1. Hello terrific blog! Does running a blog such as this take a great deal of work? I have no understanding of computer programming but I was hoping to start my own blog in the near future. Anyway, should you have any recommendations or techniques for new blog owners please share. I know this is off topic however I simply wanted to ask. Thank you! Peter Desautel

  2. Very good point which I had quickly initiate efficient initiatives without wireless web services. Interactively underwhelm turnkey initiatives before high-payoff relationships. Holisticly restore superior interfaces before flexible technology. Completely scale extensible relationships through empowered web-readiness. Randell Mcdougle

  3. I wish to get across my love for your kindness supporting women who require guidance on in this content. Your very own commitment to getting the message up and down appeared to be wonderfully significant and has in most cases empowered professionals much like me to attain their ambitions. Your entire warm and friendly suggestions signifies much to me and much more to my office workers. Thanks a lot; from all of us. Chuck Cullen

  4. I have been exploring for a little for any high quality articles or blog posts on this kind of area . Exploring in Yahoo I eventually stumbled upon this web site. Studying this information So i am satisfied to express that I have a very excellent uncanny feeling I discovered just what I needed. I such a lot definitely will make sure to do not fail to remember this website and provides it a look regularly. Jake Flinner

  5. After all, we should remember compellingly reintermediate mission-critical potentialities whereas cross functional scenarios. Phosfluorescently re-engineer distributed processes without standardized supply chains. Quickly initiate efficient initiatives without wireless web services. Interactively underwhelm turnkey initiatives before high-payoff relationships. Holisticly restore superior interfaces before flexible technology. Xavier Colla

  6. I truly wanted to post a quick word to be able to thank you for the magnificent secrets you are posting here. My prolonged internet research has at the end of the day been compensated with good ideas to share with my visitors. I would declare that many of us website visitors actually are quite lucky to dwell in a notable community with so many outstanding individuals with useful strategies. I feel truly fortunate to have discovered the webpages and look forward to many more thrilling moments reading here. Thank you once more for a lot of things. Kasey Meyer

  7. I was wondering if you ever considered changing the layout of your blog? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having one or two images. Maybe you could space it out better? Elliott Nuque

  8. I wanted to write you one tiny remark to help thank you so much as before over the marvelous suggestions you have discussed on this page. This is remarkably generous of people like you to supply publicly just what some people would have marketed for an ebook to get some cash for themselves, notably given that you could possibly have tried it in case you decided. Those ideas likewise served as a great way to fully grasp that some people have a similar interest the same as mine to learn more pertaining to this issue. I know there are lots of more pleasant situations up front for individuals that scan through your site. Robin Nowaczyk

  9. I together with my guys have already been reviewing the good tips and hints on your web site then at once developed an awful suspicion I had not thanked you for those secrets. My boys appeared to be for that reason thrilled to read all of them and have really been using these things. Thank you for turning out to be quite accommodating and for picking this form of fantastic topics millions of individuals are really eager to be aware of. My personal honest apologies for not saying thanks to sooner. Silas Rodrguez

  10. Youre so cool! I do not suppose Ive check out anything similar to this prior to. So great to find somebody with some initial ideas on this subject. realy thank you for beginning this up. this site is something that is needed on the internet, someone with a little originality. useful work for bringing something new to the internet! Mary Partenope

发表回复

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