Implement basic uptime check using /ping endpoint
This commit is contained in:
@@ -2,8 +2,9 @@
|
||||
"name": "toho-mon",
|
||||
"version": "0.0.1",
|
||||
"description": "Toho Miku monitoring",
|
||||
"main": "index.js",
|
||||
"main": "src/index.js",
|
||||
"scripts": {
|
||||
"monitor": "node src/index.js",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "Minz",
|
||||
|
||||
75
src/index.js
Normal file
75
src/index.js
Normal file
@@ -0,0 +1,75 @@
|
||||
const axios = require('axios');
|
||||
const STATUS_CODES = require('http');
|
||||
const { Webhook, MessageBuilder } = require('discord-webhook-node');
|
||||
|
||||
// API endpoint URL to ping
|
||||
const apiUrl = 'http://127.0.0.1:3080/api/v1/ping';
|
||||
|
||||
// Discord webhook URL to send messages
|
||||
const webhookUrl = 'https://discord.com/api/webhooks/1110880167536099328/GE6T1D81sGFvG08EOMdAo3sL1WIizowu-t78An0L9fcbgL6BAkoH0Lu74rtilS1KJUqO';
|
||||
|
||||
// Create a new Discord webhook instance
|
||||
const webhook = new Webhook(webhookUrl);
|
||||
|
||||
const images = {
|
||||
oh: "https://cdn.discordapp.com/attachments/1083687175998152714/1110912960152481833/81a58a9c-cee3-473a-9ca9-08efad4de576.jpeg",
|
||||
down: "https://cdn.discordapp.com/attachments/1083687175998152714/1110913210103635998/3751235c-ed2d-46dd-9af4-e24cf3ee8d7e.jpeg",
|
||||
up: "https://cdn.discordapp.com/attachments/1083687175998152714/1110913780268924958/605e7b80-9b97-4792-b903-eaa482dd7f11.jpeg"
|
||||
}
|
||||
|
||||
let consecutiveFailures = 0;
|
||||
|
||||
// Function to ping the API endpoint
|
||||
async function pingEndpoint() {
|
||||
try {
|
||||
// Send a GET request to the API endpoint
|
||||
const response = await axios({ url: apiUrl, timeout: 1000, method: 'get' });
|
||||
|
||||
// Check if the response status is successful (2xx)
|
||||
if (response.status === 200) {
|
||||
console.log('API endpoint is online.');
|
||||
if (consecutiveFailures != 0) {
|
||||
consecutiveFailures = 0;
|
||||
sendNotification("Recovered", "The system has recovered", color='#00FF00', image=images.up);
|
||||
}
|
||||
} else {
|
||||
// Send an error message to the Discord webhook
|
||||
handleFailure(`API endpoint returned status ${response.status}\n${STATUS_CODES[response.status]}`);
|
||||
|
||||
}
|
||||
} catch (error) {
|
||||
// Send an error message to the Discord webhook
|
||||
consecutiveFailures += 1;
|
||||
console.log(`ERROR PINGING API. HARD FAIL (${consecutiveFailures})`);
|
||||
handleFailure(`Error occurred while pinging the API endpoint.\n${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
function handleFailure(errorMessage) {
|
||||
console.error(errorMessage);
|
||||
|
||||
if (consecutiveFailures == 10) {
|
||||
sendNotification("Down", errorMessage, color='#ff0000', image=images.down);
|
||||
return;
|
||||
}
|
||||
if (consecutiveFailures == 5) {
|
||||
sendNotification("Intermittent issues", errorMessage, '#D9B611');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function sendNotification(statusString, reasonString, color='#00b0f4', image=images.oh) {
|
||||
const embed = new MessageBuilder()
|
||||
.setTitle('Status Update')
|
||||
.addField('Status:', statusString, true)
|
||||
.addField('Reason', reasonString)
|
||||
.setColor(color)
|
||||
.setImage(image)
|
||||
.setDescription('A change in availability has been detected!')
|
||||
.setTimestamp();
|
||||
|
||||
webhook.send(embed);
|
||||
}
|
||||
|
||||
// Call the pingEndpoint function
|
||||
setInterval(pingEndpoint, 5000);
|
||||
Reference in New Issue
Block a user