diff options
author | CloudburstSys <us@conep.one> | 2022-05-25 13:58:02 +0100 |
---|---|---|
committer | CloudburstSys <us@conep.one> | 2022-05-25 13:58:02 +0100 |
commit | 6d06d9a96bb089254aa89dae0ae49f73c8d8bfe4 (patch) | |
tree | b7d45eeaea7e2476d0b43778a3f3cd36973965c4 /refresh/servers.js | |
parent | 94fea37d627a7f11a67d4eb472300dfa5abaafae (diff) | |
download | vaportrail-6d06d9a96bb089254aa89dae0ae49f73c8d8bfe4.tar.gz vaportrail-6d06d9a96bb089254aa89dae0ae49f73c8d8bfe4.tar.bz2 vaportrail-6d06d9a96bb089254aa89dae0ae49f73c8d8bfe4.zip |
Create /servers page
Diffstat (limited to 'refresh/servers.js')
-rw-r--r-- | refresh/servers.js | 56 |
1 files changed, 42 insertions, 14 deletions
diff --git a/refresh/servers.js b/refresh/servers.js index e6b7d25..8bbdbd5 100644 --- a/refresh/servers.js +++ b/refresh/servers.js @@ -45,6 +45,28 @@ function roundToTwo(num) { return +(Math.round(num + "e+2") + "e-2"); } +function createReadableUptime(timestamp) { + let minutes = Math.floor(timestamp / 1000 / 60 % 60); + let hours = Math.floor(timestamp / 1000 / 60 / 60 % 24); + let days = Math.floor(timestamp / 1000 / 60 / 60 / 24); + + let daysString = " Day"; + if(days == 0) daysString = ""; + if(days > 1) daysString = daysString + "s"; + if(days != 0 && hours != 0) daysString = days + daysString + ", "; + + let hoursString = " Hour"; + if(hours == 0) hoursString = ""; + if(hours > 1) hoursString = hoursString + "s"; + if(hours != 0) hoursString = hours + hoursString + ", and "; + + let minutesString = " Minute"; + if(minutes == 0) minutesString = ""; + if(minutes > 1) minutesString = minutes + minutesString + "s"; + + return daysString + hoursString + minutesString; +} + module.exports = () => { // Code entered here will be run every 5 minutes return new Promise(async (res) => { @@ -61,9 +83,10 @@ module.exports = () => { for (let key in Object.keys(servers)) { key = Object.keys(servers)[key]; let status = { - online: null, cpu: null, - memory: null, + processes: null, + swapmemory: null, + physicalmemory: null, os: null, uptime: null } @@ -74,28 +97,33 @@ module.exports = () => { }) .then(data => data = data.body) .then(data => { - status.online = true; status.cpu = data.cpu; - status.cpu.load = Math.floor(data.cpu.load * 100); - status.memory = data.memory; - status.memory.swap.used = roundToTwo(data.memory.swap.used / 1073741824); - status.memory.swap.free = roundToTwo(data.memory.swap.free / 1073741824); - status.memory.swap.total = roundToTwo(data.memory.swap.total / 1073741824); - status.memory.physical.used = roundToTwo(data.memory.physical.used / 1073741824); - status.memory.physical.free = roundToTwo(data.memory.physical.free / 1073741824); - status.memory.physical.total = roundToTwo(data.memory.physical.total / 1073741824); + status.cpu.speed = data.cpu.speed + "GHz"; + status.cpu.load = Math.floor(data.cpu.load * 100) + "%"; + if(data.cpu.temperature != null) + status.cpu.temperature = data.cpu.temperature + "°C"; + status.processes = data.processes; + status.swapmemory = data.swapmemory; + status.swapmemory.used = roundToTwo(data.swapmemory.used / 1073741824) + "GB"; + status.swapmemory.free = roundToTwo(data.swapmemory.free / 1073741824) + "GB"; + status.swapmemory.total = roundToTwo(data.swapmemory.total / 1073741824) + "GB"; + status.physicalmemory = data.physicalmemory; + status.physicalmemory.used = roundToTwo(data.physicalmemory.used / 1073741824) + "GB"; + status.physicalmemory.free = roundToTwo(data.physicalmemory.free / 1073741824) + "GB"; + status.physicalmemory.total = roundToTwo(data.physicalmemory.total / 1073741824) + "GB"; status.os = data.os; - status.uptime = data.uptime; + status.uptime = createReadableUptime(data.uptime); }) .catch(reason => { if (reason.timeout) { // We timed out. - status.online = false; + status = null; } else { if(reason.code === "ECONNREFUSED") { - status.online = false; + status = null; console.warn(`Server ${key} refused our connection. `) } else { + status = null; console.error(reason); } } |