统计elasticsearch中月每天索引量的脚本
随着业务量的不断上升,最近一段时间需要对生产环境中的elasticsearch集群中的历史索引数据做迁移,而在做迁移前需要对被迁移的elasticsearch索引数据做统计用于迁移后的验证统计,所以就写了一个脚本用于es数据中查询历史索引的量生成报表文件,而在其中有使用过jq工具用于取数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
32
33
34
35
36
37
38
39
#!/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接口实现各种不同的数据统计
页:
[1]