Zabbix中优化elasticsearch存储的方法
admin
2023-02-21 10:20:06
0

场景分析

由于公司zabbix的历史数据存储在elasticsearch中,有个需求是尽可能地把监控的历史数据存储的长一点,最好是一年,目前的情况是三台ES节点,每天监控历史数据量有5G,目前最多可存储一个月的数据,超过30天的会被定时删除,每台内存分了8G,且全部使用机械硬盘,主分片为5,副本分片为1,查询需求一般只获取一周的历史数据,偶尔会有查一个月到两个月历史数据的需求。

节点规划

为了让ES能存储更长的历史数据,以及考虑到后续监控项添加导致数据的增长,我将节点数量增加至4节点,并将部分节点内存提高,部分节点采用SSD存储

192.168.179.133  200GSSD 4G内存  tag:hot node.name=es1
192.168.179.134  200GSSD 4G内存  tag:hot node.name=es2
192.168.179.135  1THDD 32G内存  tag:cold node.name=es3  node.master=false
192.168.179.136  1THDD 32G内存  tag:cold node.name=es4  node.master=false

优化思路

对数据mapping重新建模,对str类型的数据不进行分词,采用冷热节点对数据进行存储,前七天数据的索引分片设计为2主1副,索引存储在热节点上,超过七天的数据将被存储在冷节点,超过30天的索引分片设置为2主0副本,ES提供了一个shrink的api来进行压缩。由于ES是基于Lucene的搜索引擎,Lucene的索引由多个segment组成,每一个段都会消耗文件句柄,内存和CPU运行周期,段数量过多会使资源消耗变大,搜索也会变慢,这里我将前一天的索引分片强制合并为1个segment,修改refresh的时间间隔至60s,减少段的产生频率。对超过3个月的索引进行关闭。以上操作均使用ES的管理工具curator来定时执行。

zabbix与ES的对接操作

1.修改/etc/zabbix/zabbix_server.conf,添加如下内容

ES地址填写集群中任意一个节点就可以

HistoryStorageURL=192.168.179.133:9200
HistoryStorageTypes=str,text,log,uint,dbl
HistoryStorageDateIndex=1

2.修改/etc/zabbix/web/zabbix.conf.php,添加如下内容

global $DB, $HISTORY;
$HISTORY['url']   = 'http://192.168.179.133:9200';
// Value types stored in Elasticsearch.
$HISTORY['types'] = ['str', 'text', 'log','uint','dbl'];

Zabbix中优化elasticsearch存储的方法  

3.修改ES配置文件,添加冷热节点的标签

vim elasticsearch.yml
热节点配置

node.attr.box_type=hot

冷节点配置

node.attr.box_type=cold

3.在es上创建模板和管道

每种数据类型的模板都需要创建,可以根据elasticsearch.map文件来获取api的信息,模板定义内容有匹配的索引,主副分片数设置,refresh间隔,新建索引分配节点设置以及mapping的设置,这里我只是以uint和str数据的索引为例

PUT _template/uint_template
{
   "template": "uint*",
   "index_patterns": ["uint*"],
   "settings" : {
      "index" : {
         "routing.allocation.require.box_type": "hot",
         "refresh_interval": "60s",
         "number_of_replicas" : 1,
         "number_of_shards" : 2
      }
   },
   "mappings" : {
      "values" : {
         "properties" : {
            "itemid" : {
               "type" : "long"
            },
            "clock" : {
               "format" : "epoch_second",
               "type" : "date"
            },
            "value" : {
               "type" : "long"
            }
         }
      }
   }
}

PUT _template/str_template
{
   "template": "str*",
   "index_patterns": ["str*"],
   "settings" : {
      "index" : {
         "routing.allocation.require.box_type": "hot",
         "refresh_interval": "60s",
         "number_of_replicas" : 1,
         "number_of_shards" : 2
      }
   },
   "mappings" : {
      "values" : {
         "properties" : {
            "itemid" : {
               "type" : "long"
            },
            "clock" : {
               "format" : "epoch_second",
               "type" : "date"
            },
            "value" : {
               "index" : false,
               "type" : "keyword"
            }
         }
      }
   }
}

定义管道的作用是对写入索引之前的数据进行预处理,使其按天产生索引。

PUT _ingest/pipeline/uint-pipeline
{
  "description": "daily uint index naming",
  "processors": [
    {
      "date_index_name": {
        "field": "clock",
        "date_formats": ["UNIX"],
        "index_name_prefix": "uint-",
        "date_rounding": "d"
      }
    }
  ]
}
PUT _ingest/pipeline/str-pipeline
{
  "description": "daily str index naming",
  "processors": [
    {
      "date_index_name": {
        "field": "clock",
        "date_formats": ["UNIX"],
        "index_name_prefix": "str-",
        "date_rounding": "d"
      }
    }
  ]
}

4.修改完成后重启zabbix,并查看zabbix是否有数据

systemctl restart zabbix-server

使用curator对索引进行操作

curator官方文档地址如下
https://www.elastic.co/guide/en/elasticsearch/client/curator/5.8/installation.html

1.安装curator

pip install -U elasticsearch-curator

2.创建curator配置文件

mkdir /root/.curator
vim /root/.curator/curator.yml
---
client:
  hosts:
    - 192.168.179.133
    - 192.168.179.134
  port: 9200
  url_prefix:
  use_ssl: False
  certificate:
  client_cert:
  client_key:
  ssl_no_validate: False
  http_auth:
  timeout: 30
  master_only: False

logging:
  loglevel: INFO
  logfile:
  logformat: default
  blacklist: ['elasticsearch', 'urllib3']

3.编辑action.yml,定义action

将7天以前的索引分配到冷节点

1:
    action: allocation
    description: "Apply shard allocation filtering rules to the specified indices"
    options:
      key: box_type
      value: cold
      allocation_type: require
      wait_for_completion: true
      timeout_override:
      continue_if_exception: false
      disable_action: false
    filters:
    - filtertype: pattern
      kind: regex
      value: '^(uint-|dbl-|str-).*$'
    - filtertype: age
      source: name
      direction: older
      timestring: '%Y-%m-%d'
      unit: days
      unit_count: 7

将前一天的索引强制合并,每个分片1个segment。

2:
    action: forcemerge
    description: "Perform a forceMerge on selected indices to 'max_num_segments' per shard"
    options:
      max_num_segments: 1
      delay:
      timeout_override: 21600 
      continue_if_exception: false
      disable_action: false
    filters:
    - filtertype: pattern
      kind: regex
      value: '^(uint-|dbl-|str-).*$'
    - filtertype: age
      source: name
      direction: older
      timestring: '%Y-%m-%d'
      unit: days
      unit_count: 1

超过30天的索引将主分片数量修改为2,副本分片为0,执行shrink操作的节点不能作为master节点

  3:
    action: shrink
    description: "Change the number of primary shards to one, and the copy shards to 0"
    options:
      ignore_empty_list: True
      shrink_node: DETERMINISTIC
      node_filters:
        permit_masters: False
        exclude_nodes: ['es1','es2']
      number_of_shards: 2
      number_of_replicas: 0
      shrink_prefix:
      shrink_suffix: '-shrink'
      delete_after: True
      post_allocation:
        allocation_type: include
        key: box_type
        value: cold
      wait_for_active_shards: 1
      extra_settings:
        settings:
          index.codec: best_compression
      wait_for_completion: True
      wait_for_rebalance: True
      wait_interval: 9
      max_wait: -1
    filters:
      - filtertype: pattern
        kind: regex
        value: '^(uint-|dbl-|str-).*$'
      - filtertype: age
        source: name
        direction: older
        timestring: '%Y-%m-%d'
        unit: days
        unit_count: 30

对超过3个月的索引进行关闭

  4:
    action: close
    description: "Close selected indices"
    options:
      delete_aliases: false
      skip_flush: false
      ignore_sync_failures: false
    filters:
     - filtertype: pattern
       kind: regex
       value: '^(uint-|dbl-|str-).*$'
     - filtertype: age
       source: name
       direction: older
       timestring: '%Y-%m-%d'
       unit: days
       unit_count: 90

超过一年的索引进行删除

5:
    action: delete_indices
    description: "Delete selected indices"
    options:
      continue_if_exception: False
    filters:
    - filtertype: pattern
      kind: regex
      value: '^(uint-|dbl-|str-).*$'
    - filtertype: age
      source: name
      direction: older
      timestring: '%Y-%m-%d'       
      unit: days
      unit_count: 365

4.执行curator进行测试

curator action.yml

Zabbix中优化elasticsearch存储的方法    

5. 将curator操作写进定时任务,每天执行

crontab -e
10 0 * * * curator /root/action.yml

相关内容

热门资讯

给“AI”发身份证!国内首个智... 《人工智能 智能体互联》系列国家标准应用推进专题会议近日在北京中关村展示中心召开。现场发布GB/Z1...
探秘科技殿堂 点亮科学梦想—中... 为拓宽青少年科学视野,丰富课外实践内容,激发学生科技创新思维与探索精神,7月23日,原州区中河乡中心...
小米申请音频处理方法专利,能够... 国家知识产权局信息显示,北京小米移动软件有限公司申请一项名为“音频处理方法、装置、电子设备、存储介质...
中纪委最新通报:上半年立案省部... △图1:全国纪检监察机关处分人员按职级划分图△图2:全国纪检监察机关运用“四种形态”占比图2026年...
刚刚,Claude Opus ... 昨晚,科技圈刚上演了一场开源 AI 大团建。英伟达创始人黄仁勋在社交媒体发表公开信,拉上 Meta、...
瑞松科技获得发明专利授权:“一... 证券之星消息,根据天眼查APP数据显示瑞松科技(688090)新获得一项发明专利授权,专利名为“一种...
中信科智联申请数据关联方法专利... 国家知识产权局信息显示,中信科智联科技有限公司申请一项名为“一种数据关联方法及相关装置”的专利,公开...
福州AI产业发展跑出“加速度” 7月17日至20日,2026世界人工智能大会暨人工智能全球治理高级别会议在上海举行,吸引了全球目光。...
乌民众再度走上街头:不恢复他防... 虽然乌军总司令瑟尔斯基被解除了职位,但是被解职的费多罗夫并未恢复防长职务,乌克兰民众7月24日再度走...
姗姗来迟!万斯晒新生儿照片 美国副总统万斯夫妇日前迎来第4个孩子,他们7月24日终于晒出了新生儿的照片。据美国有线电视新闻网(C...