統計elasticsearch中月毎日インデックス量のスクリプト


トラフィックが増加するにつれて、最近では本番環境のelasticsearchクラスタの履歴インデックスデータを移行する必要がありますが、移行する前に移行されたelasticsearchインデックスデータを統計して移行後の検証統計を行う必要があるため、esデータで履歴インデックスをクエリーする量のレポートファイルを作成するスクリプトを書きました.その中にjqツールを使って数を取ることがあって、jqの紹介は見ることができますhttp://jim123.blog.51cto.com/4763600/1966964:
#!/bin/bash
#es_count_report.sh
#used for elasticsearch monthly numbers index
#you must install jq and curl
#writer jim
#2017.09.19
#Position parameter judgment
datetime=$(date +"%Y%m")

if [ $# -lt 1 ];then
    echo "Please enter the date 'year month'"
    echo "ex> $0 ${datetime}"
    exit 1
fi
 
if [ $# -gt 1 ]; then
        echo "The input host address are too much"
    echo "ex> $0 ${datetime}"
    exit 1
fi
#   elasticsearch       curl jq
rpm -qa | grep jq && rpm -qa | grep curl
if [ $? -ne 0 ];then
    yum -y install jq curl
fi

es_ip="192.168.2.200"
es_port="9200"
monthtime=$1
#elasticsearch           
data_index="data-${monthtime}"
index_name_all=$(curl -s "http://${es_ip}:${es_port}/_cat/indices?v" | grep ${data_index} | awk '{print $3}' | sort)
report_file="$(pwd)/index_num_"${monthtime}".txt"
cat /dev/null > $report_file
#             
for i in $index_name_all
do
    index_num=$(curl -s -XGET "http://${es_ip}:${es_port}/${i}/poll/_search/?pretty" -d '{"_source":true,"size": 0}'|jq '.hits.total') && echo "$i:$index_num" >> $report_file
done

要するに普段elasticsearchのapiインタフェースに基づいて各種の異なるデータ統計を実現することができる