脚本——根据昨天es索引的大小,提前创建明天的索引

脚本主要用到的技术点有:

1、date命令格式化输出

2、for循环

3、grep -E 用法

4、bc计算

5、es通过http接口检查索引存在方法,es通过http接口数据写入方法,es通过http创建索引方法


脚本内容如下:

========================================================================

#!/bin/bash

es_server=127.0.0.1
today=$(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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值