Añadir scripts y configuraciones para monitorización NFS, systemd, init.d y logrotate

This commit is contained in:
root 2024-10-04 15:37:59 +02:00
parent 3259caac2a
commit e5121c7c11
5 changed files with 190 additions and 34 deletions

53
monitor_nfs Normal file
View File

@ -0,0 +1,53 @@
#!/bin/bash
### BEGIN INIT INFO
# Provides: monitor_nfs
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Monitorea montajes NFS y controla un servicio
# Description: Script que arranca monitor_nfs.sh para controlar servicios según estado de montajes NFS
### END INIT INFO
# Configura la ruta del script monitor_nfs.sh
SCRIPT_PATH="/usr/local/bin/monitor_nfs.sh"
PID_FILE="/var/run/monitor_nfs.pid"
case "$1" in
start)
if [ -f "$PID_FILE" ] && kill -0 $(cat "$PID_FILE") 2>/dev/null; then
echo "El servicio monitor_nfs ya está en ejecución."
else
echo "Iniciando el servicio monitor_nfs..."
nohup bash "$SCRIPT_PATH" &> /dev/null &
echo $! > "$PID_FILE"
echo "Servicio monitor_nfs iniciado."
fi
;;
stop)
if [ -f "$PID_FILE" ] && kill -0 $(cat "$PID_FILE") 2>/dev/null; then
echo "Deteniendo el servicio monitor_nfs..."
kill $(cat "$PID_FILE") && rm -f "$PID_FILE"
echo "Servicio monitor_nfs detenido."
else
echo "El servicio monitor_nfs no está en ejecución."
fi
;;
status)
if [ -f "$PID_FILE" ] && kill -0 $(cat "$PID_FILE") 2>/dev/null; then
echo "El servicio monitor_nfs está en ejecución."
else
echo "El servicio monitor_nfs no está en ejecución."
fi
;;
restart)
"$0" stop
"$0" start
;;
*)
echo "Uso: $0 {start|stop|status|restart}"
exit 1
;;
esac
exit 0

View File

@ -3,7 +3,7 @@ Description=Servicio de monitoreo de unidad NFS
After=network.target
[Service]
ExecStart=/opt/scripts/monitor_nfs.sh
ExecStart=/usr/local/bin/monitor_nfs.sh
Restart=always
User=root

View File

@ -27,9 +27,10 @@ log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" >> "$LOG_FILE"
}
# Loop infinito para monitorear todas las unidades NFS
while true; do
# Función para controlar el servicio
control_service() {
if check_all_nfs_mounts; then
if command -v systemctl > /dev/null; then
if ! systemctl is-active --quiet "$SERVICE_NAME"; then
log_message "Todas las unidades NFS están montadas. Intentando iniciar el servicio $SERVICE_NAME..."
if systemctl start "$SERVICE_NAME"; then
@ -43,6 +44,21 @@ while true; do
log_message "Todas las unidades NFS están montadas y el servicio $SERVICE_NAME ya está activo."
fi
else
if ! service "$SERVICE_NAME" status > /dev/null; then
log_message "Todas las unidades NFS están montadas. Intentando iniciar el servicio $SERVICE_NAME..."
if service "$SERVICE_NAME" start; then
log_message "El servicio $SERVICE_NAME se ha iniciado correctamente."
else
log_message "Error al iniciar el servicio $SERVICE_NAME. Intentando detener el servicio..."
service "$SERVICE_NAME" stop
log_message "El servicio $SERVICE_NAME se ha detenido después de un error al intentar iniciarlo."
fi
else
log_message "Todas las unidades NFS están montadas y el servicio $SERVICE_NAME ya está activo."
fi
fi
else
if command -v systemctl > /dev/null; then
if systemctl is-active --quiet "$SERVICE_NAME"; then
log_message "Una o más unidades NFS no están montadas. Deteniendo el servicio $SERVICE_NAME..."
systemctl stop "$SERVICE_NAME"
@ -50,8 +66,21 @@ while true; do
else
log_message "Una o más unidades NFS no están montadas y el servicio $SERVICE_NAME ya está detenido."
fi
else
if service "$SERVICE_NAME" status > /dev/null; then
log_message "Una o más unidades NFS no están montadas. Deteniendo el servicio $SERVICE_NAME..."
service "$SERVICE_NAME" stop
log_message "El servicio $SERVICE_NAME se ha detenido."
else
log_message "Una o más unidades NFS no están montadas y el servicio $SERVICE_NAME ya está detenido."
fi
fi
fi
}
# Loop infinito para monitorear todas las unidades NFS
while true; do
control_service
# Espera 10 segundos antes de verificar nuevamente
sleep 10
done

View File

@ -4,9 +4,11 @@
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"
SCRIPT_DEST="/opt/scripts"
INITD_PATH="/etc/init.d"
SCRIPT_DEST="/usr/local/bin"
# Comprueba que se ejecuta como root
if [[ $EUID -ne 0 ]]; then
@ -20,6 +22,8 @@ mkdir -p "$SCRIPT_DEST"
cp "$MONITOR_SCRIPT" "$SCRIPT_DEST/$MONITOR_SCRIPT"
chmod +x "$SCRIPT_DEST/$MONITOR_SCRIPT"
# Verifica si systemd está 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"
@ -30,14 +34,33 @@ echo "Configurando logrotate..."
cp "$LOGROTATE_CONF" "$LOGROTATE_PATH/monitor_nfs"
chmod 644 "$LOGROTATE_PATH/monitor_nfs"
# Habilita y arranca el servicio
echo "Habilitando y arrancando el servicio de monitoreo..."
systemctl enable monitor_nfs.service
systemctl start monitor_nfs.service
# 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
echo "Estado del servicio monitor_nfs:"
systemctl status monitor_nfs.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
else
echo "No se encontró systemd ni init.d en el sistema. No se puede configurar el servicio."
exit 1
fi
fi
echo "Configuración completada con éxito."

51
uninstall_monitor_nfs.sh Executable file
View File

@ -0,0 +1,51 @@
#!/bin/bash
# Configura las rutas
SYSTEMD_SERVICE="monitor_nfs.service"
LOGROTATE_CONF="monitor_nfs"
INITD_SERVICE="monitor_nfs"
SYSTEMD_PATH="/etc/systemd/system"
LOGROTATE_PATH="/etc/logrotate.d"
INITD_PATH="/etc/init.d"
SCRIPT_DEST="/usr/local/bin/monitor_nfs.sh"
# Comprueba que se ejecuta como root
if [[ $EUID -ne 0 ]]; then
echo "Este script debe ejecutarse como root."
exit 1
fi
# Desinstala el servicio systemd si está presente
if [ -f "$SYSTEMD_PATH/$SYSTEMD_SERVICE" ]; then
echo "Deteniendo y desinstalando el servicio systemd..."
systemctl stop "$SYSTEMD_SERVICE"
systemctl disable "$SYSTEMD_SERVICE"
rm -f "$SYSTEMD_PATH/$SYSTEMD_SERVICE"
systemctl daemon-reload
echo "Servicio systemd desinstalado."
fi
# Desinstala el servicio init.d si está presente
if [ -f "$INITD_PATH/$INITD_SERVICE" ]; then
echo "Deteniendo y desinstalando el servicio init.d..."
service "$INITD_SERVICE" stop
update-rc.d -f "$INITD_SERVICE" remove
rm -f "$INITD_PATH/$INITD_SERVICE"
echo "Servicio init.d desinstalado."
fi
# Desinstala el script de monitoreo
if [ -f "$SCRIPT_DEST" ]; then
echo "Desinstalando el script de monitoreo..."
rm -f "$SCRIPT_DEST"
echo "Script de monitoreo desinstalado."
fi
# Desinstala la configuración de logrotate
if [ -f "$LOGROTATE_PATH/$LOGROTATE_CONF" ]; then
echo "Desinstalando la configuración de logrotate..."
rm -f "$LOGROTATE_PATH/$LOGROTATE_CONF"
echo "Configuración de logrotate desinstalada."
fi
echo "Desinstalación completada con éxito."