Files
hana-scripts/install.sh
2026-03-12 20:47:56 +01:00

278 lines
7.9 KiB
Bash

#!/bin/bash
# =============================================================================
# SAP HANA Monitoring Scripts Installer
# Downloads and installs HANA monitoring scripts to /root/hana-scripts/
# Preserves existing configuration if hana.conf already exists
# =============================================================================
set -e
# Configuration
INSTALL_DIR="/root/hana-scripts"
REPO_URL="https://git.technopunk.space/tomi/hana-scripts/raw/branch/main"
BACKUP_DIR="/root/hana-scripts-backup-$(date +%Y%m%d_%H%M%S)"
# List of files to install
FILES=(
"hana.conf"
"hana_lib.sh"
"hana_disk.sh"
"hana_backup.sh"
"hana_log_segments.sh"
"hana_processes.sh"
"hana_queue.sh"
"sld_watchdog.sh"
)
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if running as root
check_root() {
if [ "$EUID" -ne 0 ]; then
log_error "This script must be run as root"
exit 1
fi
}
# Check if curl is available
check_curl() {
if ! command -v curl &> /dev/null; then
log_error "curl is required but not installed. Please install curl first."
exit 1
fi
log_info "curl is available"
}
# Create backup of existing installation
backup_existing() {
if [ -d "$INSTALL_DIR" ]; then
log_info "Existing installation found at $INSTALL_DIR"
log_info "Creating backup at $BACKUP_DIR..."
mkdir -p "$BACKUP_DIR"
cp -r "$INSTALL_DIR"/* "$BACKUP_DIR"/ 2>/dev/null || true
# Save the existing hana.conf separately for easy access
if [ -f "$INSTALL_DIR/hana.conf" ]; then
cp "$INSTALL_DIR/hana.conf" "$BACKUP_DIR/hana.conf.preserved"
log_success "Existing hana.conf backed up to $BACKUP_DIR/hana.conf.preserved"
fi
log_success "Backup completed"
fi
}
# Create installation directory
create_install_dir() {
log_info "Creating installation directory: $INSTALL_DIR"
mkdir -p "$INSTALL_DIR"
}
# Download a file from the repository
download_file() {
local filename="$1"
local url="${REPO_URL}/${filename}"
local dest="${INSTALL_DIR}/${filename}"
log_info "Downloading ${filename}..."
if curl -sSf -o "$dest" "$url"; then
log_success "Downloaded ${filename}"
return 0
else
log_error "Failed to download ${filename}"
return 1
fi
}
# Restore preserved configuration
restore_config() {
local preserved_config="$BACKUP_DIR/hana.conf.preserved"
if [ -f "$preserved_config" ]; then
log_info "Preserving existing hana.conf..."
# Download the new config to a temp location
local temp_config="${INSTALL_DIR}/hana.conf.new"
if curl -sSf -o "$temp_config" "${REPO_URL}/hana.conf"; then
# Compare configs to check for new settings
if ! diff -q "$preserved_config" "$temp_config" > /dev/null 2>&1; then
log_warning "Configuration file has updates. Merging changes..."
# Keep the old config but add any new settings from the new config
# First, copy the preserved config
cp "$preserved_config" "$dest"
# Extract new settings from the new config (settings not in old config)
# This is a simple merge - new variables are appended if not present
while IFS= read -r line; do
# Skip comments and empty lines
if [[ "$line" =~ ^[[:space:]]*# ]] || [[ -z "${line// }" ]]; then
continue
fi
# Extract variable name
var_name="${line%%=*}"
var_name="${var_name// /}"
# Check if variable exists in preserved config
if ! grep -q "^${var_name}=" "$preserved_config" 2>/dev/null; then
log_info "Adding new configuration: ${var_name}"
echo "" >> "$dest"
echo "$line" >> "$dest"
fi
done < "$temp_config"
log_success "Configuration merged successfully"
else
log_info "No configuration changes detected, keeping existing config"
cp "$preserved_config" "$dest"
fi
# Clean up temp file
rm -f "$temp_config"
else
log_warning "Could not download new hana.conf, keeping existing config"
cp "$preserved_config" "$dest"
fi
return 0
fi
return 1
}
# Set executable permissions
set_permissions() {
log_info "Setting executable permissions..."
chmod +x "${INSTALL_DIR}"/*.sh 2>/dev/null || true
log_success "Permissions set"
}
# Verify installation
verify_installation() {
log_info "Verifying installation..."
local errors=0
for file in "${FILES[@]}"; do
if [ ! -f "${INSTALL_DIR}/${file}" ]; then
log_error "Missing file: ${file}"
errors=$((errors + 1))
fi
done
if [ $errors -eq 0 ]; then
log_success "Installation verified successfully"
return 0
else
log_error "Verification failed with ${errors} errors"
return 1
fi
}
# Print summary
print_summary() {
echo ""
echo "=============================================="
echo " Installation Complete!"
echo "=============================================="
echo ""
echo "Installation directory: ${INSTALL_DIR}"
echo ""
if [ -d "$BACKUP_DIR" ]; then
echo "Backup location: ${BACKUP_DIR}"
echo ""
fi
echo "Next steps:"
echo " 1. Review and update ${INSTALL_DIR}/hana.conf with your settings"
echo " 2. Make scripts executable: chmod +x ${INSTALL_DIR}/*.sh"
echo " 3. Test individual scripts manually"
echo " 4. Set up cron jobs for automated monitoring"
echo ""
echo "Example cron entries (run crontab -e):"
echo " */5 * * * * ${INSTALL_DIR}/hana_disk.sh"
echo " */5 * * * * ${INSTALL_DIR}/hana_processes.sh"
echo " */10 * * * * ${INSTALL_DIR}/hana_backup.sh"
echo " */5 * * * * ${INSTALL_DIR}/hana_queue.sh"
echo " */5 * * * * ${INSTALL_DIR}/hana_log_segments.sh"
echo " */5 * * * * ${INSTALL_DIR}/sld_watchdog.sh"
echo ""
}
# Main installation function
main() {
echo ""
echo "=============================================="
echo " SAP HANA Monitoring Scripts Installer"
echo "=============================================="
echo ""
# Pre-flight checks
check_root
check_curl
# Backup existing installation
backup_existing
# Create installation directory
create_install_dir
# Download all files
local download_errors=0
for file in "${FILES[@]}"; do
if [ "$file" == "hana.conf" ]; then
# Handle config file specially
if ! restore_config; then
# No existing config, download fresh
download_file "$file" || download_errors=$((download_errors + 1))
fi
else
download_file "$file" || download_errors=$((download_errors + 1))
fi
done
if [ $download_errors -gt 0 ]; then
log_error "Failed to download ${download_errors} files"
exit 1
fi
# Set permissions
set_permissions
# Verify installation
if ! verify_installation; then
exit 1
fi
# Print summary
print_summary
}
# Run main function
main "$@"