check new version
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
#!/bin/bash
|
||||
# {{ ansible_managed }}
|
||||
# Script de monitoring pour le serveur Minecraft
|
||||
|
||||
SERVER_DIR="{{ minecraft_server_dir }}"
|
||||
MCRCON="{{ minecraft_tools_dir }}/mcrcon"
|
||||
RCON_PORT="{{ rcon_port }}"
|
||||
RCON_PASS="{{ rcon_password }}"
|
||||
SERVICE_NAME="{{ minecraft_service_name }}"
|
||||
LOG_FILE="/var/log/minecraft-monitor.log"
|
||||
|
||||
# Fonction de log
|
||||
log_message() {
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
|
||||
}
|
||||
|
||||
# Fonction pour vérifier le statut
|
||||
check_server_status() {
|
||||
if ! systemctl is-active --quiet "$SERVICE_NAME"; then
|
||||
echo "OFFLINE"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Tester RCON
|
||||
if ! $MCRCON -H localhost -P $RCON_PORT -p "$RCON_PASS" "list" &>/dev/null; then
|
||||
echo "RCON_FAILED"
|
||||
return 2
|
||||
fi
|
||||
|
||||
echo "ONLINE"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Fonction pour obtenir les métriques
|
||||
get_metrics() {
|
||||
local status=$(check_server_status)
|
||||
|
||||
if [ "$status" = "ONLINE" ]; then
|
||||
# Nombre de joueurs
|
||||
local players=$($MCRCON -H localhost -P $RCON_PORT -p "$RCON_PASS" "list" 2>/dev/null | grep -oP '\d+(?= of a max)' || echo "0")
|
||||
|
||||
# TPS (si disponible avec un plugin)
|
||||
local tps=$($MCRCON -H localhost -P $RCON_PORT -p "$RCON_PASS" "tps" 2>/dev/null || echo "N/A")
|
||||
|
||||
# Utilisation mémoire du processus
|
||||
local pid=$(systemctl show -p MainPID "$SERVICE_NAME" | cut -d= -f2)
|
||||
local mem="0"
|
||||
if [ "$pid" != "0" ]; then
|
||||
mem=$(ps -o rss= -p "$pid" 2>/dev/null | awk '{printf "%.2f", $1/1024}' || echo "0")
|
||||
fi
|
||||
|
||||
# Utilisation CPU
|
||||
local cpu="0"
|
||||
if [ "$pid" != "0" ]; then
|
||||
cpu=$(ps -o %cpu= -p "$pid" 2>/dev/null | awk '{print $1}' || echo "0")
|
||||
fi
|
||||
|
||||
# Espace disque du serveur
|
||||
local disk=$(du -sh "$SERVER_DIR" 2>/dev/null | cut -f1 || echo "N/A")
|
||||
|
||||
# Affichage JSON pour intégration
|
||||
cat <<EOF
|
||||
{
|
||||
"status": "$status",
|
||||
"players": $players,
|
||||
"tps": "$tps",
|
||||
"memory_mb": $mem,
|
||||
"cpu_percent": $cpu,
|
||||
"disk_usage": "$disk",
|
||||
"timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||
}
|
||||
EOF
|
||||
else
|
||||
cat <<EOF
|
||||
{
|
||||
"status": "$status",
|
||||
"players": 0,
|
||||
"tps": "N/A",
|
||||
"memory_mb": 0,
|
||||
"cpu_percent": 0,
|
||||
"disk_usage": "N/A",
|
||||
"timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||
}
|
||||
EOF
|
||||
fi
|
||||
}
|
||||
|
||||
# Fonction de redémarrage automatique
|
||||
auto_restart_check() {
|
||||
local status=$(check_server_status)
|
||||
|
||||
case "$status" in
|
||||
"OFFLINE")
|
||||
log_message "ALERTE: Serveur hors ligne, tentative de redémarrage..."
|
||||
systemctl restart "$SERVICE_NAME"
|
||||
sleep 30
|
||||
if check_server_status >/dev/null; then
|
||||
log_message "INFO: Serveur redémarré avec succès"
|
||||
else
|
||||
log_message "ERREUR: Échec du redémarrage du serveur"
|
||||
fi
|
||||
;;
|
||||
"RCON_FAILED")
|
||||
log_message "AVERTISSEMENT: RCON ne répond pas, vérification..."
|
||||
# Attendre 60 secondes avant de considérer comme critique
|
||||
sleep 60
|
||||
if [ "$(check_server_status)" = "RCON_FAILED" ]; then
|
||||
log_message "ERREUR: RCON toujours en échec, redémarrage du serveur..."
|
||||
systemctl restart "$SERVICE_NAME"
|
||||
fi
|
||||
;;
|
||||
"ONLINE")
|
||||
# Tout va bien
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Main
|
||||
case "${1:-status}" in
|
||||
status)
|
||||
check_server_status
|
||||
;;
|
||||
metrics)
|
||||
get_metrics
|
||||
;;
|
||||
monitor)
|
||||
auto_restart_check
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 {status|metrics|monitor}"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
Reference in New Issue
Block a user