我们以监控elasticsearch7为例来编写一下具体的实现过程

!> 实现过程有两种,一种是通过grafana直接通过连接elasticsearch数据源,通过自己编写或者使用仪表盘模板导入的方式来实现监控,另一种是借助prometheus的exporter插件来抓取数据并由grafana来导入仪表盘模板实现监控。这里我们选择后者,因为通过在官网模板搜索发现比较好的监控模板都是需要依赖prometheus的exporter插件的。

监控什么指标下载对应系统的exporter插件,统一下载地址:https://prometheus.io/download/

监控指标对应的grafana展示模板,统一下载地址:https://grafana.com/grafana/dashboards

elasticsearch-exporter地址:https://github.com/prometheus-community/elasticsearch_exporter/tags

环境准备

  • 已完成Prometheus的配置安装
  • 已完成Grafana的配置安装
  • 下载elasticsearch-exporter插件,(**注意:**此插件是prometheus社区开源出来的,并不是官方提供的,故无法在上述地址找到,并且版本之前下载的最新的1.2.1总是报“段错误”,故我们下载1.1.0)
# 在安装elasticsearch服务器上执行以下命令
wget https://github.com/justwatchcom/elasticsearch_exporter/releases/download/v1.1.0/elasticsearch_exporter-1.1.0.linux-amd64.tar.gz

# 解压运行
tar -zxvf elasticsearch_exporter-1.1.0.linux-amd64.tar.gz

开始运行

第一步

# 由于运行命令中指定的是远程的es地址,所以,关于elasticsearch-exporter在哪里就无关重要了,只要网络通即可
./elasticsearch_exporter --es.all --es.indices --es.cluster_settings --es.indices_settings --es.shards --es.snapshots --es.timeout=10s --web.listen-address=":9115" --web.telemetry-path="/metrics" --es.uri http://60.191.64.4:13500/

脚本启动后,可以看到脚本为我们提供了一个基于9115的http服务,http://192.168.5.135:9115/metrics,看到监控数据即为成功

第二步

  • 我们需要将上述脚本提供的http地址配置到prometheus的配置文件中
scrape_configs:
  - job_name: 'elasticsearch7'
    static_configs:
     - targets: ['192.168.5.135:9115']
  • 保存退出并启动prometheus
./prometheus --config.file=prometheus.yml --web.listen-address=:9099

第三步

  • 配置grafana
    • 登录grafana

新建数据源,并选择prometheus源(如果存在可跳过,这里不做赘述)

创建仪表板 - 使用导入监控服务器grafana模板的方式

  • 经过在官网查找监控服务器指标模板id为2322(由于我们服务器都联网了,所以直接输入id,load即可)

[!TIP|style:callout]
此处特殊说明一下,导入模板的目的本身是为了避免我们重复造轮子,自己去针对每个监控面板写查询prometheus的语句,当然模板本身也是由其他开发者提交的,所以2322并不是我们唯一选择

  • 选择prometheus源, 点击import,右上角我们修改为5s刷新一次,等一会可以看到elasticsearch7的监控指标如下:

截止到此,针对elasticsearch7的监控算是完成了,但是在导入上述模板时,关于Cluster Health指标一直显示N/A,经过edit该项指标发现在针对prometheus查询的表达式中yellow那里为+22,当我们的集群为黄色是,所以该项得到的结果为23

elasticsearch_cluster_health_status{cluster="$cluster",color="red"}==1 or (elasticsearch_cluster_health_status{cluster="$cluster",color="green"}==1)+4 or (elasticsearch_cluster_health_status{cluster="$cluster",color="yellow"}==1)+22

而查看左侧面板中的映射,这里只有1,3,5值的映射关系,所以我们需要进上面表达式中的22修改为2

并且同时需要修改,映射从哪里读取,也就是修改取值来源,最后点击右上角apply即可

上述修改模板的过程,总结下来无非就是可能仪表盘中查询prometheus表达式写错了,修改正确,并且重新修改左侧面板的取值以及取值对应的映射即可。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐