swtich from ELK to Loki!
All checks were successful
Test and Publish Templates / test-and-publish (push) Successful in 40s
All checks were successful
Test and Publish Templates / test-and-publish (push) Successful in 40s
This commit is contained in:
@@ -2,128 +2,48 @@
|
||||
source "${AGENT_PATH}/common.sh"
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
# Check required environment variables
|
||||
_check_required_env_vars "CONTAINER_NAME" "ES_VERSION" "LS_VERSION" "KIBANA_VERSION"
|
||||
_check_required_env_vars "CONTAINER_NAME"
|
||||
|
||||
# Check Docker and Docker Compose are available
|
||||
# Check Docker
|
||||
_check_docker_installed || _die "Docker test failed"
|
||||
docker compose version >/dev/null 2>&1 || _die "Docker Compose is not installed (requires Docker Compose V2)"
|
||||
|
||||
# Check vm.max_map_count for Elasticsearch
|
||||
current_max_map_count=$(sysctl -n vm.max_map_count 2>/dev/null || echo 0)
|
||||
if [ "$current_max_map_count" -lt 262144 ]; then
|
||||
echo "WARNING: vm.max_map_count is too low ($current_max_map_count)"
|
||||
echo "Elasticsearch requires at least 262144"
|
||||
echo "Please run: sudo sysctl -w vm.max_map_count=262144"
|
||||
echo "And add to /etc/sysctl.conf to persist"
|
||||
_die "System configuration needs adjustment"
|
||||
fi
|
||||
|
||||
# Check available memory
|
||||
available_mem=$(free -m | awk '/^Mem:/{print $7}')
|
||||
if [ "$available_mem" -lt 3000 ]; then
|
||||
echo "WARNING: Low available memory (${available_mem}MB)"
|
||||
echo "ELK stack requires at least 3-4GB free memory for proper operation"
|
||||
echo "Services may take longer to start or fail to start"
|
||||
echo ""
|
||||
fi
|
||||
docker compose version >/dev/null 2>&1 || _die "Docker Compose V2 is required"
|
||||
|
||||
# Stop any existing containers
|
||||
bash ./stop.sh || true
|
||||
bash ./stop.sh 2>/dev/null || true
|
||||
|
||||
# Remove old containers
|
||||
docker compose down --remove-orphans 2>/dev/null || true
|
||||
# Create config directory
|
||||
mkdir -p "${CONFIG_PATH}/dashboards"
|
||||
|
||||
# Pull the Docker images
|
||||
echo "Pulling ELK stack images..."
|
||||
docker pull docker.elastic.co/elasticsearch/elasticsearch:${ES_VERSION} || _die "Failed to pull Elasticsearch"
|
||||
docker pull docker.elastic.co/logstash/logstash:${LS_VERSION} || _die "Failed to pull Logstash"
|
||||
docker pull docker.elastic.co/kibana/kibana:${KIBANA_VERSION} || _die "Failed to pull Kibana"
|
||||
# Copy configuration files
|
||||
cp "$SCRIPT_DIR/config/"*.yaml "$SCRIPT_DIR/config/"*.yml "$SCRIPT_DIR/config/"*.conf "${CONFIG_PATH}/" 2>/dev/null || true
|
||||
cp "$SCRIPT_DIR/config/dashboards/"*.json "${CONFIG_PATH}/dashboards/" 2>/dev/null || true
|
||||
|
||||
# Create volumes using common function
|
||||
source "$SCRIPT_DIR/_volumes.sh"
|
||||
echo "Creating volumes..."
|
||||
create_items $(get_logserver_volumes)
|
||||
|
||||
# Ensure config directory exists
|
||||
mkdir -p "${CONFIG_PATH}"
|
||||
|
||||
# Initialize API keys file if it doesn't exist
|
||||
if [ ! -f "${CONFIG_PATH}/api-keys.yml" ]; then
|
||||
echo "No API keys configured yet."
|
||||
echo "Run ./generate-api-key.sh to add client keys"
|
||||
echo "api_keys:" > "${CONFIG_PATH}/api-keys.yml"
|
||||
# Generate htpasswd file for Loki authentication
|
||||
echo "Generating authentication file..."
|
||||
# Use openssl to generate htpasswd (available on most systems)
|
||||
if command -v openssl >/dev/null 2>&1; then
|
||||
# Generate password hash
|
||||
PASS_HASH=$(openssl passwd -apr1 "${LOKI_PASSWORD:-changeme}")
|
||||
echo "${LOKI_USER:-logclient}:$PASS_HASH" > "${CONFIG_PATH}/.htpasswd"
|
||||
elif command -v htpasswd >/dev/null 2>&1; then
|
||||
# Use htpasswd if available
|
||||
htpasswd -cb "${CONFIG_PATH}/.htpasswd" "${LOKI_USER:-logclient}" "${LOKI_PASSWORD:-changeme}"
|
||||
else
|
||||
echo "WARNING: Cannot generate password file - no openssl or htpasswd found"
|
||||
echo "Using basic auth with plain text (NOT SECURE):"
|
||||
echo "${LOKI_USER:-logclient}:${LOKI_PASSWORD:-changeme}" > "${CONFIG_PATH}/.htpasswd"
|
||||
fi
|
||||
|
||||
# Copy Logstash configurations if they don't exist
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
# Start the stack
|
||||
echo "Starting Log Server..."
|
||||
docker compose up -d || _die "Failed to start"
|
||||
|
||||
if [ ! -f "${CONFIG_PATH}/logstash.conf" ]; then
|
||||
if [ -f "$SCRIPT_DIR/config/logstash.conf" ]; then
|
||||
cp "$SCRIPT_DIR/config/logstash.conf" "${CONFIG_PATH}/logstash.conf"
|
||||
echo "Copied Logstash pipeline configuration to ${CONFIG_PATH}"
|
||||
else
|
||||
echo "WARNING: logstash.conf not found in template"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ ! -f "${CONFIG_PATH}/logstash.yml" ]; then
|
||||
if [ -f "$SCRIPT_DIR/config/logstash.yml" ]; then
|
||||
cp "$SCRIPT_DIR/config/logstash.yml" "${CONFIG_PATH}/logstash.yml"
|
||||
echo "Copied Logstash settings to ${CONFIG_PATH}"
|
||||
else
|
||||
echo "WARNING: logstash.yml not found in template"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Start the ELK stack
|
||||
echo "Starting ELK stack..."
|
||||
docker compose up -d --build || _die "Failed to start ELK stack"
|
||||
|
||||
# Wait for services to be ready with polling
|
||||
echo "Waiting for services to start (this can take 2-3 minutes on first run)..."
|
||||
MAX_WAIT=240 # Maximum 4 minutes
|
||||
# Wait for services
|
||||
echo -n "Waiting for services to start..."
|
||||
MAX_WAIT=60
|
||||
WAITED=0
|
||||
while [ $WAITED -lt $MAX_WAIT ]; do
|
||||
# Check if all services are running
|
||||
if bash ./status.sh 2>/dev/null | grep -q "Running"; then
|
||||
echo " All services are up!"
|
||||
break
|
||||
fi
|
||||
|
||||
# Show progress with time elapsed
|
||||
if [ $((WAITED % 10)) -eq 0 ] && [ $WAITED -gt 0 ]; then
|
||||
echo -n " [${WAITED}s]"
|
||||
else
|
||||
echo -n "."
|
||||
fi
|
||||
|
||||
sleep 2
|
||||
WAITED=$((WAITED + 2))
|
||||
done
|
||||
echo ""
|
||||
|
||||
if [ $WAITED -ge $MAX_WAIT ]; then
|
||||
echo "Warning: Services are still starting. This is normal on first run."
|
||||
echo "Current status:"
|
||||
bash ./status.sh || true
|
||||
echo ""
|
||||
echo "You can check the logs with:"
|
||||
echo " docker logs ${CONTAINER_NAME}_elasticsearch"
|
||||
echo " docker logs ${CONTAINER_NAME}_logstash"
|
||||
echo " docker logs ${CONTAINER_NAME}_kibana"
|
||||
echo ""
|
||||
echo "The services will continue starting in the background."
|
||||
fi
|
||||
|
||||
# Create custom user
|
||||
echo "Setting up custom user '${KIBANA_USERNAME:-admin}'..."
|
||||
echo -n "Waiting for Elasticsearch API..."
|
||||
|
||||
# First wait for Elasticsearch to be ready
|
||||
WAITED=0
|
||||
while [ $WAITED -lt 60 ]; do
|
||||
if docker exec ${CONTAINER_NAME}_elasticsearch curl -s -u elastic:${ELASTIC_PASSWORD} http://localhost:9200/_cluster/health 2>/dev/null | grep -q '"status":"yellow"\|"status":"green"'; then
|
||||
if curl -s "http://localhost:${WEB_PORT:-3000}/api/health" 2>/dev/null | grep -q "ok"; then
|
||||
echo " Ready!"
|
||||
break
|
||||
fi
|
||||
@@ -131,56 +51,28 @@ while [ $WAITED -lt 60 ]; do
|
||||
sleep 2
|
||||
WAITED=$((WAITED + 2))
|
||||
done
|
||||
echo ""
|
||||
|
||||
if [ $WAITED -lt 60 ]; then
|
||||
# Now create the user
|
||||
docker exec ${CONTAINER_NAME}_elasticsearch bash -c "
|
||||
|
||||
result=\$(curl -s -X POST -u elastic:${ELASTIC_PASSWORD} \
|
||||
-H 'Content-Type: application/json' \
|
||||
http://localhost:9200/_security/user/${KIBANA_USERNAME:-admin} \
|
||||
-d '{
|
||||
\"password\" : \"${KIBANA_USER_PASSWORD:-changeme}\",
|
||||
\"roles\" : [ \"superuser\" ],
|
||||
\"full_name\" : \"Admin User\"
|
||||
}' 2>/dev/null)
|
||||
|
||||
if echo \"\$result\" | grep -q '\"created\":true'; then
|
||||
echo \"User created successfully\"
|
||||
else
|
||||
echo \"User already exists (this is fine)\"
|
||||
fi
|
||||
"
|
||||
else
|
||||
echo "Warning: Elasticsearch API not ready after 60 seconds"
|
||||
fi
|
||||
|
||||
echo "Installation of ${CONTAINER_NAME} complete"
|
||||
echo ""
|
||||
echo "========================================="
|
||||
echo "Kibana UI: ${SERVER_PUBLICBASEURL:-http://$(hostname -I | awk '{print $1}'):${KIBANA_PORT}}"
|
||||
echo ""
|
||||
echo "Login with your custom user:"
|
||||
echo " Username: ${KIBANA_USERNAME:-admin}"
|
||||
echo " Password: ${KIBANA_USER_PASSWORD:-changeme}"
|
||||
echo ""
|
||||
echo "Or the superuser:"
|
||||
echo " Username: elastic"
|
||||
echo " Password: ${ELASTIC_PASSWORD:-changeme}"
|
||||
echo "Log Server Installed!"
|
||||
echo "========================================="
|
||||
echo ""
|
||||
# Only show reminder if using default values
|
||||
if [ "${ELASTIC_PASSWORD}" = "changeme" ] || [ -z "${SERVER_PUBLICBASEURL}" ] || [ "${SERVER_PUBLICBASEURL}" = "http://localhost:5601" ]; then
|
||||
echo "REMINDER: Update service.env with:"
|
||||
if [ "${ELASTIC_PASSWORD}" = "changeme" ]; then
|
||||
echo " - A secure password in ELASTIC_PASSWORD"
|
||||
fi
|
||||
if [ -z "${SERVER_PUBLICBASEURL}" ] || [ "${SERVER_PUBLICBASEURL}" = "http://localhost:5601" ]; then
|
||||
echo " - Your actual server IP/domain in SERVER_PUBLICBASEURL"
|
||||
fi
|
||||
echo ""
|
||||
fi
|
||||
echo "Logstash listening on port ${LOGSTASH_BEATS_PORT} for Filebeat clients"
|
||||
echo "Web UI: http://$(hostname -I | awk '{print $1}'):${WEB_PORT:-3000}"
|
||||
echo "Login: ${ADMIN_USER:-admin} / ${ADMIN_PASSWORD:-changeme}"
|
||||
echo ""
|
||||
echo "To add client authentication:"
|
||||
echo " ./generate-api-key.sh"
|
||||
echo "TO VIEW LOGS:"
|
||||
echo "1. Click 'Dashboards' (4 squares icon)"
|
||||
echo "2. Click 'Central Logs'"
|
||||
echo "3. See all logs from all servers!"
|
||||
echo ""
|
||||
echo "FOR CLIENTS TO SEND LOGS HERE:"
|
||||
echo "Server: $(hostname -I | awk '{print $1}')"
|
||||
echo "Port: ${LOKI_PORT:-3100}"
|
||||
echo "Username: ${LOKI_USER:-logclient}"
|
||||
echo "Password: ${LOKI_PASSWORD:-changeme}"
|
||||
echo ""
|
||||
echo "IMPORTANT: Change LOKI_PASSWORD in service.env!"
|
||||
echo ""
|
||||
echo "Install 'logclient' on other servers"
|
||||
echo "========================================="
|
Reference in New Issue
Block a user