diff options
Diffstat (limited to 'refresh/servers.js')
-rw-r--r-- | refresh/servers.js | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/refresh/servers.js b/refresh/servers.js new file mode 100644 index 0000000..e6b7d25 --- /dev/null +++ b/refresh/servers.js @@ -0,0 +1,112 @@ +/* + * MIT License + * + * Copyright (c) 2022- Equestria.dev Developers + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +const dns = require("dns"); +const superagent = require("superagent"); + +function resolveDNSAsync(domain) { + return new Promise((res, rej) => { + if (!/([\--z]+\.)?([\--z]+)\.([A-z]+)/g.test(domain)) return rej("Not a valid domain"); + + const options = { + family: 4, + hints: dns.ADDRCONFIG | dns.V4MAPPED + }; + dns.lookup(domain, options, (err, address) => { + if (err) return rej(err); + res(address); + }); + }); +} + +function roundToTwo(num) { + return +(Math.round(num + "e+2") + "e-2"); +} + +module.exports = () => { + // Code entered here will be run every 5 minutes + return new Promise(async (res) => { + let servers = { + canterlot: [await resolveDNSAsync("canterlot.equestria.dev"), 52937], + ponyville: [await resolveDNSAsync("ponyville-ipv4.equestria.dev"), 52937], + bridlewood: [await resolveDNSAsync("bridlewood.equestria.dev"), 52937], + zephyrheights: [await resolveDNSAsync("zephyrheights-ipv4.equestria.dev"), 52938], + maretimebay: [await resolveDNSAsync("maretimebay-ipv4.equestria.dev"), 52937] + } + + let stats = {} + + for (let key in Object.keys(servers)) { + key = Object.keys(servers)[key]; + let status = { + online: null, + cpu: null, + memory: null, + os: null, + uptime: null + } + + superagent.get(`http://${servers[key][0]}:${servers[key][1]}/json`) + .timeout({ + response: 5000, + }) + .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.os = data.os; + status.uptime = data.uptime; + }) + .catch(reason => { + if (reason.timeout) { + // We timed out. + status.online = false; + } else { + if(reason.code === "ECONNREFUSED") { + status.online = false; + console.warn(`Server ${key} refused our connection. `) + } else { + console.error(reason); + } + } + }) + .finally(() => { + stats[key] = status; + + if(Object.keys(stats).length === Object.keys(servers).length) { + res(stats); + } + }); + } + }); +} |