aboutsummaryrefslogtreecommitdiff
path: root/refresh/servers.js
blob: e6b7d25260134b8b78a2a8508900dc2254b4b1ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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);
                    }
                });
        }
    });
}