文章声明:此文基于木子实操撰写 生产环境:CentOS Linux release 7.9.2009 (Core), Elasticsearch 7.9.1, Zabbix 5.0.7 问题关键字:Zabbix Elasticsearch
写在最前面 昨天木子写了一篇关于在《CentOS 7.9上部署Elasticsearch 7.9.1单机版》 ,今天我们来聊一聊怎样使用Zabbix监控Elasticsearch集群健康状态,Elasticsearch自身提供了对应接口,可以获取其集群的健康状态curl --user username:password -XGET http://127.0.0.1:9200/_cluster/health?pretty
,还有很多其它命令可以获取对应ES集群信息,详细参考[参考文献]中ES API文档链接。
为了能够给后面的Zabbix自定义监控项打好一个基础,这里木子简单说一下对应接口的返回值,详细参考如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 [root@cs-es01 ~] { "cluster_name" : "cs-es" , "status" : "green" , "timed_out" : false , "number_of_nodes" : 1, "number_of_data_nodes" : 1, "active_primary_shards" : 13, "active_shards" : 13, "relocating_shards" : 0, "initializing_shards" : 0, "unassigned_shards" : 0, "delayed_unassigned_shards" : 0, "number_of_pending_tasks" : 0, "number_of_in_flight_fetch" : 0, "task_max_waiting_in_queue_millis" : 0, "active_shards_percent_as_number" : 100.0 } [root@cs-es01 ~] epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1610079865 04:24:25 cs-es green 1 1 13 13 0 0 0 0 - 100.0% [root@cs-es01 ~] ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name 172.16.3.25 17 77 1 0.01 0.04 0.00 dilmrt * node-1
Zabbix客户端安装与配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm yum -y install zabbix-agent sed -i "s/ServerActive=127.0.0.1/ServerActive=172.16.3.16/g" /etc/zabbix/zabbix_agentd.conf sed -i "s/Server=127.0.0.1/Server=172.16.3.16/g" /etc/zabbix/zabbix_agentd.conf sed -i "s/Hostname=Zabbix server/Hostname=`hostname`/g" /etc/zabbix/zabbix_agentd.conf [root@cs-es01 scripts] PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log /zabbix/zabbix_agentd.log LogFileSize=0 Server=172.16.3.16 ServerActive=172.16.3.16 Hostname=cs-es01 Include=/etc/zabbix/zabbix_agentd.d/*.conf
安装Python脚本所需要的模块 1 2 3 4 yum -y install epel-release yum install python2-pip pip install --upgrade pip pip install requests
Python监控脚本 Python脚本存放在/etc/zabbix/scripts
目录。
1 mkdir /etc/zabbix/scripts
监控脚本详细如下,这只是一个简单的监控脚本,对于多节点集群模式,还需要根据实际情况进行调整。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 import requestsimport jsonimport sysurl = "http://127.0.0.1:9200/_cluster/health" headers = { "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36" } response = requests.get(url=url, auth=('elastic' , 'ES@2020#Pro' ), headers=headers) s = json.loads(response.content.decode()) parm = sys.argv[1 ] itemlist = [ "cluster_name" , "status" , "timed_out" , "number_of_nodes" , "number_of_data_nodes" , "active_primary_shards" , "active_shards" , "relocating_shards" , "initializing_shards" , "unassigned_shards" , "delayed_unassigned_shards" , "number_of_pending_tasks" , "number_of_in_flight_fetch" , "task_max_waiting_in_queue_millis" , "active_shards_percent_as_number" ] if parm not in itemlist: print("not find" ) sys.exit(1 ) else : print(s[parm])
Zabbix自定义监控项
1 2 3 4 [root@cs-es01 ~] UserParameter=es.[*],/usr/bin/python /etc/zabbix/scripts/es_monitor.py $1 [root@cs-es01 ~]
Zabbix服务器端检测 确保在Zabbix服务器端通过zabbix_get
取值正常。
1 2 3 4 [root@zabbix ~] cs-es [root@zabbix ~] green
监控模板制作 根据下图一步一步操作即可,在图中有标注对应说明信息。 创建监控模板的流程为:
创建模板
创建应用集
创建监控项(需要监控的信息)
创建触发器(报警)
创建图形(可以不需要)
最后再将对应的监控项,应用到主机。 应用模板至主机节点 上面木子仅仅只是说明怎么添加一个监控项,实际木子将所有的输出都添加成了监控项,而且这里可以看到都已经取到了最新数据。 至此,一个简单的Elasticsearch监控就配置完成。
参考文献 [1] ES集群健康检测API文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html [2] ES所有接口文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/rest-apis.html