-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
231 lines (201 loc) · 6.45 KB
/
app.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
// see https://github.com/mu-semtech/mu-javascript-template for more info
import { app, query, errorHandler } from 'mu';
import http from 'http';
import request from 'request';
class MonitoredContainer {
/**
* URI which identifies the container.
*/
uri;
/**
* Docker ID of the container.
*/
dockerId;
/**
* The name of the container for enriching the log.
*/
name;
/**
* The name of the project for enriching the log.
*/
project;
/**
* Date indicating when this container was last queried for changes.
*/
lastScanAt;
/**
* JSON object containing information about the last scan.
*
* This entity is used to calculate differences between different scans.
*/
lastScanContent;
constructor( options ) {
for( const key in options ) {
this[key] = options[key];
}
}
}
/**
* Contains the list of all containers which should be monitored.
*/
let monitoredContainers = [];
updateMonitoredContainers();
setInterval(fetchContainerStats, 10000);
/**
* Delta messages endpoint.
*
* Upon receiving a delta, we fetch the new statusus of the
* containers. This should be sufficient for the vast majority of
* cases. Only situation to cater for still, is a crashing service
* that auto-restarts.
*/
app.post("/.mu/delta", async (_req, res) => {
await updateMonitoredContainers();
res.sendStatus(204);
});
// DONE: Query for containers to watch on boot
// DONE: Inspect incoming delta changes to refetch list of servers to monitor
// DONE: Build inspection loop to fetch container information
/**
* Updates the monitored containers.
*
* Assumes the global variable `monitoredContainers` can be set.
*/
async function updateMonitoredContainers() {
// first query the database to see if it is up.
try {
await query(`SELECT * WHERE { ?s ?P ?o. } LIMIT 1`);
// get a list of all running containers
const dbContainers =
(await query(
`PREFIX docker: <https://w3.org/ns/bde/docker#>
SELECT DISTINCT ?uri ?dockerId ?name WHERE {
?uri a docker:Container;
docker:id ?dockerId;
docker:name ?name;
docker:state/docker:status "running";
docker:label/docker:key "logging".
}`))
.results
.bindings;
for (const dbContainer of dbContainers) {
const result = await query(
`PREFIX docker: <https://w3.org/ns/bde/docker#>
SELECT ?project WHERE {
<${dbContainer.uri.value}> docker:label ?label .
?label docker:key "com.docker.compose.project";
docker:value ?project.
} LIMIT 1`);
if (result.results.bindings.length) {
dbContainer.project = result.results.bindings[0].project;
}
}
// filter out elements in the current array which don't exist anymore
let monitoredContainersCopy = [...monitoredContainers];
monitoredContainersCopy =
monitoredContainersCopy
.filter( (container) => {
const foundContainer = dbContainers.find( (binding) => binding.uri.value == container.uri );
if( foundContainer )
return true;
else {
return false;
}
});
// add new elements to the array
let newContainers =
dbContainers
.filter( (bindings) =>
{
const foundContainer = monitoredContainersCopy.find( (container) =>
container.uri == bindings.uri.value );
if( foundContainer )
return false;
else {
return true;
}
} )
.map( (bindings) =>
new MonitoredContainer( {
uri: bindings.uri.value,
dockerId: bindings.dockerId.value,
name: bindings.name.value,
project: bindings.project.value
} ) );
monitoredContainers = [...monitoredContainersCopy, ...newContainers];
} catch (e) {
// could not fetch containers, retrying in a moment
console.log("SPARQL endpoint does not seem to be up yet, retrying in 2500ms");
setTimeout( updateMonitoredContainers, 2500 );
}
}
async function fetchContainerStats() {
// console.log(`Fetching stats for ${monitoredContainers.length} containers.`);
monitoredContainers.forEach( async (container) => {
// Get new stats from backend
const req = http.request({
socketPath: "/var/run/docker.sock",
path: `http:/v1.24/containers/${container.dockerId}/stats?stream=false`
}, (req) => {
let data = "";
req
.on('data', (d) => data += d )
.on('end', async () => {
// Parse the data from the stats instance
const newData = cleanupData(JSON.parse( data ), container);
const oldData = container.lastScanContent;
if( newData.message ) {
// could not find the container, most likely.
return;
}
// Enrich with relative numbers
if( oldData ) {
// TODO: add diffs to the newData
}
// Update stats in monitored container
container.lastScanContent = newData;
// Store stats through logstash
try {
request({
url: "http://logstash:8080/",
method: "POST",
json: true,
body: newData
}, (error, _response, _body) => {
if( error ) {
console.error(`Error whilst sending content to logstash: ${error}`);
} else {
// console.log(`Sent stats for ${container.project} / ${container.name} / ${container.dockerId}`);
}
});
} catch (e) {
console.error(`Error whilst sending content to logstash: ${e}`);
}
});
});
req.end();
});
}
/**
* Cleans up data received from the stats event.
*/
function cleanupData( data, container ) {
const storedInfo = {};
storedInfo.created = data.read;
storedInfo.fields = {
compose_project: container.project,
compose_service: container.name
};
if( data.Labels ) {
storedInfo.project = data.Labels["com.docker.compose.project"];
storedInfo.service = data.Labels["com.docker.compose.service"];
}
storedInfo.network = data.network || {};
storedInfo.io = data.blkio_stats;
storedInfo.cpu = data.cpu_stats;
storedInfo.procs = data.num_procs;
storedInfo.mem = data.memory_stats;
storedInfo.net = data.networks;
return storedInfo;
}
app.use(errorHandler);