NodeServerStats/server_stats.js

72 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-05-02 19:15:26 +00:00
const express = require('express');
const os = require('os');
const app = express();
const diskusage = require('diskusage');
const si = require('systeminformation');
2024-01-07 13:28:21 +00:00
const axios = require('axios');
// Configuration for the server
2023-05-02 19:15:26 +00:00
const hostname = "localhost";
const port = "3001";
2024-01-07 13:28:21 +00:00
// Get the number of active connections to Nginx
async function getNginxConnections() {
try {
const response = await axios.get('https://cdn00.lordchannel.com/nginx_stats');
const { data } = response;
const activeConnectionsLine = data.substring(0, data.indexOf('\n'));
return activeConnectionsLine.substring(activeConnectionsLine.indexOf(':') + 1).trim();
} catch (error) {
console.error(error);
return {};
}
}
2023-05-02 19:15:26 +00:00
async function getNetworkStats() {
try {
2024-01-07 13:28:21 +00:00
return await si.networkStats("eno1");
2023-05-02 19:15:26 +00:00
} 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();
2024-01-07 13:28:21 +00:00
const nginxConnections = await getNginxConnections();
2023-05-02 19:15:26 +00:00
res.json({
cpuServer,
cpuUsage,
ramUsage,
totalRam,
totalHddSpace,
freeHddSpace,
2024-01-07 13:28:21 +00:00
nginxConnections,
2023-05-02 19:15:26 +00:00
networkStats
});
});
})
app.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
})