-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockstatapi.js
378 lines (330 loc) · 14.7 KB
/
dockstatapi.js
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
368
369
370
371
372
373
374
375
376
377
378
const express = require('express');
const path = require('path');
const yaml = require('yamljs');
const Docker = require('dockerode');
const cors = require('cors');
const fs = require('fs');
const { exec } = require('child_process');
const logger = require('./logger');
const updateAvailable = require('./modules/updateAvailable')
const app = express();
const port = 7070;
const key = process.env.SECRET || 'CHANGE-ME';
const skipAuth = process.env.SKIP_AUTH || 'True'
const cupUrl = process.env.CUP_URL || 'null'
let config = yaml.load('./config/hosts.yaml');
let hosts = config.hosts;
let containerConfigs = config.container || {};
let maxlogsize = config.log.logsize || 1;
let LogAmount = config.log.LogCount || 5;
let queryInterval = config.mintimeout || 5000;
let latestStats = {};
let hostQueues = {};
let previousNetworkStats = {};
let generalStats = {};
let previousContainerStates = {};
let previousRunningContainers = {};
app.use(cors());
app.use(express.json());
const authenticateHeader = (req, res, next) => {
const authHeader = req.headers['authorization'];
if (skipAuth === 'True') {
next();
} else {
if (!authHeader || authHeader !== key) {
logger.error(`${authHeader} != ${key}`);
return res.status(401).json({ error: "Unauthorized" });
}
else {
next();
}
}
};
function createDockerClient(hostConfig) {
return new Docker({
host: hostConfig.url,
port: hostConfig.port,
});
}
function getTagColor(tag) {
const tagsConfig = config.tags || {};
return tagsConfig[tag] || '';
}
async function getContainerStats(docker, containerId) {
const container = docker.getContainer(containerId);
return new Promise((resolve, reject) => {
container.stats({ stream: false }, (err, stats) => {
if (err) return reject(err);
resolve(stats);
});
});
}
async function handleContainerStateChanges(hostName, currentContainers) {
const currentRunningContainers = currentContainers
.filter(container => container.state === 'running')
.reduce((map, container) => {
map[container.id] = container;
return map;
}, {});
const previousHostContainers = previousRunningContainers[hostName] || {};
// Check for containers that have been removed or exited
for (const containerId of Object.keys(previousHostContainers)) {
const container = previousHostContainers[containerId];
if (!currentRunningContainers[containerId]) {
if (container.state === 'running') {
// Container removed
exec(`bash ./scripts/notify.sh REMOVE ${containerId} ${container.name} ${hostName} ${container.state}`, (error, stdout, stderr) => {
if (error) {
logger.error(`Error executing REMOVE notify.sh: ${error.message}`);
} else {
logger.info(`Container removed: ${container.name} (${containerId}) from host ${hostName}`);
logger.info(stdout);
}
});
}
else if (container.state === 'exited') {
// Container exited
exec(`bash ./scripts/notify.sh EXIT ${containerId} ${container.name} ${hostName} ${container.state}`, (error, stdout, stderr) => {
if (error) {
logger.error(`Error executing EXIT notify.sh: ${error.message}`);
} else {
logger.info(`Container exited: ${container.name} (${containerId}) from host ${hostName}`);
logger.info(stdout);
}
});
}
}
}
// Check for new containers or state changes
for (const containerId of Object.keys(currentRunningContainers)) {
const container = currentRunningContainers[containerId];
const previousContainer = previousHostContainers[containerId];
if (!previousContainer) {
// New container added
exec(`bash ./scripts/notify.sh ADD ${containerId} ${container.name} ${hostName} ${container.state}`, (error, stdout, stderr) => {
if (error) {
logger.error(`Error executing ADD notify.sh: ${error.message}`);
} else {
logger.info(`Container added: ${container.name} (${containerId}) to host ${hostName}`);
logger.info(stdout);
}
});
} else if (previousContainer.state !== container.state) {
// Container state has changed
const newState = container.state;
if (newState === 'exited') {
exec(`bash ./scripts/notify.sh EXIT ${containerId} ${container.name} ${hostName} ${newState}`, (error, stdout, stderr) => {
if (error) {
logger.error(`Error executing EXIT notify.sh: ${error.message}`);
} else {
logger.info(`Container exited: ${container.name} (${containerId}) from host ${hostName}`);
logger.info(stdout);
}
});
} else {
// Any other state change
exec(`bash ./scripts/notify.sh ANY ${containerId} ${container.name} ${hostName} ${newState}`, (error, stdout, stderr) => {
if (error) {
logger.error(`Error executing ANY notify.sh: ${error.message}`);
} else {
logger.info(`Container state changed to ${newState}: ${container.name} (${containerId}) from host ${hostName}`);
logger.info(stdout);
}
});
}
}
}
// Update the previous state for the next comparison
previousRunningContainers[hostName] = currentRunningContainers;
}
async function queryHostStats(hostName, hostConfig) {
logger.debug(`Querying Docker stats for host: ${hostName} (${hostConfig.url}:${hostConfig.port})`);
const docker = createDockerClient(hostConfig);
try {
const info = await docker.info();
const totalMemory = info.MemTotal;
const totalCPUs = info.NCPU;
const containers = await docker.listContainers({ all: true });
const statsPromises = containers.map(async (container) => {
try {
const containerName = container.Names[0].replace('/', '');
const containerState = container.State;
const updateAvailableFlag = await updateAvailable(container.Image, cupUrl);
let networkMode = container.HostConfig.NetworkMode;
// Check if network mode is in the format "container:IDXXXXXXXX"
if (networkMode.startsWith("container:")) {
const linkedContainerId = networkMode.split(":")[1];
const linkedContainer = await docker.getContainer(linkedContainerId).inspect();
const linkedContainerName = linkedContainer.Name.replace('/', ''); // Remove leading slash
networkMode = `Container: ${linkedContainerName}`; // Format the network mode
}
if (containerState !== 'running') {
previousContainerStates[container.Id] = containerState;
return {
name: containerName,
id: container.Id,
hostName: hostName,
state: containerState,
image: container.Image,
update_available: updateAvailableFlag || false,
cpu_usage: 0,
mem_usage: 0,
mem_limit: 0,
net_rx: 0,
net_tx: 0,
current_net_rx: 0,
current_net_tx: 0,
networkMode: networkMode,
link: containerConfigs[containerName]?.link || '',
icon: containerConfigs[containerName]?.icon || '',
tags: getTagColor(containerConfigs[containerName]?.tags || ''),
};
}
// Fetch container stats for running containers
const containerStats = await getContainerStats(docker, container.Id);
const containerCpuUsage = containerStats.cpu_stats.cpu_usage.total_usage;
const containerMemoryUsage = containerStats.memory_stats.usage;
let netRx = 0, netTx = 0, currentNetRx = 0, currentNetTx = 0;
if (networkMode !== 'host' && containerStats.networks?.eth0) {
const previousStats = previousNetworkStats[container.Id] || { rx_bytes: 0, tx_bytes: 0 };
currentNetRx = containerStats.networks.eth0.rx_bytes - previousStats.rx_bytes;
currentNetTx = containerStats.networks.eth0.tx_bytes - previousStats.tx_bytes;
previousNetworkStats[container.Id] = {
rx_bytes: containerStats.networks.eth0.rx_bytes,
tx_bytes: containerStats.networks.eth0.tx_bytes,
};
netRx = containerStats.networks.eth0.rx_bytes;
netTx = containerStats.networks.eth0.tx_bytes;
}
previousContainerStates[container.Id] = containerState;
const config = containerConfigs[containerName] || {};
const tagArray = (config.tags || '')
.split(',')
.map(tag => {
const color = getTagColor(tag);
return color ? `${tag}:${color}` : tag;
})
.join(',');
return {
name: containerName,
id: container.Id,
hostName: hostName,
image: container.Image,
update_available: updateAvailableFlag || false,
state: containerState,
cpu_usage: containerCpuUsage,
mem_usage: containerMemoryUsage,
mem_limit: containerStats.memory_stats.limit,
net_rx: netRx,
net_tx: netTx,
current_net_rx: currentNetRx,
current_net_tx: currentNetTx,
networkMode: networkMode,
link: config.link || '',
icon: config.icon || '',
tags: tagArray,
};
} catch (err) {
logger.error(`Failed to fetch stats for container ${container.Names[0]} (${container.Id}): ${err.message}`);
return null;
}
});
const hostStats = await Promise.all(statsPromises);
const validStats = hostStats.filter(stat => stat !== null);
const totalCpuUsage = validStats.reduce((acc, container) => acc + parseFloat(container.cpu_usage), 0);
const totalMemoryUsage = validStats.reduce((acc, container) => acc + container.mem_usage, 0);
const memoryUsagePercent = ((totalMemoryUsage / totalMemory) * 100).toFixed(2);
generalStats[hostName] = {
containerCount: validStats.length,
totalCPUs: totalCPUs,
totalMemory: totalMemory,
cpuUsage: totalCpuUsage,
memoryUsage: memoryUsagePercent,
};
latestStats[hostName] = validStats;
logger.debug(`Fetched stats for ${validStats.length} containers from ${hostName}`);
// Handle container state changes
await handleContainerStateChanges(hostName, validStats);
} catch (err) {
logger.error(`Failed to fetch containers from ${hostName}: ${err.message}`);
}
}
async function handleHostQueue(hostName, hostConfig) {
while (true) {
await queryHostStats(hostName, hostConfig);
await new Promise(resolve => setTimeout(resolve, queryInterval));
}
}
// Initialize the host queues
function initializeHostQueues() {
for (const [hostName, hostConfig] of Object.entries(hosts)) {
hostQueues[hostName] = handleHostQueue(hostName, hostConfig);
}
}
// Dynamically reloads the yaml file
function reloadConfig() {
for (const hostName in hostQueues) {
hostQueues[hostName] = null;
}
try {
config = yaml.load('./config/hosts.yaml');
hosts = config.hosts;
containerConfigs = config.container || {};
maxlogsize = config.log.logsize || 1;
LogAmount = config.log.LogCount || 5;
queryInterval = config.mintimeout || 5000;
logger.info('Configuration reloaded successfully.');
initializeHostQueues();
} catch (err) {
logger.error(`Failed to reload configuration: ${err.message}`);
}
}
// Watch the YAML file for changes and reload the config
fs.watchFile('./config/hosts.yaml', (curr, prev) => {
if (curr.mtime !== prev.mtime) {
logger.info('Detected change in configuration file. Reloading...');
reloadConfig();
}
});
// Endpoint to get stats
app.get('/stats', authenticateHeader, (req, res) => {
res.json(latestStats);
});
// Endpoint for general Host based statistics
app.get('/hosts', authenticateHeader, (req, res) => {
res.json(generalStats);
});
// Read Only config endpoint
app.get('/config', authenticateHeader, (req, res) => {
const filePath = path.join(__dirname, './config/hosts.yaml');
res.set('Content-Type', 'text/plain'); // Keep as plain text
fs.readFile(filePath, 'utf8', (err, data) => {
logger.debug('Requested config file: ' + filePath);
if (err) {
logger.error(err);
res.status(500).send('Error reading file');
} else {
res.send(data);
}
});
});
app.get('/', (req, res) => {
res.redirect(301, '/stats');
});
app.get('/status', (req, res) => {
logger.info("Healthcheck requested");
return res.status(200).send('UP');
});
// Start the server and log the startup message
app.listen(port, () => {
logger.info('=============================== DockStat ===============================')
logger.info(`DockStatAPI is running on http://localhost:${port}/stats`);
logger.info(`Minimum timeout between stats queries is: ${queryInterval} milliseconds`);
logger.info(`The max size for Log files is: ${maxlogsize}MB`)
logger.info(`The amount of log files to keep is: ${LogAmount}`);
logger.info(`Secret Key: ${key}`)
logger.info(`Cup URL: ${cupUrl}`)
logger.info("Press Ctrl+C to stop the server.");
logger.info('========================================================================')
});
initializeHostQueues();