Files
ServerManagementUtils/orchestrator/setup-logstash.sh
T
2025-09-26 07:08:32 +02:00

288 lines
9.7 KiB
Bash
Executable File

#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "${SCRIPT_DIR}/../share/logging.sh"
# Configuration
LOGSTASH_CONFIG_DIR="${SCRIPT_DIR}/../logstash"
LOGSTASH_INSTALL_DIR="/etc/logstash"
LOGSTASH_CONF_D_DIR="${LOGSTASH_INSTALL_DIR}/conf.d"
OUTPUT_CONFIG_FILE="conf.d/90-output.conf"
CheckRoot() {
if [[ $EUID -ne 0 ]]; then
LogError "This script must be run as root (use sudo)"
exit 1
fi
}
CheckLogstashInstalled() {
if ! command -v logstash &> /dev/null; then
LogWarning "logstash command not found in PATH"
LogWarning "Make sure Logstash is installed and configured"
fi
if [[ ! -d "$LOGSTASH_INSTALL_DIR" ]]; then
LogInformation "Creating Logstash config directory: $LOGSTASH_INSTALL_DIR"
mkdir -p "$LOGSTASH_INSTALL_DIR"
chmod 755 "$LOGSTASH_INSTALL_DIR"
fi
if [[ ! -d "$LOGSTASH_CONF_D_DIR" ]]; then
LogInformation "Creating Logstash conf.d directory: $LOGSTASH_CONF_D_DIR"
mkdir -p "$LOGSTASH_CONF_D_DIR"
chmod 755 "$LOGSTASH_CONF_D_DIR"
fi
}
UpdateOutputConfig() {
local user="$1"
local password="$2"
local host="$3"
local output_file="${LOGSTASH_CONFIG_DIR}/${OUTPUT_CONFIG_FILE}"
local temp_file=$(mktemp)
if [[ ! -f "$output_file" ]]; then
LogError "Output config file not found: $output_file"
exit 1
fi
LogInformation "Updating Elasticsearch configuration in $OUTPUT_CONFIG_FILE"
sed -e "s/user => \"[^\"]*\"/user => \"$user\"/" \
-e "s/password => \"[^\"]*\"/password => \"$password\"/" \
-e "s|hosts => \[\"[^\"]*\"\]|hosts => [\"$host\"]|" \
"$output_file" > "$temp_file"
if grep -q "user => \"$user\"" "$temp_file" && \
grep -q "password => \"$password\"" "$temp_file" && \
grep -q "hosts => \[\"$host\"\]" "$temp_file"; then
mv "$temp_file" "$output_file"
LogInformation "Updated Elasticsearch settings: user=$user"
else
rm -f "$temp_file"
LogError "Failed to update Elasticsearch configuration"
exit 1
fi
}
InstallLogstashConfigs() {
local installed_count=0
local failed_count=0
# Install main configuration files
LogInformation "Installing main Logstash configuration files"
# Install logstash.yml
if [[ -f "${LOGSTASH_CONFIG_DIR}/logstash.yml" ]]; then
LogInformation "Installing logstash.yml..."
if cp "${LOGSTASH_CONFIG_DIR}/logstash.yml" "${LOGSTASH_INSTALL_DIR}/logstash.yml" && chmod 644 "${LOGSTASH_INSTALL_DIR}/logstash.yml"; then
LogInformation "Installed: ${LOGSTASH_INSTALL_DIR}/logstash.yml"
((installed_count++))
else
LogError "Failed to install: logstash.yml"
((failed_count++))
fi
else
LogWarning "logstash.yml not found in source directory"
fi
# Install pipelines.yml
if [[ -f "${LOGSTASH_CONFIG_DIR}/pipelines.yml" ]]; then
LogInformation "Installing pipelines.yml..."
if cp "${LOGSTASH_CONFIG_DIR}/pipelines.yml" "${LOGSTASH_INSTALL_DIR}/pipelines.yml" && chmod 644 "${LOGSTASH_INSTALL_DIR}/pipelines.yml"; then
LogInformation "Installed: ${LOGSTASH_INSTALL_DIR}/pipelines.yml"
((installed_count++))
else
LogError "Failed to install: pipelines.yml"
((failed_count++))
fi
else
LogWarning "pipelines.yml not found in source directory"
fi
# Install conf.d configurations
LogInformation "Installing Logstash pipeline configurations from ${LOGSTASH_CONFIG_DIR}/conf.d"
if [[ -d "${LOGSTASH_CONFIG_DIR}/conf.d" ]]; then
while IFS= read -r -d '' config_file; do
local filename=$(basename "$config_file")
local target_file="${LOGSTASH_CONF_D_DIR}/${filename}"
LogInformation "Installing $filename..."
if cp "$config_file" "$target_file" && chmod 644 "$target_file"; then
LogInformation "Installed: $target_file"
((installed_count++))
else
LogError "Failed to install: $filename"
((failed_count++))
fi
done < <(find "${LOGSTASH_CONFIG_DIR}/conf.d" -name "*.conf" -type f -print0)
else
LogWarning "conf.d directory not found in source"
fi
LogInformation "Installation summary: $installed_count installed, $failed_count failed"
if [[ $failed_count -gt 0 ]]; then
exit 1
fi
}
ValidateLogstashConfig() {
LogInformation "Validating Logstash configuration..."
if command -v logstash &> /dev/null; then
if logstash -t --path.settings=/etc/logstash 2>/dev/null; then
LogInformation "Logstash configuration validation passed"
else
LogWarning "Logstash configuration validation failed or unavailable"
LogWarning "Run manually: sudo logstash -t --path.settings=/etc/logstash"
fi
else
LogWarning "Logstash not available for config validation"
fi
}
ShowStatus() {
LogInformation "Logstash configuration status:"
LogInformation ""
# Check main config files
if [[ -f "${LOGSTASH_INSTALL_DIR}/logstash.yml" ]]; then
LogInformation "✓ Main config: ${LOGSTASH_INSTALL_DIR}/logstash.yml"
else
LogWarning "✗ Main config not found: ${LOGSTASH_INSTALL_DIR}/logstash.yml"
fi
if [[ -f "${LOGSTASH_INSTALL_DIR}/pipelines.yml" ]]; then
LogInformation "✓ Pipelines config: ${LOGSTASH_INSTALL_DIR}/pipelines.yml"
else
LogWarning "✗ Pipelines config not found: ${LOGSTASH_INSTALL_DIR}/pipelines.yml"
fi
# Check conf.d directory
if [[ -d "$LOGSTASH_CONF_D_DIR" ]]; then
local config_count=$(find "$LOGSTASH_CONF_D_DIR" -name "*.conf" -type f | wc -l)
LogInformation "Pipeline configurations: $config_count"
if [[ $config_count -gt 0 ]]; then
LogInformation "Configuration files:"
find "$LOGSTASH_CONF_D_DIR" -name "*.conf" -type f -exec basename {} \; | sort | while read -r file; do
LogInformation "$file"
done
fi
else
LogWarning "Logstash conf.d directory not found: $LOGSTASH_CONF_D_DIR"
fi
LogInformation ""
LogInformation "Commands:"
LogInformation " Test config: sudo logstash -t --path.settings=/etc/logstash"
LogInformation " Start Logstash: sudo systemctl start logstash"
LogInformation " Check status: sudo systemctl status logstash"
}
RemoveLogstashConfigs() {
local removed_count=0
LogInformation "Removing Logstash configurations..."
# Remove main config files
if [[ -f "${LOGSTASH_INSTALL_DIR}/logstash.yml" ]]; then
LogInformation "Removing logstash.yml..."
rm -f "${LOGSTASH_INSTALL_DIR}/logstash.yml"
((removed_count++))
fi
if [[ -f "${LOGSTASH_INSTALL_DIR}/pipelines.yml" ]]; then
LogInformation "Removing pipelines.yml..."
rm -f "${LOGSTASH_INSTALL_DIR}/pipelines.yml"
((removed_count++))
fi
# Remove conf.d configurations
if [[ -d "$LOGSTASH_CONF_D_DIR" ]]; then
while IFS= read -r -d '' source_file; do
local filename=$(basename "$source_file")
local target_file="${LOGSTASH_CONF_D_DIR}/${filename}"
if [[ -f "$target_file" ]]; then
LogInformation "Removing $filename..."
rm -f "$target_file"
((removed_count++))
fi
done < <(find "${LOGSTASH_CONFIG_DIR}/conf.d" -name "*.conf" -type f -print0 2>/dev/null)
fi
if [[ $removed_count -gt 0 ]]; then
LogInformation "Removed $removed_count Logstash configurations"
else
LogWarning "No configurations were found to remove"
fi
}
ShowUsage() {
echo "Usage: $0 install <user> <password> <host>"
echo " $0 [remove|status|help]"
echo ""
echo "Commands:"
echo " install - Install Logstash configurations with Elasticsearch settings"
echo " remove - Remove all Logstash configurations"
echo " status - Show status of configurations"
echo " help - Show this help message"
echo ""
echo "Parameters for install:"
echo " user - Elasticsearch username"
echo " password - Elasticsearch password"
echo " host - Elasticsearch host (e.g., http://localhost:9200)"
echo ""
echo "Examples:"
echo " sudo $0 install elastic mypassword http://localhost:9200"
echo " sudo $0 status"
echo " sudo $0 remove"
}
Main() {
local action="$1"
case "$action" in
install)
if [[ $# -ne 4 ]]; then
LogError "Install command requires 3 parameters: user, password, host"
ShowUsage
exit 1
fi
local user="$2"
local password="$3"
local host="$4"
LogInformation "Installing Logstash configurations"
LogInformation "Elasticsearch settings: user=$user"
CheckRoot
CheckLogstashInstalled
UpdateOutputConfig "$user" "$password" "$host"
InstallLogstashConfigs
ValidateLogstashConfig
LogInformation "Logstash setup completed successfully!"
;;
remove)
LogInformation "Removing Logstash configurations"
CheckRoot
RemoveLogstashConfigs
;;
status)
ShowStatus
;;
help|--help|-h)
ShowUsage
;;
*)
LogError "Unknown action: $action"
ShowUsage
exit 1
;;
esac
}
Main "$@"