Files
ServerManagementUtils/orchestrator/setup-logstash.sh
T
2025-06-28 14:28:48 +02:00

217 lines
6.8 KiB
Bash

#!/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/conf.d"
OUTPUT_CONFIG_FILE="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
}
UpdateOutputConfig() {
local index="$1"
local user="$2"
local password="$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/index => \"[^\"]*\"/index => \"$index\"/" \
-e "s/user => \"[^\"]*\"/user => \"$user\"/" \
-e "s/password => \"[^\"]*\"/password => \"$password\"/" \
"$output_file" > "$temp_file"
if grep -q "index => \"$index\"" "$temp_file" && \
grep -q "user => \"$user\"" "$temp_file" && \
grep -q "password => \"$password\"" "$temp_file"; then
mv "$temp_file" "$output_file"
LogInformation "Updated Elasticsearch settings: index=$index, 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
LogInformation "Installing Logstash configurations from $LOGSTASH_CONFIG_DIR"
while IFS= read -r -d '' config_file; do
local filename=$(basename "$config_file")
local target_file="${LOGSTASH_INSTALL_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" -name "*.conf" -type f -print0)
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 ""
if [[ -d "$LOGSTASH_INSTALL_DIR" ]]; then
local config_count=$(find "$LOGSTASH_INSTALL_DIR" -name "*.conf" -type f | wc -l)
LogInformation "Installed configurations: $config_count"
LogInformation "Configuration files:"
find "$LOGSTASH_INSTALL_DIR" -name "*.conf" -type f -exec basename {} \; | sort | while read -r file; do
LogInformation "$file"
done
else
LogWarning "Logstash config directory not found: $LOGSTASH_INSTALL_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
if [[ ! -d "$LOGSTASH_INSTALL_DIR" ]]; then
LogWarning "Logstash config directory not found: $LOGSTASH_INSTALL_DIR"
return
fi
LogInformation "Removing Logstash configurations..."
while IFS= read -r -d '' source_file; do
local filename=$(basename "$source_file")
local target_file="${LOGSTASH_INSTALL_DIR}/${filename}"
if [[ -f "$target_file" ]]; then
LogInformation "Removing $filename..."
rm -f "$target_file"
((removed_count++))
fi
done < <(find "$LOGSTASH_CONFIG_DIR" -name "*.conf" -type f -print0)
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 <index> <user> <password>"
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 " index - Elasticsearch index name"
echo " user - Elasticsearch username"
echo " password - Elasticsearch password"
echo ""
echo "Examples:"
echo " sudo $0 install myserver-logs elastic mypassword"
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: index, user, password"
ShowUsage
exit 1
fi
local index="$2"
local user="$3"
local password="$4"
LogInformation "Installing Logstash configurations"
LogInformation "Elasticsearch settings: index=$index, user=$user"
CheckRoot
CheckLogstashInstalled
UpdateOutputConfig "$index" "$user" "$password"
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 "$@"