-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnose-network.sh
More file actions
executable file
Β·367 lines (316 loc) Β· 12.6 KB
/
diagnose-network.sh
File metadata and controls
executable file
Β·367 lines (316 loc) Β· 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/bin/bash
# Network Diagnostic Script for Three-Tier Task App
# Use this script to troubleshoot network issues on Linux machines
echo "π Three-Tier Task App - Network Diagnostic Tool"
echo "================================================="
echo ""
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print status
print_status() {
if [ $1 -eq 0 ]; then
echo -e "${GREEN}β
$2${NC}"
else
echo -e "${RED}β $2${NC}"
if [ -n "$3" ]; then
echo -e "${YELLOW} π‘ Fix: $3${NC}"
fi
fi
}
print_info() {
echo -e "${BLUE}βΉοΈ $1${NC}"
}
print_warning() {
echo -e "${YELLOW}β οΈ $1${NC}"
}
echo "1. π Docker System Check"
echo "------------------------"
# Check if Docker is installed
if command -v docker &> /dev/null; then
print_status 0 "Docker is installed"
docker --version
else
print_status 1 "Docker is not installed" "Install Docker: https://docs.docker.com/engine/install/"
exit 1
fi
# Check if Docker daemon is running
if docker info &> /dev/null; then
print_status 0 "Docker daemon is running"
else
print_status 1 "Docker daemon is not running" "Start Docker service: sudo systemctl start docker"
exit 1
fi
# Check if Docker Compose is available
if command -v docker > /dev/null 2>&1 && docker compose version &> /dev/null; then
print_status 0 "Docker Compose (plugin) is available"
docker compose version
elif command -v docker-compose &> /dev/null; then
print_status 0 "Docker Compose (standalone) is available"
docker-compose --version
print_info "Consider upgrading to Docker with integrated Compose plugin"
else
print_status 1 "Docker Compose is not available" "Install Docker with Compose plugin or standalone docker-compose"
exit 1
fi
echo ""
echo "2. π User Permissions Check"
echo "----------------------------"
# Check if user can run Docker without sudo
if docker ps &> /dev/null; then
print_status 0 "User can run Docker commands"
else
print_status 1 "User cannot run Docker without sudo" "Add user to docker group: sudo usermod -aG docker \$USER && newgrp docker"
fi
echo ""
echo "3. π Port Availability Check"
echo "-----------------------------"
check_port() {
local port=$1
local service=$2
if netstat -tuln 2>/dev/null | grep -q ":$port "; then
print_status 1 "Port $port is already in use" "Stop service using port $port or change $service port"
print_info "Services using port $port:"
lsof -i :$port 2>/dev/null || netstat -tulpn 2>/dev/null | grep ":$port "
else
print_status 0 "Port $port is available for $service"
fi
}
check_port 3000 "frontend"
check_port 3001 "backend"
check_port 5432 "database"
echo ""
echo "4. π₯ Firewall Check"
echo "-------------------"
# Check if firewall is active and might block ports
if command -v ufw &> /dev/null; then
if ufw status | grep -q "Status: active"; then
print_warning "UFW firewall is active"
echo " Current UFW rules:"
ufw status
print_info "If connections fail, allow ports: sudo ufw allow 3000 && sudo ufw allow 3001"
else
print_status 0 "UFW firewall is inactive"
fi
elif command -v firewall-cmd &> /dev/null; then
if firewall-cmd --state &> /dev/null; then
print_warning "Firewalld is active"
echo " Current zones:"
firewall-cmd --get-active-zones
print_info "If connections fail, allow ports: sudo firewall-cmd --permanent --add-port=3000-3001/tcp && sudo firewall-cmd --reload"
else
print_status 0 "Firewalld is inactive"
fi
elif command -v iptables &> /dev/null; then
if iptables -L | grep -q "DROP\|REJECT"; then
print_warning "iptables rules detected that might block traffic"
print_info "Check iptables rules: sudo iptables -L"
else
print_status 0 "No blocking iptables rules detected"
fi
fi
echo ""
echo "5. π Host IP Detection and Configuration"
echo "----------------------------------------"
# Function to detect host IP
detect_host_ip() {
local detected_ip=""
# Try to detect the primary network interface IP
if command -v ip &> /dev/null; then
# Linux with ip command
detected_ip=$(ip route get 1.1.1.1 | grep -oP 'src \K\S+' 2>/dev/null)
elif command -v ifconfig &> /dev/null; then
# Linux/macOS with ifconfig
detected_ip=$(ifconfig | grep -E 'inet.*broadcast' | grep -v 127.0.0.1 | awk '{print $2}' | head -1)
fi
echo "$detected_ip"
}
# Detect current host IP
DETECTED_HOST_IP=$(detect_host_ip)
CURRENT_HOST_IP=${HOST_IP:-$DETECTED_HOST_IP}
if [[ -n "$DETECTED_HOST_IP" && "$DETECTED_HOST_IP" != "127.0.0.1" ]]; then
print_status 0 "Host IP detected: $DETECTED_HOST_IP"
if [[ "$CURRENT_HOST_IP" == "localhost" || "$CURRENT_HOST_IP" == "127.0.0.1" ]]; then
print_warning "Current configuration uses localhost, but detected IP is $DETECTED_HOST_IP"
print_info "For remote access, export HOST_IP=$DETECTED_HOST_IP before running docker compose"
fi
else
print_warning "Could not detect non-localhost IP address"
print_info "You may need to manually set HOST_IP environment variable"
fi
# Check if HOST_IP environment variable is set
if [[ -n "$HOST_IP" ]]; then
print_status 0 "HOST_IP environment variable is set: $HOST_IP"
# Check if it's a hostname or IP
if [[ $HOST_IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
print_info "HOST_IP is an IP address"
elif [[ $HOST_IP =~ ^[a-zA-Z0-9][a-zA-Z0-9\.-]*[a-zA-Z0-9]$ ]] || [[ $HOST_IP == "localhost" ]]; then
print_info "HOST_IP is a hostname: $HOST_IP"
print_warning "Development mode with hostnames may show 'Invalid Host header'"
print_info "For remote access with hostnames, use: PRODUCTION=true ./start.sh"
else
print_warning "HOST_IP format is unusual: $HOST_IP"
fi
else
print_warning "HOST_IP environment variable not set - will default to localhost"
print_info "For remote access: export HOST_IP=your-server-ip-or-hostname"
fi
# Check docker-compose configuration
if [[ -f "docker-compose.yml" ]]; then
if grep -q '\${HOST_IP' docker-compose.yml; then
print_status 0 "docker-compose.yml is configured for dynamic HOST_IP"
else
print_status 1 "docker-compose.yml uses hardcoded localhost" "Update docker-compose.yml to use \${HOST_IP:-localhost}"
fi
else
print_status 1 "docker-compose.yml not found" "Ensure you're in the correct directory"
fi
echo ""
echo "6. π‘ Network Connectivity Check"
echo "--------------------------------"
# Check if localhost resolves
if ping -c 1 localhost &> /dev/null; then
print_status 0 "localhost resolves correctly"
else
print_status 1 "localhost does not resolve" "Check /etc/hosts file for localhost entry"
fi
# Check if 127.0.0.1 is reachable
if ping -c 1 127.0.0.1 &> /dev/null; then
print_status 0 "127.0.0.1 is reachable"
else
print_status 1 "127.0.0.1 is not reachable" "Check network configuration"
fi
echo ""
echo "7. π§ SELinux Check (RHEL/CentOS/Fedora)"
echo "----------------------------------------"
if command -v getenforce &> /dev/null; then
selinux_status=$(getenforce)
if [ "$selinux_status" = "Enforcing" ]; then
print_warning "SELinux is enforcing - might block Docker networking"
print_info "Allow Docker in SELinux: sudo setsebool -P container_manage_cgroup on"
elif [ "$selinux_status" = "Permissive" ]; then
print_status 0 "SELinux is permissive"
else
print_status 0 "SELinux is disabled"
fi
else
print_info "SELinux not detected (not a RHEL-based system)"
fi
echo ""
echo "8. π System Resources Check"
echo "----------------------------"
# Check available memory
total_mem=$(free -m | awk 'NR==2{printf "%.1f", $2/1024}')
available_mem=$(free -m | awk 'NR==2{printf "%.1f", $7/1024}')
if (( $(echo "$available_mem > 1.0" | bc -l) )); then
print_status 0 "Sufficient memory available (${available_mem}GB available of ${total_mem}GB total)"
else
print_status 1 "Low memory available (${available_mem}GB available of ${total_mem}GB total)" "Consider closing other applications"
fi
# Check disk space
disk_usage=$(df -h . | awk 'NR==2 {print $5}' | sed 's/%//')
if [ "$disk_usage" -lt 90 ]; then
print_status 0 "Sufficient disk space (${disk_usage}% used)"
else
print_status 1 "Low disk space (${disk_usage}% used)" "Free up disk space before running Docker containers"
fi
echo ""
echo "9. π§ Docker Network Check"
echo "--------------------------"
# Check if docker networks can be created
if docker network ls &> /dev/null; then
print_status 0 "Docker networking is functional"
# Check if the app network exists
if docker network ls | grep -q "taskapp_network"; then
print_info "taskapp_network already exists"
docker network inspect taskapp_network --format='{{.Driver}}' | head -1
else
print_info "taskapp_network will be created when starting the app"
fi
else
print_status 1 "Docker networking is not functional" "Restart Docker daemon: sudo systemctl restart docker"
fi
echo ""
echo "10. π DNS Resolution Check"
echo "---------------------------"
# Check if external DNS works (needed for pulling images)
if nslookup docker.io &> /dev/null; then
print_status 0 "External DNS resolution works"
else
print_status 1 "External DNS resolution failed" "Check DNS settings in /etc/resolv.conf"
fi
echo ""
echo "11. π Container Status Check"
echo "-----------------------------"
if docker compose ps &> /dev/null 2>&1; then
echo "Current container status:"
docker compose ps
# Check if containers are running
if docker compose ps | grep -q "running"; then
print_info "Some containers are running"
# Test connectivity to running services
echo ""
echo "π Testing service connectivity:"
# Use detected or configured host IP for testing
TEST_HOST_IP=${HOST_IP:-${DETECTED_HOST_IP:-localhost}}
if curl -f -s http://${TEST_HOST_IP}:3001/health &> /dev/null; then
print_status 0 "Backend API is responding on ${TEST_HOST_IP}:3001"
else
print_status 1 "Backend API is not responding on ${TEST_HOST_IP}:3001" "Check backend logs: docker compose logs backend"
fi
if curl -f -s http://${TEST_HOST_IP}:3000 &> /dev/null; then
print_status 0 "Frontend is responding on ${TEST_HOST_IP}:3000"
else
# Check for Invalid Host header error
curl_output=$(curl -s http://${TEST_HOST_IP}:3000 2>&1)
if echo "$curl_output" | grep -q "Invalid Host header"; then
print_status 1 "Frontend showing 'Invalid Host header' error" "Use production mode: PRODUCTION=true ./start.sh, or use IP address instead of hostname"
else
print_status 1 "Frontend is not responding on ${TEST_HOST_IP}:3000" "Check frontend logs: docker compose logs frontend"
fi
fi
else
print_info "No containers are currently running"
fi
else
print_info "No docker compose services found in current directory"
fi
echo ""
echo "π― Quick Fixes for Common Issues:"
echo "================================="
echo ""
echo "1. π Host IP/Hostname Configuration:"
echo " Detect IP: ip route get 1.1.1.1 | grep -oP 'src \\K\\S+'"
echo " Set IP manually: export HOST_IP=192.168.1.100"
echo " Set hostname: export HOST_IP=myserver.local"
echo " Use domain: export HOST_IP=server.example.com"
echo ""
echo "2. π« Invalid Host Header (when using hostnames):"
echo " RECOMMENDED: Use production mode: PRODUCTION=true ./start.sh"
echo " ALTERNATIVE: Use IP address instead of hostname"
echo " DEVELOPMENT: Accept warning and use localhost for access"
echo ""
echo "3. π₯ Firewall blocking connections:"
echo " Ubuntu/Debian: sudo ufw allow 3000:3001/tcp"
echo " RHEL/CentOS: sudo firewall-cmd --permanent --add-port=3000-3001/tcp && sudo firewall-cmd --reload"
echo ""
echo "4. π¦ Docker permission issues:"
echo " sudo usermod -aG docker \\$USER && newgrp docker"
echo ""
echo "5. π Port conflicts:"
echo " Check what's using ports: sudo lsof -i :3000 -i :3001 -i :5432"
echo " Kill conflicting processes: sudo kill -9 <PID>"
echo ""
echo "6. π§ SELinux issues (RHEL/CentOS):"
echo " sudo setsebool -P container_manage_cgroup on"
echo ""
echo "7. π Reset Docker environment:"
echo " docker compose down -v && docker system prune -f"
echo ""
echo "8. π View detailed logs:"
echo " docker compose logs -f"
echo ""
echo "β
Diagnostic complete! Check the issues marked with β above."