4NK_vault/storage/dev/generate_promtail_config.sh
4NK Dev 4834c40503 ci: docker_tag=dev-test
Séparation des scripts de génération et amélioration de l'architecture

- Séparé generate_grafana_dashboards.sh en 3 scripts distincts :
  * generate_grafana_dashboards.sh (dashboards uniquement)
  * generate_promtail_config.sh (configuration Promtail)
  * generate_logrotate_configs.sh (configurations Logrotate)

- Supprimé generate_docker_compose.sh et generate_docker_variables.sh
- Centralisé la génération des variables dans generate_variables.sh
- Mis à jour generate.sh pour une architecture en 5 étapes
- Corrigé les chemins de sortie et les références de variables
- Ajouté la gestion d'erreurs pour les fichiers .env problématiques
- Généré toutes les configurations Nginx, Grafana, Promtail et Logrotate
- Amélioré la modularité et la maintenabilité du code
2025-10-03 17:13:19 +00:00

122 lines
3.2 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Script de génération de la configuration Promtail
# Génère promtail.yml pour la collecte de logs
set -euo pipefail
# Couleurs pour les messages
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Fonctions d'affichage
print_error() { echo -e "${RED}❌ ERREUR${NC}: $1"; }
print_success() { echo -e "${GREEN}✅ SUCCÈS${NC}: $1"; }
print_info() { echo -e "${BLUE} INFO${NC}: $1"; }
print_warning() { echo -e "${YELLOW}⚠️ ATTENTION${NC}: $1"; }
# Charger les variables d'environnement
load_env_files() {
local env_files=(".env.secrets" ".env")
for env_file in "${env_files[@]}"; do
if [ -f "$env_file" ]; then
echo "📄 Chargement de $env_file..."
# shellcheck source=/dev/null
source "$env_file"
else
print_warning "Fichier $env_file non trouvé"
fi
done
}
# Fonction pour générer la configuration Promtail
generate_promtail_config() {
echo "🔧 Génération de la configuration Promtail"
# Créer le répertoire si nécessaire
mkdir -p "_4NK_modules/promtail"
# Générer le fichier promtail.yml
cat > "_4NK_modules/promtail/promtail.yml" << EOF
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: http://loki:3100/loki/api/v1/push
scrape_configs:
EOF
# Ajouter une configuration de scraping pour chaque service
for SERVICE_VAR in "${SERVICES[@]}"; do
echo " 🔄 Ajout config Promtail pour $SERVICE_VAR"
cat >> "_4NK_modules/promtail/promtail.yml" << EOF
- job_name: '$SERVICE_VAR'
static_configs:
- targets:
- localhost
labels:
job: $SERVICE_VAR
__path__: \$${SERVICE_VAR}_LOGS_DIR/*.log
pipeline_stages:
- match:
selector: '{job="$SERVICE_VAR"}'
stages:
- regex:
expression: '^(?P<timestamp>\S+\s\S+)\s+(?P<level>\S+)\s+(?P<message>.*)'
- labels:
level:
- timestamp:
source: timestamp
format: '2006-01-02 15:04:05'
EOF
done
echo "✅ Configuration promtail.yml générée"
}
# Fonction principale
main() {
echo "🚀 GÉNÉRATION DE LA CONFIGURATION PROMTAIL"
echo "==========================================="
# Charger les variables d'environnement
load_env_files
# Supprimer le fichier de sortie existant (remplacer complètement)
rm -f "_4NK_modules/promtail/promtail.yml" 2>/dev/null || true
# Créer le répertoire si nécessaire
mkdir -p "_4NK_modules/promtail"
# Vérifier que SERVICES est défini
if [ -z "${SERVICES[*]:-}" ]; then
print_error "Variable SERVICES non définie dans .env"
exit 1
fi
echo "📋 Génération de la configuration Promtail pour ${#SERVICES[@]} services..."
# Générer la configuration Promtail
generate_promtail_config
echo ""
echo "✅ GÉNÉRATION TERMINÉE !"
echo "📁 Configuration générée: _4NK_modules/promtail/promtail.yml"
echo "🔧 Configuration Promtail pour ${#SERVICES[@]} services"
echo "📊 Services traités: ${#SERVICES[@]}"
}
# Exécuter le script principal
main "$@"