From 8f5a1abdc7a59d499f360a1ab7fe9a8452be55d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Gro=C3=9F?= Date: Wed, 24 May 2023 15:02:06 +0200 Subject: [PATCH] Implement basic uptime check using /ping endpoint --- package.json | 3 ++- src/index.js | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/index.js diff --git a/package.json b/package.json index 2181f06..fc9f8a8 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..3619848 --- /dev/null +++ b/src/index.js @@ -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);