mirror of
				https://github.com/bashclub/zamba-lxc-toolbox.git
				synced 2025-11-04 00:02:26 +01:00 
			
		
		
		
	Update install-service.sh
This commit is contained in:
		@@ -3,15 +3,12 @@
 | 
			
		||||
# Zamba LXC Toolbox - Service Installer
 | 
			
		||||
# Service: icinga-stack
 | 
			
		||||
#
 | 
			
		||||
# Description: Führt die Installation und Konfiguration des Icinga2 Stacks durch.
 | 
			
		||||
# Description: Führt die Installation und Konfiguration des Icinga2 Stacks mit MariaDB durch.
 | 
			
		||||
# Dieses Skript ist eigenständig und verwendet nur Standard-OS-Befehle.
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
# --- Internal Helper Functions ---
 | 
			
		||||
# Diese Funktion ist skript-spezifisch und nicht Teil eines Frameworks.
 | 
			
		||||
_generate_local_password() {
 | 
			
		||||
    # Erzeugt eine sichere, zufällige Zeichenkette.
 | 
			
		||||
    # $1: Länge der Zeichenkette
 | 
			
		||||
    openssl rand -base64 "$1"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -21,7 +18,7 @@ _generate_local_password() {
 | 
			
		||||
_install() {
 | 
			
		||||
    echo ""
 | 
			
		||||
    echo "================================================="
 | 
			
		||||
    echo "  Phase 1: Installation der Pakete"
 | 
			
		||||
    echo "  Phase 1: Installation der Pakete (MariaDB Edition)"
 | 
			
		||||
    echo "================================================="
 | 
			
		||||
    echo ""
 | 
			
		||||
    
 | 
			
		||||
@@ -35,27 +32,18 @@ _install() {
 | 
			
		||||
    if [ ! -f /etc/apt/sources.list.d/icinga.list ]; then
 | 
			
		||||
        curl -fsSL https://packages.icinga.com/icinga.key | gpg --dearmor -o /usr/share/keyrings/icinga-archive-keyring.gpg
 | 
			
		||||
        echo "deb [signed-by=/usr/share/keyrings/icinga-archive-keyring.gpg] https://packages.icinga.com/debian icinga-${OS_CODENAME} main" > /etc/apt/sources.list.d/icinga.list
 | 
			
		||||
        echo "[INFO] Icinga Repository für ${OS_CODENAME} hinzugefügt."
 | 
			
		||||
    else
 | 
			
		||||
        echo "[INFO] Icinga Repository existiert bereits."
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    # InfluxDB Repo
 | 
			
		||||
    if [ ! -f /etc/apt/sources.list.d/influxdata.list ]; then
 | 
			
		||||
        curl -fsSL https://repos.influxdata.com/influxdata-archive_compat.key | gpg --dearmor -o /usr/share/keyrings/influxdata-archive_compat-keyring.gpg
 | 
			
		||||
        echo "deb [signed-by=/usr/share/keyrings/influxdata-archive_compat-keyring.gpg] https://repos.influxdata.com/debian ${OS_CODENAME} stable" > /etc/apt/sources.list.d/influxdata.list
 | 
			
		||||
        echo "[INFO] InfluxDB Repository für ${OS_CODENAME} hinzugefügt."
 | 
			
		||||
    else
 | 
			
		||||
        echo "[INFO] InfluxDB Repository existiert bereits."
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    # Grafana Repo
 | 
			
		||||
    if [ ! -f /etc/apt/sources.list.d/grafana.list ]; then
 | 
			
		||||
        wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor -o /usr/share/keyrings/grafana-archive-keyring.gpg
 | 
			
		||||
        echo "deb [signed-by=/usr/share/keyrings/grafana-archive-keyring.gpg] https://apt.grafana.com stable main" > /etc/apt/sources.list.d/grafana.list
 | 
			
		||||
        echo "[INFO] Grafana Repository hinzugefügt."
 | 
			
		||||
    else
 | 
			
		||||
        echo "[INFO] Grafana Repository existiert bereits."
 | 
			
		||||
    fi
 | 
			
		||||
    
 | 
			
		||||
    echo "[INFO] Paketlisten werden erneut aktualisiert."
 | 
			
		||||
@@ -63,15 +51,14 @@ _install() {
 | 
			
		||||
 | 
			
		||||
    echo "[INFO] Hauptkomponenten werden installiert (PHP Version: ${PHP_VERSION})."
 | 
			
		||||
    apt-get install -y \
 | 
			
		||||
        icinga2 icinga2-ido-pgsql \
 | 
			
		||||
        nginx php${PHP_VERSION}-fpm php${PHP_VERSION}-pgsql php${PHP_VERSION}-intl php${PHP_VERSION}-imagick php${PHP_VERSION}-xml php${PHP_VERSION}-gd php${PHP_VERSION}-ldap \
 | 
			
		||||
        postgresql postgresql-client \
 | 
			
		||||
        icinga2 icinga2-ido-mysql \
 | 
			
		||||
        nginx php${PHP_VERSION}-fpm php${PHP_VERSION}-mysql php${PHP_VERSION}-intl php${PHP_VERSION}-imagick php${PHP_VERSION}-xml php${PHP_VERSION}-gd php${PHP_VERSION}-ldap \
 | 
			
		||||
        mariadb-server mariadb-client \
 | 
			
		||||
        influxdb2 \
 | 
			
		||||
        grafana \
 | 
			
		||||
        icingaweb2 icingacli
 | 
			
		||||
 | 
			
		||||
    echo "[INFO] Icinga Web 2 Module (Abhängigkeiten für Director) werden installiert."
 | 
			
		||||
    # Funktion zum Herunterladen und Entpacken von Modulen
 | 
			
		||||
    install_icinga_module() {
 | 
			
		||||
        local module_name="$1"
 | 
			
		||||
        local repo_name="$2"
 | 
			
		||||
@@ -82,8 +69,6 @@ _install() {
 | 
			
		||||
            tar -C /usr/share/icingaweb2/modules -xzf "/tmp/${module_name}.tar.gz"
 | 
			
		||||
            mv "/usr/share/icingaweb2/modules/${repo_name}-"* "/usr/share/icingaweb2/modules/${module_name}"
 | 
			
		||||
            rm "/tmp/${module_name}.tar.gz"
 | 
			
		||||
        else
 | 
			
		||||
            echo "[INFO] Modul ${module_name} ist bereits installiert."
 | 
			
		||||
        fi
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
@@ -92,14 +77,13 @@ _install() {
 | 
			
		||||
    install_icinga_module "director" "icingaweb2-module-director"
 | 
			
		||||
 | 
			
		||||
    echo "[INFO] Systemd Services werden aktiviert."
 | 
			
		||||
    # Der Service für InfluxDB v2 heißt 'influxdb', nicht 'influxdb2'
 | 
			
		||||
    systemctl enable --now icinga2 postgresql nginx php${PHP_VERSION}-fpm influxdb grafana-server
 | 
			
		||||
    systemctl enable --now icinga2 mariadb nginx php${PHP_VERSION}-fpm influxdb grafana-server
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
_configure() {
 | 
			
		||||
    echo ""
 | 
			
		||||
    echo "================================================="
 | 
			
		||||
    echo "  Phase 2: Konfiguration der Komponenten"
 | 
			
		||||
    echo "  Phase 2: Konfiguration der Komponenten (MariaDB Edition)"
 | 
			
		||||
    echo "================================================="
 | 
			
		||||
    echo ""
 | 
			
		||||
 | 
			
		||||
@@ -113,64 +97,42 @@ _configure() {
 | 
			
		||||
    GRAFANA_ADMIN_PASS=$(_generate_local_password 16)
 | 
			
		||||
    INFLUX_ADMIN_TOKEN=$(_generate_local_password 40)
 | 
			
		||||
    
 | 
			
		||||
    # 2. PostgreSQL konfigurieren
 | 
			
		||||
    echo "[INFO] PostgreSQL wird konfiguriert."
 | 
			
		||||
    sudo -u postgres psql -c "CREATE ROLE icingaweb2 WITH LOGIN PASSWORD '${ICINGAWEB_DB_PASS}';" &>/dev/null || echo "[INFO] Postgres-Rolle 'icingaweb2' existiert bereits."
 | 
			
		||||
    sudo -u postgres psql -c "CREATE ROLE director WITH LOGIN PASSWORD '${DIRECTOR_DB_PASS}';" &>/dev/null || echo "[INFO] Postgres-Rolle 'director' existiert bereits."
 | 
			
		||||
    sudo -u postgres psql -c "CREATE ROLE icinga_ido WITH LOGIN PASSWORD '${ICINGA_IDO_DB_PASS}';" &>/dev/null || echo "[INFO] Postgres-Rolle 'icinga_ido' existiert bereits."
 | 
			
		||||
    sudo -u postgres createdb -O icingaweb2 icingaweb2 &>/dev/null || echo "[INFO] Postgres-DB 'icingaweb2' existiert bereits."
 | 
			
		||||
    sudo -u postgres createdb -O director director &>/dev/null || echo "[INFO] Postgres-DB 'director' existiert bereits."
 | 
			
		||||
    sudo -u postgres createdb -O icinga_ido icinga_ido &>/dev/null || echo "[INFO] Postgres-DB 'icinga_ido' existiert bereits."
 | 
			
		||||
    sudo -u postgres psql -d icinga_ido -c "GRANT ALL ON SCHEMA public TO icinga_ido;"
 | 
			
		||||
    # 2. MariaDB konfigurieren
 | 
			
		||||
    echo "[INFO] MariaDB wird konfiguriert."
 | 
			
		||||
    mysql -e "CREATE DATABASE IF NOT EXISTS icingaweb2 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
 | 
			
		||||
    mysql -e "CREATE DATABASE IF NOT EXISTS director CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
 | 
			
		||||
    mysql -e "CREATE DATABASE IF NOT EXISTS icinga_ido CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
 | 
			
		||||
    
 | 
			
		||||
    mysql -e "CREATE USER IF NOT EXISTS 'icingaweb2'@'localhost' IDENTIFIED BY '${ICINGAWEB_DB_PASS}';"
 | 
			
		||||
    mysql -e "CREATE USER IF NOT EXISTS 'director'@'localhost' IDENTIFIED BY '${DIRECTOR_DB_PASS}';"
 | 
			
		||||
    mysql -e "CREATE USER IF NOT EXISTS 'icinga_ido'@'localhost' IDENTIFIED BY '${ICINGA_IDO_DB_PASS}';"
 | 
			
		||||
 | 
			
		||||
    # 3. InfluxDB 2 konfigurieren und Icinga-Token generieren
 | 
			
		||||
    mysql -e "GRANT ALL PRIVILEGES ON icingaweb2.* TO 'icingaweb2'@'localhost';"
 | 
			
		||||
    mysql -e "GRANT ALL PRIVILEGES ON director.* TO 'director'@'localhost';"
 | 
			
		||||
    mysql -e "GRANT ALL PRIVILEGES ON icinga_ido.* TO 'icinga_ido'@'localhost';"
 | 
			
		||||
    mysql -e "FLUSH PRIVILEGES;"
 | 
			
		||||
 | 
			
		||||
    # 3. InfluxDB 2 konfigurieren
 | 
			
		||||
    echo "[INFO] InfluxDB 2 wird konfiguriert."
 | 
			
		||||
    influx setup --skip-verify --username admin --password "$GRAFANA_ADMIN_PASS" --org icinga --bucket icinga --token "$INFLUX_ADMIN_TOKEN" -f
 | 
			
		||||
    
 | 
			
		||||
    echo "[INFO] Erstelle dedizierten InfluxDB Token für Icinga und Grafana."
 | 
			
		||||
    INFLUX_ICINGA_TOKEN=$(influx auth create --org icinga --all-access --json | grep -oP '"token": "\K[^"]+')
 | 
			
		||||
    if [ -z "$INFLUX_ICINGA_TOKEN" ]; then
 | 
			
		||||
        echo "[ERROR] Konnte InfluxDB Token für Icinga nicht erstellen." >&2
 | 
			
		||||
        exit 1
 | 
			
		||||
    fi
 | 
			
		||||
    echo "[INFO] InfluxDB Token erfolgreich erstellt."
 | 
			
		||||
    if [ -z "$INFLUX_ICINGA_TOKEN" ]; then echo "[ERROR] Konnte InfluxDB Token nicht erstellen." >&2; exit 1; fi
 | 
			
		||||
 | 
			
		||||
    # 4. Credentials-Datei schreiben (jetzt sind alle Werte bekannt)
 | 
			
		||||
    # 4. Credentials-Datei schreiben
 | 
			
		||||
    echo "[INFO] Zugangsdaten werden in ${CRED_FILE} gespeichert."
 | 
			
		||||
    mkdir -p "$(dirname "$CRED_FILE")"
 | 
			
		||||
    chmod 700 "$(dirname "$CRED_FILE")"
 | 
			
		||||
    mkdir -p "$(dirname "$CRED_FILE")" && chmod 700 "$(dirname "$CRED_FILE")"
 | 
			
		||||
    {
 | 
			
		||||
      echo "# --- Icinga Monitoring Stack Credentials ---"
 | 
			
		||||
      echo "# Automatisch generiert am $(date)"
 | 
			
		||||
      echo "# OS: Debian ${OS_CODENAME}"
 | 
			
		||||
      echo ""
 | 
			
		||||
      echo "## Icinga Web 2"
 | 
			
		||||
      echo "URL: https://${ZAMBA_HOSTNAME:-$(hostname -f)}/icingaweb2"
 | 
			
		||||
      echo "Benutzer: icingaadmin"
 | 
			
		||||
      echo "Passwort: ${ICINGAWEB_ADMIN_PASS}"
 | 
			
		||||
      echo ""
 | 
			
		||||
      echo "## Grafana"
 | 
			
		||||
      echo "URL: https://${ZAMBA_HOSTNAME:-$(hostname -f)}/grafana"
 | 
			
		||||
      echo "Benutzer: admin"
 | 
			
		||||
      echo "Passwort: ${GRAFANA_ADMIN_PASS}"
 | 
			
		||||
      echo ""
 | 
			
		||||
      echo "## InfluxDB 2 (für API-Nutzung)"
 | 
			
		||||
      echo "URL: http://localhost:8086"
 | 
			
		||||
      echo "Admin Token: ${INFLUX_ADMIN_TOKEN}"
 | 
			
		||||
      echo "Icinga Token: ${INFLUX_ICINGA_TOKEN}"
 | 
			
		||||
      echo "Organisation: icinga"
 | 
			
		||||
      echo "Bucket: icinga"
 | 
			
		||||
      echo ""
 | 
			
		||||
      echo "## Icinga2 Director API"
 | 
			
		||||
      echo "Benutzer: director"
 | 
			
		||||
      echo "Passwort: ${ICINGA_API_USER_PASS}"
 | 
			
		||||
    } > "$CRED_FILE"
 | 
			
		||||
    chmod 600 "$CRED_FILE"
 | 
			
		||||
      echo "URL: https://${ZAMBA_HOSTNAME:-$(hostname -f)}/icingaweb2; Benutzer: icingaadmin; Passwort: ${ICINGAWEB_ADMIN_PASS}"
 | 
			
		||||
      echo "URL: https://${ZAMBA_HOSTNAME:-$(hostname -f)}/grafana; Benutzer: admin; Passwort: ${GRAFANA_ADMIN_PASS}"
 | 
			
		||||
      echo "InfluxDB Admin Token: ${INFLUX_ADMIN_TOKEN}"
 | 
			
		||||
      echo "Icinga Director API: Benutzer: director; Passwort: ${ICINGA_API_USER_PASS}"
 | 
			
		||||
    } > "$CRED_FILE" && chmod 600 "$CRED_FILE"
 | 
			
		||||
 | 
			
		||||
    # 5. Icinga2 Konfigurationsdateien schreiben
 | 
			
		||||
    echo "[INFO] Icinga2 Konfigurationsdateien werden geschrieben."
 | 
			
		||||
    bash -c "cat > /etc/icinga2/features-available/ido-pgsql.conf" <<EOF
 | 
			
		||||
object IdoPgsqlConnection "ido-pgsql" {
 | 
			
		||||
    bash -c "cat > /etc/icinga2/features-available/ido-mysql.conf" <<EOF
 | 
			
		||||
object IdoMysqlConnection "ido-mysql" {
 | 
			
		||||
  user = "icinga_ido",
 | 
			
		||||
  password = "${ICINGA_IDO_DB_PASS}",
 | 
			
		||||
  host = "localhost",
 | 
			
		||||
@@ -189,8 +151,6 @@ object Influxdb2Writer "influxdb2-writer" {
 | 
			
		||||
  organization = "icinga"
 | 
			
		||||
  bucket = "icinga"
 | 
			
		||||
  token = "${INFLUX_ICINGA_TOKEN}"
 | 
			
		||||
  flush_interval = 10s
 | 
			
		||||
  flush_threshold = 1024
 | 
			
		||||
}
 | 
			
		||||
EOF
 | 
			
		||||
 | 
			
		||||
@@ -200,27 +160,24 @@ EOF
 | 
			
		||||
    bash -c "cat > /etc/icingaweb2/resources.ini" <<EOF
 | 
			
		||||
[icingaweb_db]
 | 
			
		||||
type = "db"
 | 
			
		||||
db = "pgsql"
 | 
			
		||||
db = "mysql"
 | 
			
		||||
host = "localhost"
 | 
			
		||||
port = "5432"
 | 
			
		||||
dbname = "icingaweb2"
 | 
			
		||||
username = "icingaweb2"
 | 
			
		||||
password = "${ICINGAWEB_DB_PASS}"
 | 
			
		||||
 | 
			
		||||
[director_db]
 | 
			
		||||
type = "db"
 | 
			
		||||
db = "pgsql"
 | 
			
		||||
db = "mysql"
 | 
			
		||||
host = "localhost"
 | 
			
		||||
port = "5432"
 | 
			
		||||
dbname = "director"
 | 
			
		||||
username = "director"
 | 
			
		||||
password = "${DIRECTOR_DB_PASS}"
 | 
			
		||||
 | 
			
		||||
[icinga_ido]
 | 
			
		||||
type = "db"
 | 
			
		||||
db = "pgsql"
 | 
			
		||||
db = "mysql"
 | 
			
		||||
host = "localhost"
 | 
			
		||||
port = "5432"
 | 
			
		||||
dbname = "icinga_ido"
 | 
			
		||||
username = "icinga_ido"
 | 
			
		||||
password = "${ICINGA_IDO_DB_PASS}"
 | 
			
		||||
@@ -228,11 +185,8 @@ EOF
 | 
			
		||||
    
 | 
			
		||||
    # 7. Grafana konfigurieren
 | 
			
		||||
    echo "[INFO] Grafana wird konfiguriert."
 | 
			
		||||
    # Grafana-Dienst stoppen, um DB-Sperre zu vermeiden
 | 
			
		||||
    echo "[INFO] Stoppe Grafana-Dienst für Passwort-Reset..."
 | 
			
		||||
    systemctl stop grafana-server
 | 
			
		||||
    grafana-cli admin reset-admin-password "$GRAFANA_ADMIN_PASS"
 | 
			
		||||
    echo "[INFO] Starte Grafana-Dienst neu."
 | 
			
		||||
    systemctl start grafana-server
 | 
			
		||||
    
 | 
			
		||||
    mkdir -p /etc/grafana/provisioning/datasources
 | 
			
		||||
@@ -243,13 +197,8 @@ datasources:
 | 
			
		||||
  type: influxdb
 | 
			
		||||
  access: proxy
 | 
			
		||||
  url: http://localhost:8086
 | 
			
		||||
  jsonData:
 | 
			
		||||
    version: Flux
 | 
			
		||||
    organization: icinga
 | 
			
		||||
    defaultBucket: icinga
 | 
			
		||||
    tlsSkipVerify: true
 | 
			
		||||
  secureJsonData:
 | 
			
		||||
    token: "${INFLUX_ICINGA_TOKEN}"
 | 
			
		||||
  jsonData: { version: "Flux", organization: "icinga", defaultBucket: "icinga" }
 | 
			
		||||
  secureJsonData: { token: "${INFLUX_ICINGA_TOKEN}" }
 | 
			
		||||
EOF
 | 
			
		||||
    chown grafana:grafana /etc/grafana/provisioning/datasources/influxdb.yaml
 | 
			
		||||
    
 | 
			
		||||
@@ -261,21 +210,16 @@ EOF
 | 
			
		||||
        ln -s /etc/ssl/private/ssl-cert-snakeoil.key /etc/nginx/ssl/privkey.pem
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    # Sicherstellen, dass der 'icinga'-Benutzer existiert, bevor er modifiziert wird.
 | 
			
		||||
    if ! id -u icinga >/dev/null 2>&1; then
 | 
			
		||||
        echo "[WARN] Systembenutzer 'icinga' nicht gefunden. Wird erstellt."
 | 
			
		||||
        useradd --system --shell /usr/sbin/nologin --home-dir /var/lib/icinga2 icinga
 | 
			
		||||
    fi
 | 
			
		||||
    # Icinga-Benutzer zur ssl-cert Gruppe hinzufügen, um den Schlüssel lesen zu können
 | 
			
		||||
    usermod -a -G ssl-cert icinga
 | 
			
		||||
 | 
			
		||||
    # api.conf anpassen, um die Nginx/Snakeoil-Zertifikate zu verwenden
 | 
			
		||||
    bash -c "cat > /etc/icinga2/features-available/api.conf" <<EOF
 | 
			
		||||
object ApiListener "api" {
 | 
			
		||||
  cert_path = "/etc/nginx/ssl/fullchain.pem"
 | 
			
		||||
  key_path = "/etc/nginx/ssl/privkey.pem"
 | 
			
		||||
  ca_path = "/etc/ssl/certs/ca-certificates.crt"
 | 
			
		||||
  
 | 
			
		||||
  accept_config = true
 | 
			
		||||
  accept_commands = true
 | 
			
		||||
}
 | 
			
		||||
@@ -284,49 +228,32 @@ EOF
 | 
			
		||||
    bash -c "cat > /etc/nginx/sites-available/icinga-stack" <<EOF
 | 
			
		||||
server {
 | 
			
		||||
    listen 80;
 | 
			
		||||
    listen [::]:80;
 | 
			
		||||
    server_name ${ZAMBA_HOSTNAME:-$(hostname -f)};
 | 
			
		||||
    return 301 https://\$host\$request_uri;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
server {
 | 
			
		||||
    listen 443 ssl http2;
 | 
			
		||||
    listen [::]:443 ssl http2;
 | 
			
		||||
    server_name ${ZAMBA_HOSTNAME:-$(hostname -f)};
 | 
			
		||||
 | 
			
		||||
    ssl_certificate /etc/nginx/ssl/fullchain.pem;
 | 
			
		||||
    ssl_certificate_key /etc/nginx/ssl/privkey.pem;
 | 
			
		||||
    ssl_protocols TLSv1.2 TLSv1.3;
 | 
			
		||||
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
 | 
			
		||||
 | 
			
		||||
    root /usr/share/icingaweb2/public;
 | 
			
		||||
    index index.php;
 | 
			
		||||
 | 
			
		||||
    location / {
 | 
			
		||||
        try_files \$uri \$uri/ /index.php\$is_args\$args;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    location / { try_files \$uri \$uri/ /index.php\$is_args\$args; }
 | 
			
		||||
    location ~ \.php$ {
 | 
			
		||||
        include fastcgi_params;
 | 
			
		||||
        fastcgi_pass unix:/run/php/php${PHP_VERSION}-fpm.sock;
 | 
			
		||||
        fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
 | 
			
		||||
        fastcgi_param ICINGAWEB_CONFIGDIR /etc/icingaweb2;
 | 
			
		||||
        fastcgi_param REMOTE_USER \$remote_user;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    location /grafana {
 | 
			
		||||
        proxy_pass http://localhost:3000;
 | 
			
		||||
        proxy_set_header Host \$http_host;
 | 
			
		||||
        proxy_set_header X-Real-IP \$remote_addr;
 | 
			
		||||
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
 | 
			
		||||
        proxy_set_header X-Forwarded-Proto \$scheme;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
EOF
 | 
			
		||||
    ln -sf /etc/nginx/sites-available/icinga-stack /etc/nginx/sites-enabled/
 | 
			
		||||
    rm -f /etc/nginx/sites-enabled/default
 | 
			
		||||
 | 
			
		||||
    # PHP-FPM für Nginx anpassen
 | 
			
		||||
    sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' "/etc/php/${PHP_VERSION}/fpm/php.ini"
 | 
			
		||||
    sed -i "s|;date.timezone =|date.timezone = $(cat /etc/timezone)|" "/etc/php/${PHP_VERSION}/fpm/php.ini"
 | 
			
		||||
}
 | 
			
		||||
@@ -334,83 +261,67 @@ EOF
 | 
			
		||||
_setup() {
 | 
			
		||||
    echo ""
 | 
			
		||||
    echo "================================================="
 | 
			
		||||
    echo "  Phase 3: Setup und finaler Neustart"
 | 
			
		||||
    echo "  Phase 3: Setup und finaler Neustart (MariaDB Edition)"
 | 
			
		||||
    echo "================================================="
 | 
			
		||||
    echo ""
 | 
			
		||||
    
 | 
			
		||||
    # 1. Warten, bis PostgreSQL bereit ist
 | 
			
		||||
    echo "[INFO] Warte auf PostgreSQL-Dienst..."
 | 
			
		||||
    while ! pg_isready -q -h localhost -U postgres; do
 | 
			
		||||
        echo "[INFO] PostgreSQL ist noch nicht bereit, warte 2 Sekunden..."
 | 
			
		||||
    echo "[INFO] Warte auf MariaDB-Dienst..."
 | 
			
		||||
    while ! mysqladmin ping -h localhost --silent; do
 | 
			
		||||
        echo "[INFO] MariaDB ist noch nicht bereit, warte 2 Sekunden..."
 | 
			
		||||
        sleep 2
 | 
			
		||||
    done
 | 
			
		||||
    echo "[INFO] PostgreSQL ist bereit."
 | 
			
		||||
    echo "[INFO] MariaDB ist bereit."
 | 
			
		||||
 | 
			
		||||
    # 2. Datenbank-Schemas importieren (als postgres-Benutzer für Robustheit)
 | 
			
		||||
    echo "[INFO] Datenbank-Schemas werden importiert."
 | 
			
		||||
    
 | 
			
		||||
    local IDO_SCHEMA="/usr/share/icinga2-ido-pgsql/schema/pgsql.sql"
 | 
			
		||||
    # KORREKTUR: Korrekter Pfad zur Schema-Datei für Icinga Web 2
 | 
			
		||||
    local IWEB_SCHEMA="/usr/share/icingaweb2/schema/pgsql.schema.sql"
 | 
			
		||||
    local IDO_SCHEMA="/usr/share/icinga2-ido-mysql/schema/mysql.sql"
 | 
			
		||||
    local IWEB_SCHEMA="/usr/share/icingaweb2/schema/mysql.schema.sql"
 | 
			
		||||
 | 
			
		||||
    if [ ! -f "$IDO_SCHEMA" ]; then
 | 
			
		||||
        echo "[ERROR] IDO-Schema-Datei nicht gefunden: $IDO_SCHEMA" >&2
 | 
			
		||||
        exit 1
 | 
			
		||||
    fi
 | 
			
		||||
    if [ ! -f "$IWEB_SCHEMA" ]; then
 | 
			
		||||
        echo "[ERROR] IcingaWeb-Schema-Datei nicht gefunden: $IWEB_SCHEMA" >&2
 | 
			
		||||
        exit 1
 | 
			
		||||
    fi
 | 
			
		||||
    if [ ! -f "$IDO_SCHEMA" ]; then echo "[ERROR] IDO-Schema nicht gefunden: $IDO_SCHEMA" >&2; exit 1; fi
 | 
			
		||||
    if [ ! -f "$IWEB_SCHEMA" ]; then echo "[ERROR] IcingaWeb-Schema nicht gefunden: $IWEB_SCHEMA" >&2; exit 1; fi
 | 
			
		||||
 | 
			
		||||
    # Prüfen, ob die Tabellen bereits existieren, um Idempotenz zu gewährleisten
 | 
			
		||||
    if sudo -u postgres psql -d icinga_ido -tAc "SELECT 1 FROM information_schema.tables WHERE table_name = 'icinga_dbversion'" | grep -q 1; then
 | 
			
		||||
    if mysql -e "use icinga_ido; show tables;" | grep -q "icinga_dbversion"; then
 | 
			
		||||
        echo "[INFO] Icinga IDO-Schema scheint bereits importiert zu sein."
 | 
			
		||||
    else
 | 
			
		||||
        echo "[INFO] Importiere Icinga IDO-Schema..."
 | 
			
		||||
        sudo -u postgres psql -d icinga_ido -f "$IDO_SCHEMA" &>/dev/null
 | 
			
		||||
        mysql icinga_ido < "$IDO_SCHEMA"
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    if sudo -u postgres psql -d icingaweb2 -tAc "SELECT 1 FROM information_schema.tables WHERE table_name = 'icingaweb_user'" | grep -q 1; then
 | 
			
		||||
    if mysql -e "use icingaweb2; show tables;" | grep -q "icingaweb_user"; then
 | 
			
		||||
        echo "[INFO] IcingaWeb2-Schema scheint bereits importiert zu sein."
 | 
			
		||||
    else
 | 
			
		||||
        echo "[INFO] Importiere IcingaWeb2-Schema..."
 | 
			
		||||
        sudo -u postgres psql -d icingaweb2 -f "$IWEB_SCHEMA" &>/dev/null
 | 
			
		||||
        mysql icingaweb2 < "$IWEB_SCHEMA"
 | 
			
		||||
    fi
 | 
			
		||||
    
 | 
			
		||||
    # 3. Icinga2 Features aktivieren (NACHDEM die DB bereit ist)
 | 
			
		||||
    echo "[INFO] Icinga2 Features werden aktiviert."
 | 
			
		||||
    icinga2 feature enable ido-pgsql api influxdb2-writer >/dev/null
 | 
			
		||||
    icinga2 feature enable ido-mysql api influxdb2-writer >/dev/null
 | 
			
		||||
 | 
			
		||||
    # 4. Icinga Web 2 Module in korrekter Reihenfolge aktivieren
 | 
			
		||||
    echo "[INFO] Icinga Web 2 Module werden aktiviert."
 | 
			
		||||
    icingacli module enable ipl
 | 
			
		||||
    icingacli module enable reactbundle
 | 
			
		||||
    icingacli module enable director
 | 
			
		||||
 | 
			
		||||
    # 5. Alle Dienste neu starten
 | 
			
		||||
    echo "[INFO] Alle Services werden neu gestartet, um Konfigurationen zu laden."
 | 
			
		||||
    systemctl restart postgresql
 | 
			
		||||
    echo "[INFO] Alle Services werden neu gestartet."
 | 
			
		||||
    systemctl restart mariadb
 | 
			
		||||
    systemctl restart icinga2
 | 
			
		||||
    systemctl restart php${PHP_VERSION}-fpm
 | 
			
		||||
    systemctl restart nginx
 | 
			
		||||
    systemctl restart grafana-server
 | 
			
		||||
 | 
			
		||||
    # 6. Icinga Web 2 Setup ausführen (NACHDEM die Dienste laufen)
 | 
			
		||||
    echo "[INFO] Icinga Web 2 Setup wird ausgeführt."
 | 
			
		||||
    ICINGAWEB_SETUP_TOKEN=$(icingacli setup token create)
 | 
			
		||||
    icingacli setup config webserver nginx --document-root /usr/share/icingaweb2/public
 | 
			
		||||
    icingacli setup --unattended --module icingaweb2 --setup-token "$ICINGAWEB_SETUP_TOKEN" \
 | 
			
		||||
        --db-type pgsql --db-host localhost --db-port 5432 --db-name icingaweb2 \
 | 
			
		||||
        --db-type mysql --db-host localhost --db-name icingaweb2 \
 | 
			
		||||
        --db-user icingaweb2 --db-pass "$ICINGAWEB_DB_PASS"
 | 
			
		||||
    icingacli setup --unattended --module monitoring --setup-token "$ICINGAWEB_SETUP_TOKEN" \
 | 
			
		||||
        --backend-type ido --resource icinga_ido
 | 
			
		||||
    icingacli user add icingaadmin --password "$ICINGAWEB_ADMIN_PASS" --role "Administrators"
 | 
			
		||||
 | 
			
		||||
    # 7. Director Setup ausführen (als letzter Schritt)
 | 
			
		||||
    echo "[INFO] Warte auf Icinga2 API..."
 | 
			
		||||
    sleep 15 # Gibt Icinga2 Zeit, vollständig zu starten
 | 
			
		||||
    sleep 15
 | 
			
		||||
    echo "[INFO] Icinga Director Setup wird ausgeführt."
 | 
			
		||||
    icingacli director migration run # Importiert das Director DB Schema
 | 
			
		||||
    icingacli director migration run
 | 
			
		||||
    icingacli director kickstart --endpoint localhost --user director --password "${ICINGA_API_USER_PASS}"
 | 
			
		||||
    icingacli director config set 'endpoint' 'localhost' --user 'director' --password "${ICINGA_API_USER_PASS}"
 | 
			
		||||
    icingacli director automation run
 | 
			
		||||
@@ -434,44 +345,26 @@ _info() {
 | 
			
		||||
    echo "  Icinga Web 2: https://${ZAMBA_HOSTNAME:-$(hostname -f)}/icingaweb2"
 | 
			
		||||
    echo "  Grafana:      https://${ZAMBA_HOSTNAME:-$(hostname -f)}/grafana"
 | 
			
		||||
    echo ""
 | 
			
		||||
    echo "Hinweis zu TLS: Der Server verwendet aktuell die Icinga2-eigenen, selbst-signierten Zertifikate."
 | 
			
		||||
    echo "Wenn Sie externe Zertifikate (z.B. von Let's Encrypt) verwenden möchten,"
 | 
			
		||||
    echo "passen Sie die Pfade in /etc/nginx/sites-available/icinga-stack und /etc/icinga2/features-available/api.conf an und starten Sie die Dienste neu."
 | 
			
		||||
    echo ""
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# --- Main Execution Logic ---
 | 
			
		||||
# Dieser Block wird nur ausgeführt, wenn das Skript direkt aufgerufen wird,
 | 
			
		||||
# nicht wenn es von der Zamba Toolbox als Bibliothek geladen wird.
 | 
			
		||||
# Ideal für Standalone-Tests.
 | 
			
		||||
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
 | 
			
		||||
    
 | 
			
		||||
    if [ "$EUID" -ne 0 ]; then
 | 
			
		||||
      echo "[ERROR] Dieses Skript muss als Root ausgeführt werden."
 | 
			
		||||
      exit 1
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    # Lade Konstanten, wenn das Skript standalone läuft
 | 
			
		||||
    if [ -f ./constants-service.conf ]; then
 | 
			
		||||
        source ./constants-service.conf
 | 
			
		||||
    else
 | 
			
		||||
        echo "[ERROR] Die Datei 'constants-service.conf' wird für den Standalone-Betrieb benötigt."
 | 
			
		||||
        exit 1
 | 
			
		||||
    fi
 | 
			
		||||
    
 | 
			
		||||
    # Setze einen Fallback-Hostnamen, falls ZAMBA_HOSTNAME nicht gesetzt ist
 | 
			
		||||
    ZAMBA_HOSTNAME=${ZAMBA_HOSTNAME:-$(hostname -f)}
 | 
			
		||||
 | 
			
		||||
    # Aktiviere den Bash Strict Mode für eine sichere Ausführung
 | 
			
		||||
    set -euo pipefail
 | 
			
		||||
 | 
			
		||||
    # Führe die Installationsphasen nacheinander aus
 | 
			
		||||
    _install
 | 
			
		||||
    _configure
 | 
			
		||||
    _setup
 | 
			
		||||
    _info
 | 
			
		||||
 | 
			
		||||
    set +euo pipefail
 | 
			
		||||
    
 | 
			
		||||
    exit 0
 | 
			
		||||
fi
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user