Version 1.0.0 - Ajout de metrics
This commit is contained in:
parent
33bd37aacd
commit
c0ce8b9338
3
config.json
Normal file
3
config.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"AUTH_KEY": "0"
|
||||
}
|
60
main.js
Normal file
60
main.js
Normal file
@ -0,0 +1,60 @@
|
||||
const http = require('http');
|
||||
const url = require('url');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const osutils = require('os-utils');
|
||||
const {statfs} = require('fs');
|
||||
|
||||
const server = http.createServer(async (req, res) => {
|
||||
const parsedUrl = url.parse(req.url, true);
|
||||
const query = parsedUrl.query;
|
||||
|
||||
if (parsedUrl.pathname === '/metrics' && query.key) {
|
||||
const config = JSON.parse(fs.readFileSync('config.json'));
|
||||
if (query.key === config.AUTH_KEY) {
|
||||
var metrics = await getMetrics()
|
||||
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(JSON.stringify(metrics));
|
||||
} else {
|
||||
res.statusCode = 401;
|
||||
res.end('Unauthorized');
|
||||
}
|
||||
} else {
|
||||
res.statusCode = 404;
|
||||
res.end('Not Found');
|
||||
}
|
||||
});
|
||||
|
||||
server.listen(4000, () => {
|
||||
console.log('Server is running on port 4000');
|
||||
});
|
||||
|
||||
|
||||
function getMetrics() {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const space = statfs("/", (err, stats) => {
|
||||
if (err) {
|
||||
throw err
|
||||
}
|
||||
|
||||
var resp = null
|
||||
|
||||
osutils.cpuUsage(function(cpuUsage) {
|
||||
resp = {
|
||||
cpu: Math.round(cpuUsage * 1000),
|
||||
usedram: osutils.totalmem() - osutils.freemem(),
|
||||
totalram: osutils.totalmem(),
|
||||
usedisk: stats.blocks - stats.bfree,
|
||||
totaldisk: stats.blocks
|
||||
}
|
||||
|
||||
resolve(resp)
|
||||
|
||||
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
}
|
24
package-lock.json
generated
Normal file
24
package-lock.json
generated
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "neutral-link",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "neutral-link",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"os-utils": "^0.0.14"
|
||||
}
|
||||
},
|
||||
"node_modules/os-utils": {
|
||||
"version": "0.0.14",
|
||||
"resolved": "https://registry.npmjs.org/os-utils/-/os-utils-0.0.14.tgz",
|
||||
"integrity": "sha512-ajB8csaHLBvJOYsHJkp8YdO2FvlBbf/ZxaYQwXXRDyQ84UoE+uTuLXxqd0shekXMX6Qr/pt/DDyLMRAMsgfWzg==",
|
||||
"engines": {
|
||||
"node": "*"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
15
package.json
Normal file
15
package.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "neutral-link",
|
||||
"version": "1.0.0",
|
||||
"description": "Lien vers Neutral",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "node main.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"os-utils": "^0.0.14"
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user