-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (80 loc) · 2.66 KB
/
index.js
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
const winston = require("winston");
const { format } = winston;
const fs = require("fs");
const path = require("path");
const LOG_DIR = "/var/log/basement-agent";
const homedir = require("os").homedir();
const ETC_DIR = path.join(homedir, ".config/basement");
const CONFIG_PATH = path.join(homedir, ".config/basement/config.json");
const SONAR_HOST = (process.env.API_HOST ? process.env.API_HOST : "http://basement.test") + "/api/metrics/";
const si = require("systeminformation");
const axios = require("axios");
const io = require("socket.io")(4000, { origins: "*:*" });
io.origins((origin, callback) => {
callback(null, true);
});
if (!fs.existsSync(ETC_DIR) || !fs.existsSync(CONFIG_PATH)) {
if (!process.env.API_TOKEN) {
console.error("You MUST run this command with the API_TOKEN env set on the first run!");
console.error("API_TOKEN=YOUR_API_TOKEN node index.js");
process.exit(1);
}
try {
fs.mkdirSync(ETC_DIR);
} catch (e) {}
let config = {
uuid: require("uuid/v4")(),
token: process.env.API_TOKEN,
};
try {
fs.writeFileSync(CONFIG_PATH, JSON.stringify(config));
} catch (e) {}
axios
.post(
SONAR_HOST + "register",
{ uuid: config.uuid },
{
headers: {
Authorization: "Bearer " + config.token,
},
}
)
.then(({ data }) => {
console.log(data);
})
.catch(({ response: { data } }) => {
console.log(data);
});
}
try {
fs.mkdirSync(LOG_DIR);
} catch (e) {}
const config = require(CONFIG_PATH);
const cpuProvider = require("./Providers/CPUProvider");
const ramProvider = require("./Providers/MemoryProvider");
const diskProvider = require("./Providers/DiskProvider");
const load = (provider, name, accessor) => () => {
provider
.loadData()
.then(data => {
io.sockets.in("server." + config.uuid).emit(name, accessor ? data[accessor] : data);
})
.catch(e => console.error(e));
};
const getRamData = load(ramProvider, "ram");
const getCpuData = load(cpuProvider, "cpu", "currentload");
const getDiskData = load(diskProvider, "disk");
io.on("connection", function(socket) {
socket.join("server." + config.uuid);
let cpuInterval = setInterval(getCpuData, 2500);
let ramInterval = setInterval(getRamData, 2500);
let diskInterval = setInterval(getDiskData, 2500);
getRamData();
getCpuData();
getDiskData();
socket.on("disconnect", () => {
clearInterval(ramInterval);
clearInterval(cpuInterval);
clearInterval(diskInterval);
});
});