Add HA watchdog

This commit is contained in:
2026-03-27 19:44:00 -03:00
parent f0557a0a12
commit a0b67cf30b
3 changed files with 95 additions and 5 deletions

39
Docker/Nginx/compose.yml Normal file
View File

@@ -0,0 +1,39 @@
services:
app:
image: 'jc21/nginx-proxy-manager:latest'
restart: unless-stopped
ports:
- '80:80' # Public HTTP Port
- '443:443' # Public HTTPS Port
- '81:81' # Admin Web Port
environment:
TZ: "America/Santiago"
DB_POSTGRES_HOST: 'db'
DB_POSTGRES_PORT: '5432'
DB_POSTGRES_USER: 'npm'
DB_POSTGRES_PASSWORD: '4d621b77253c5e32beba2ea33a57dc3f'
DB_POSTGRES_NAME: 'npm'
DISABLE_IPV6: 'true'
volumes:
- ./data:/data
- ./letsencrypt:/etc/letsencrypt
networks:
- homelogic
depends_on:
- db
db:
image: postgres:17
environment:
POSTGRES_USER: 'npm'
POSTGRES_PASSWORD: '4d621b77253c5e32beba2ea33a57dc3f'
POSTGRES_DB: 'npm'
volumes:
- ./postgresql:/var/lib/postgresql
networks:
- homelogic
networks:
homelogic:
name: homelogic
external: true

22
ha_watchdog.sh Normal file
View File

@@ -0,0 +1,22 @@
#!/bin/bash
HA_URL="http://localhost:8123"
STATE_FILE="/tmp/ha_watchdog_state"
if curl -s -m 10 "$HA_URL" > /dev/null; then
echo "0" > "$STATE_FILE"
else
STATE="0"
if [ -f "$STATE_FILE" ]; then
STATE=$(cat "$STATE_FILE")
fi
if [ "$STATE" -eq 1 ]; then
# Already tried restarting once, failed again -> Reboot
echo "0" > "$STATE_FILE"
sudo reboot
else
# Try returning the container to life
docker restart HomeAssistant
echo "1" > "$STATE_FILE"
fi
fi

View File

@@ -14,6 +14,7 @@ LINE_DOCKER=5
LINE_ZSH=6 LINE_ZSH=6
LINE_TAILSCALE=7 LINE_TAILSCALE=7
LINE_WIFITUI=8 LINE_WIFITUI=8
LINE_HA_WATCHDOG=9
teardown_ui() { teardown_ui() {
local ROWS local ROWS
@@ -32,7 +33,7 @@ setup_ui() {
LOG_END=$ROWS LOG_END=$ROWS
LOG_START=$(( LOG_END - MAX_LOG_LINES + 1 )) LOG_START=$(( LOG_END - MAX_LOG_LINES + 1 ))
local MIN_LOG_START=$(( LINE_WIFITUI + 4 )) local MIN_LOG_START=$(( LINE_HA_WATCHDOG + 4 ))
if [ "$LOG_START" -le "$(( MIN_LOG_START - 1 ))" ]; then if [ "$LOG_START" -le "$(( MIN_LOG_START - 1 ))" ]; then
LOG_START=$MIN_LOG_START LOG_START=$MIN_LOG_START
fi fi
@@ -49,6 +50,7 @@ setup_ui() {
printf "\033[${LINE_ZSH};1H [ ] ZSH\n" printf "\033[${LINE_ZSH};1H [ ] ZSH\n"
printf "\033[${LINE_TAILSCALE};1H [ ] Tailscale\n" printf "\033[${LINE_TAILSCALE};1H [ ] Tailscale\n"
printf "\033[${LINE_WIFITUI};1H [ ] Wifitui\n" printf "\033[${LINE_WIFITUI};1H [ ] Wifitui\n"
printf "\033[${LINE_HA_WATCHDOG};1H [ ] HA Watchdog\n"
printf "\033[${SEP_LINE};1H${YELLOW}--- Verbose Logs ------------------------${NC}\n" printf "\033[${SEP_LINE};1H${YELLOW}--- Verbose Logs ------------------------${NC}\n"
@@ -57,6 +59,7 @@ setup_ui() {
update_status "$LINE_ZSH" "Pending" update_status "$LINE_ZSH" "Pending"
update_status "$LINE_TAILSCALE" "Pending" update_status "$LINE_TAILSCALE" "Pending"
update_status "$LINE_WIFITUI" "Pending" update_status "$LINE_WIFITUI" "Pending"
update_status "$LINE_HA_WATCHDOG" "Pending"
printf "\033[${LOG_START};${LOG_END}r" printf "\033[${LOG_START};${LOG_END}r"
printf "\033[${LOG_END};1H" printf "\033[${LOG_END};1H"
@@ -70,7 +73,7 @@ update_status() {
Pending) color="${NC}" ;; Pending) color="${NC}" ;;
Running) color="${BLUE}" ;; Running) color="${BLUE}" ;;
Installed) color="${GREEN}" ;; Installed) color="${GREEN}" ;;
Skipped) color="${YELLOW}" ;; Skipped|Removed) color="${YELLOW}" ;;
esac esac
printf "\0337\033[%d;4H${color}%-10s${NC}\0338" "$line" "$status" printf "\0337\033[%d;4H${color}%-10s${NC}\0338" "$line" "$status"
} }
@@ -80,7 +83,7 @@ print_step() {
} }
print_info() { print_info() {
local IL=${INFO_LINE:-$(( LINE_WIFITUI + 2 ))} local IL=${INFO_LINE:-$(( LINE_HA_WATCHDOG + 2 ))}
printf "\0337\033[${IL};1H\033[K${CYAN} ➜ %s${NC}\0338" "$1" printf "\0337\033[${IL};1H\033[K${CYAN} ➜ %s${NC}\0338" "$1"
printf "${CYAN}➜ %s${NC}\n" "$1" printf "${CYAN}➜ %s${NC}\n" "$1"
} }
@@ -109,8 +112,10 @@ setup_server() {
print_step "Setting up Server" print_step "Setting up Server"
print_info "Updating packages and configuring firewall..." print_info "Updating packages and configuring firewall..."
sudo apt-get update sudo apt-get update
sudo ufw allow 22/tcp sudo ufw allow 22/tcp #ssh
sudo ufw allow 443/tcp sudo ufw allow 80/tcp #nginx
sudo ufw allow 81/tcp #nginx
sudo ufw allow 443/tcp #nginx
sudo ufw allow 8123/tcp #homeassistant sudo ufw allow 8123/tcp #homeassistant
sudo ufw allow 8123/udp #homeassistant sudo ufw allow 8123/udp #homeassistant
sudo ufw allow 3000/tcp #nextcloud sudo ufw allow 3000/tcp #nextcloud
@@ -195,6 +200,27 @@ setup_wifitui() {
update_status "$line" "Installed" update_status "$line" "Installed"
} }
setup_ha_watchdog() {
local line="$1"
local watchdog_path="$(cd "$(dirname "$0")" && pwd)/ha_watchdog.sh"
print_step "Setting up HA Watchdog"
if [ -f "/etc/cron.d/ha_watchdog" ]; then
print_info "Uninstalling HA Watchdog..."
sudo rm -f "/etc/cron.d/ha_watchdog"
sudo rm -f "/usr/local/bin/ha_watchdog.sh" 2>/dev/null || true
update_status "$line" "Removed"
else
print_info "Configuring HA Watchdog script permissions..."
chmod +x "$watchdog_path"
print_info "Creating cron job (every 5 minutes)..."
echo "*/5 * * * * root $watchdog_path" | sudo tee /etc/cron.d/ha_watchdog > /dev/null
update_status "$line" "Installed"
fi
}
main() { main() {
clear clear
printf "\n${BLUE}=========================================${NC}\n" printf "\n${BLUE}=========================================${NC}\n"
@@ -231,10 +257,12 @@ main() {
setup_zsh setup_zsh
ask_optional_step "$LINE_TAILSCALE" "Tailscale" setup_tailscale ask_optional_step "$LINE_TAILSCALE" "Tailscale" setup_tailscale
ask_optional_step "$LINE_WIFITUI" "Wifitui" setup_wifitui ask_optional_step "$LINE_WIFITUI" "Wifitui" setup_wifitui
ask_optional_step "$LINE_HA_WATCHDOG" "HA Watchdog" setup_ha_watchdog
;; ;;
2) 2)
update_status "$LINE_TAILSCALE" "Skipped" update_status "$LINE_TAILSCALE" "Skipped"
update_status "$LINE_WIFITUI" "Skipped" update_status "$LINE_WIFITUI" "Skipped"
update_status "$LINE_HA_WATCHDOG" "Skipped"
setup_server setup_server
setup_docker setup_docker
setup_zsh setup_zsh
@@ -245,6 +273,7 @@ main() {
update_status "$LINE_ZSH" "Skipped" update_status "$LINE_ZSH" "Skipped"
ask_optional_step "$LINE_TAILSCALE" "Tailscale" setup_tailscale ask_optional_step "$LINE_TAILSCALE" "Tailscale" setup_tailscale
ask_optional_step "$LINE_WIFITUI" "Wifitui" setup_wifitui ask_optional_step "$LINE_WIFITUI" "Wifitui" setup_wifitui
ask_optional_step "$LINE_HA_WATCHDOG" "HA Watchdog" setup_ha_watchdog
;; ;;
esac esac