/
/
/
1#!/bin/bash
2# ==============================================================================
3# Docker Status Script
4# ==============================================================================
5#
6# Description: Shows comprehensive status of all connectivity Docker services
7# Usage: ./docker-status.sh [detailed|summary|health|resources]
8#
9# This script is automatically generated by Ansible - DO NOT EDIT MANUALLY
10# Template: docker-status.sh.j2
11#
12# ==============================================================================
13
14set -euo pipefail
15
16# Configuration
17DOCKER_COMPOSE_DIR="{{ docker_base_path }}"
18LOG_FILE="/var/log/docker-status.log"
19
20# Service order
21SERVICES=(
22 "{{ unbound_service_name }}"
23 "{{ pihole_service_name }}"
24 "{{ nginx_proxy_db_service_name }}"
25 "{{ nginx_proxy_service_name }}"
26 "{{ wireguard_service_name }}"
27)
28
29# Logging function
30log() {
31 echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "${LOG_FILE}"
32}
33
34# Check if container exists
35container_exists() {
36 docker inspect "$1" >/dev/null 2>&1
37}
38
39# Check if container is running
40container_running() {
41 docker inspect -f '{{.State.Running}}' "$1" 2>/dev/null | grep -q "true"
42}
43
44# Get container health status
45get_health_status() {
46 local container="$1"
47 docker inspect -f '{{.State.Health.Status}}' "${container}" 2>/dev/null || echo "no healthcheck"
48}
49
50# Get container restart count
51get_restart_count() {
52 local container="$1"
53 docker inspect -f '{{.RestartCount}}' "${container}" 2>/dev/null || echo "0"
54}
55
56# Get container resource usage
57get_resource_usage() {
58 local container="$1"
59 docker stats "${container}" --no-stream --format "{{.MemUsage}}|{{.CPUPerc}}|{{.NetIO}}|{{.BlockIO}}" 2>/dev/null || echo "N/A|N/A|N/A|N/A"
60}
61
62# Get container uptime
63get_uptime() {
64 local container="$1"
65 docker inspect -f '{{.State.StartedAt}}' "${container}" 2>/dev/null || echo "N/A"
66}
67
68# Show detailed service status
69show_detailed_status() {
70 log "Detailed Docker Service Status"
71 echo "=============================================================================="
72
73 for service in "${SERVICES[@]}"; do
74 echo ""
75 echo "=== ${service} ==="
76
77 if ! container_exists "${service}"; then
78 echo "Status: DOES NOT EXIST"
79 continue
80 fi
81
82 if container_running "${service}"; then
83 local status="RUNNING"
84 local health=$(get_health_status "${service}")
85 local restarts=$(get_restart_count "${service}")
86 local uptime=$(get_uptime "${service}")
87 local resources=$(get_resource_usage "${service}")
88
89 echo "Status: ${status}"
90 echo "Health: ${health}"
91 echo "Restarts: ${restarts}"
92 echo "Uptime: ${uptime}"
93
94 # Parse resource usage
95 local memory_usage=$(echo "${resources}" | cut -d'|' -f1)
96 local cpu_usage=$(echo "${resources}" | cut -d'|' -f2)
97 local network_io=$(echo "${resources}" | cut -d'|' -f3)
98 local block_io=$(echo "${resources}" | cut -d'|' -f4)
99
100 echo "Memory: ${memory_usage}"
101 echo "CPU: ${cpu_usage}"
102 echo "Network: ${network_io}"
103 echo "Disk: ${block_io}"
104
105 # Show container IP address
106 local ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "${service}" 2>/dev/null || echo "N/A")
107 echo "IP Address: ${ip}"
108
109 # Show exposed ports
110 local ports=$(docker inspect -f '{{range $p, $conf := .NetworkSettings.Ports}}{{$p}} {{end}}' "${service}" 2>/dev/null || echo "N/A")
111 echo "Ports: ${ports}"
112
113 else
114 echo "Status: STOPPED"
115 local exit_code=$(docker inspect -f '{{.State.ExitCode}}' "${service}" 2>/dev/null || echo "N/A")
116 echo "Exit Code: ${exit_code}"
117 fi
118 done
119
120 echo ""
121 echo "=============================================================================="
122}
123
124# Show summary status
125show_summary_status() {
126 log "Docker Service Summary"
127 echo "=============================================================================="
128 echo "Service Status Health Restarts Uptime"
129 echo "------------------------------------------------------------------------------"
130
131 local running_count=0
132 local stopped_count=0
133
134 for service in "${SERVICES[@]}"; do
135 if ! container_exists "${service}"; then
136 printf "%-22s %-10s %-12s %-8s %s\n" "${service}" "MISSING" "N/A" "N/A" "N/A"
137 continue
138 fi
139
140 if container_running "${service}"; then
141 running_count=$((running_count + 1))
142 local health=$(get_health_status "${service}")
143 local restarts=$(get_restart_count "${service}")
144 local uptime=$(get_uptime "${service}" | cut -d'T' -f1)
145
146 printf "%-22s %-10s %-12s %-8s %s\n" "${service}" "RUNNING" "${health}" "${restarts}" "${uptime}"
147 else
148 stopped_count=$((stopped_count + 1))
149 printf "%-22s %-10s %-12s %-8s %s\n" "${service}" "STOPPED" "N/A" "N/A" "N/A"
150 fi
151 done
152
153 echo "------------------------------------------------------------------------------"
154 echo "Summary: ${running_count} running, ${stopped_count} stopped"
155 echo "=============================================================================="
156}
157
158# Show health-focused status
159show_health_status() {
160 log "Docker Service Health Status"
161 echo "=============================================================================="
162 echo "Service Health Status Issues"
163 echo "------------------------------------------------------------------------------"
164
165 local healthy_count=0
166 local unhealthy_count=0
167
168 for service in "${SERVICES[@]}"; do
169 if ! container_exists "${service}"; then
170 printf "%-22s %-12s %-10s %s\n" "${service}" "N/A" "MISSING" "Container does not exist"
171 unhealthy_count=$((unhealthy_count + 1))
172 continue
173 fi
174
175 if container_running "${service}"; then
176 local health=$(get_health_status "${service}")
177 local restarts=$(get_restart_count "${service}")
178
179 if [[ "${health}" == "healthy" ]]; then
180 healthy_count=$((healthy_count + 1))
181 printf "%-22s %-12s %-10s %s\n" "${service}" "${health}" "RUNNING" "OK"
182 else
183 unhealthy_count=$((unhealthy_count + 1))
184 local issues=""
185
186 if [[ ${restarts} -gt 0 ]]; then
187 issues="${issues}Restarted ${restarts} times, "
188 fi
189
190 if [[ "${health}" == "unhealthy" ]]; then
191 issues="${issues}Health check failing, "
192 fi
193
194 printf "%-22s %-12s %-10s %s\n" "${service}" "${health}" "RUNNING" "${issues%%, }"
195 fi
196 else
197 unhealthy_count=$((unhealthy_count + 1))
198 printf "%-22s %-12s %-10s %s\n" "${service}" "N/A" "STOPPED" "Container not running"
199 fi
200 done
201
202 echo "------------------------------------------------------------------------------"
203 echo "Health Summary: ${healthy_count} healthy, ${unhealthy_count} unhealthy"
204 echo "=============================================================================="
205}
206
207# Show resource usage
208show_resource_usage() {
209 log "Docker Service Resource Usage"
210 echo "=============================================================================="
211 echo "Service CPU Usage Memory Usage Network I/O Disk I/O"
212 echo "------------------------------------------------------------------------------"
213
214 for service in "${SERVICES[@]}"; do
215 if ! container_exists "${service}" || ! container_running "${service}"; then
216 printf "%-22s %-12s %-14s %-13s %s\n" "${service}" "N/A" "N/A" "N/A" "N/A"
217 continue
218 fi
219
220 local resources=$(get_resource_usage "${service}")
221 local cpu_usage=$(echo "${resources}" | cut -d'|' -f2)
222 local memory_usage=$(echo "${resources}" | cut -d'|' -f1)
223 local network_io=$(echo "${resources}" | cut -d'|' -f3)
224 local block_io=$(echo "${resources}" | cut -d'|' -f4)
225
226 printf "%-22s %-12s %-14s %-13s %s\n" "${service}" "${cpu_usage}" "${memory_usage}" "${network_io}" "${block_io}"
227 done
228
229 echo "------------------------------------------------------------------------------"
230
231 # Show overall system resource usage
232 echo "Overall System Resources:"
233 docker system df --verbose 2>/dev/null || true
234 echo "=============================================================================="
235}
236
237# Show usage
238usage() {
239 cat << EOF
240Docker Status Script
241
242Usage: $0 [mode]
243
244Modes:
245 detailed Detailed service information (default)
246 summary Summary table view
247 health Health-focused status
248 resources Resource usage information
249 help Show this help message
250
251Examples:
252 $0 detailed
253 $0 summary
254 $0 health
255 $0 resources
256 $0 help
257
258Service Order:
259 1. {{ unbound_service_name }} (DNS resolver)
260 2. {{ pihole_service_name }} (DNS/ad-blocker)
261 3. {{ nginx_proxy_db_service_name }} (Database)
262 4. {{ nginx_proxy_service_name }} (Reverse proxy)
263 5. {{ wireguard_service_name }} (VPN)
264EOF
265}
266
267# Main execution
268main() {
269 local mode="${1:-detailed}"
270
271 case "${mode}" in
272 detailed)
273 show_detailed_status
274 ;;
275 summary)
276 show_summary_status
277 ;;
278 health)
279 show_health_status
280 ;;
281 resources)
282 show_resource_usage
283 ;;
284 help|*)
285 usage
286 ;;
287 esac
288}
289
290# Run main function with all arguments
291main "$@"