Nuova gestion errori
This commit is contained in:
284
hosts.js
284
hosts.js
@@ -30,29 +30,47 @@ async function Request() {
|
||||
}
|
||||
|
||||
async function isConnected(hostID) {
|
||||
try {
|
||||
const response = await axios.get(
|
||||
`https://api.netbird.io/api/peers/${hostID}`,
|
||||
{
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Authorization: `Token ${shared.getToken()}`,
|
||||
},
|
||||
let loopError;
|
||||
let errorCount = 0;
|
||||
do {
|
||||
try {
|
||||
const response = await axios.get(
|
||||
`https://api.netbird.io/api/peers/${hostID}`,
|
||||
{
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Authorization: `Token ${shared.getToken()}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
Log(`[${shared.now()}] ${hostID} connected: ${response.data.connected}`);
|
||||
return response.data.connected;
|
||||
} catch (error) {
|
||||
loopError = true;
|
||||
Log(
|
||||
`[${shared.now()}] Errore nella verifica dello stato per l'host ${hostID}`
|
||||
);
|
||||
Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`);
|
||||
Log(`[${shared.now()}] Debug: ${error}`);
|
||||
for (const codice of shared.codiciNonAccettati) {
|
||||
if (error.response?.status === codice) {
|
||||
Log(
|
||||
`[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
|
||||
);
|
||||
Log(
|
||||
`[${shared.now()}] Per favore riavviare il programma con un token valido.`
|
||||
);
|
||||
process.exit(3);
|
||||
}
|
||||
}
|
||||
);
|
||||
Log(`[${shared.now()}] ${hostID} connected: ${response.data.connected}`);
|
||||
return response.data.connected;
|
||||
} catch (error) {
|
||||
Log(
|
||||
`[${shared.now()}] Errore nella verifica dello stato per l'host ${hostID}`
|
||||
);
|
||||
Log(`[${shared.now()}] ${error}`);
|
||||
Log(`[${shared.now()}] Debug: ${error.stack}`);
|
||||
process.exit(3);
|
||||
}
|
||||
Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
|
||||
}
|
||||
} while (loopError);
|
||||
}
|
||||
|
||||
async function initHostList() {
|
||||
let loopError;
|
||||
let errorCount = 0;
|
||||
if ("HOSTNAMES" in process.env) {
|
||||
let envHostnames = process.env.HOSTNAMES;
|
||||
|
||||
@@ -65,9 +83,49 @@ async function initHostList() {
|
||||
envHostnames = envHostnames.split(";");
|
||||
|
||||
for (const host of envHostnames) {
|
||||
do {
|
||||
loopError = false;
|
||||
try {
|
||||
const response = await axios.get(
|
||||
`https://api.netbird.io/api/peers?name=${host}`,
|
||||
{
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Authorization: `Token ${shared.getToken()}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
for (const host of response.data) {
|
||||
hostIDs.push(host.id);
|
||||
}
|
||||
} catch (error) {
|
||||
loopError = true;
|
||||
Log(
|
||||
`[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.`
|
||||
);
|
||||
Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`);
|
||||
Log(`[${shared.now()}] Debug: ${error}`);
|
||||
for (const codice of shared.codiciNonAccettati) {
|
||||
if (error.response?.status === codice) {
|
||||
Log(
|
||||
`[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
|
||||
);
|
||||
Log(
|
||||
`[${shared.now()}] Per favore riavviare il programma con un token valido.`
|
||||
);
|
||||
process.exit(3);
|
||||
}
|
||||
}
|
||||
Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
|
||||
}
|
||||
} while (loopError);
|
||||
}
|
||||
} else {
|
||||
do {
|
||||
loopError = false;
|
||||
try {
|
||||
const response = await axios.get(
|
||||
`https://api.netbird.io/api/peers?name=${host}`,
|
||||
`https://api.netbird.io/api/peers?name=${envHostnames}`,
|
||||
{
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
@@ -75,43 +133,28 @@ async function initHostList() {
|
||||
},
|
||||
}
|
||||
);
|
||||
for (const host of response.data) {
|
||||
hostIDs.push(host.id);
|
||||
}
|
||||
/*
|
||||
response.data.forEach((host) => {
|
||||
hostIDs.push(host.id);
|
||||
});
|
||||
*/
|
||||
hostIDs.push(response.data[0].id);
|
||||
} catch (error) {
|
||||
loopError = true;
|
||||
Log(
|
||||
`[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.`
|
||||
);
|
||||
Log(`[${shared.now()}] ${error}`);
|
||||
Log(`[${shared.now()}] Debug: ${error.stack}`);
|
||||
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()}`,
|
||||
},
|
||||
Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`);
|
||||
Log(`[${shared.now()}] Debug: ${error}`);
|
||||
for (const codice of shared.codiciNonAccettati) {
|
||||
if (error.response?.status === codice) {
|
||||
Log(
|
||||
`[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
|
||||
);
|
||||
Log(
|
||||
`[${shared.now()}] Per favore riavviare il programma con un token valido.`
|
||||
);
|
||||
process.exit(3);
|
||||
}
|
||||
}
|
||||
);
|
||||
hostIDs.push(response.data[0].id);
|
||||
} catch (error) {
|
||||
Log(
|
||||
`[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.`
|
||||
);
|
||||
Log(`[${shared.now()}] ${error}`);
|
||||
Log(`[${shared.now()}] Debug: ${error.stack}`);
|
||||
process.exit(3);
|
||||
}
|
||||
Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
|
||||
}
|
||||
} while (loopError);
|
||||
}
|
||||
}
|
||||
} else if ("HOSTIDS" in process.env) {
|
||||
@@ -122,9 +165,47 @@ async function initHostList() {
|
||||
envHostIDs = envHostIDs.split(";");
|
||||
|
||||
for (const host of envHostIDs) {
|
||||
do {
|
||||
loopError = false;
|
||||
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) {
|
||||
loopError = true;
|
||||
Log(
|
||||
`[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.`
|
||||
);
|
||||
Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`);
|
||||
Log(`[${shared.now()}] Debug: ${error}`);
|
||||
for (const codice of shared.codiciNonAccettati) {
|
||||
if (error.response?.status === codice) {
|
||||
Log(
|
||||
`[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
|
||||
);
|
||||
Log(
|
||||
`[${shared.now()}] Per favore riavviare il programma con un token valido.`
|
||||
);
|
||||
process.exit(3);
|
||||
}
|
||||
}
|
||||
Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
|
||||
}
|
||||
} while (loopError);
|
||||
}
|
||||
} else {
|
||||
do {
|
||||
loopError = false;
|
||||
try {
|
||||
const response = await axios.get(
|
||||
`https://api.netbird.io/api/peers/${host}`,
|
||||
`https://api.netbird.io/api/peers/${envHostIDs}`,
|
||||
{
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
@@ -134,75 +215,70 @@ async function initHostList() {
|
||||
);
|
||||
hostIDs.push(response.data.id);
|
||||
} catch (error) {
|
||||
loopError = true;
|
||||
Log(
|
||||
`[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.`
|
||||
);
|
||||
Log(`[${shared.now()}] ${error}`);
|
||||
Log(`[${shared.now()}] Debug: ${error.stack}`);
|
||||
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()}`,
|
||||
},
|
||||
Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`);
|
||||
Log(`[${shared.now()}] Debug: ${error}`);
|
||||
for (const codice of shared.codiciNonAccettati) {
|
||||
if (error.response?.status === codice) {
|
||||
Log(
|
||||
`[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
|
||||
);
|
||||
Log(
|
||||
`[${shared.now()}] Per favore riavviare il programma con un token valido.`
|
||||
);
|
||||
process.exit(3);
|
||||
}
|
||||
}
|
||||
);
|
||||
hostIDs.push(response.data.id);
|
||||
} catch (error) {
|
||||
Log(
|
||||
`[${shared.now()}] Errore nella verifica e aggiunta al monitoraggio di un host.`
|
||||
);
|
||||
Log(`[${shared.now()}] ${error}`);
|
||||
Log(`[${shared.now()}] Debug: ${error.stack}`);
|
||||
process.exit(3);
|
||||
}
|
||||
Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
|
||||
}
|
||||
} while (loopError);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Log(
|
||||
`[${shared.now()}] 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()}`,
|
||||
},
|
||||
});
|
||||
do {
|
||||
loopError = false;
|
||||
try {
|
||||
const response = await axios.get(`https://api.netbird.io/api/peers`, {
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Authorization: `Token ${shared.getToken()}`,
|
||||
},
|
||||
});
|
||||
|
||||
for (const element of response.data) {
|
||||
hostIDs.push(element.id);
|
||||
for (const element of response.data) {
|
||||
hostIDs.push(element.id);
|
||||
}
|
||||
} catch (error) {
|
||||
loopError = true;
|
||||
Log(`[${shared.now()}] Errore nel tentativo di reperire tutti i peer.`);
|
||||
Log(`[${shared.now()}] Codice HTTP ${error.response?.status}`);
|
||||
Log(`[${shared.now()}] Debug: ${error}`);
|
||||
for (const codice of shared.codiciNonAccettati) {
|
||||
if (error.response?.status === codice) {
|
||||
Log(
|
||||
`[${shared.now()}] Errore grave: il token inserito non esiste, non è valido o è scaduto.`
|
||||
);
|
||||
Log(
|
||||
`[${shared.now()}] Per favore riavviare il programma con un token valido.`
|
||||
);
|
||||
process.exit(3);
|
||||
}
|
||||
}
|
||||
Log(`[${shared.now()}] Nuovo tentativo n. ${errorCount++}`);
|
||||
}
|
||||
|
||||
/*
|
||||
response.data.forEach((element) => {
|
||||
hostIDs.push(element.id);
|
||||
});
|
||||
*/
|
||||
} catch (error) {
|
||||
Log(`[${shared.now()}] Errore nel tentativo di reperire tutti i peer.`);
|
||||
Log(`[${shared.now()}] ${error}`);
|
||||
Log(`[${shared.now()}] Debug: ${error.stack}`);
|
||||
process.exit(3);
|
||||
}
|
||||
} while (loopError);
|
||||
}
|
||||
|
||||
Log(`[${shared.now()}] ID rilevati:`);
|
||||
for (const element of hostIDs) {
|
||||
Log(`[${shared.now()}] ${element}`);
|
||||
}
|
||||
|
||||
/*
|
||||
hostIDs.forEach((element) => {
|
||||
Log(`[${shared.now()}] ${element}`);
|
||||
});
|
||||
*/
|
||||
}
|
||||
|
||||
module.exports = { Monitoring };
|
||||
|
||||
Reference in New Issue
Block a user