monitor_nfs/setup_monitor_nfs.sh
2024-10-04 15:16:15 +00:00

96 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
# Configura las rutas
MONITOR_SCRIPT="monitor_nfs.sh"
SYSTEMD_SERVICE="monitor_nfs.service"
LOGROTATE_CONF="monitor_nfs_logrotate"
INITD_SERVICE="monitor_nfs" # Nombre del script de servicio init.d
SYSTEMD_PATH="/etc/systemd/system"
LOGROTATE_PATH="/etc/logrotate.d"
INITD_PATH="/etc/init.d"
SCRIPT_DEST="/usr/local/bin"
# Inicializa el estado de exito
installation_success=true
# Comprueba que se ejecuta como root
if [[ $EUID -ne 0 ]]; then
echo "Este script debe ejecutarse como root."
exit 1
fi
# Copia el script de monitoreo al directorio de destino y otorga permisos de ejecucion
echo "Copiando el script de monitoreo a $SCRIPT_DEST..."
mkdir -p "$SCRIPT_DEST"
if cp "$MONITOR_SCRIPT" "$SCRIPT_DEST/$MONITOR_SCRIPT"; then
chmod +x "$SCRIPT_DEST/$MONITOR_SCRIPT"
else
echo "Error al copiar el script de monitoreo."
installation_success=false
fi
# Verifica si systemd esta disponible
if command -v systemctl > /dev/null; then
# Copia el archivo de servicio systemd y recarga el daemon
echo "Instalando el servicio de systemd..."
if cp "$SYSTEMD_SERVICE" "$SYSTEMD_PATH/$SYSTEMD_SERVICE"; then
systemctl daemon-reload
else
echo "Error al copiar el archivo de servicio systemd."
installation_success=false
fi
# Copia el archivo de configuracion de logrotate
echo "Configurando logrotate..."
if cp "$LOGROTATE_CONF" "$LOGROTATE_PATH/monitor_nfs"; then
chmod 644 "$LOGROTATE_PATH/monitor_nfs"
else
echo "Error al configurar logrotate."
installation_success=false
fi
# Habilita y arranca el servicio de systemd
echo "Habilitando y arrancando el servicio de monitoreo (systemd)..."
if systemctl enable "$SYSTEMD_SERVICE" && systemctl start "$SYSTEMD_SERVICE"; then
systemctl status "$SYSTEMD_SERVICE"
else
echo "Error al habilitar o arrancar el servicio systemd."
installation_success=false
fi
elif [ -d "$INITD_PATH" ] && command -v service > /dev/null; then
# Copia el archivo de servicio init.d
echo "Instalando el servicio init.d..."
if cp "$INITD_SERVICE" "$INITD_PATH/$INITD_SERVICE"; then
chmod +x "$INITD_PATH/$INITD_SERVICE"
else
echo "Error al copiar el archivo de servicio init.d."
installation_success=false
fi
# Habilita el servicio init.d en Alpine Linux (usando rc-update)
if [[ -f /etc/alpine-release ]]; then
echo "Detectado Alpine Linux. Usando rc-update para habilitar el servicio."
if rc-update add "$INITD_SERVICE" default; then
rc-service "$INITD_SERVICE" start
rc-service "$INITD_SERVICE" status
else
echo "Error al habilitar el servicio init.d con rc-update."
installation_success=false
fi
else
echo "Sistema sin soporte, no es Alpine ni tiene systemd. No se puede configurar el servicio."
installation_success=false
fi
else
echo "No se encontro ni systemd ni init.d en el sistema. No se puede configurar el servicio."
exit 1
fi
# Verifica el estado de exito y muestra el mensaje final
if $installation_success; then
echo "Configuracion completada con exito."
else
echo "Hubo errores durante la instalacion. Verifica los mensajes anteriores para mas detalles."
fi