脚本主要用到的技术点有:
1、date命令格式化输出
2、for循环
3、grep -E 用法
4、bc计算
5、es通过http接口检查索引存在方法,es通过http接口数据写入方法,es通过http创建索引方法
脚本内容如下:
========================================================================
#!/bin/bash
es_server=127.0.0.1today=$(date +%s)
t_today=$(date -d @${today} +%Y%m%d)
yesterday=$(expr $today - 86400)
t_yesterday=$(date -d @${yesterday} +%Y%m%d)
tommorow=$(expr $today + 86400)
t_tommorow=$(date -d @${tommorow} +%Y%m%d)
for i in $(curl $es_server:9200/_cat/indices?v |grep ${t_yesterday} |grep -E -v "collectload|collect300|collect3600|collect86400" |awk '{print $3","$9}')
do
yesterday_indice=$(echo $i |awk -F',' '{print $1}')
yesterday_indice_size=$(echo $i |awk -F',' '{print $2}')
shards_num=3
if echo $yesterday_indice_size |grep -i -q kb;then
shards_num=1
fi
if echo $yesterday_indice_size |grep -i -q mb;then
shards_num=1
fi
if echo $yesterday_indice_size |grep -i -q gb;then
size=$(echo $yesterday_indice_size |awk -F 'gb' '{print $1}')
shards_num=$(echo "$size / 40 + 1" |bc)
fi
indice=$(echo $yesterday_indice |sed "s/$t_yesterday/$t_tommorow/g")
echo "$yesterday_indice $shards_num $yesterday_indice_size"
if [ -z "$(curl -XHEAD -i 127.0.0.1:9200/$indice |grep 404)" ];then
echo -n "$(date) "
echo $indice already exist
echo
else
echo -n "$(date) "
echo create index $indice
curl -XPUT $es_server:9200/${indice}/ -d '{"settings":{"number_of_shards":'$shards_num',"number_of_replicas":1}}'
echo
sleep 60
fi
done >>/data/disk/sda1/software/scripts/auto-create-indices.log