Files
dropshell-templates/logserver/setup-kibana.sh
Your Name cb4bf8451f
All checks were successful
Test and Publish Templates / test-and-publish (push) Successful in 40s
make it easy
2025-09-20 11:25:54 +12:00

108 lines
3.4 KiB
Bash
Executable File

#!/bin/bash
# Setup Kibana for simple log viewing
# This creates index patterns and saved searches for easy log access
source "${AGENT_PATH}/common.sh"
_check_required_env_vars "CONTAINER_NAME" "ELASTIC_PASSWORD" "KIBANA_PORT"
KIBANA_URL="http://localhost:${KIBANA_PORT}"
AUTH="elastic:${ELASTIC_PASSWORD}"
echo "Setting up Kibana for simple log viewing..."
echo ""
# Wait for Kibana to be ready
echo -n "Waiting for Kibana to be ready..."
MAX_WAIT=60
WAITED=0
while [ $WAITED -lt $MAX_WAIT ]; do
if docker exec ${CONTAINER_NAME}_kibana curl -s -u "$AUTH" "${KIBANA_URL}/api/status" 2>/dev/null | grep -q '"level":"available"'; then
echo " Ready!"
break
fi
echo -n "."
sleep 2
WAITED=$((WAITED + 2))
done
if [ $WAITED -ge $MAX_WAIT ]; then
echo ""
echo "ERROR: Kibana is not ready after ${MAX_WAIT} seconds"
exit 1
fi
# Create index pattern for Filebeat
echo "Creating Filebeat index pattern..."
docker exec ${CONTAINER_NAME}_kibana curl -s -X POST \
-u "$AUTH" \
-H "Content-Type: application/json" \
-H "kbn-xsrf: true" \
"${KIBANA_URL}/api/saved_objects/index-pattern/filebeat-*" \
-d '{
"attributes": {
"title": "filebeat-*",
"timeFieldName": "@timestamp",
"fields": "[]"
}
}' > /dev/null 2>&1
# Set as default index pattern
docker exec ${CONTAINER_NAME}_kibana curl -s -X POST \
-u "$AUTH" \
-H "Content-Type: application/json" \
-H "kbn-xsrf: true" \
"${KIBANA_URL}/api/kibana/settings" \
-d '{
"changes": {
"defaultIndex": "filebeat-*"
}
}' > /dev/null 2>&1
# Create a simple saved search for Docker logs
echo "Creating saved searches..."
docker exec ${CONTAINER_NAME}_kibana curl -s -X POST \
-u "$AUTH" \
-H "Content-Type: application/json" \
-H "kbn-xsrf: true" \
"${KIBANA_URL}/api/saved_objects/search" \
-d '{
"attributes": {
"title": "Docker Container Logs",
"description": "View all Docker container logs",
"columns": ["container_name", "message"],
"sort": ["@timestamp", "desc"],
"kibanaSavedObjectMeta": {
"searchSourceJSON": "{\"index\":\"filebeat-*\",\"query\":{\"match_all\":{}},\"filter\":[{\"meta\":{\"index\":\"filebeat-*\",\"negate\":false,\"disabled\":false,\"alias\":null,\"type\":\"exists\",\"key\":\"container_name\",\"value\":\"exists\"},\"exists\":{\"field\":\"container_name\"}}]}"
}
}
}' > /dev/null 2>&1
echo ""
echo "========================================="
echo "Kibana Setup Complete!"
echo "========================================="
echo ""
echo "QUICK START GUIDE:"
echo ""
echo "1. Open Kibana: ${SERVER_PUBLICBASEURL:-http://$(hostname -I | awk '{print $1}'):${KIBANA_PORT}}"
echo ""
echo "2. Login with:"
echo " Username: ${KIBANA_USERNAME:-elastic}"
echo " Password: [your password]"
echo ""
echo "3. TO VIEW LOGS SIMPLY:"
echo " a) Click 'Discover' in the left menu"
echo " b) Time range is in top-right (set to 'Last 15 minutes' or 'Today')"
echo " c) Your logs will appear below"
echo ""
echo "4. TO FILTER LOGS:"
echo " - By container: Click '+' next to any 'container_name' value"
echo " - By host: Click '+' next to any 'host.name' value"
echo " - Search box: Type keywords to search all logs"
echo ""
echo "5. TO VIEW LIVE LOGS:"
echo " - Click the 'Refresh' button in top-right"
echo " - Set it to refresh every 5 seconds"
echo ""
echo "========================================="