Zabbix4.x 历史数据存储到Elasticsearch7.x

一、简介

Zabbix 3.4.6 版本开始支持历史数据存储到 Elasticsearch, 早就想测试这个功能,最近有个需求需保存 zabbix 的历史数据上达十亿条,因此决定测试这功能的实用性,事实证明确实效果挺好。从今以后 zabbix 也支持大量的历史数据。

二、安装 ELK(这里不再讲解,不懂的可以参考:https://www.cnblogs.com/liugp/p/11789933.html

三、添加 Elasticsearch mapping

Elasticsearch 支持 Zabbix 的监控项类型:uint,dbl,str,log,text,对应如下:

Zabbix 监控项数据类型 对应 Zabbix 表 对应 Elasticsearch 类型
Numeric(unsigned)(无符号整型) history_uint uint
Numeric(float)(浮点型) history dbl
Character(字符) history_str str
Log(日志) history_log log
Text history_text text

ES7.x创建五中类型数据索引

PUT /uint
{
    "settings":{
        "number_of_replicas":1,
        "number_of_shards":5
    },
    "mappings":{
        "properties":{
            "itemid":{
                "type":"long"
            },
            "clock":{
                "format":"epoch_second",
                "type":"date"
            },
            "value":{
                "type":"long"
            }
        }
    }
}


PUT /dbl
{
    "settings":{
        "number_of_replicas":1,
        "number_of_shards":5
    },
    "mappings":{
        "properties":{
            "itemid":{
                "type":"long"
            },
            "clock":{
                "format":"epoch_second",
                "type":"date"
            },
            "value":{
                "type":"double"
            }
        }
    }
}

PUT /log
{
    "settings":{
        "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"
            }
        }
    }
}

PUT /text
{
    "settings":{
        "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"
            }
        }
    }
}

PUT /str
{
    "settings":{
        "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"
            }
        }
    }
}
View Code

注意:通过上面创建的索引,zabbix-server无法添加数据,因为ES7移除了type类型导致的,但是可以让zabbix自动创建

ES6.x创建五中类型数据索引

PUT /uint
{
    "settings":{
        "index":{
            "number_of_replicas":1,
            "number_of_shards":5
        }
    },
    "mappings":{
        "values":{
            "properties":{
                "itemid":{
                    "type":"long"
                },
                "clock":{
                    "format":"epoch_second",
                    "type":"date"
                },
                "value":{
                    "type":"long"
                }
            }
        }
    }
}


PUT /dbl
{
    "settings":{
        "index":{
            "number_of_replicas":1,
            "number_of_shards":5
        }
    },
    "mappings":{
        "values":{
            "properties":{
                "itemid":{
                    "type":"long"
                },
                "clock":{
                    "format":"epoch_second",
                    "type":"date"
                },
                "value":{
                    "type":"double"
                }
            }
        }
    }
}

PUT /log
{
    "settings":{
        "index":{
            "number_of_replicas":1,
            "number_of_shards":5
        }
    },
    "mappings":{
        "values":{
            "properties":{
                "itemid":{
                    "type":"long"
                },
                "clock":{
                    "format":"epoch_second",
                    "type":"date"
                },
                "value":{
                    "fields":{
                        "analyzed":{
                            "index":true,
                            "type":"text",
                            "analyzer":"standard"
                        }
                    },
                    "index":false,
                    "type":"text"
                }
            }
        }
    }
}

PUT /text
{
    "settings":{
        "index":{
            "number_of_replicas":1,
            "number_of_shards":5
        }
    },
    "mappings":{
        "values":{
            "properties":{
                "itemid":{
                    "type":"long"
                },
                "clock":{
                    "format":"epoch_second",
                    "type":"date"
                },
                "value":{
                    "fields":{
                        "analyzed":{
                            "index":true,
                            "type":"text",
                            "analyzer":"standard"
                        }
                    },
                    "index":false,
                    "type":"text"
                }
            }
        }
    }
}

PUT /str
{
    "settings":{
        "index":{
            "number_of_replicas":1,
            "number_of_shards":5
        }
    },
    "mappings":{
        "values":{
            "properties":{
                "itemid":{
                    "type":"long"
                },
                "clock":{
                    "format":"epoch_second",
                    "type":"date"
                },
                "value":{
                    "fields":{
                        "analyzed":{
                            "index":true,
                            "type":"text",
                            "analyzer":"standard"
                        }
                    },
                    "index":false,
                    "type":"text"
                }
            }
        }
    }
}
View Code

es6.x curl创建方式

curl -H "Content-Type:application/json" -XPUT http://192.168.182.132:9200/uint -d ' { "settings" : { "index" : { "number_of_replicas" : 1, "number_of_shards" : 5 } }, "mappings" : { "values" : { "properties" : { "itemid" : { "type" : "long" }, "clock" : { "format" : "epoch_second", "type" : "date" }, "value" : { "type" : "long" } } } } } '

curl -H "Content-Type:application/json" -XPUT http://192.168.182.132:9200/dbl -d ' { "settings" : { "index" : { "number_of_replicas" : 1, "number_of_shards" : 5 } }, "mappings" : { "values" : { "properties" : { "itemid" : { "type" : "long" }, "clock" : { "format" : "epoch_second", "type" : "date" }, "value" : { "type" : "double" } } } } } '

curl -H "Content-Type:application/json" -XPUT http://192.168.182.132:9200/log -d ' { "settings" : { "index" : { "number_of_replicas" : 1, "number_of_shards" : 5 } }, "mappings" : { "values" : { "properties" : { "itemid" : { "type" : "long" }, "clock" : { "format" : "epoch_second", "type" : "date" }, "value" : { "fields" : { "analyzed" : { "index" : true, "type" : "text", "analyzer" : "standard" } }, "index" : false, "type" : "text" } } } } } '

curl -H "Content-Type:application/json" -XPUT http://192.168.182.132:9200/text -d ' { "settings" : { "index" : { "number_of_replicas" : 1, "number_of_shards" : 5 } }, "mappings" : { "values" : { "properties" : { "itemid" : { "type" : "long" }, "clock" : { "format" : "epoch_second", "type" : "date" }, "value" : { "fields" : { "analyzed" : { "index" : true, "type" : "text", "analyzer" : "standard" } }, "index" : false, "type" : "text" } } } } } '

curl -H "Content-Type:application/json" -XPUT http://192.168.182.132:9200/str -d ' { "settings" : { "index" : { "number_of_replicas" : 1, "number_of_shards" : 5 } }, "mappings" : { "values" : { "properties" : { "itemid" : { "type" : "long" }, "clock" : { "format" : "epoch_second", "type" : "date" }, "value" : { "fields" : { "analyzed" : { "index" : true, "type" : "text", "analyzer" : "standard" } }, "index" : false, "type" : "text" } } } } } '

注意:zabbix的大坑,如果是es7,es7上有数据,但是图形会显示无数据,这个后期有待解决。如果有哪位大神已经解决了,可以在评论区回复我,Thank you!!!!。

 

四、配置zabbix服务器

Zabbix 安装过程忽略

配置 zabbix_server.conf 文件

/etc/zabbix/zabbix_server.conf 文件下添加 elasticsearch 配置,指定历时数据使用 elasticsearch。

# vim /etc/zabbix/zabbix_server.conf

HistoryStorageURL=http://192.168.182.132:9200
HistoryStorageTypes=uint,dbl,str,log,text

配置zabbix.conf.php文件

/home/wwwroot/default/zabbix/conf/zabbix.conf.php文件下添加elasticsearch配置

# vim /home/wwwroot/default/zabbix/conf/zabbix.conf.php

// Elasticsearch url (can be string if same url is used for all types).
$HISTORY['url'] = 'http://192.168.182.132:9200';
// Value types stored in Elasticsearch.
$HISTORY['types'] = ['uint', 'text', 'log', 'str', 'dbl'];

多台 elasticsearch 集群可按以下格式配置

$HISTORY['url'] = [ 'uint' => 'http://192.168.182.132:9200 ', 'text' => 'http://192.168.182.133:9200', 'log' => 'http://192.168.182.134:9200'];
$HISTORY['types'] = ['uint', 'text','log'];

五、验证数据

启动 zabbix 后,使用 kibana 查看 es 集群是否有相关索引,方法这里就忽略。

原文地址:https://www.cnblogs.com/liugp/p/12021938.html