# Mise en place de prometheus

##### Mise en place de Prometheus pour la supervision

<span style="white-space: pre-wrap;">Ce guide explique comment installer et configurer </span>****Prometheus****<span style="white-space: pre-wrap;"> sur une machine dédiée (VM ou LXC), afin de collecter les métriques d'autres machines (VPS, Proxmox, etc.) via des agents comme </span>****node\_exporter****.

<p class="callout info">Vous pouvez aussi utilisé Helper-Script pour l'installation :  
****Prometheus PVE Exporter :**** [https://community-scripts.github.io/ProxmoxVE/scripts?id=prometheus-pve-exporter](https://community-scripts.github.io/ProxmoxVE/scripts?id=prometheus-pve-exporter)  
****Prometheus :****<span style="white-space: pre-wrap;"> </span>[https://community-scripts.github.io/ProxmoxVE/scripts?id=prometheus](https://community-scripts.github.io/ProxmoxVE/scripts?id=prometheus)</p>

##### ✨ Objectif

<span style="white-space: pre-wrap;">Mettre en place un serveur </span>****Prometheus****<span style="white-space: pre-wrap;"> pour collecter des métriques depuis plusieurs machines :</span>

- VPS avec node\_exporter
- Proxmox exporter (exporteur dédié pour Proxmox VE)
- Machines locales avec node\_exporter

---

##### 🌐 Prérequis

- Une VM ou LXC dédiée à Prometheus (Debian/Ubuntu recommandé)
- Accès root ou sudo

---

##### 🛠️ Installation de Prometheus

1\. Mise à jour et création d'utilisateur

```bash
sudo apt update && sudo apt install -y curl tar
sudo useradd --no-create-home --shell /bin/false prometheus
```

2\. Téléchargement

```bash
cd /tmp
curl -LO https://github.com/prometheus/prometheus/releases/latest/download/prometheus-2.52.0.linux-amd64.tar.gz
tar xvf prometheus-2.52.0.linux-amd64.tar.gz
cd prometheus-2.52.0.linux-amd64
```

<p class="callout info"><span style="white-space: pre-wrap;">Si la commande </span>`<span class="editor-theme-code">curl -LO</span>`<span style="white-space: pre-wrap;"> ne fonctionne pas, utilisé </span>`<span class="editor-theme-code">wget</span>`</p>

3\. Déplacement des fichiers

```bash
sudo mkdir -p /etc/prometheus /var/lib/prometheus
sudo cp prometheus promtool /usr/local/bin/
sudo cp -r consoles/ console_libraries/ /etc/prometheus/
sudo cp prometheus.yml /etc/prometheus/
```

4\. Droits

```bash
sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus
sudo chown prometheus:prometheus /usr/local/bin/prometheus /usr/local/bin/promtool
```

---

##### 🔧 Création du service systemd

```bash
sudo tee /etc/systemd/system/prometheus.service > /dev/null <<EOF
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.path=/var/lib/prometheus/

[Install]
WantedBy=multi-user.target
EOF
```

---

##### <span style="white-space: pre-wrap;">🖊️ Configuration de </span>`<span class="editor-theme-code">/etc/prometheus/prometheus.yml</span>`

<span style="white-space: pre-wrap;">Voici un exemple </span>****valide****<span style="white-space: pre-wrap;"> pour plusieurs machines :</span>

```yaml
global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'nom_de_la_machine'
    static_configs:
      - targets: ['IP_DE_VOTRE_MACHINE:9090']

  - job_name: 'nom_de_la_machine'
    static_configs:
      - targets: ['IP_DE_VOTRE_MACHINE:9221']

  - job_name: 'nom_de_la_machine'
    static_configs:
      - targets: ['IP_DE_VOTRE_MACHINE:9100']

  - job_name: 'nom_de_la_machine'
    static_configs:
      - targets: ['IP_DE_VOTRE_MACHINE:9100']
```

---

##### 🚀 Lancement de Prometheus

1\. Activer le service

```bash
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable --now prometheus
```

2\. Vérification

```bash
systemctl status prometheus
```

3\. Interface Web

Accédez à :

```
http://IP_DU_SERVEUR_PROMETHEUS:9090
```

<span style="white-space: pre-wrap;">Puis allez dans : </span>`<span class="editor-theme-code">Status > Targets</span>`

<span style="white-space: pre-wrap;">Vous devez voir toutes les cibles listées comme </span>****UP****.

---

##### 🌍 Connexion à Grafana

1. <span style="white-space: pre-wrap;">Sur Grafana (VM différente), aller dans </span>****Configuration &gt; Data Sources****
2. <span style="white-space: pre-wrap;">Cliquer sur </span>****Add Data Source****<span style="white-space: pre-wrap;"> &gt; choisir </span>****Prometheus****
3. <span style="white-space: pre-wrap;">Mettre comme URL : </span>`<span class="editor-theme-code">http://IP_DU_SERVEUR_PROMETHEUS:9090</span>`
4. <span style="white-space: pre-wrap;">Cliquer sur </span>****Save &amp; Test****

---

##### 📄 Notes utiles

- <span style="white-space: pre-wrap;">Le fichier </span>`<span class="editor-theme-code">prometheus.yml</span>`<span style="white-space: pre-wrap;"> doit avoir une </span>****seule section** `<strong class="editor-theme-bold editor-theme-code">scrape_configs:</strong>`**
- <span style="white-space: pre-wrap;">Chaque </span>`<span class="editor-theme-code">job_name</span>`<span style="white-space: pre-wrap;"> correspond à une cible identifiée dans Grafana</span>
- Si le service ne démarre pas, vérifiez les erreurs YAML :

```bash
/usr/local/bin/prometheus --config.file=/etc/prometheus/prometheus.yml
```

- Exemple d'erreur courante :

```
yaml: unmarshal errors: field scrape_configs already set
```

<span style="white-space: pre-wrap;">=&gt; Ça signifie que </span>`<span class="editor-theme-code">scrape_configs:</span>`<span style="white-space: pre-wrap;"> est déclaré deux fois</span>

---

🎉 Prometheus est maintenant prêt à collecter des métriques sur tous vos serveurs ✅