Merge branch 'master' into hevans/add-heii-on-call-notification-provider

This commit is contained in:
Frank Elsinga 2024-03-11 20:49:42 +01:00 committed by GitHub
commit 1db750a2e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 158 additions and 64 deletions

View File

@ -8,6 +8,7 @@ const User = require("../server/model/user");
const { io } = require("socket.io-client");
const { localWebSocketURL } = require("../server/config");
const args = require("args-parser")(process.argv);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
@ -19,10 +20,10 @@ const main = async () => {
}
console.log("Connecting the database");
Database.initDataDir(args);
await Database.connect(false, false, true);
try {
Database.initDataDir(args);
await Database.connect(false, false, true);
// No need to actually reset the password for testing, just make sure no connection problem. It is ok for now.
if (!process.env.TEST_BACKEND) {
const user = await R.findOne("user");

View File

@ -328,9 +328,9 @@ class Monitor extends BeanModel {
/**
* Start monitor
* @param {Server} io Socket server instance
* @returns {void}
* @returns {Promise<void>}
*/
start(io) {
async start(io) {
let previousBeat = null;
let retries = 0;
@ -1102,9 +1102,9 @@ class Monitor extends BeanModel {
/**
* Stop monitor
* @returns {void}
* @returns {Promise<void>}
*/
stop() {
async stop() {
clearTimeout(this.heartbeatInterval);
this.isStop = true;

View File

@ -81,7 +81,8 @@ class MqttMonitorType extends MonitorType {
let client = mqtt.connect(mqttUrl, {
username,
password
password,
clientId: "uptime-kuma_" + Math.random().toString(16).substr(2, 8)
});
client.on("connect", () => {

View File

@ -1,6 +1,7 @@
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const { DOWN, UP } = require("../../src/util");
const { setting } = require("../util-server");
const { DOWN, UP, getMonitorRelativeURL } = require("../../src/util");
class Teams extends NotificationProvider {
name = "teams";
@ -9,89 +10,172 @@ class Teams extends NotificationProvider {
* Generate the message to send
* @param {const} status The status constant
* @param {string} monitorName Name of monitor
* @param {boolean} withStatusSymbol If the status should be prepended as symbol
* @returns {string} Status message
*/
_statusMessageFactory = (status, monitorName) => {
_statusMessageFactory = (status, monitorName, withStatusSymbol) => {
if (status === DOWN) {
return `🔴 Application [${monitorName}] went down`;
return (withStatusSymbol ? "🔴 " : "") + `[${monitorName}] went down`;
} else if (status === UP) {
return `✅ Application [${monitorName}] is back online`;
return (withStatusSymbol ? "✅ " : "") + `[${monitorName}] is back online`;
}
return "Notification";
};
/**
* Select theme color to use based on status
* Select the style to use based on status
* @param {const} status The status constant
* @returns {string} Selected color in hex RGB format
* @returns {string} Selected style for adaptive cards
*/
_getThemeColor = (status) => {
_getStyle = (status) => {
if (status === DOWN) {
return "ff0000";
return "attention";
}
if (status === UP) {
return "00e804";
return "good";
}
return "008cff";
return "emphasis";
};
/**
* Generate payload for notification
* @param {object} args Method arguments
* @param {const} args.status The status of the monitor
* @param {string} args.monitorMessage Message to send
* @param {string} args.monitorName Name of monitor affected
* @param {string} args.monitorUrl URL of monitor affected
* @param {object} args.heartbeatJSON Heartbeat details
* @param {string} args.monitorName Name of the monitor affected
* @param {string} args.monitorUrl URL of the monitor affected
* @param {string} args.dashboardUrl URL of the dashboard affected
* @returns {object} Notification payload
*/
_notificationPayloadFactory = ({
status,
monitorMessage,
heartbeatJSON,
monitorName,
monitorUrl,
dashboardUrl,
}) => {
const notificationMessage = this._statusMessageFactory(
status,
monitorName
);
const status = heartbeatJSON?.status;
const facts = [];
const actions = [];
if (dashboardUrl) {
actions.push({
"type": "Action.OpenUrl",
"title": "Visit Uptime Kuma",
"url": dashboardUrl
});
}
if (heartbeatJSON?.msg) {
facts.push({
title: "Description",
value: heartbeatJSON.msg,
});
}
if (monitorName) {
facts.push({
name: "Monitor",
title: "Monitor",
value: monitorName,
});
}
if (monitorUrl && monitorUrl !== "https://") {
facts.push({
name: "URL",
value: monitorUrl,
title: "URL",
// format URL as markdown syntax, to be clickable
value: `[${monitorUrl}](${monitorUrl})`,
});
actions.push({
"type": "Action.OpenUrl",
"title": "Visit Monitor URL",
"url": monitorUrl
});
}
return {
"@context": "https://schema.org/extensions",
"@type": "MessageCard",
themeColor: this._getThemeColor(status),
summary: notificationMessage,
sections: [
if (heartbeatJSON?.localDateTime) {
facts.push({
title: "Time",
value: heartbeatJSON.localDateTime + (heartbeatJSON.timezone ? ` (${heartbeatJSON.timezone})` : ""),
});
}
const payload = {
"type": "message",
// message with status prefix as notification text
"summary": this._statusMessageFactory(status, monitorName, true),
"attachments": [
{
activityImage:
"https://raw.githubusercontent.com/louislam/uptime-kuma/master/public/icon.png",
activityTitle: "**Uptime Kuma**",
},
{
activityTitle: notificationMessage,
},
{
activityTitle: "**Description**",
text: monitorMessage,
facts,
},
],
"contentType": "application/vnd.microsoft.card.adaptive",
"contentUrl": "",
"content": {
"type": "AdaptiveCard",
"body": [
{
"type": "Container",
"verticalContentAlignment": "Center",
"items": [
{
"type": "ColumnSet",
"style": this._getStyle(status),
"columns": [
{
"type": "Column",
"width": "auto",
"verticalContentAlignment": "Center",
"items": [
{
"type": "Image",
"width": "32px",
"style": "Person",
"url": "https://raw.githubusercontent.com/louislam/uptime-kuma/master/public/icon.png",
"altText": "Uptime Kuma Logo"
}
]
},
{
"type": "Column",
"width": "stretch",
"items": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": `**${this._statusMessageFactory(status, monitorName, false)}**`,
},
{
"type": "TextBlock",
"size": "Small",
"weight": "Default",
"text": "Uptime Kuma Alert",
"isSubtle": true,
"spacing": "None"
}
]
}
]
}
]
},
{
"type": "FactSet",
"separator": false,
"facts": facts
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.5"
}
}
]
};
if (actions) {
payload.attachments[0].content.body.push({
"type": "ActionSet",
"actions": actions,
});
}
return payload;
};
/**
@ -112,7 +196,9 @@ class Teams extends NotificationProvider {
*/
_handleGeneralNotification = (webhookUrl, msg) => {
const payload = this._notificationPayloadFactory({
monitorMessage: msg
heartbeatJSON: {
msg: msg
}
});
return this._sendNotification(webhookUrl, payload);
@ -130,26 +216,32 @@ class Teams extends NotificationProvider {
return okMsg;
}
let url;
let monitorUrl;
switch (monitorJSON["type"]) {
case "http":
case "keywork":
url = monitorJSON["url"];
monitorUrl = monitorJSON["url"];
break;
case "docker":
url = monitorJSON["docker_host"];
monitorUrl = monitorJSON["docker_host"];
break;
default:
url = monitorJSON["hostname"];
monitorUrl = monitorJSON["hostname"];
break;
}
const baseURL = await setting("primaryBaseURL");
let dashboardUrl;
if (baseURL) {
dashboardUrl = baseURL + getMonitorRelativeURL(monitorJSON.id);
}
const payload = this._notificationPayloadFactory({
monitorMessage: heartbeatJSON.msg,
heartbeatJSON: heartbeatJSON,
monitorName: monitorJSON.name,
monitorUrl: url,
status: heartbeatJSON.status,
monitorUrl: monitorUrl,
dashboardUrl: dashboardUrl,
});
await this._sendNotification(notification.webhookUrl, payload);

View File

@ -986,7 +986,7 @@ let needSetup = false;
log.info("manage", `Delete Monitor: ${monitorID} User ID: ${socket.userID}`);
if (monitorID in server.monitorList) {
server.monitorList[monitorID].stop();
await server.monitorList[monitorID].stop();
delete server.monitorList[monitorID];
}
@ -1703,11 +1703,11 @@ async function startMonitor(userID, monitorID) {
]);
if (monitor.id in server.monitorList) {
server.monitorList[monitor.id].stop();
await server.monitorList[monitor.id].stop();
}
server.monitorList[monitor.id] = monitor;
monitor.start(io);
await monitor.start(io);
}
/**
@ -1737,7 +1737,7 @@ async function pauseMonitor(userID, monitorID) {
]);
if (monitorID in server.monitorList) {
server.monitorList[monitorID].stop();
await server.monitorList[monitorID].stop();
server.monitorList[monitorID].active = 0;
}
}
@ -1754,7 +1754,7 @@ async function startMonitors() {
}
for (let monitor of list) {
monitor.start(io);
await monitor.start(io);
// Give some delays, so all monitors won't make request at the same moment when just start the server.
await sleep(getRandomInt(300, 1000));
}
@ -1775,7 +1775,7 @@ async function shutdownFunction(signal) {
log.info("server", "Stopping all monitors");
for (let id in server.monitorList) {
let monitor = server.monitorList[id];
monitor.stop();
await monitor.stop();
}
await sleep(2000);
await Database.close();