当收集的数据出现在图形之后,我慢慢感觉,整个ES的监控都没什么意义。其实,只要能保证ES的正常状态,大部分参数(特别是图形上的大多数监控值),对于小企业来说,并没有什么现实意义。既然搞出来了,那就索性整理好笔记吧。笔记完成后,我准备把大部分ES的监控全部禁用!


1. 获取集群健康状态的api

  ES提供了一个可以获取集群健康状态的api,在浏览器访问:http://10.253.40.87:9200/_cluster/health?pretty

  和Elasticsearch里其他API一样,“cluster-health”会返回一个JSON响应。

blob.png

  响应的内容解释:

"cluster_name" : "my-application",     #集群名

"status" : "green",             #集群健康状态,正常的话是“green”,缺少副本分片为“yellow”,缺少主分片为“red”

"timed_out" : false,

"number_of_nodes" : 2,           #集群节点数

"number_of_data_nodes" : 2,        #数据节点数

 "active_primary_shards" : 138,      #主分片数

 "active_shards" : 274,          #可用的分片数

"relocating_shards" : 0,          #正在迁移的分片数

 "initializing_shards" : 0,        #正在初始化的分片数

"unassigned_shards" : 0,          #未分配的分片,但在集群中存在

"delayed_unassigned_shards" : 0,      #延时待分配到具体节点上的分片数

 "number_of_pending_tasks" : 0,      #待处理的任务数,指主节点创建索引并分配shards等任务

"number_of_in_flight_fetch" : 0,

"task_max_waiting_in_queue_millis" : 0,

"active_shards_percent_as_number" : 100.0  #可用分片数占总分片的比例


2. 编写采集脚本获取集群状态

[root@iZejm6lkdZ ~]# cat /etc/zabbix/scripts/monitor_es.sh
#!/bin/bash
case $1 in
    cluster_name)
        curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\" '/cluster_name/ {print $4}' ;;
    status)
        curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\" 'NR==3 {print $4}' ;;
    timed_out)
        curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==4 {print $1}' |awk -F: '{print $2}' ;;
    number_nodes)
        curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==5 {print $1}' |awk -F: '{print $2}' ;;
    data_nodes)
        curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==6 {print $1}' |awk -F: '{print $2}' ;;
    active_primary_shards)
        curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==7 {print $1}' |awk -F: '{print $2}' ;;
    active_shards)
        curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==8 {print $1}' |awk -F: '{print $2}' ;;
    relocating_shards)
        curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==9 {print $1}' |awk -F: '{print $2}' ;;
    initializing_shards)
        curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==10 {print $1}' |awk -F: '{print $2}' ;;
    unassigned_shards)
        curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==11 {print $1}' |awk -F: '{print $2}' ;;
    delayed_unassigned_shards)
        curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==12 {print $1}' |awk -F: '{print $2}' ;;
    number_of_pending_tasks)
        curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==13 {print $1}' |awk -F: '{print $2}' ;;
 
 
    active_shards_percent_as_number)
        curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==16 {print $1}' |awk -F: '{print $2}' ;;
    *)
 
        echo "Usage: $0 { cluster_name | status | timed_out | number_nodes | data_nodes | active_primary_shards | active_shards | relocating_shards | initializing_shards | unassigned_shards|delayed_unassigned_shards|number_of_pending_tasks|active_shards_percent_as_number}" ;;
esac

  在shell脚本里,“curl -s -XGET 'http://10.253.40.87:9200/_cluster/health?pretty' |awk -F\, 'NR==16 {print $1}' |awk -F: '{print $2}'”这样的命令,“NR==16”是指在浏览器访问http://10.253.40.87:9200/_cluster/health?pretty,获取页面的第16行(从第1行的“{”开始计数)。

  给脚本授予执行权限:

chmod +x monitor_es.sh

  属主、属组可能也需要授权:

chown zabbix:zabbix monitor_es.sh

3. 增加zabbix-agent配置文件

[root@iZejm6lkdZ ~]# cat /etc/zabbix/zabbix_agentd.d/monitor_es.conf
UserParameter=es_cluster_name,/etc/zabbix/scripts/monitor_es.sh cluster_name
UserParameter=es_status,/etc/zabbix/scripts/monitor_es.sh status
#UserParameter=timed_out,/etc/zabbix/scripts/monitor_es.sh timed_out
 
UserParameter=es_number_nodes,/etc/zabbix/scripts/monitor_es.sh number_nodes
UserParameter=es_data_nodes,/etc/zabbix/scripts/monitor_es.sh data_nodes
UserParameter=es_active_primary_shards,/etc/zabbix/scripts/monitor_es.sh active_primary_shards
UserParameter=es_active_shards,/etc/zabbix/scripts/monitor_es.sh active_shards
UserParameter=es_relocating_shards,/etc/zabbix/scripts/monitor_es.sh relocating_shards
UserParameter=es_initializing_shards,/etc/zabbix/scripts/monitor_es.sh initializing_shards
UserParameter=es_unassigned_shards,/etc/zabbix/scripts/monitor_es.sh unassigned_shards
UserParameter=es_delayed_unassigned_shards,/etc/zabbix/scripts/monitor_es.sh delayed_unassigned_shards
UserParameter=es_number_of_pending_tasks,/etc/zabbix/scripts/monitor_es.sh number_of_pending_tasks
UserParameter=es_active_shards_percent_as_number,/etc/zabbix/scripts/monitor_es.sh active_shards_percent_as_number

  有行脚本被注释,只是没有加入监控,没有其他意思。目前不确定这个监控项的含义,以后可以再添加。

4. 重启zabbix-agent服务(zabbix-agent其实有多种重启方式,好像有3种方法,这应该和安装方法有关。这里是我们生产环境唯一生效的方法)

[root@iZejm6lkdZ ~]# pkill -f /etc/zabbix/zabbix_agentd.conf
[root@iZejm6lkdZ ~]# ps -ef|grep zabbix_agent
root     26152  8156  0 16:06 pts/3    00:00:00 grep --color=auto zabbix_agent
[root@iZejm6lkdZ ~]# zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
[root@iZejm6lkdZ ~]# ps -ef|grep zabbix_agent
root     26155     1  0 16:06 ?        00:00:00 zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
root     26156 26155  0 16:06 ?        00:00:00 zabbix_agentd: collector [idle 1 sec]
root     26157 26155  0 16:06 ?        00:00:00 zabbix_agentd: listener #1 [waiting for connection]
root     26158 26155  0 16:06 ?        00:00:00 zabbix_agentd: listener #2 [waiting for connection]
root     26159 26155  0 16:06 ?        00:00:00 zabbix_agentd: listener #3 [waiting for connection]
root     26160 26155  0 16:06 ?        00:00:00 zabbix_agentd: active checks #1 [idle 1 sec]
root     26162  8156  0 16:06 pts/3    00:00:00 grep --color=auto zabbix_agent

5. web页面配置

  item添加:

blob.png

  由于收集的信息只有“green”、“yellow”和“red”,所以,这里的信息类型为字符型。

  只有“es集群名称”和“es集群状态”2个监控项的“Type of information”(字段类型)是字符型的,其他监控项必须是数值型的。例如:

blob.png

  由于“es集群名称”和“es集群状态”的字段类型是字符型的,所以“Trends”列这个监控项是空的。

  这一点很重要。

  如果所有监控项都是字符型,那么,在后面添加“图形”时,监控项是找不到的。

blob.png

  放大图:

blob.png

  触发器的创建:

blob.png

  点击“Add”按纽,需要填写的信息:

blob.png

  注意:“Function”和“N”是重点。

  这个触发器表达式的意思是,当字符串长度为3,也就是状态值为“red”,触发报警。

  图形创建:

blob.png

  点击下面的“Add”按纽,在弹出的对话框里能看到这次创建的监控项,就是因为这些监控项的字段类型是数值型。当前,字符型的2个监控项这里就不存在。

blob.png

  当前zabbix收集数据没有问题:

blob.png


  参考文档:

https://blog.51cto.com/766792592/1891112——zabbix监控elasticsearch集群

https://mp.weixin.qq.com/s?__biz=MzIyMDY2MTE3Mw==&mid=2247484711&idx=1&sn=c011a564d2eec95e64ef01cc6410314e&chksm=97c9d1fda0be58ebd945bcf2805900a4d61f4cf57722bf3a7682f285fdf00c95491462d0808d&mpshare=1&scene=23&srcid=#rd——Zabbix监控es集群状态