aboutsummaryrefslogtreecommitdiff
path: root/refresh/servers.js
diff options
context:
space:
mode:
authorMinteck <contact@minteck.org>2022-05-24 23:06:46 +0200
committerMinteck <contact@minteck.org>2022-05-24 23:06:46 +0200
commit94fea37d627a7f11a67d4eb472300dfa5abaafae (patch)
tree1729703dbc173ec7ffd574aacb24434d4cb96e38 /refresh/servers.js
parenta06bd771ff4ea43841edb8cba97df21258a9912b (diff)
downloadvaportrail-94fea37d627a7f11a67d4eb472300dfa5abaafae.tar.gz
vaportrail-94fea37d627a7f11a67d4eb472300dfa5abaafae.tar.bz2
vaportrail-94fea37d627a7f11a67d4eb472300dfa5abaafae.zip
Pair programming with @CloudburstSys
Diffstat (limited to 'refresh/servers.js')
-rw-r--r--refresh/servers.js112
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);
+ }
+ });
+ }
+ });
+}