Files
nano_metrics/deploy/install.sh
T
Gilles Soulier 0430c0f2a8 feat(agent v0.1.6): métriques réseau enrichies + hardware dmidecode
- Nouveaux types payload: NetworkInterface, HardwareInfo
- Config: slow_daily_time (HH:MM), network_info, hardware_info
- Module network_info: interfaces locales, type ETH/WIFI, speed, MAC, WoL, iperf3
- Module hardware: dmidecode (carte mère, CPU, slots RAM, type/vitesse)
- Scheduler: collecte au démarrage + 1×/jour à l'heure configurée
- install.sh: ajout iperf3, dmidecode dans paquets

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-23 06:16:02 +02:00

206 lines
6.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# Installe l'agent Nanometrics depuis la dernière release Gitea.
# Usage :
# curl -fsSL https://git.maison43gil.com/gilles/nano_metrics/raw/branch/main/deploy/install.sh | bash
# SERVER_IP=10.0.0.50 SERVER_PORT=9999 curl -fsSL ... | bash
set -euo pipefail
REPO_API="https://git.maison43gil.com/api/v1/repos/gilles/nano_metrics"
SERVICE_URL="https://git.maison43gil.com/gilles/nano_metrics/raw/branch/main/deploy/nanometrics-agent.service"
INSTALL_BIN="/usr/local/bin/nanometrics-agent"
CONFIG_DIR="/etc/nanometrics"
CONFIG_FILE="$CONFIG_DIR/config.toml"
SERVICE_FILE="/etc/systemd/system/nanometrics-agent.service"
# ── Couleurs ───────────────────────────────────────────────────────────────────
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
ok() { echo -e "${GREEN}${NC} $*"; }
warn() { echo -e "${YELLOW}!${NC} $*"; }
err() { echo -e "${RED}${NC} $*" >&2; }
# ── Root check ─────────────────────────────────────────────────────────────────
if [ "$(id -u)" -ne 0 ]; then
err "Ce script doit être lancé en root (sudo bash ou sudo curl | bash)"
exit 1
fi
echo "======================================"
echo " Nanometrics Agent — Installation"
echo "======================================"
echo ""
# ── 1. Dépendances système ─────────────────────────────────────────────────────
PKGS_NEEDED=()
for pkg in curl python3 smartmontools ethtool iperf3 dmidecode; do
dpkg -l "$pkg" 2>/dev/null | grep -q '^ii' || PKGS_NEEDED+=("$pkg")
done
if [ ${#PKGS_NEEDED[@]} -gt 0 ]; then
echo "→ Installation des paquets manquants : ${PKGS_NEEDED[*]}"
apt-get update -qq
apt-get install -y -qq "${PKGS_NEEDED[@]}"
ok "Paquets installés : ${PKGS_NEEDED[*]}"
else
ok "Dépendances système déjà présentes"
fi
echo ""
# ── 3. Détection de l'architecture ────────────────────────────────────────────
ARCH="$(uname -m)"
case "$ARCH" in
x86_64) LABEL="linux-amd64" ;;
aarch64) LABEL="linux-arm64" ;;
*)
err "Architecture non supportée : $ARCH"
err "Seules x86_64 et aarch64 sont supportées."
exit 1
;;
esac
ok "Architecture détectée : $ARCH$LABEL"
# ── 4. Récupérer l'URL du binaire depuis la dernière release ──────────────────
echo "→ Récupération de la dernière release..."
ASSETS_JSON=$(curl -sf "$REPO_API/releases?limit=1&page=1")
ASSET_URL=$(echo "$ASSETS_JSON" | python3 -c "
import sys, json
releases = json.load(sys.stdin)
if not releases:
raise SystemExit('Aucune release trouvée sur le dépôt.')
assets = releases[0].get('assets', [])
name = 'nanometrics-agent-$LABEL'
for a in assets:
if a['name'] == name:
print(a['browser_download_url'])
break
else:
raise SystemExit(f'Asset {name!r} introuvable dans la release.')
")
TAG=$(echo "$ASSETS_JSON" | python3 -c "
import sys, json
releases = json.load(sys.stdin)
print(releases[0]['tag_name'])
")
ok "Release : $TAG — URL : $ASSET_URL"
# ── 5. Télécharger le binaire ─────────────────────────────────────────────────
TMP_BIN="$(mktemp)"
trap 'rm -f "$TMP_BIN"' EXIT
echo "→ Téléchargement du binaire..."
curl -fsSL -o "$TMP_BIN" "$ASSET_URL"
chmod 755 "$TMP_BIN"
ok "Binaire téléchargé ($(du -sh "$TMP_BIN" | cut -f1))"
# ── 6. Paramètres de configuration ────────────────────────────────────────────
echo ""
echo "--- Configuration du serveur ---"
SERVER_IP="${SERVER_IP:-10.0.0.50}"
SERVER_PORT="${SERVER_PORT:-9999}"
MQTT_HOST="${MQTT_HOST:-10.0.0.3}"
MQTT_ENABLED="${MQTT_ENABLED:-false}"
ok "Serveur : $SERVER_IP:$SERVER_PORT | MQTT broker : $MQTT_HOST"
# ── 7. Installer le binaire ────────────────────────────────────────────────────
echo ""
echo "[1/5] Installation du binaire..."
# Arrêter le service si en cours (le binaire ne peut pas être écrasé à chaud)
if systemctl is-active --quiet nanometrics-agent 2>/dev/null; then
warn "Service en cours — arrêt temporaire..."
systemctl stop nanometrics-agent
fi
cp "$TMP_BIN" "$INSTALL_BIN"
chmod 755 "$INSTALL_BIN"
ok "Binaire installé"
# ── 8. Créer le répertoire de configuration ───────────────────────────────────
echo "[2/5] Création de $CONFIG_DIR"
mkdir -p "$CONFIG_DIR"
chmod 755 "$CONFIG_DIR"
ok "Répertoire créé"
# ── 9. Écrire config.toml ─────────────────────────────────────────────────────
echo "[3/5] Écriture de $CONFIG_FILE"
cat > "$CONFIG_FILE" << TOML
[server]
ip = "$SERVER_IP"
port = $SERVER_PORT
[protocols.udp]
enabled = true
[protocols.mqtt]
enabled = $MQTT_ENABLED
host = "$MQTT_HOST"
port = 1883
topic_base = "nanometrics/agents"
auto_discovery = true
birth_message = true
last_will = true
[metrics.cpu]
udp = true
mqtt = false
[metrics.memory]
udp = true
mqtt = false
[metrics.disk]
udp = true
mqtt = false
[metrics.network]
udp = false
mqtt = false
[metrics.uptime]
udp = true
mqtt = false
[metrics.temperature]
udp = true
mqtt = false
[metrics.smart]
udp = true
mqtt = false
TOML
chmod 644 "$CONFIG_FILE"
ok "config.toml écrit"
# S'assurer que le répertoire est accessible
chmod 644 "$CONFIG_FILE" 2>/dev/null || true
chmod 755 "$CONFIG_DIR"
# ── 10. Installer le fichier service ─────────────────────────────────────────
echo "[4/5] Installation du service systemd"
curl -fsSL -o "$SERVICE_FILE" "$SERVICE_URL"
chmod 644 "$SERVICE_FILE"
systemctl daemon-reload
systemctl enable nanometrics-agent
ok "Service installé et activé"
# ── 11. Démarrer le service ───────────────────────────────────────────────────
echo "[5/5] Démarrage du service"
systemctl restart nanometrics-agent
sleep 2
echo ""
echo "=== Statut ==="
systemctl status nanometrics-agent --no-pager || true
echo ""
echo "======================================"
echo -e " ${GREEN}${NC} Nanometrics Agent ${TAG} installé"
echo "======================================"
echo " Config : $CONFIG_FILE"
echo " Logs : journalctl -u nanometrics-agent -f"