如何用Base Shell获取ES集群状态?
- By : Will
- Category : Bash, Elastic Stack
Bash
1 前言
一个问题,一篇文章,一出故事。
笔者想要通过Base Shell获取Elasticsearch集群的状态,于是创建此脚本。
2 最佳实践
2.1 环境信息
如果你需要参考我们的环境,请参阅如下章节,
2.2 安装所需的软件包
dnf install -y bc
2.3 创建获取脚本
mkdir ~/scripts/ vim ~/scripts/getElasticSearchStatus.sh
加入如下配置,
#!/bin/bash esHost="http://azelasticsearch01:9200" esUser="elastic" esPasswd="elasticpwd" getClusterHealth() { clusterStatus=$(curl -u $esUser:$esPasswd -s "$esHost/_cluster/health" | jq -r '.status') echo "Cluster Status: $clusterStatus" } getClusterDiskUsage() { diskTotal=$(curl -u $esUser:$esPasswd -s "$esHost/_cluster/stats" | jq -r '.nodes.fs.total_in_bytes') diskFree=$(curl -u $esUser:$esPasswd -s "$esHost/_cluster/stats" | jq -r '.nodes.fs.free_in_bytes') diskAvailable=$(curl -u $esUser:$esPasswd -s "$esHost/_cluster/stats" | jq -r '.nodes.fs.available_in_bytes') diskUsage=$(echo "scale=2; ($diskTotal - $diskFree) / $diskTotal * 100" | bc) echo "Cluster Disk Usage: $diskUsage%" } getClusterMemoryUsage() { memTotal=$(curl -u $esUser:$esPasswd -s "$esHost/_cluster/stats" | jq -r '.nodes.jvm.mem.heap_max_in_bytes') memUsed=$(curl -u $esUser:$esPasswd -s "$esHost/_cluster/stats" | jq -r '.nodes.jvm.mem.heap_used_in_bytes') memUsage=$(echo "scale=2; $memUsed / $memTotal * 100" | bc) echo "Cluster Mem Usage: $memUsage%" } getClusterCpuUsage() { cpuUsage=$(curl -u $esUser:$esPasswd -s "$esHost/_cluster/stats" | jq -r '.nodes.process.cpu.percent') echo "Cluster CPU Usage: $cpuUsage%" } getNodeDiskUsage() { curl -u $esUser:$esPasswd -s "$esHost/_cat/allocation?pretty" | awk -F ' ' '{print "Disk Usage "$12": "$9"%"}' } getNodeMemoryUsage() { curl -u $esUser:$esPasswd -s "$esHost/_cat/nodes?pretty" | awk -F ' ' '{print "Memory Usage "$10": "$2"%"}' } getNodeCpuUsage() { curl -u $esUser:$esPasswd -s "$esHost/_cat/nodes?pretty" | awk -F ' ' '{print "CPU Usage"$10": "$4"%"}' } getClusterHealth getClusterDiskUsage getClusterMemoryUsage getClusterCpuUsage getNodeDiskUsage getNodeMemoryUsage getNodeCpuUsage
2.4 测试脚本
sh ~/scripts/getElasticSearchStatus.sh
可见如下输出,
Cluster Status: green Cluster Disk Usage: 12.00% Cluster Mem Usage: 37.00% Cluster CPU Usage: 4% Disk Usage azelasticsearch03: 12% Disk Usage azelasticsearch02: 12% Disk Usage azelasticsearch04: 12% Disk Usage azelasticsearch05: 12% Disk Usage azelasticsearch01: 13% Memory Usage azelasticsearch03: 44% Memory Usage azelasticsearch01: 14% Memory Usage azelasticsearch04: 48% Memory Usage azelasticsearch05: 32% Memory Usage azelasticsearch02: 47% CPU Usageazelasticsearch03: 0% CPU Usageazelasticsearch01: 2% CPU Usageazelasticsearch04: 0% CPU Usageazelasticsearch05: 1% CPU Usageazelasticsearch02: 1%
没有评论