/
/
/
1#!/bin/bash
2# Connectivity Services Health Check Script
3# {{ ansible_managed }}
4
5set -euo pipefail
6
7# Configuration
8DOCKER_BASE_PATH="{{ connectivity_docker_base_path }}"
9HOST_IP="{{ ansible_default_ipv4.address }}"
10
11# Colors for output
12RED='\033[0;31m'
13GREEN='\033[0;32m'
14YELLOW='\033[1;33m'
15BLUE='\033[0;34m'
16NC='\033[0m' # No Color
17
18# Logging function
19log() {
20 echo -e "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
21}
22
23# Check if service is running
24check_container() {
25 local container_name="$1"
26 local service_name="$2"
27
28 if docker ps --format "table {{.Names}}" | grep -q "^${container_name}$"; then
29 log "${GREEN}â${NC} ${service_name} container is running"
30 return 0
31 else
32 log "${RED}â${NC} ${service_name} container is not running"
33 return 1
34 fi
35}
36
37# Check if port is listening
38check_port() {
39 local port="$1"
40 local service_name="$2"
41 local protocol="${3:-tcp}"
42
43 if nc -z "${HOST_IP}" "${port}" 2>/dev/null; then
44 log "${GREEN}â${NC} ${service_name} is listening on port ${port}/${protocol}"
45 return 0
46 else
47 log "${RED}â${NC} ${service_name} is not listening on port ${port}/${protocol}"
48 return 1
49 fi
50}
51
52# Check HTTP endpoint
53check_http() {
54 local url="$1"
55 local service_name="$2"
56
57 if curl -sf "${url}" > /dev/null 2>&1; then
58 log "${GREEN}â${NC} ${service_name} HTTP endpoint is responding"
59 return 0
60 else
61 log "${RED}â${NC} ${service_name} HTTP endpoint is not responding"
62 return 1
63 fi
64}
65
66# Check DNS resolution
67check_dns() {
68 local dns_server="$1"
69 local test_domain="${2:-google.com}"
70
71 if dig "@${dns_server}" "${test_domain}" +short | grep -q "^[0-9]"; then
72 log "${GREEN}â${NC} DNS resolution working through ${dns_server}"
73 return 0
74 else
75 log "${RED}â${NC} DNS resolution failed through ${dns_server}"
76 return 1
77 fi
78}
79
80# Main health check function
81main() {
82 log "${BLUE}Starting Connectivity Services Health Check${NC}"
83 log "Host: ${HOST_IP}"
84 echo
85
86 local overall_status=0
87
88 # Docker daemon check
89 if ! docker info > /dev/null 2>&1; then
90 log "${RED}â${NC} Docker daemon is not running"
91 exit 1
92 fi
93 log "${GREEN}â${NC} Docker daemon is running"
94 echo
95
96 # Service checks
97{% if connectivity_wireguard_enabled %}
98 log "${YELLOW}Checking WireGuard VPN...${NC}"
99 check_container "{{ connectivity_wireguard_container_name }}" "WireGuard" || overall_status=1
100 check_port "{{ connectivity_wireguard_web_port }}" "WireGuard Web UI" "tcp" || overall_status=1
101 check_http "http://${HOST_IP}:{{ connectivity_wireguard_web_port }}" "WireGuard Web UI" || overall_status=1
102 echo
103{% endif %}
104
105{% if connectivity_nginx_proxy_enabled %}
106 log "${YELLOW}Checking Nginx Proxy Manager...${NC}"
107 check_container "{{ connectivity_nginx_proxy_container_name }}" "Nginx Proxy Manager" || overall_status=1
108 check_port "{{ connectivity_nginx_proxy_admin_port }}" "Nginx Proxy Admin" "tcp" || overall_status=1
109 check_port "{{ connectivity_nginx_proxy_http_port }}" "HTTP Proxy" "tcp" || overall_status=1
110 check_port "{{ connectivity_nginx_proxy_https_port }}" "HTTPS Proxy" "tcp" || overall_status=1
111 check_http "http://${HOST_IP}:{{ connectivity_nginx_proxy_admin_port }}" "Nginx Proxy Admin UI" || overall_status=1
112{% if connectivity_nginx_proxy_db_enabled %}
113 check_container "{{ connectivity_nginx_proxy_db_container_name }}" "Nginx Proxy Database" || overall_status=1
114{% endif %}
115 echo
116{% endif %}
117
118{% if connectivity_unbound_enabled %}
119 log "${YELLOW}Checking Unbound DNS...${NC}"
120 check_container "{{ connectivity_unbound_container_name }}" "Unbound" || overall_status=1
121 check_port "{{ connectivity_unbound_port }}" "Unbound DNS" "udp" || overall_status=1
122 echo
123{% endif %}
124
125{% if connectivity_pihole_enabled %}
126 log "${YELLOW}Checking Pi-hole DNS...${NC}"
127 check_container "{{ connectivity_pihole_container_name }}" "Pi-hole" || overall_status=1
128 check_port "{{ connectivity_pihole_dns_port }}" "Pi-hole DNS" "udp" || overall_status=1
129 check_port "{{ connectivity_pihole_web_port }}" "Pi-hole Web UI" "tcp" || overall_status=1
130 check_http "http://${HOST_IP}:{{ connectivity_pihole_web_port }}" "Pi-hole Web UI" || overall_status=1
131 check_dns "${HOST_IP}" "google.com" || overall_status=1
132 echo
133{% endif %}
134
135 # Network connectivity check
136 log "${YELLOW}Checking network connectivity...${NC}"
137 if ping -c 1 8.8.8.8 > /dev/null 2>&1; then
138 log "${GREEN}â${NC} Internet connectivity is working"
139 else
140 log "${RED}â${NC} Internet connectivity is not working"
141 overall_status=1
142 fi
143 echo
144
145 # Disk space check
146 log "${YELLOW}Checking disk space...${NC}"
147 local disk_usage=$(df "${DOCKER_BASE_PATH}" | tail -1 | awk '{print $5}' | sed 's/%//')
148 if [ "${disk_usage}" -lt 90 ]; then
149 log "${GREEN}â${NC} Disk space is adequate (${disk_usage}% used)"
150 else
151 log "${YELLOW}â ${NC} Disk space is running low (${disk_usage}% used)"
152 fi
153 echo
154
155 # Summary
156 if [ ${overall_status} -eq 0 ]; then
157 log "${GREEN}â All connectivity services are healthy${NC}"
158 else
159 log "${RED}â Some connectivity services have issues${NC}"
160 log "${BLUE}Check individual service logs: docker logs <service_name>${NC}"
161 fi
162
163 exit ${overall_status}
164}
165
166main "$@"