diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..c943d44 --- /dev/null +++ b/.env.example @@ -0,0 +1,10 @@ +# Configuration Discord Bot +TOKEN=your_discord_bot_token_here +STATUS=online +INTERVAL=3600 + +# Configuration Zabbix (optionnel) +ENABLE_ZABBIX=false +ZABBIX_SERVER=zabbix-server.example.com +ZABBIX_HOSTNAME=mamie-henrriette-bot +ZABBIX_PORT=10050 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index a8d3033..78bf22b 100755 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,16 @@ -FROM python:3.9-slim +FROM alpine:3.22.1 WORKDIR /app +RUN apk add --no-cache python3 py3-pip zabbix-agent2 + COPY requirements.txt . COPY bot.py . COPY statuts.txt . +COPY zabbix_agent2.conf /etc/zabbix/zabbix_agent2.conf +COPY start.sh /start.sh -RUN pip install --no-cache-dir -r requirements.txt +RUN pip3 install --no-cache-dir --break-system-packages --root-user-action=ignore -r requirements.txt && \ + chmod +x /start.sh -CMD ["python", "bot.py"] \ No newline at end of file +CMD ["/start.sh"] \ No newline at end of file diff --git a/README.md b/README.md index 396264c..1da58b9 100755 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ -Voici le README.md pour votre projet : # 👵 Mamie Henrriette - Discord Status Bot 🤖 @@ -13,11 +12,13 @@ Mamie Henrriette est un bot Discord intelligent qui change automatiquement de st - Gestion des erreurs et logging - Support multi-statuts Discord - Déploiement simplifié avec Docker +- 📊 Surveillance optionnelle avec Zabbix ## 🛠 Prérequis -- Docker +- Docker et Docker Compose - Compte Discord et Token du bot +- (Optionnel) Serveur Zabbix pour la surveillance ## 📦 Installation @@ -27,19 +28,69 @@ git clone https://git.favrep.ch/lapatatedouce/MamieHenrriette cd MamieHenrriette ``` -2. Conteneur Docker +2. Copiez le fichier de configuration +```bash +cp .env.example .env +``` +3. Éditez le fichier `.env` avec vos paramètres +```bash +nano .env +``` + +4. Démarrez le conteneur Docker + +**Mode développement (avec logs):** ```bash docker-compose up --build ``` +**Mode production (en arrière-plan):** +```bash +docker-compose up --build -d +``` + +**Voir les logs:** +```bash +docker-compose logs -f discord-bot +``` + +**Arrêter le conteneur:** +```bash +docker-compose down +``` + ## 🔧 Configuration -### Variables d'environnement +### Variables d'environnement principales - `TOKEN`: Votre token Discord (obligatoire) - `STATUS`: Statut initial (défaut: online) -- `INTERVAL`: Intervalle de changement de statut (défaut: 60 secondes) +- `INTERVAL`: Intervalle de changement de statut (défaut: 3600 secondes) + +### 📊 Configuration Zabbix (optionnelle) + +- `ENABLE_ZABBIX`: Activer la surveillance Zabbix (défaut: false) +- `ZABBIX_SERVER`: Adresse du serveur Zabbix +- `ZABBIX_HOSTNAME`: Nom d'hôte pour identifier le bot +- `ZABBIX_PORT`: Port d'exposition Zabbix (défaut: 10050) + +#### Métriques surveillées par Zabbix + +- Statut du bot Discord +- Temps de fonctionnement (uptime) +- Utilisation mémoire +- Erreurs et avertissements dans les logs +- Connectivité à Discord + +#### Activation de Zabbix + +Dans votre fichier `.env` : +```bash +ENABLE_ZABBIX=true +ZABBIX_SERVER=votre-serveur-zabbix.com +ZABBIX_HOSTNAME=mamie-henrriette-bot +``` ### Fichier `statuts.txt` @@ -57,33 +108,38 @@ En mode supervision - discord.py==2.3.2 - python-dotenv==1.0.0 +--- +# 🖥️ Installation environnement de développement -# Installation poste de dev -## installation des dépendences systeme -~~~ -sudo apt install python3 python3-pip p -~~~ +## Installation des dépendances système -## creation de l'environnement python locale -Dans le projet -~~~ +```bash +sudo apt install python3 python3-pip +``` + +## Création de l'environnement Python local + +Dans le dossier du projet : + +```bash python3 -m venv .venv -~~~ +``` -Puis activer l'environnement : -~~~ +Puis activer l'environnement : + +```bash source .venv/bin/activate -~~~ +``` -## installation des dépendences python +## Installation des dépendances Python -~~~ +```bash pip install -r requirements.txt -~~~ +``` -## execution +## Exécution -~~~ -TOKEN=truc python3 bot.py -~~~ \ No newline at end of file +```bash +TOKEN=votre_token_discord python3 bot.py +``` \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 0e83bdf..1c54d54 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,5 +7,12 @@ services: - TOKEN=your token - STATUS=online - INTERVAL=3600 + - ENABLE_ZABBIX=${ENABLE_ZABBIX:-false} + - ZABBIX_SERVER=${ZABBIX_SERVER:-zabbix-server.example.com} + - ZABBIX_HOSTNAME=${ZABBIX_HOSTNAME:-mamie-henrriette-bot} volumes: - - ./statuts.txt:/app/statuts.txt \ No newline at end of file + - ./statuts.txt:/app/statuts.txt + ports: + - "${ZABBIX_PORT:-10050}:10050" + profiles: + - default \ No newline at end of file diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..4371cb5 --- /dev/null +++ b/start.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +# Script de démarrage conditionnel pour Zabbix et le bot Discord + +# Vérifier si Zabbix est activé +if [ "$ENABLE_ZABBIX" = "true" ]; then + echo "Zabbix activé - Configuration de l'agent..." + + # Remplacer les variables dans la config Zabbix + sed -i "s/Server=.*/Server=$ZABBIX_SERVER/" /etc/zabbix/zabbix_agent2.conf + sed -i "s/ServerActive=.*/ServerActive=$ZABBIX_SERVER:10051/" /etc/zabbix/zabbix_agent2.conf + sed -i "s/Hostname=.*/Hostname=$ZABBIX_HOSTNAME/" /etc/zabbix/zabbix_agent2.conf + + zabbix_agent2 -f & + echo "Zabbix Agent démarré" +else + echo "Zabbix désactivé" +fi + +echo "Démarrage du bot Discord..." +exec python3 bot.py \ No newline at end of file diff --git a/zabbix_agent2.conf b/zabbix_agent2.conf new file mode 100644 index 0000000..5cf04b4 --- /dev/null +++ b/zabbix_agent2.conf @@ -0,0 +1,30 @@ +# Configuration Zabbix Agent 2 pour Mamie Henrriette Bot + +# Serveur Zabbix +Server=zabbix-server.example.com +ServerActive=zabbix-server.example.com:10051 + +# Configuration de l'agent +Hostname=mamie-henrriette-bot +ListenPort=10050 +ListenIP=0.0.0.0 + +# Logs +LogType=console +LogLevel=3 +DebugLevel=3 + +# Timeout +Timeout=30 + +# Métriques personnalisées pour le bot Discord +UserParameter=discord.bot.status,python3 /app/bot_status.py +UserParameter=discord.bot.uptime,cat /proc/uptime | awk '{print $1}' +UserParameter=discord.bot.memory,ps -o pid,vsz,rss,comm -p $(pgrep -f bot.py) | tail -1 | awk '{print $3}' + +# Surveillance des logs +UserParameter=discord.bot.errors,grep -c "ERROR" /var/log/discord-bot.log 2>/dev/null || echo 0 +UserParameter=discord.bot.warnings,grep -c "WARNING" /var/log/discord-bot.log 2>/dev/null || echo 0 + +# Vérification de la connectivité Discord +UserParameter=discord.connectivity,timeout 5 curl -s https://discord.com/api/v10/gateway >/dev/null && echo 1 || echo 0 \ No newline at end of file