Merge branch 'master' into general_notification_reformatting

pull/3182/head
Frank Elsinga 2 months ago
commit c963690b09

@ -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");

@ -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;

@ -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", () => {

@ -0,0 +1,52 @@
const { UP, DOWN, getMonitorRelativeURL } = require("../../src/util");
const { setting } = require("../util-server");
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
class HeiiOnCall extends NotificationProvider {
name = "HeiiOnCall";
/**
* @inheritdoc
*/
async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
const okMsg = "Sent Successfully.";
const payload = heartbeatJSON || {};
const baseURL = await setting("primaryBaseURL");
if (baseURL && monitorJSON) {
payload["url"] = baseURL + getMonitorRelativeURL(monitorJSON.id);
}
const config = {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: "Bearer " + notification.heiiOnCallApiKey,
},
};
const heiiUrl = `https://heiioncall.com/triggers/${notification.heiiOnCallTriggerId}/`;
// docs https://heiioncall.com/docs#manual-triggers
try {
if (!heartbeatJSON) {
// Testing or general notification like certificate expiry
payload["msg"] = msg;
await axios.post(heiiUrl + "alert", payload, config);
return okMsg;
}
if (heartbeatJSON.status === DOWN) {
await axios.post(heiiUrl + "alert", payload, config);
return okMsg;
}
if (heartbeatJSON.status === UP) {
await axios.post(heiiUrl + "resolve", payload, config);
return okMsg;
}
} catch (error) {
this.throwGeneralAxiosError(error);
}
}
}
module.exports = HeiiOnCall;

@ -54,14 +54,17 @@ class Ntfy extends NotificationProvider {
"priority": priority,
"title": monitorJSON.name + " " + status + " [Uptime-Kuma]",
"tags": tags,
"actions": [
};
if (monitorJSON.url && monitorJSON.url !== "https://") {
data.actions = [
{
"action": "view",
"label": "Open " + monitorJSON.name,
"url": monitorJSON.url,
}
]
};
},
];
}
if (notification.ntfyIcon) {
data.icon = notification.ntfyIcon;

@ -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: [
{
activityImage:
"https://raw.githubusercontent.com/louislam/uptime-kuma/master/public/icon.png",
activityTitle: "**Uptime Kuma**",
},
{
activityTitle: notificationMessage,
},
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": [
{
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);

@ -16,6 +16,7 @@ const Gorush = require("./notification-providers/gorush");
const Gotify = require("./notification-providers/gotify");
const GrafanaOncall = require("./notification-providers/grafana-oncall");
const HomeAssistant = require("./notification-providers/home-assistant");
const HeiiOnCall = require("./notification-providers/heii-oncall");
const Kook = require("./notification-providers/kook");
const Line = require("./notification-providers/line");
const LineNotify = require("./notification-providers/linenotify");
@ -87,6 +88,7 @@ class Notification {
new Gotify(),
new GrafanaOncall(),
new HomeAssistant(),
new HeiiOnCall(),
new Kook(),
new Line(),
new LineNotify(),

@ -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();

@ -120,6 +120,7 @@ export default {
"gorush": "Gorush",
"gotify": "Gotify",
"GrafanaOncall": "Grafana Oncall",
"HeiiOnCall": "Heii On-Call",
"HomeAssistant": "Home Assistant",
"Kook": "Kook",
"line": "LINE Messenger",

@ -0,0 +1,34 @@
<template>
<div class="mb-3">
<label for="heiioncall-apikey" class="form-label">{{ $t("API Key") }}<span
style="color: red;"
><sup>*</sup></span></label>
<HiddenInput
id="heiioncall-apikey" v-model="$parent.notification.heiiOnCallApiKey" required="true"
autocomplete="false"
></HiddenInput>
</div>
<div class="mb-3">
<label for="heiioncall-trigger-id" class="form-label">Trigger ID<span
style="color: red;"
><sup>*</sup></span></label>
<HiddenInput
id="heiioncall-trigger-id" v-model="$parent.notification.heiiOnCallTriggerId" required="true"
autocomplete="false"
></HiddenInput>
</div>
<i18n-t tag="p" keypath="wayToGetHeiiOnCallDetails" class="form-text mt-3">
<template #documentation>
<a href="https://heiioncall.com/docs" target="_blank">{{ $t("documentationOf", ["Heii On-Call"]) }}</a>
</template>
</i18n-t>
</template>
<script>
import HiddenInput from "../HiddenInput.vue";
export default {
components: {
HiddenInput,
},
};
</script>

@ -1,17 +1,17 @@
<template>
<div class="mb-3">
<label for="line-channel-access-token" class="form-label">{{ $t("Channel access token") }}</label>
<label for="line-channel-access-token" class="form-label">{{ $t("Channel access token (Long-lived)") }}</label>
<HiddenInput id="line-channel-access-token" v-model="$parent.notification.lineChannelAccessToken" :required="true" autocomplete="new-password"></HiddenInput>
</div>
<i18n-t tag="div" keypath="lineDevConsoleTo" class="form-text">
<b>{{ $t("Basic Settings") }}</b>
<b>{{ $t("Messaging API") }}</b>
</i18n-t>
<div class="mb-3" style="margin-top: 12px;">
<label for="line-user-id" class="form-label">{{ $t("User ID") }}</label>
<label for="line-user-id" class="form-label">{{ $t("Your User ID") }}</label>
<input id="line-user-id" v-model="$parent.notification.lineUserID" type="text" class="form-control" required>
</div>
<i18n-t tag="div" keypath="lineDevConsoleTo" class="form-text">
<b>{{ $t("Messaging API") }}</b>
<b>{{ $t("Basic Settings") }}</b>
</i18n-t>
<i18n-t tag="div" keypath="wayToGetLineChannelToken" class="form-text" style="margin-top: 8px;">
<a href="https://developers.line.biz/console/" target="_blank">{{ $t("Line Developers Console") }}</a>

@ -14,6 +14,7 @@ import Gorush from "./Gorush.vue";
import Gotify from "./Gotify.vue";
import GrafanaOncall from "./GrafanaOncall.vue";
import HomeAssistant from "./HomeAssistant.vue";
import HeiiOnCall from "./HeiiOnCall.vue";
import Kook from "./Kook.vue";
import Line from "./Line.vue";
import LineNotify from "./LineNotify.vue";
@ -74,6 +75,7 @@ const NotificationFormList = {
"gotify": Gotify,
"GrafanaOncall": GrafanaOncall,
"HomeAssistant": HomeAssistant,
"HeiiOnCall": HeiiOnCall,
"Kook": Kook,
"line": Line,
"LineNotify": LineNotify,

@ -532,10 +532,12 @@
"Recipients": "Recipients",
"Access Token": "Access Token",
"Channel access token": "Channel access token",
"Channel access token (Long-lived)": "Channel access token (Long-lived)",
"Line Developers Console": "Line Developers Console",
"lineDevConsoleTo": "Line Developers Console - {0}",
"Basic Settings": "Basic Settings",
"User ID": "User ID",
"Your User ID": "Your user ID",
"Messaging API": "Messaging API",
"wayToGetLineChannelToken": "First access the {0}, create a provider and channel (Messaging API), then you can get the channel access token and user ID from the above mentioned menu items.",
"Icon URL": "Icon URL",
@ -883,5 +885,7 @@
"deleteRemoteBrowserMessage": "Are you sure want to delete this Remote Browser for all monitors?",
"GrafanaOncallUrl": "Grafana Oncall URL",
"Browser Screenshot": "Browser Screenshot",
"What is a Remote Browser?": "What is a Remote Browser?"
"What is a Remote Browser?": "What is a Remote Browser?",
"wayToGetHeiiOnCallDetails": "How to get the Trigger ID and API Keys is explained in the {documentation}",
"documentationOf": "{0} Documentation"
}

Loading…
Cancel
Save