NodeServerStats/server_stats.js

55 lines
1.3 KiB
JavaScript

const express = require('express');
const os = require('os');
const app = express();
const diskusage = require('diskusage');
const si = require('systeminformation');
const hostname = "localhost";
const port = "3001";
async function getNetworkStats() {
try {
const data = await si.networkStats("eno1");
return data;
} catch (error) {
console.error(error);
return {};
}
}
async function getCurrentCPULoad() {
try {
const data = await si.currentLoad();
const { currentLoad } = data;
return currentLoad;
} catch (error) {
console.log(error);
return {};
}
}
app.get('/server-stats/', (req, res) => {
const cpuServer = os.cpus()[0].model;
const totalRam = (os.totalmem() / 1024 ** 3);
const ramUsage = (os.totalmem() - os.freemem()) / os.totalmem();
diskusage.check('/', async (err, result) => {
const totalHddSpace = (result.total / 1024 ** 3);
const freeHddSpace = (result.available / 1024 ** 3);
const networkStats = await getNetworkStats();
const cpuUsage = await getCurrentCPULoad();
res.json({
cpuServer,
cpuUsage,
ramUsage,
totalRam,
totalHddSpace,
freeHddSpace,
networkStats
});
});
})
app.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
})