diff --git a/setup_monitor_nfs.sh b/setup_monitor_nfs.sh index 095143c..0d9ecc5 100755 --- a/setup_monitor_nfs.sh +++ b/setup_monitor_nfs.sh @@ -10,57 +10,86 @@ 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 ejecución +# 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" -cp "$MONITOR_SCRIPT" "$SCRIPT_DEST/$MONITOR_SCRIPT" -chmod +x "$SCRIPT_DEST/$MONITOR_SCRIPT" +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 está disponible +# 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..." - cp "$SYSTEMD_SERVICE" "$SYSTEMD_PATH/$SYSTEMD_SERVICE" - systemctl daemon-reload + 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 configuración de logrotate + # Copia el archivo de configuracion de logrotate echo "Configurando logrotate..." - cp "$LOGROTATE_CONF" "$LOGROTATE_PATH/monitor_nfs" - chmod 644 "$LOGROTATE_PATH/monitor_nfs" + 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)..." - systemctl enable "$SYSTEMD_SERVICE" - systemctl start "$SYSTEMD_SERVICE" - - # Verifica el estado del servicio systemd - echo "Estado del servicio monitor_nfs (systemd):" - systemctl status "$SYSTEMD_SERVICE" -else - # Si systemd no está disponible, verifica si init.d está disponible - if [ -f "$INITD_PATH/$INITD_SERVICE" ]; then - # Copia el archivo de servicio init.d - echo "Instalando el servicio init.d..." - cp "$INITD_SERVICE" "$INITD_PATH/$INITD_SERVICE" - chmod +x "$INITD_PATH/$INITD_SERVICE" - - # Habilita el servicio init.d - echo "Habilitando el servicio de monitoreo (init.d)..." - update-rc.d "$INITD_SERVICE" defaults - - # Verifica el estado del servicio init.d - echo "Estado del servicio monitor_nfs (init.d):" - service "$INITD_SERVICE" status + if systemctl enable "$SYSTEMD_SERVICE" && systemctl start "$SYSTEMD_SERVICE"; then + systemctl status "$SYSTEMD_SERVICE" else - echo "No se encontró systemd ni init.d en el sistema. No se puede configurar el servicio." - exit 1 + 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 -echo "Configuración completada con éxito." +# 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