const express = require('express'); const os = require('os'); const app = express(); const diskusage = require('diskusage'); const si = require('systeminformation'); const axios = require('axios'); // Configuration for the server const hostname = "localhost"; const port = "3001"; // 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 {}; } } async function getNetworkStats() { try { return await si.networkStats("eno1"); } 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(); const nginxConnections = await getNginxConnections(); res.json({ cpuServer, cpuUsage, ramUsage, totalRam, totalHddSpace, freeHddSpace, nginxConnections, networkStats }); }); }) app.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); })