Monitoring
Ce guide explique comment surveiller les performances et la santé de PCH-SIG.
Architecture de monitoring
┌─────────────────────────────────────────────────────────────┐
│ Machine Locale │
│ ┌─────────────┐ ┌─────────────┐ │
│ │ Grafana │◀───│ Prometheus │ │
│ │ :3001 │ │ :9090 │ │
│ └─────────────┘ └──────┬──────┘ │
└────────────────────────────┼────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Serveur serveur-production │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ cAdvisor │ │ NodeExporter│ │ PG Exporter │ │
│ │ :8080 │ │ :9100 │ │ :9187 │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │Redis Export │ │ Loki │ │ Promtail │ │
│ │ :9121 │ │ :3100 │ │ │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────────┘
Démarrer le monitoring
Prometheus (collecte des métriques)
cd monitoring/prometheus
.\prometheus.exe --config.file=prometheus.yml
Grafana (visualisation)
& "C:\Program Files\GrafanaLabs\grafana\bin\grafana-server.exe" `
--homepath="C:\Program Files\GrafanaLabs\grafana" `
--config="C:\Users\clave\WebstormProjects\pch-sig\monitoring\grafana\grafana-custom.ini"
Accès Grafana
- URL : http://localhost:3001
- Login :
admin - Mot de passe :
Passer@123
Dashboards disponibles
Docker Overview
URL : http://localhost:3001/d/pch-docker-overview
Métriques affichées :
- CPU par conteneur
- Mémoire utilisée
- Réseau (octets entrants/sortants)
- I/O disque
PostgreSQL
URL : http://localhost:3001/d/pch-postgresql
Métriques affichées :
- Connexions actives
- Transactions par seconde
- Requêtes lentes
- Taille des tables
- Cache hit ratio
Docker Logs
URL : http://localhost:3001/d/pch-docker-logs
Fonctionnalités :
- Logs en temps réel
- Filtrage par conteneur
- Recherche dans les logs
- Historique
Métriques clés
Application
| Métrique | Description | Seuil d'alerte |
|---|---|---|
| Temps de réponse API | Latence moyenne des requêtes | > 500ms |
| Erreurs 5xx | Nombre d'erreurs serveur | > 10/min |
| Requêtes/seconde | Charge de l'API | > 100/s |
Base de données
| Métrique | Description | Seuil d'alerte |
|---|---|---|
| Connexions actives | Connexions PostgreSQL | > 80% max |
| Cache hit ratio | Efficacité du cache | < 95% |
| Transactions/seconde | Activité DB | Variable |
| Requêtes lentes | Requêtes > 1s | > 5/min |
Conteneurs
| Métrique | Description | Seuil d'alerte |
|---|---|---|
| CPU | Utilisation CPU | > 80% |
| Mémoire | Utilisation RAM | > 90% |
| Redémarrages | Nombre de restarts | > 0 en 1h |
Système
| Métrique | Description | Seuil d'alerte |
|---|---|---|
| Espace disque | Utilisation disque | > 85% |
| Load average | Charge système | > nb CPUs |
| Mémoire système | RAM utilisée | > 90% |
Configuration Prometheus
Fichier : monitoring/prometheus/prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'cadvisor'
static_configs:
- targets: ['serveur-production:8080']
- job_name: 'node'
static_configs:
- targets: ['serveur-production:9100']
- job_name: 'postgresql'
static_configs:
- targets: ['serveur-production:9187']
- job_name: 'redis'
static_configs:
- targets: ['serveur-production:9121']
Alertes
Configurer une alerte Grafana
- Ouvrez un dashboard
- Cliquez sur le panneau à surveiller
- Edit > Alert
- Configurez :
- Condition (ex: avg > 80)
- Durée (ex: for 5m)
- Notification (email, Slack, etc.)
Exemple d'alerte CPU
alert: HighCPUUsage
expr: container_cpu_usage_seconds_total{name="pch_backend"} > 0.8
for: 5m
labels:
severity: warning
annotations:
summary: "CPU élevé sur le backend"
description: "Le backend utilise plus de 80% CPU depuis 5 minutes"
Canaux de notification
| Canal | Configuration |
|---|---|
| Serveur SMTP | |
| Slack | Webhook URL |
| Teams | Webhook URL |
| PagerDuty | Integration key |
Vérifier l'état du monitoring
Script de vérification
Fichier : monitoring/check-ports.ps1
# Vérifier les ports du monitoring
$Ports = @{
"cAdvisor" = "serveur-production:8080"
"Node Exporter" = "serveur-production:9100"
"PostgreSQL Exporter" = "serveur-production:9187"
"Redis Exporter" = "serveur-production:9121"
"Loki" = "serveur-production:3100"
}
foreach ($Service in $Ports.Keys) {
$Target = $Ports[$Service]
try {
$Response = Invoke-WebRequest -Uri "http://$Target" -TimeoutSec 5 -UseBasicParsing
Write-Host "$Service ($Target): OK" -ForegroundColor Green
} catch {
Write-Host "$Service ($Target): ERREUR" -ForegroundColor Red
}
}
Vérifier Prometheus targets
- Ouvrez http://localhost:9090/targets
- Tous les targets doivent être en état "UP"
Endpoint de santé
API Health Check
curl http://localhost:8000/api/health
Réponse attendue :
{
"status": "ok",
"database": "connected",
"redis": "connected",
"timestamp": "2024-01-15T10:30:00Z"
}
Implémenter un health check
// src/Controller/HealthController.php
#[Route('/api/health', methods: ['GET'])]
public function health(): JsonResponse
{
$checks = [];
// PostgreSQL
try {
$this->entityManager->getConnection()->executeQuery('SELECT 1');
$checks['database'] = 'connected';
} catch (\Exception $e) {
$checks['database'] = 'error';
}
// Redis
try {
$this->redis->ping();
$checks['redis'] = 'connected';
} catch (\Exception $e) {
$checks['redis'] = 'error';
}
return new JsonResponse([
'status' => 'ok',
...$checks,
'timestamp' => (new \DateTime())->format('c')
]);
}
Exporters sur le serveur
Démarrer cAdvisor
docker run -d \
--name=pch_cadvisor \
--restart=always \
--privileged \
-p 8080:8080 \
-v /:/rootfs:ro \
-v /var/run:/var/run:ro \
-v /sys:/sys:ro \
-v /var/lib/docker/:/var/lib/docker:ro \
gcr.io/cadvisor/cadvisor:latest
Démarrer Node Exporter
docker run -d \
--name=pch_node_exporter \
--restart=always \
--net=host \
-v /:/host:ro \
quay.io/prometheus/node-exporter:latest \
--path.rootfs=/host
Démarrer PostgreSQL Exporter
docker run -d \
--name=pch_postgres_exporter \
--restart=always \
-p 9187:9187 \
-e DATA_SOURCE_NAME="postgresql://pch_admin:pch_secure_2025@pch_postgres:5432/pch_sig?sslmode=disable" \
quay.io/prometheuscommunity/postgres-exporter:latest
Démarrer Redis Exporter
docker run -d \
--name=pch_redis_exporter \
--restart=always \
-p 9121:9121 \
-e REDIS_ADDR=pch_redis:6379 \
-e REDIS_PASSWORD=redis_secure_2025 \
oliver006/redis_exporter:latest
Requêtes PromQL utiles
CPU
# CPU par conteneur
rate(container_cpu_usage_seconds_total{name=~"pch_.*"}[5m]) * 100
# Top 5 consommateurs CPU
topk(5, rate(container_cpu_usage_seconds_total[5m]))
Mémoire
# Mémoire par conteneur
container_memory_usage_bytes{name=~"pch_.*"} / 1024 / 1024
# Pourcentage mémoire
container_memory_usage_bytes / container_spec_memory_limit_bytes * 100
PostgreSQL
# Connexions actives
pg_stat_activity_count
# Transactions par seconde
rate(pg_stat_database_xact_commit{datname="pch_sig"}[5m])
Bonnes pratiques
Rétention des métriques
- Métriques détaillées : 15 jours
- Métriques agrégées : 90 jours
- Métriques de tendance : 1 an
Surveillance proactive
- Configurer des alertes AVANT les problèmes
- Définir des seuils réalistes
- Réviser régulièrement les alertes
Documentation
- Documenter les seuils et leur justification
- Tenir un journal des incidents
- Créer des runbooks pour chaque alerte