ansible/dns : switch to grafana alloy
This commit is contained in:
@@ -12,4 +12,4 @@
|
||||
become: true
|
||||
roles:
|
||||
- role: dns_server
|
||||
- role: dnsmasq_exporter
|
||||
- role: grafana_alloy
|
||||
|
||||
@@ -13,4 +13,4 @@
|
||||
become: true
|
||||
roles:
|
||||
- role: dns_server
|
||||
- role: dnsmasq_exporter
|
||||
- role: grafana_alloy
|
||||
|
||||
@@ -28,6 +28,9 @@ server={{ server }}
|
||||
# Taille du cache
|
||||
cache-size={{ dnsmasq_cache_size }}
|
||||
|
||||
# Configuration log
|
||||
log-facility=/var/log/dnsmasq.log
|
||||
|
||||
# On indique explicitement où trouver nos listes d'hôtes additionnelles
|
||||
addn-hosts=/etc/dnsmasq.hosts.d/records.home.arnodo.fr
|
||||
addn-hosts=/etc/dnsmasq.hosts.d/records.lab.arnodo.fr
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
---
|
||||
# ================================================================
|
||||
# Handlers for dnsmasq_exporter role
|
||||
# ================================================================
|
||||
|
||||
- name: Reload systemd
|
||||
ansible.builtin.systemd:
|
||||
daemon_reload: true
|
||||
become: true
|
||||
listen: Reload systemd
|
||||
@@ -1,56 +0,0 @@
|
||||
---
|
||||
# ================================================================
|
||||
# Rôle : dnsmasq_exporter
|
||||
# Tâches principales
|
||||
#
|
||||
# Objectif : Installer la dernière version de dnsmasq_exporter
|
||||
# via `go install` et la lancer comme service systemd.
|
||||
#
|
||||
# Remarque : cette implémentation reste volontairement simple.
|
||||
# - Aucune gestion fine de la version : toujours la « latest ».
|
||||
# - Repose sur le paquet Debian/Ubuntu `golang`.
|
||||
# - Le binaire est installé directement dans /usr/local/bin
|
||||
# grâce à la variable d’environnement GOBIN.
|
||||
# ================================================================
|
||||
|
||||
- name: "[dnsmasq_exporter] Dépendances build (golang, git)"
|
||||
ansible.builtin.apt:
|
||||
name:
|
||||
- golang-go
|
||||
- git
|
||||
state: present
|
||||
update_cache: true
|
||||
tags:
|
||||
- dnsmasq_exporter
|
||||
- install
|
||||
|
||||
- name: "[dnsmasq_exporter] Installation du binaire (go install @latest)"
|
||||
ansible.builtin.command:
|
||||
cmd: go install github.com/google/dnsmasq_exporter@latest
|
||||
creates: /usr/local/bin/dnsmasq_exporter
|
||||
environment:
|
||||
GOBIN: /usr/local/bin
|
||||
tags:
|
||||
- dnsmasq_exporter
|
||||
- build
|
||||
|
||||
- name: "[dnsmasq_exporter] Déploiement du service systemd"
|
||||
ansible.builtin.template:
|
||||
src: dnsmasq_exporter.service.j2
|
||||
dest: /etc/systemd/system/dnsmasq_exporter.service
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
notify: Reload systemd
|
||||
tags:
|
||||
- dnsmasq_exporter
|
||||
- config
|
||||
|
||||
- name: "[dnsmasq_exporter] Activation et démarrage du service"
|
||||
ansible.builtin.service:
|
||||
name: dnsmasq_exporter
|
||||
state: started
|
||||
enabled: true
|
||||
tags:
|
||||
- dnsmasq_exporter
|
||||
- service
|
||||
@@ -1,13 +0,0 @@
|
||||
[Unit]
|
||||
Description=Prometheus dnsmasq_exporter
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=/usr/local/bin/dnsmasq_exporter
|
||||
Restart=on-failure
|
||||
RestartSec=5s
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
24
ansible/roles/grafana_alloy/handlers/main.yml
Normal file
24
ansible/roles/grafana_alloy/handlers/main.yml
Normal file
@@ -0,0 +1,24 @@
|
||||
---
|
||||
# ================================================================
|
||||
# Handlers for grafana_alloy role
|
||||
# ================================================================
|
||||
|
||||
- name: Reload systemd
|
||||
ansible.builtin.systemd:
|
||||
daemon_reload: true
|
||||
become: true
|
||||
listen: Reload systemd
|
||||
|
||||
- name: Restart alloy
|
||||
ansible.builtin.service:
|
||||
name: alloy
|
||||
state: restarted
|
||||
become: true
|
||||
listen: Restart alloy
|
||||
|
||||
- name: Convert GPG key
|
||||
ansible.builtin.command:
|
||||
cmd: gpg --dearmor -o /etc/apt/keyrings/grafana.gpg /tmp/grafana.gpg.key
|
||||
creates: /etc/apt/keyrings/grafana.gpg
|
||||
become: true
|
||||
listen: Convert GPG key
|
||||
139
ansible/roles/grafana_alloy/tasks/main.yml
Normal file
139
ansible/roles/grafana_alloy/tasks/main.yml
Normal file
@@ -0,0 +1,139 @@
|
||||
---
|
||||
# ================================================================
|
||||
# Rôle : grafana_alloy
|
||||
# Tâches principales
|
||||
#
|
||||
# Objectif : Installer Grafana Alloy depuis le dépôt officiel
|
||||
# Grafana sur une distribution Debian/Ubuntu, puis
|
||||
# déployer la configuration et le service systemd.
|
||||
#
|
||||
# Étapes CLI équivalentes :
|
||||
# 1. sudo apt install gpg
|
||||
# 2. sudo mkdir -p /etc/apt/keyrings/
|
||||
# 3. wget -q -O - https://apt.grafana.com/gpg.key | \
|
||||
# gpg --dearmor | sudo tee /etc/apt/keyrings/grafana.gpg >/dev/null
|
||||
# 4. echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] \
|
||||
# https://apt.grafana.com stable main" \
|
||||
# | sudo tee /etc/apt/sources.list.d/grafana.list
|
||||
# 5. sudo apt-get update
|
||||
# 6. sudo apt-get install alloy
|
||||
# 7. sudo systemctl start alloy
|
||||
# 8. sudo systemctl enable alloy.service
|
||||
#
|
||||
# Cette implémentation reprend ces étapes en Ansible et ajoute
|
||||
# les déploiements de configuration + unité systemd.
|
||||
# ================================================================
|
||||
|
||||
- name: "[grafana_alloy] Paquets de base (gpg, wget, apt-transport-https, ca-certificates)"
|
||||
ansible.builtin.apt:
|
||||
name:
|
||||
- gpg
|
||||
- wget
|
||||
- apt-transport-https
|
||||
- ca-certificates
|
||||
state: present
|
||||
update_cache: true
|
||||
tags:
|
||||
- grafana_alloy
|
||||
- install
|
||||
|
||||
- name: "[grafana_alloy] Création du répertoire /etc/apt/keyrings"
|
||||
ansible.builtin.file:
|
||||
path: /etc/apt/keyrings
|
||||
state: directory
|
||||
mode: "0755"
|
||||
tags:
|
||||
- grafana_alloy
|
||||
- repo
|
||||
|
||||
- name: "[grafana_alloy] Téléchargement de la clé GPG Grafana"
|
||||
ansible.builtin.get_url:
|
||||
url: https://apt.grafana.com/gpg.key
|
||||
dest: /tmp/grafana.gpg.key
|
||||
mode: "0644"
|
||||
notify: Convert GPG key
|
||||
tags:
|
||||
- grafana_alloy
|
||||
- repo
|
||||
|
||||
- name: "[grafana_alloy] Conversion de la clé GPG au format keyring"
|
||||
ansible.builtin.command:
|
||||
cmd: gpg --dearmor -o /etc/apt/keyrings/grafana.gpg /tmp/grafana.gpg.key
|
||||
creates: /etc/apt/keyrings/grafana.gpg
|
||||
tags:
|
||||
- grafana_alloy
|
||||
- repo
|
||||
|
||||
- name: "[grafana_alloy] Ajout du dépôt Grafana"
|
||||
ansible.builtin.apt_repository:
|
||||
repo: "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main"
|
||||
filename: grafana
|
||||
state: present
|
||||
tags:
|
||||
- grafana_alloy
|
||||
- repo
|
||||
|
||||
- name: "[grafana_alloy] Installation du paquet alloy"
|
||||
ansible.builtin.apt:
|
||||
name: alloy
|
||||
state: present
|
||||
update_cache: true
|
||||
tags:
|
||||
- grafana_alloy
|
||||
- install
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Déploiement de la configuration d'Alloy (River)
|
||||
# ----------------------------------------------------------------
|
||||
- name: "[grafana_alloy] Dossier de configuration /etc/alloy"
|
||||
ansible.builtin.file:
|
||||
path: /etc/alloy
|
||||
state: directory
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0755"
|
||||
tags:
|
||||
- grafana_alloy
|
||||
- config
|
||||
|
||||
- name: "[grafana_alloy] Déploiement de la configuration Alloy"
|
||||
ansible.builtin.template:
|
||||
src: config.alloy.j2
|
||||
dest: "{{ alloy_config_file | default('/etc/alloy/config.alloy') }}"
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
notify:
|
||||
- Restart alloy
|
||||
tags:
|
||||
- grafana_alloy
|
||||
- config
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Déploiement/override de l’unité systemd
|
||||
# ----------------------------------------------------------------
|
||||
- name: "[grafana_alloy] Déploiement du service systemd (override)"
|
||||
ansible.builtin.template:
|
||||
src: alloy.service.j2
|
||||
dest: /etc/systemd/system/alloy.service
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0644"
|
||||
notify:
|
||||
- Reload systemd
|
||||
- Restart alloy
|
||||
tags:
|
||||
- grafana_alloy
|
||||
- config
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Activation et démarrage du service
|
||||
# ----------------------------------------------------------------
|
||||
- name: "[grafana_alloy] Démarrage et activation du service alloy"
|
||||
ansible.builtin.service:
|
||||
name: alloy
|
||||
state: started
|
||||
enabled: true
|
||||
tags:
|
||||
- grafana_alloy
|
||||
- service
|
||||
27
ansible/roles/grafana_alloy/templates/alloy.service.j2
Normal file
27
ansible/roles/grafana_alloy/templates/alloy.service.j2
Normal file
@@ -0,0 +1,27 @@
|
||||
[Unit]
|
||||
Description=Grafana Alloy
|
||||
Documentation=https://grafana.com/docs/alloy/latest/
|
||||
Wants=network-online.target
|
||||
After=network-online.target
|
||||
|
||||
[Service]
|
||||
User=alloy
|
||||
Group=alloy
|
||||
Type=simple
|
||||
|
||||
# Commande de démarrage finale et correcte, basée sur la sortie de "alloy run --help"
|
||||
ExecStart=/usr/bin/alloy run /etc/alloy/config.alloy \
|
||||
--storage.path=/var/lib/alloy \
|
||||
--server.http.listen-addr=127.0.0.1:12345
|
||||
|
||||
# Sécurité via systemd
|
||||
ReadWritePaths=/var/lib/alloy
|
||||
ReadOnlyPaths=/etc/alloy
|
||||
|
||||
# Politiques de redémarrage et de limites de fichiers
|
||||
Restart=on-failure
|
||||
RestartSec=5s
|
||||
LimitNOFILE=65536
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
70
ansible/roles/grafana_alloy/templates/config.alloy.j2
Normal file
70
ansible/roles/grafana_alloy/templates/config.alloy.j2
Normal file
@@ -0,0 +1,70 @@
|
||||
// =============================================================================
|
||||
// Template Grafana Alloy final pour le monitoring complet du Raspberry Pi
|
||||
// Envoie les métriques à Prometheus et les logs à Loki.
|
||||
// =============================================================================
|
||||
|
||||
logging {
|
||||
level = "{{ alloy_log_level | default("info") }}"
|
||||
format = "{{ alloy_log_format | default("logfmt") }}"
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Section 1 : Définition des destinations sur le serveur de monitoring
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Destination pour les métriques
|
||||
prometheus.remote_write "to_prometheus" {
|
||||
endpoint {
|
||||
// URL de votre serveur Prometheus
|
||||
url = "{{ alloy_prometheus_url | default("http://vision.lab.home.arnodo.fr:9090/api/v1/write") }}"
|
||||
}
|
||||
}
|
||||
|
||||
// Destination pour les logs
|
||||
loki.write "to_loki" {
|
||||
endpoint {
|
||||
// URL de votre serveur Loki
|
||||
url = "{{ alloy_loki_url | default("http://vision.lab.home.arnodo.fr:3100/loki/api/v1/push") }}"
|
||||
}
|
||||
// On ajoute un label à tous les logs pour savoir d'où ils viennent
|
||||
external_labels = {
|
||||
source = "raspberrypi",
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Section 2 : Collecte des données sur le Raspberry Pi
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// --- Collecte des métriques ---
|
||||
|
||||
// Active le module interne pour les métriques système (CPU, RAM, etc.)
|
||||
prometheus.exporter.unix "system" {}
|
||||
|
||||
// Active le module interne pour vérifier le statut de Dnsmasq
|
||||
prometheus.exporter.dnsmasq "dnsmasq_status" {}
|
||||
|
||||
// Scrape les métriques des deux modules ci-dessus et les envoie à Prometheus
|
||||
prometheus.scrape "local_metrics" {
|
||||
targets = concat(
|
||||
prometheus.exporter.unix.system.targets,
|
||||
prometheus.exporter.dnsmasq.dnsmasq_status.targets,
|
||||
)
|
||||
forward_to = [prometheus.remote_write.to_prometheus.receiver]
|
||||
}
|
||||
|
||||
|
||||
// --- Collecte des logs ---
|
||||
|
||||
// Lit les logs du journal systemd (la méthode la plus propre et complète)
|
||||
loki.source.journal "system_logs" {
|
||||
// Ajoute un label "job" aux logs pour les identifier dans Grafana
|
||||
relabel_rules {
|
||||
rule {
|
||||
target_label = "job"
|
||||
action = "replace"
|
||||
replacement = "journal"
|
||||
}
|
||||
}
|
||||
forward_to = [loki.write.to_loki.receiver]
|
||||
}
|
||||
Reference in New Issue
Block a user