argus/src/log/kibana/build/kibana-post-start.sh

134 lines
4.0 KiB
Bash

#!/bin/bash
set -euo pipefail
ES_HOST="${ELASTICSEARCH_HOSTS:-http://es:9200}"
KB_HOST="${KB_HOST:-http://127.0.0.1:5601}"
echo "[INFO] Starting Kibana post-start configuration..."
# 等待 Elasticsearch 可用
wait_for_elasticsearch() {
echo "[INFO] Waiting for Elasticsearch..."
local max_attempts=60
local attempt=1
while [ $attempt -le $max_attempts ]; do
if curl -fs "$ES_HOST/_cluster/health" >/dev/null 2>&1; then
echo "[OK] Elasticsearch is available"
return 0
fi
echo " Waiting for ES... ($attempt/$max_attempts)"
sleep 5
((attempt++))
done
echo "[ERROR] Elasticsearch timeout"
return 1
}
# 等待 Kibana 可用
wait_for_kibana() {
echo "[INFO] Waiting for Kibana..."
local max_attempts=120
local attempt=1
while [ $attempt -le $max_attempts ]; do
if curl -fs "$KB_HOST/api/status" >/dev/null 2>&1; then
local status=$(curl -s "$KB_HOST/api/status" | grep -o '"level":"available"' || echo "")
if [ -n "$status" ]; then
echo "[OK] Kibana is available"
return 0
fi
echo " Waiting for Kibana... ($attempt/$max_attempts, status: $status)"
else
echo " Waiting for Kibana... ($attempt/$max_attempts, connection failed)"
fi
sleep 5
((attempt++))
done
echo "[ERROR] Kibana timeout"
return 1
}
# 幂等设置索引副本数为0
fix_replicas_idempotent() {
echo "[INFO] Checking and fixing index replicas..."
# 获取所有 train-* 和 infer-* 索引
local indices=$(curl -s "$ES_HOST/_cat/indices/train-*,infer-*?h=index" 2>/dev/null || echo "")
if [ -z "$indices" ]; then
echo "[INFO] No train-*/infer-* indices found, skipping replica adjustment"
return 0
fi
for idx in $indices; do
# 检查当前副本数
local current_replicas=$(curl -s "$ES_HOST/$idx/_settings" | grep -o '"number_of_replicas":"[^"]*"' | cut -d'"' -f4 || echo "")
if [ "$current_replicas" != "0" ]; then
echo "[INFO] Setting replicas to 0 for index: $idx (current: $current_replicas)"
curl -fsS -X PUT "$ES_HOST/$idx/_settings" \
-H 'Content-Type: application/json' \
-d '{"index":{"number_of_replicas":0}}' >/dev/null || {
echo "[WARN] Failed to set replicas for $idx"
continue
}
echo "[OK] Updated replicas for $idx"
else
echo "[INFO] Index $idx already has 0 replicas, skipping"
fi
done
}
# 幂等创建数据视图
create_or_ensure_data_view() {
local name="$1"
local title="$2"
local list_response
list_response=$(curl -fsS "$KB_HOST/api/data_views" -H 'kbn-xsrf: true' 2>/dev/null || echo "")
if [ -z "$list_response" ]; then
echo "[WARN] Failed to list data views, skipping creation check for $title"
return
fi
if echo "$list_response" | grep -Fq "\"title\":\"$title\""; then
echo "[INFO] Data view $title already exists, skipping"
return
fi
echo "[INFO] Creating data view for $title indices (allowNoIndex)"
curl -fsS -X POST "$KB_HOST/api/data_views/data_view?allowNoIndex=true" \
-H 'kbn-xsrf: true' \
-H 'Content-Type: application/json' \
-d "{\"data_view\":{\"name\":\"$name\",\"title\":\"$title\",\"timeFieldName\":\"@timestamp\",\"allowNoIndex\":true}}" \
>/dev/null && echo "[OK] Created $name data view" || echo "[WARN] Failed to create $name data view"
}
create_data_views_idempotent() {
echo "[INFO] Checking and creating data views..."
create_or_ensure_data_view "train" "train-*"
create_or_ensure_data_view "infer" "infer-*"
}
# 主逻辑
main() {
# 等待服务可用
wait_for_elasticsearch || exit 1
wait_for_kibana || exit 1
# 执行幂等配置
fix_replicas_idempotent
create_data_views_idempotent
echo "[INFO] Kibana post-start configuration completed"
}
# 运行主逻辑
main