Initial commit
This commit is contained in:
170
hosts.js
Normal file
170
hosts.js
Normal file
@@ -0,0 +1,170 @@
|
||||
const axios = require("axios");
|
||||
const shared = require("./shared.js");
|
||||
let hostIDs = [];
|
||||
var monitorInterval;
|
||||
|
||||
async function Monitoring() {
|
||||
await initHostList();
|
||||
console.log("\nInizio monitoring");
|
||||
//monitora tutti gli host della lista
|
||||
shared.eventEmitter.on("tokenRenewalStart", () => {
|
||||
clearInterval(monitorInterval);
|
||||
});
|
||||
shared.eventEmitter.on("tokenRenewalEnd", () => {
|
||||
monitorInterval = setInterval(Request, minute - 1);
|
||||
});
|
||||
monitorInterval = setInterval(Request, minute - 1);
|
||||
}
|
||||
|
||||
async function Request() {
|
||||
shared.setHosts([]);
|
||||
var hosts = [];
|
||||
for (const element of hostIDs) {
|
||||
var obj = {
|
||||
id: element,
|
||||
isConnected: await isConnected(element),
|
||||
};
|
||||
hosts.push(obj);
|
||||
}
|
||||
shared.setHosts(hosts);
|
||||
}
|
||||
|
||||
async function isConnected(hostID) {
|
||||
const response = await axios.get(
|
||||
`https://api.netbird.io/api/peers/${hostID}`,
|
||||
{
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Authorization: `Token ${shared.getToken()}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
console.log(`${hostID} connected: ${response.data.connected}`);
|
||||
return response.data.connected;
|
||||
}
|
||||
|
||||
async function initHostList() {
|
||||
if ("HOSTNAMES" in process.env) {
|
||||
let envHostnames = process.env.HOSTNAMES;
|
||||
|
||||
if (
|
||||
envHostnames !== null &&
|
||||
envHostnames !== undefined &&
|
||||
envHostnames !== ""
|
||||
) {
|
||||
if (envHostnames.includes(";")) {
|
||||
envHostnames = envHostnames.split(";");
|
||||
|
||||
for (const host of envHostnames) {
|
||||
try {
|
||||
const response = await axios.get(
|
||||
`https://api.netbird.io/api/peers?name=${host}`,
|
||||
{
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Authorization: `Token ${shared.getToken()}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
response.data.forEach((host) => {
|
||||
hostIDs.push(host.id);
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(
|
||||
"Errore nella verifica e aggiunta al monitoraggio di un host."
|
||||
);
|
||||
process.exit(3);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
const response = await axios.get(
|
||||
`https://api.netbird.io/api/peers?name=${envHostnames}`,
|
||||
{
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Authorization: `Token ${shared.getToken()}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
hostIDs.push(response.data[0].id);
|
||||
} catch (error) {
|
||||
console.log(
|
||||
"Errore nella verifica e aggiunta al monitoraggio di un host."
|
||||
);
|
||||
process.exit(3);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if ("HOSTIDS" in process.env) {
|
||||
let envHostIDs = process.env.HOSTIDS;
|
||||
|
||||
if (envHostIDs !== null && envHostIDs !== undefined && envHostIDs !== "") {
|
||||
if (envHostIDs.includes(";")) {
|
||||
envHostIDs = envHostIDs.split(";");
|
||||
|
||||
for (const host of envHostIDs) {
|
||||
try {
|
||||
const response = await axios.get(
|
||||
`https://api.netbird.io/api/peers/${host}`,
|
||||
{
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Authorization: `Token ${shared.getToken()}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
hostIDs.push(response.data.id);
|
||||
} catch (error) {
|
||||
console.log(
|
||||
"Errore nella verifica e aggiunta al monitoraggio di un host."
|
||||
);
|
||||
process.exit(3);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
const response = await axios.get(
|
||||
`https://api.netbird.io/api/peers/${envHostIDs}`,
|
||||
{
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Authorization: `Token ${shared.getToken()}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
hostIDs.push(response.data.id);
|
||||
} catch (error) {
|
||||
console.log(
|
||||
"Errore nella verifica e aggiunta al monitoraggio di un host."
|
||||
);
|
||||
process.exit(3);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log(
|
||||
"Nessun peer specificato, verranno monitorati tutti i peer disponibili"
|
||||
);
|
||||
try {
|
||||
const response = await axios.get(`https://api.netbird.io/api/peers`, {
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Authorization: `Token ${shared.getToken()}`,
|
||||
},
|
||||
});
|
||||
response.data.forEach((element) => {
|
||||
hostIDs.push(element.id);
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
console.log("ID rilevati:");
|
||||
hostIDs.forEach((element) => {
|
||||
console.log(element);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { Monitoring };
|
||||
Reference in New Issue
Block a user