写在开篇

不管zabbix的后端数据库是oracle还是mysql,当zabbix监控的量级达到了一定程度后,那么对数据库的性能是一个非常严峻的挑战。特别是对历史数据的查询,将会变得非常非常的慢,别告诉我可以建索引优化,当量级达到一定的程度的时候,索引真的没啥效果了。如果再不继续寻找合适的解决方案,那么就一定会引发数据库层面的问题,最终导致服务不可用。当监控数据越来越大的时候,存储不足的时候,怎么办?那就删历史数据呗,但如果要求至少要保存半年甚至1年以上的历史数据,且又高端存储磁阵紧缺面临扩容难题的时候怎么办?而且又同时面临着单个历史表非常庞大的时候怎么办?分库、分表、分区?做读写分离?不!一切都是浮云,还有一个更值得推荐的解决方案,那就是利用Zabbix本身对ES支持的机制来将历史数据存储到ES集群。目前,官方虽然表示Zabbix对Elasticsearch的支持仍处于试验阶段,但笔者认为还是值得一试,且在测试阶段未发现有啥不妥。“生产环境”上也改造了几套对接ES的架构,目前运行均一切正常,ES可快速横向扩展的能力是人尽皆知啊!谁用谁知道。

下面笔者附上对接ES的官方文档链接:https://www.zabbix.com/documentation/5.0/en/manual/appendix/install/elastic_search_setup,且利用测试环境输出了本篇的“精华”。希望可以起到抛砖引玉的效果,欢迎广大盆友可以和笔者一起共同探讨。

架构图

笔者简单画了一下大概的架构图,如下:

zabbix proxy 数据库密码 zabbix数据库满了_数据库

zabbix proxy 数据库密码 zabbix数据库满了_php_02

 

环境搭建

由于Oracle、ES、Kibana、Zabbix不是本文的主题,因此这几个组件的安装过程笔者在本文就省略了哈。关于Oracle的安装,笔者在以前的文章中有所讲到,那么ES、Kibana、Zabbix的相关知识点笔者后续也会抽时间输出“精华”,望广大朋友们多多关注哦,非常感谢!

在es中创建索引

  1. 添加数字(无符号)类型的索引
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"
         }
      }
   }
}'
  1. 添加数字(浮点型)类型的索引
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"
         }
      }
   }
}'
  1. 添加字符类型的索引
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"
         }
      }
   }
}'
  1. 添加日志类型的索引
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"
         }
      }
   }
}'
  1. 添加文本类型的索引
curl -X PUT \
 http://localhost:9200/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

1. zabbix server对接es

  • vi /opt/aspire/product/zabbixsvr5/etc/zabbix_server.conf
HistoryStorageURL=local.es.svr:9200
HistoryStorageTypes=uint,dbl,str,log,text

2. zabbix web前端对接es

  • vi /opt/aspire/product/nginx/html/conf/zabbix.conf.php
<?php
// Zabbix GUI configuration file.
global $DB, $HISTORY;
$HISTORY['url'] = 'http://local.es.svr:9200';
$HISTORY['types'] = ['uint', 'text', 'log', 'str', 'dbl'];

3. 重启zabbix server和php-fpm即可

# 杀死和拉起zabbix server
ps -aux | grep zabbix_server | grep -v grep | awk '{print $2}' | xargs kill -9
/opt/aspire/product/zabbixsvr5/sbin/zabbix_server -c /opt/aspire/product/zabbixsvr5/etc/zabbix_server.conf

# 杀死和拉起php-fpm
pkill php-fpm
/opt/aspire/product/php7/sbin/php-fpm

对接完成后的验证

1. zabbix查看最新上报的数据

zabbix proxy 数据库密码 zabbix数据库满了_elasticsearch_03

上图可看出上报监控数据正常

2. 在zabbix数据库(oracle)中查询相关历史表是否有数据

笔者通过plsql登录了oracle 19c进行select count操作

zabbix proxy 数据库密码 zabbix数据库满了_elasticsearch_04

zabbix proxy 数据库密码 zabbix数据库满了_zabbix proxy 数据库密码_05

 

通过上图可看到,history、history_log、history_str、history_text、history_uint 这5张表都没有数据写入了

3. 登录kibana查看

zabbix proxy 数据库密码 zabbix数据库满了_数据库_06

上图可看到,历史数据都写入到对应的索引了。