Oracle Server
De RJGS
Scrips
[editar]Monitoramento do Apache
[editar]Crie o arquivo
vim monitora_apache.sh
Cola o conteúdo abaixo
#!/bin/bash
#
##########################################
# NOME...: Robison Joel Guimarães Simon
# DATA...: 18/07/2026
# ASSUNTO: Verificar e restaurar status do Apache2
##########################################
# Arquivo de log
LOG="/var/log/verifica_apache.log"
# Redireciona toda a saída para o log
exec >> "$LOG" 2>&1
echo ""
echo "[$(date +%d-%m-%y_%H:%M)] #######################################"
echo "[$(date +%d-%m-%y_%H:%M)] ### VERIFICACAO DO APACHE2 ###########"
echo "[$(date +%d-%m-%y_%H:%M)] #######################################"
# Verificar status do serviço
STATUS=$(systemctl is-active apache2)
echo "[$(date +%d-%m-%y_%H:%M)] Status do Apache2: $STATUS"
# Se o serviço não estiver ativo, reiniciar
if [ "$STATUS" != "active" ]; then
echo "[$(date +%d-%m-%y_%H:%M)] Apache2 não está ativo ($STATUS). Reiniciando..."
systemctl restart apache2
sleep 5
NOVO_STATUS=$(systemctl is-active apache2)
if [ "$NOVO_STATUS" = "active" ]; then
echo "[$(date +%d-%m-%y_%H:%M)] Apache2 reiniciado com sucesso."
else
echo "[$(date +%d-%m-%y_%H:%M)] ERRO: Falha ao reiniciar o Apache2."
echo "[$(date +%d-%m-%y_%H:%M)] Últimas mensagens do serviço:"
journalctl -u apache2 -n 20 --no-pager
echo "[$(date +%d-%m-%y_%H:%M)] #######################################"
exit 1
fi
fi
# Verificar se o Apache realmente está respondendo
curl -k -I https://localhost --connect-timeout 10 >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "[$(date +%d-%m-%y_%H:%M)] Apache2 ativo, porém sem resposta HTTPS."
echo "[$(date +%d-%m-%y_%H:%M)] Reiniciando serviço..."
systemctl restart apache2
sleep 5
curl -k -I https://localhost --connect-timeout 10 >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "[$(date +%d-%m-%y_%H:%M)] Apache2 voltou a responder normalmente."
else
echo "[$(date +%d-%m-%y_%H:%M)] ERRO: Apache2 continua sem responder."
journalctl -u apache2 -n 20 --no-pager
exit 1
fi
else
echo "[$(date +%d-%m-%y_%H:%M)] Apache2 está ativo e respondendo normalmente."
fi
echo "[$(date +%d-%m-%y_%H:%M)] #######################################"