mirror of
https://github.com/AlexMacocian/ServerManagementUtils.git
synced 2026-07-15 15:19:58 +00:00
263 lines
9.0 KiB
Bash
Executable File
263 lines
9.0 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
. "${SCRIPT_DIR}/../share/logging.sh"
|
|
|
|
# Configuration
|
|
METRICBEAT_CONFIG_DIR="${SCRIPT_DIR}/../metricbeat"
|
|
METRICBEAT_INSTALL_DIR="/etc/metricbeat"
|
|
METRICBEAT_MODULES_D_DIR="${METRICBEAT_INSTALL_DIR}/modules.d"
|
|
CONFIG_FILE="metricbeat.yml"
|
|
|
|
CheckRoot() {
|
|
if [[ $EUID -ne 0 ]]; then
|
|
LogError "This script must be run as root (use sudo)"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
CheckMetricbeatInstalled() {
|
|
if ! command -v metricbeat &> /dev/null; then
|
|
LogWarning "metricbeat command not found in PATH"
|
|
LogWarning "Make sure Metricbeat is installed and configured"
|
|
fi
|
|
|
|
if [[ ! -d "$METRICBEAT_INSTALL_DIR" ]]; then
|
|
LogInformation "Creating Metricbeat config directory: $METRICBEAT_INSTALL_DIR"
|
|
mkdir -p "$METRICBEAT_INSTALL_DIR"
|
|
chmod 755 "$METRICBEAT_INSTALL_DIR"
|
|
fi
|
|
|
|
if [[ ! -d "$METRICBEAT_MODULES_D_DIR" ]]; then
|
|
LogInformation "Creating Metricbeat modules.d directory: $METRICBEAT_MODULES_D_DIR"
|
|
mkdir -p "$METRICBEAT_MODULES_D_DIR"
|
|
chmod 755 "$METRICBEAT_MODULES_D_DIR"
|
|
fi
|
|
}
|
|
|
|
UpdateMetricbeatConfig() {
|
|
local user="$1"
|
|
local password="$2"
|
|
local host="$3"
|
|
local config_file="${METRICBEAT_CONFIG_DIR}/${CONFIG_FILE}"
|
|
local temp_file=$(mktemp)
|
|
|
|
if [[ ! -f "$config_file" ]]; then
|
|
LogError "Metricbeat config file not found: $config_file"
|
|
exit 1
|
|
fi
|
|
|
|
LogInformation "Updating Elasticsearch configuration in $CONFIG_FILE"
|
|
|
|
sed -e "s/username: \"[^\"]*\"/username: \"$user\"/" \
|
|
-e "s/password: \"[^\"]*\"/password: \"$password\"/" \
|
|
-e "s|hosts: \[\"[^\"]*\"\]|hosts: [\"$host\"]|" \
|
|
"$config_file" > "$temp_file"
|
|
|
|
if grep -q "username: \"$user\"" "$temp_file" && \
|
|
grep -q "password: \"$password\"" "$temp_file" && \
|
|
grep -q "hosts: [\"$host\"]" "$temp_file"; then
|
|
mv "$temp_file" "$config_file"
|
|
LogInformation "Updated Elasticsearch settings: user=$user"
|
|
else
|
|
rm -f "$temp_file"
|
|
LogError "Failed to update Elasticsearch configuration"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
InstallMetricbeatConfigs() {
|
|
local installed_count=0
|
|
local failed_count=0
|
|
|
|
# Install main configuration file
|
|
LogInformation "Installing main Metricbeat configuration file"
|
|
|
|
# Install metricbeat.yml
|
|
if [[ -f "${METRICBEAT_CONFIG_DIR}/${CONFIG_FILE}" ]]; then
|
|
LogInformation "Installing ${CONFIG_FILE}..."
|
|
if cp "${METRICBEAT_CONFIG_DIR}/${CONFIG_FILE}" "${METRICBEAT_INSTALL_DIR}/${CONFIG_FILE}" && chmod 644 "${METRICBEAT_INSTALL_DIR}/${CONFIG_FILE}"; then
|
|
LogInformation "Installed: ${METRICBEAT_INSTALL_DIR}/${CONFIG_FILE}"
|
|
((installed_count++))
|
|
else
|
|
LogError "Failed to install: ${CONFIG_FILE}"
|
|
((failed_count++))
|
|
fi
|
|
else
|
|
LogWarning "${CONFIG_FILE} not found in source directory"
|
|
fi
|
|
|
|
# Install modules.d configurations
|
|
LogInformation "Installing Metricbeat modules from ${METRICBEAT_CONFIG_DIR}/modules.d"
|
|
if [[ -d "${METRICBEAT_CONFIG_DIR}/modules.d" ]]; then
|
|
while IFS= read -r -d '' module_file; do
|
|
local filename=$(basename "$module_file")
|
|
local target_file="${METRICBEAT_MODULES_D_DIR}/${filename}"
|
|
|
|
LogInformation "Installing module $filename..."
|
|
if cp "$module_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 "${METRICBEAT_CONFIG_DIR}/modules.d" -name "*.yml" -type f -print0)
|
|
else
|
|
LogWarning "modules.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
|
|
}
|
|
|
|
ValidateMetricbeatConfig() {
|
|
LogInformation "Validating Metricbeat configuration..."
|
|
if command -v metricbeat &> /dev/null; then
|
|
if metricbeat test config -c "${METRICBEAT_INSTALL_DIR}/${CONFIG_FILE}" 2>/dev/null; then
|
|
LogInformation "Metricbeat configuration validation passed"
|
|
else
|
|
LogWarning "Metricbeat configuration validation failed or unavailable"
|
|
LogWarning "Run manually: sudo metricbeat test config -c ${METRICBEAT_INSTALL_DIR}/${CONFIG_FILE}"
|
|
fi
|
|
else
|
|
LogWarning "Metricbeat not available for config validation"
|
|
fi
|
|
}
|
|
|
|
ShowStatus() {
|
|
LogInformation "Metricbeat configuration status:"
|
|
LogInformation ""
|
|
|
|
# Check main config file
|
|
if [[ -f "${METRICBEAT_INSTALL_DIR}/${CONFIG_FILE}" ]]; then
|
|
LogInformation "✓ Main config: ${METRICBEAT_INSTALL_DIR}/${CONFIG_FILE}"
|
|
else
|
|
LogWarning "✗ Main config not found: ${METRICBEAT_INSTALL_DIR}/${CONFIG_FILE}"
|
|
fi
|
|
|
|
# Check modules.d directory
|
|
if [[ -d "$METRICBEAT_MODULES_D_DIR" ]]; then
|
|
local module_count=$(find "$METRICBEAT_MODULES_D_DIR" -name "*.yml" -type f | wc -l)
|
|
LogInformation "Module configurations: $module_count"
|
|
|
|
if [[ $module_count -gt 0 ]]; then
|
|
LogInformation "Module files:"
|
|
find "$METRICBEAT_MODULES_D_DIR" -name "*.yml" -type f -exec basename {} \; | sort | while read -r file; do
|
|
LogInformation " ✓ $file"
|
|
done
|
|
fi
|
|
else
|
|
LogWarning "Metricbeat modules.d directory not found: $METRICBEAT_MODULES_D_DIR"
|
|
fi
|
|
|
|
LogInformation ""
|
|
LogInformation "Commands:"
|
|
LogInformation " Test config: sudo metricbeat test config -c ${METRICBEAT_INSTALL_DIR}/${CONFIG_FILE}"
|
|
LogInformation " Test output: sudo metricbeat test output -c ${METRICBEAT_INSTALL_DIR}/${CONFIG_FILE}"
|
|
LogInformation " Start Metricbeat: sudo systemctl start metricbeat"
|
|
LogInformation " Check status: sudo systemctl status metricbeat"
|
|
}
|
|
|
|
RemoveMetricbeatConfigs() {
|
|
local removed_count=0
|
|
|
|
LogInformation "Removing Metricbeat configurations..."
|
|
|
|
# Remove main config file
|
|
if [[ -f "${METRICBEAT_INSTALL_DIR}/${CONFIG_FILE}" ]]; then
|
|
LogInformation "Removing ${CONFIG_FILE}..."
|
|
rm -f "${METRICBEAT_INSTALL_DIR}/${CONFIG_FILE}"
|
|
((removed_count++))
|
|
fi
|
|
|
|
# Remove modules.d configurations
|
|
if [[ -d "$METRICBEAT_MODULES_D_DIR" ]]; then
|
|
while IFS= read -r -d '' source_file; do
|
|
local filename=$(basename "$source_file")
|
|
local target_file="${METRICBEAT_MODULES_D_DIR}/${filename}"
|
|
|
|
if [[ -f "$target_file" ]]; then
|
|
LogInformation "Removing module $filename..."
|
|
rm -f "$target_file"
|
|
((removed_count++))
|
|
fi
|
|
done < <(find "${METRICBEAT_CONFIG_DIR}/modules.d" -name "*.yml" -type f -print0 2>/dev/null)
|
|
fi
|
|
|
|
if [[ $removed_count -gt 0 ]]; then
|
|
LogInformation "Removed $removed_count Metricbeat 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 Metricbeat configurations with Elasticsearch settings"
|
|
echo " remove - Remove all Metricbeat 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., localhost:9200)"
|
|
echo ""
|
|
echo "Examples:"
|
|
echo " sudo $0 install elastic mypassword"
|
|
echo " sudo $0 status"
|
|
echo " sudo $0 remove"
|
|
}
|
|
|
|
Main() {
|
|
local action="$1"
|
|
|
|
case "$action" in
|
|
install)
|
|
if [[ $# -ne 3 ]]; then
|
|
LogError "Install command requires 2 parameters: user, password"
|
|
ShowUsage
|
|
exit 1
|
|
fi
|
|
|
|
local user="$2"
|
|
local password="$3"
|
|
local host="$4"
|
|
|
|
LogInformation "Installing Metricbeat configurations"
|
|
LogInformation "Elasticsearch settings: user=$user"
|
|
|
|
CheckRoot
|
|
CheckMetricbeatInstalled
|
|
UpdateMetricbeatConfig "$user" "$password" "$host"
|
|
InstallMetricbeatConfigs
|
|
ValidateMetricbeatConfig
|
|
LogInformation "Metricbeat setup completed successfully!"
|
|
;;
|
|
remove)
|
|
LogInformation "Removing Metricbeat configurations"
|
|
CheckRoot
|
|
RemoveMetricbeatConfigs
|
|
;;
|
|
status)
|
|
ShowStatus
|
|
;;
|
|
help|--help|-h)
|
|
ShowUsage
|
|
;;
|
|
*)
|
|
LogError "Unknown action: $action"
|
|
ShowUsage
|
|
exit 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
Main "$@"
|