fix : code review
This commit is contained in:
@@ -1,4 +1,23 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
###############################################################################
|
||||
# CHARGEMENT DU .env
|
||||
###############################################################################
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
ENV_FILE="${SCRIPT_DIR}/.env"
|
||||
|
||||
if [[ ! -f "$ENV_FILE" ]]; then
|
||||
echo "ERROR: fichier .env introuvable : $ENV_FILE" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
set -a
|
||||
# shellcheck disable=SC1090
|
||||
source "$ENV_FILE"
|
||||
set +a
|
||||
|
||||
###############################################################################
|
||||
# CONFIGURATION
|
||||
###############################################################################
|
||||
@@ -6,14 +25,10 @@
|
||||
# Limite maximale d'utilisation du disque en pourcentage
|
||||
limit=70
|
||||
|
||||
# Récupération du webhook Discord depuis le fichier .env
|
||||
WEBHOOK_URL=$(grep -E '^WEBHOOK_URL=' .env | cut -d '=' -f2-)
|
||||
|
||||
###############################################################################
|
||||
# RÉCUPÉRATION DES INFORMATIONS DISQUE
|
||||
###############################################################################
|
||||
|
||||
# extraction des informations
|
||||
read -r total_bytes used_bytes avail_bytes usage <<<"$(df -B1 / | awk 'NR==2 {gsub(/%/,"",$5); print $2, $3, $4, $5}')"
|
||||
|
||||
# Calcul du pourcentage d'espace libre
|
||||
@@ -23,25 +38,24 @@ free=$((100 - usage))
|
||||
# CONVERSION EN GIGAOCTETS
|
||||
###############################################################################
|
||||
|
||||
# Conversion bytes → gigaoctets pour un affichage plus lisible
|
||||
used_gb=$(awk -v b="$used_bytes" 'BEGIN {printf "%.2f", b/1024/1024/1024}')
|
||||
total_gb=$(awk -v b="$total_bytes" 'BEGIN {printf "%.2f", b/1024/1024/1024}')
|
||||
avail_gb=$(awk -v b="$avail_bytes" 'BEGIN {printf "%.2f", b/1024/1024/1024}')
|
||||
|
||||
|
||||
###############################################################################
|
||||
# VÉRIFICATION DU SEUIL D'UTILISATION
|
||||
###############################################################################
|
||||
|
||||
# Si l'utilisation dépasse la limite définie, une alerte est envoyée sur Discord
|
||||
if [ "$usage" -ge "$limit" ]; then
|
||||
|
||||
msgLimit="@here\n**CHECK STOCKAGE :red_circle:**\nLimite autorisé : ${limit}%\nUtilisation actuelle: ${usage}%\nEspace restant: ${free}%\nUtilise / total: ${used_gb} GB / ${total_gb} GB\nDisponible: ${avail_gb} GB\nHeure: $(date)"
|
||||
|
||||
payload="$(jq -n --arg content "$msgLimit" '{content: $content}')"
|
||||
|
||||
curl -X POST \
|
||||
-H "Accept: application/json" \
|
||||
-H "Content-Type: application/json; charset=utf-8" \
|
||||
-d "{\"content\":\"$msgLimit\"}" \
|
||||
-d "$payload" \
|
||||
"$WEBHOOK_URL"
|
||||
|
||||
fi
|
||||
@@ -50,9 +64,6 @@ fi
|
||||
# AFFICHAGE DES INFORMATIONS STOCKAGE
|
||||
###############################################################################
|
||||
|
||||
# Affichage des informations disque dans la console
|
||||
echo "Espace disponible : ${avail_gb} GB"
|
||||
echo "Espace utilise / espace total : ${used_gb} GB / ${total_gb} GB"
|
||||
|
||||
# Nom de la machine exécutant le script
|
||||
echo "Name: ${HOSTNAME}"
|
||||
Reference in New Issue
Block a user