#!/bin/bash
# Elasticsearch集群的URL
es_url="http://localhost:9200"
# 索引名称的前缀(假设索引名称为 "logs-2023.06.15")
index_prefix="logs-"
# 获取当前日期
current_date=$(date +%Y.%m.%d)
# 获取前一天的日期
previous_date=$(date -d "yesterday" +%Y.%m.%d)
# 构建前一天和当前日期的索引名称
previous_index="${index_prefix}${previous_date}"
current_index="${index_prefix}${current_date}"
# 获取前一天和当前日期的文档数量
previous_docs=$(curl -s "${es_url}/${previous_index}/_count" | jq -r '.count')
current_docs=$(curl -s "${es_url}/${current_index}/_count" | jq -r '.count')
# 获取前一天和当前日期的索引大小(字节)
previous_size=$(curl -s "${es_url}/${previous_index}/_stats/store" | jq -r '.indices."'"${previous_index}"'".total.store.size_in_bytes')
current_size=$(curl -s "${es_url}/${current_index}/_stats/store" | jq -r '.indices."'"${current_index}"'".total.store.size_in_bytes')
# 计算索引增量
index_increment=$((current_docs - previous_docs))
# 计算索引大小增量(以MB为单位)
size_increment=$(( (current_size - previous_size) / 1024 / 1024 ))
# 保存结果到文件
result_file="${current_date}.txt"
echo "Index Increment on $current_date:" > "$result_file"
echo "Previous Index: $previous_index ($previous_docs documents, ${previous_size} bytes)" >> "$result_file"
echo "Current Index: $current_index ($current_docs documents, ${current_size} bytes)" >> "$result_file"
echo "Index Increment: $index_increment documents" >> "$result_file"
echo "Size Increment: ${size_increment} MB" >> "$result_file"