commit d97f2ae7b4d2a6f23caac05f3f1b1de32f21efda Author: root Date: Tue May 2 21:15:26 2023 +0200 File Upload diff --git a/package.json b/package.json new file mode 100644 index 0000000..de2ac3a --- /dev/null +++ b/package.json @@ -0,0 +1,8 @@ +{ + "dependencies": { + "diskusage": "^1.1.3", + "express": "^4.18.2", + "pm2": "^5.2.2", + "systeminformation": "^5.17.1" + } +} diff --git a/server_stats.js b/server_stats.js new file mode 100644 index 0000000..5ee1ead --- /dev/null +++ b/server_stats.js @@ -0,0 +1,54 @@ +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}/`); +})