-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.js
More file actions
263 lines (224 loc) · 7.46 KB
/
debug.js
File metadata and controls
263 lines (224 loc) · 7.46 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
// debug.js
// Simple debugging script to check AWARE system components
const http = require('http');
// Function to make HTTP requests
function makeRequest(options, postData) {
return new Promise((resolve, reject) => {
const req = http.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve({
statusCode: res.statusCode,
headers: res.headers,
data: data
});
});
});
req.on('error', (error) => {
reject(error);
});
// If we have post data, send it
if (postData) {
req.write(postData);
}
req.end();
});
}
// Function to check backend health
async function checkBackendHealth() {
console.log('Checking backend health...');
try {
const response = await makeRequest({
hostname: 'localhost',
port: 3000,
path: '/health',
method: 'GET'
});
console.log(`Backend health check: ${response.statusCode}`);
if (response.statusCode === 200) {
const data = JSON.parse(response.data);
console.log(`Status: ${data.status}`);
console.log(`Version: ${data.version}`);
console.log(`Timestamp: ${data.timestamp}`);
return true;
} else {
console.log(`Backend returned status ${response.statusCode}`);
return false;
}
} catch (error) {
console.error('Backend health check failed:', error.message);
return false;
}
}
// Function to check frontend availability
async function checkFrontend() {
console.log('\nChecking frontend availability...');
try {
const response = await makeRequest({
hostname: 'localhost',
port: 3001,
path: '/',
method: 'GET'
});
console.log(`Frontend check: ${response.statusCode}`);
if (response.statusCode === 200) {
// Check if it's actually serving the React app
if (response.data.includes('AWARE - Cluster Management Dashboard')) {
console.log('Frontend is serving the correct application');
return true;
} else {
console.log('Frontend is serving unexpected content');
return false;
}
} else {
console.log(`Frontend returned status ${response.statusCode}`);
return false;
}
} catch (error) {
console.error('Frontend check failed:', error.message);
return false;
}
}
// Function to check API endpoints
async function checkApiEndpoints() {
console.log('\nChecking API endpoints...');
// First, let's try to login to get a token
let authToken = null;
try {
const loginData = JSON.stringify({
username: 'admin',
password: 'password'
});
const loginResponse = await makeRequest({
hostname: 'localhost',
port: 3000,
path: '/login',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(loginData)
}
}, loginData);
console.log(`Login endpoint: ${loginResponse.statusCode}`);
if (loginResponse.statusCode === 200) {
try {
const loginResult = JSON.parse(loginResponse.data);
authToken = loginResult.token;
console.log('Login successful, got auth token');
} catch (parseError) {
console.log('Login successful but failed to parse token');
}
} else {
console.log(`Login failed with status ${loginResponse.statusCode}`);
console.log(`Response: ${loginResponse.data}`);
}
} catch (error) {
console.error(`Login endpoint: Failed - ${error.message}`);
}
const endpoints = [
{ path: '/api/cluster/status', name: 'Cluster Status' },
{ path: '/api/cluster/config', name: 'Cluster Config' },
{ path: '/api/cluster/metrics', name: 'Cluster Metrics' },
{ path: '/api/nodes', name: 'Nodes' },
{ path: '/api/alerts', name: 'Alerts' }
];
let successCount = 0;
let unauthorizedCount = 0;
for (const endpoint of endpoints) {
try {
const options = {
hostname: 'localhost',
port: 3000,
path: endpoint.path,
method: 'GET'
};
// Add auth header if we have a token
if (authToken) {
options.headers = {
'Authorization': `Bearer ${authToken}`
};
}
const response = await makeRequest(options);
console.log(`${endpoint.name}: ${response.statusCode}`);
if (response.statusCode === 200) {
successCount++;
} else if (response.statusCode === 401 || response.statusCode === 403) {
unauthorizedCount++;
console.log(` Response: ${response.data}`);
}
} catch (error) {
console.error(`${endpoint.name}: Failed - ${error.message}`);
}
}
console.log(`\nAPI endpoints working: ${successCount}/${endpoints.length}`);
if (unauthorizedCount > 0) {
console.log(`${unauthorizedCount} endpoints returned 401/403 (Unauthorized/Forbidden)`);
}
// If all endpoints work, that's great
// If all endpoints return 401/403, that means auth is working but we have bad tokens
const allGood = successCount === endpoints.length || (successCount === 0 && unauthorizedCount === endpoints.length);
return successCount === endpoints.length; // Only return true if all endpoints worked
}
// Function to check Docker containers
async function checkDockerContainers() {
console.log('\nChecking Docker containers...');
const { exec } = require('child_process');
return new Promise((resolve) => {
exec('docker ps --filter "name=aware-" --format "table {{.Names}}\t{{.Status}}"', (error, stdout, stderr) => {
if (error) {
console.error('Failed to check Docker containers:', error.message);
resolve(false);
return;
}
console.log('Docker containers:');
console.log(stdout);
if (stdout.includes('aware-backend') && stdout.includes('aware-frontend')) {
console.log('Both AWARE containers are running');
resolve(true);
} else {
console.log('Some AWARE containers are missing');
resolve(false);
}
});
});
}
// Main debugging function
async function debugSystem() {
console.log('=== AWARE System Debugging ===\n');
// Check Docker containers
const containersOk = await checkDockerContainers();
// Check backend health
const backendOk = await checkBackendHealth();
// Check frontend
const frontendOk = await checkFrontend();
// Check API endpoints
const apiOk = await checkApiEndpoints();
// Summary
console.log('\n=== Debugging Summary ===');
console.log(`Docker containers: ${containersOk ? 'OK' : 'FAILED'}`);
console.log(`Backend health: ${backendOk ? 'OK' : 'FAILED'}`);
console.log(`Frontend availability: ${frontendOk ? 'OK' : 'FAILED'}`);
console.log(`API endpoints: ${apiOk ? 'OK' : 'FAILED'}`);
const overall = containersOk && backendOk && frontendOk && apiOk;
console.log(`\nOverall system status: ${overall ? 'HEALTHY' : 'ISSUES DETECTED'}`);
if (!overall) {
console.log('\nRecommendations:');
if (!containersOk) {
console.log('- Check Docker containers with "docker ps"');
}
if (!backendOk) {
console.log('- Check backend logs with "docker logs aware-backend"');
}
if (!frontendOk) {
console.log('- Check frontend logs with "docker logs aware-frontend"');
}
if (!apiOk) {
console.log('- Check API endpoints individually');
}
}
}
// Run the debugging
debugSystem().catch(console.error);