diff --git a/CNAME b/CNAME index a5348b076..442505166 100644 --- a/CNAME +++ b/CNAME @@ -1 +1 @@ -git.kuma.pet \ No newline at end of file +git.kuma.pet diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index a6499ef9f..f01f0ea52 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -8,7 +8,7 @@ services: image: louislam/uptime-kuma:1 container_name: uptime-kuma volumes: - - ./uptime-kuma:/app/data + - ./uptime-kuma-data:/app/data ports: - - 3001:3001 + - 3001:3001 # : restart: always diff --git a/package-lock.json b/package-lock.json index 30d84cb87..86d37100f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29,7 +29,7 @@ "chroma-js": "^2.1.2", "command-exists": "~1.2.9", "compare-versions": "~3.6.0", - "dayjs": "~1.10.8", + "dayjs": "^1.11.0", "express": "~4.17.3", "express-basic-auth": "~1.2.1", "favico.js": "^0.3.10", @@ -52,7 +52,7 @@ "prom-client": "~13.2.0", "prometheus-api-metrics": "~3.2.1", "qrcode": "~1.5.0", - "redbean-node": "0.1.3", + "redbean-node": "0.1.4", "socket.io": "~4.4.1", "socket.io-client": "~4.4.1", "socks-proxy-agent": "^6.1.1", @@ -5855,9 +5855,9 @@ } }, "node_modules/dayjs": { - "version": "1.10.8", - "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.10.8.tgz", - "integrity": "sha512-wbNwDfBHHur9UOzNUjeKUOJ0fCb0a52Wx0xInmQ7Y8FstyajiV1NmK1e00cxsr9YrE9r7yAChE0VvpuY5Rnlow==" + "version": "1.11.0", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.0.tgz", + "integrity": "sha512-JLC809s6Y948/FuCZPm5IX8rRhQwOiyMb2TfVVQEixG7P8Lm/gt5S7yoQZmC8x1UehI9Pb7sksEt4xx14m+7Ug==" }, "node_modules/debug": { "version": "4.3.4", @@ -14184,15 +14184,15 @@ } }, "node_modules/redbean-node": { - "version": "0.1.3", - "resolved": "https://registry.npmjs.org/redbean-node/-/redbean-node-0.1.3.tgz", - "integrity": "sha512-itAouTnNK12QXy10DxScFRDv/R3Zt1sZw+tfUQCsBALxDDCNXVUdkNTgClouUwbTDG1YMQkeoD1Je9ujN7u3yg==", + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/redbean-node/-/redbean-node-0.1.4.tgz", + "integrity": "sha512-c1U6wnTeWS0c44tn9hkJWzjGgckLNJ8sN1E2bxnnnQsULOfvEVFLf8dLMjqhyyMrZ1L1mp8UvV4OfhRtH/ZrgQ==", "dependencies": { - "@types/node": "^14.17.7", + "@types/node": "^14.18.12", "await-lock": "^2.1.0", - "dayjs": "^1.10.6", - "glob": "^7.1.7", - "knex": "^0.95.9", + "dayjs": "^1.11.0", + "glob": "^7.2.0", + "knex": "^0.95.15", "lodash": "^4.17.21" } }, @@ -21391,9 +21391,9 @@ "dev": true }, "dayjs": { - "version": "1.10.8", - "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.10.8.tgz", - "integrity": "sha512-wbNwDfBHHur9UOzNUjeKUOJ0fCb0a52Wx0xInmQ7Y8FstyajiV1NmK1e00cxsr9YrE9r7yAChE0VvpuY5Rnlow==" + "version": "1.11.0", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.0.tgz", + "integrity": "sha512-JLC809s6Y948/FuCZPm5IX8rRhQwOiyMb2TfVVQEixG7P8Lm/gt5S7yoQZmC8x1UehI9Pb7sksEt4xx14m+7Ug==" }, "debug": { "version": "4.3.4", @@ -27680,15 +27680,15 @@ } }, "redbean-node": { - "version": "0.1.3", - "resolved": "https://registry.npmjs.org/redbean-node/-/redbean-node-0.1.3.tgz", - "integrity": "sha512-itAouTnNK12QXy10DxScFRDv/R3Zt1sZw+tfUQCsBALxDDCNXVUdkNTgClouUwbTDG1YMQkeoD1Je9ujN7u3yg==", + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/redbean-node/-/redbean-node-0.1.4.tgz", + "integrity": "sha512-c1U6wnTeWS0c44tn9hkJWzjGgckLNJ8sN1E2bxnnnQsULOfvEVFLf8dLMjqhyyMrZ1L1mp8UvV4OfhRtH/ZrgQ==", "requires": { - "@types/node": "^14.17.7", + "@types/node": "^14.18.12", "await-lock": "^2.1.0", - "dayjs": "^1.10.6", - "glob": "^7.1.7", - "knex": "^0.95.9", + "dayjs": "^1.11.0", + "glob": "^7.2.0", + "knex": "^0.95.15", "lodash": "^4.17.21" }, "dependencies": { diff --git a/package.json b/package.json index a0391ed0d..f40954642 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "uptime-kuma", - "version": "1.15.1", + "version": "1.16.1", "license": "MIT", "repository": { "type": "git", @@ -39,7 +39,7 @@ "build-docker-nightly-alpine": "docker buildx build -f docker/dockerfile-alpine --platform linux/amd64,linux/arm64,linux/arm/v7 -t louislam/uptime-kuma:nightly-alpine --target nightly . --push", "build-docker-nightly-amd64": "docker buildx build -f docker/dockerfile --platform linux/amd64 -t louislam/uptime-kuma:nightly-amd64 --target nightly . --push --progress plain", "upload-artifacts": "docker buildx build -f docker/dockerfile --platform linux/amd64 -t louislam/uptime-kuma:upload-artifact --build-arg VERSION --build-arg GITHUB_TOKEN --target upload-artifact . --progress plain", - "setup": "git checkout 1.15.1 && npm ci --production && npm run download-dist", + "setup": "git checkout 1.16.1 && npm ci --production && npm run download-dist", "download-dist": "node extra/download-dist.js", "mark-as-nightly": "node extra/mark-as-nightly.js", "reset-password": "node extra/reset-password.js", @@ -80,7 +80,7 @@ "chroma-js": "^2.1.2", "command-exists": "~1.2.9", "compare-versions": "~3.6.0", - "dayjs": "~1.10.8", + "dayjs": "^1.11.0", "express": "~4.17.3", "express-basic-auth": "~1.2.1", "favico.js": "^0.3.10", @@ -103,7 +103,7 @@ "prom-client": "~13.2.0", "prometheus-api-metrics": "~3.2.1", "qrcode": "~1.5.0", - "redbean-node": "0.1.3", + "redbean-node": "0.1.4", "socket.io": "~4.4.1", "socket.io-client": "~4.4.1", "socks-proxy-agent": "^6.1.1", diff --git a/public/icon.svg b/public/icon.svg index 825c344e2..9c0bc6cae 100644 --- a/public/icon.svg +++ b/public/icon.svg @@ -1,3 +1,3 @@ - \ No newline at end of file + diff --git a/server/model/monitor.js b/server/model/monitor.js index f2d16524b..643d34a6f 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -182,7 +182,7 @@ class Monitor extends BeanModel { // undefined if not https let tlsInfo = undefined; - if (!previousBeat) { + if (!previousBeat || this.type === "push") { previousBeat = await R.findOne("heartbeat", " monitor_id = ? ORDER BY time DESC", [ this.id, ]); @@ -192,7 +192,7 @@ class Monitor extends BeanModel { let bean = R.dispense("heartbeat"); bean.monitor_id = this.id; - bean.time = R.isoDateTime(dayjs.utc()); + bean.time = R.isoDateTimeMillis(dayjs.utc()); bean.status = DOWN; if (this.isUpsideDown()) { @@ -330,7 +330,7 @@ class Monitor extends BeanModel { let startTime = dayjs().valueOf(); let dnsMessage = ""; - let dnsRes = await dnsResolve(this.hostname, this.dns_resolve_server, this.dns_resolve_type); + let dnsRes = await dnsResolve(this.hostname, this.dns_resolve_server, this.port, this.dns_resolve_type); bean.ping = dayjs().valueOf() - startTime; if (this.dns_resolve_type === "A" || this.dns_resolve_type === "AAAA" || this.dns_resolve_type === "TXT") { @@ -367,25 +367,33 @@ class Monitor extends BeanModel { bean.msg = dnsMessage; bean.status = UP; } else if (this.type === "push") { // Type: Push - const time = R.isoDateTime(dayjs.utc().subtract(this.interval, "second")); + log.debug("monitor", `[${this.name}] Checking monitor at ${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")}`); + const bufferTime = 1000; // 1s buffer to accommodate clock differences - let heartbeatCount = await R.count("heartbeat", " monitor_id = ? AND time > ? ", [ - this.id, - time - ]); + if (previousBeat) { + const msSinceLastBeat = dayjs.utc().valueOf() - dayjs.utc(previousBeat.time).valueOf(); - log.debug("monitor", "heartbeatCount" + heartbeatCount + " " + time); + log.debug("monitor", `[${this.name}] msSinceLastBeat = ${msSinceLastBeat}`); - if (heartbeatCount <= 0) { - // Fix #922, since previous heartbeat could be inserted by api, it should get from database - previousBeat = await Monitor.getPreviousHeartbeat(this.id); - - throw new Error("No heartbeat in the time window"); + // If the previous beat was down or pending we use the regular + // beatInterval/retryInterval in the setTimeout further below + if (previousBeat.status !== UP || msSinceLastBeat > beatInterval * 1000 + bufferTime) { + throw new Error("No heartbeat in the time window"); + } else { + let timeout = beatInterval * 1000 - msSinceLastBeat; + if (timeout < 0) { + timeout = bufferTime; + } else { + timeout += bufferTime; + } + // No need to insert successful heartbeat for push type, so end here + retries = 0; + log.debug("monitor", `[${this.name}] timeout = ${timeout}`); + this.heartbeatInterval = setTimeout(beat, timeout); + return; + } } else { - // No need to insert successful heartbeat for push type, so end here - retries = 0; - this.heartbeatInterval = setTimeout(beat, beatInterval * 1000); - return; + throw new Error("No heartbeat in the time window"); } } else if (this.type === "steam") { diff --git a/server/notification-providers/apprise.js b/server/notification-providers/apprise.js index 2d795d4e5..887afbf55 100644 --- a/server/notification-providers/apprise.js +++ b/server/notification-providers/apprise.js @@ -6,9 +6,14 @@ class Apprise extends NotificationProvider { name = "apprise"; async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { - let s = childProcess.spawnSync("apprise", [ "-vv", "-b", msg, notification.appriseURL ]); + const args = [ "-vv", "-b", msg, notification.appriseURL ]; + if (notification.title) { + args.push("-t"); + args.push(notification.title); + } + const s = childProcess.spawnSync("apprise", args); - let output = (s.stdout) ? s.stdout.toString() : "ERROR: maybe apprise not found"; + const output = (s.stdout) ? s.stdout.toString() : "ERROR: maybe apprise not found"; if (output) { diff --git a/server/notification-providers/discord.js b/server/notification-providers/discord.js index dd63e74b6..28ead7b7a 100644 --- a/server/notification-providers/discord.js +++ b/server/notification-providers/discord.js @@ -22,16 +22,23 @@ class Discord extends NotificationProvider { return okMsg; } - let url; + let address; - if (monitorJSON["type"] === "port") { - url = monitorJSON["hostname"]; - if (monitorJSON["port"]) { - url += ":" + monitorJSON["port"]; - } - - } else { - url = monitorJSON["url"]; + switch (monitorJSON["type"]) { + case "ping": + address = monitorJSON["hostname"]; + break; + case "port": + case "dns": + case "steam": + address = monitorJSON["hostname"]; + if (monitorJSON["port"]) { + address += ":" + monitorJSON["port"]; + } + break; + default: + address = monitorJSON["url"]; + break; } // If heartbeatJSON is not null, we go into the normal alerting loop. @@ -48,8 +55,8 @@ class Discord extends NotificationProvider { value: monitorJSON["name"], }, { - name: "Service URL", - value: url, + name: monitorJSON["type"] === "push" ? "Service Type" : "Service URL", + value: monitorJSON["type"] === "push" ? "Heartbeat" : address, }, { name: "Time (UTC)", @@ -83,8 +90,8 @@ class Discord extends NotificationProvider { value: monitorJSON["name"], }, { - name: "Service URL", - value: url.startsWith("http") ? "[Visit Service](" + url + ")" : url, + name: monitorJSON["type"] === "push" ? "Service Type" : "Service URL", + value: monitorJSON["type"] === "push" ? "Heartbeat" : address.startsWith("http") ? "[Visit Service](" + address + ")" : address, }, { name: "Time (UTC)", @@ -92,7 +99,7 @@ class Discord extends NotificationProvider { }, { name: "Ping", - value: heartbeatJSON["ping"] + "ms", + value: heartbeatJSON["ping"] == null ? "N/A" : heartbeatJSON["ping"] + " ms", }, ], }], diff --git a/server/notification-providers/pagerduty.js b/server/notification-providers/pagerduty.js new file mode 100644 index 000000000..86e9a0992 --- /dev/null +++ b/server/notification-providers/pagerduty.js @@ -0,0 +1,113 @@ +const NotificationProvider = require("./notification-provider"); +const axios = require("axios"); +const { UP, DOWN, getMonitorRelativeURL } = require("../../src/util"); +const { setting } = require("../util-server"); +let successMessage = "Sent Successfully."; + +class PagerDuty extends NotificationProvider { + name = "PagerDuty"; + + /** + * @inheritdoc + */ + async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { + try { + if (heartbeatJSON == null) { + const title = "Uptime Kuma Alert"; + const monitor = { + type: "ping", + url: "Uptime Kuma Test Button", + }; + return this.postNotification(notification, title, msg, monitor); + } + + if (heartbeatJSON.status === UP) { + const title = "Uptime Kuma Monitor ✅ Up"; + const eventAction = notification.pagerdutyAutoResolve || null; + + return this.postNotification(notification, title, heartbeatJSON.msg, monitorJSON, eventAction); + } + + if (heartbeatJSON.status === DOWN) { + const title = "Uptime Kuma Monitor 🔴 Down"; + return this.postNotification(notification, title, heartbeatJSON.msg, monitorJSON, "trigger"); + } + } catch (error) { + this.throwGeneralAxiosError(error); + } + } + + /** + * Check if result is successful, result code should be in range 2xx + * @param {Object} result Axios response object + * @throws {Error} The status code is not in range 2xx + */ + checkResult(result) { + if (result.status == null) { + throw new Error("PagerDuty notification failed with invalid response!"); + } + if (result.status < 200 || result.status >= 300) { + throw new Error("PagerDuty notification failed with status code " + result.status); + } + } + + /** + * Send the message + * @param {BeanModel} notification Message title + * @param {string} title Message title + * @param {string} body Message + * @param {Object} monitorInfo Monitor details (For Up/Down only) + * @param {?string} eventAction Action event for PagerDuty (trigger, acknowledge, resolve) + * @returns {string} + */ + async postNotification(notification, title, body, monitorInfo, eventAction = "trigger") { + + if (eventAction == null) { + return "No action required"; + } + + let monitorUrl; + if (monitorInfo.type === "port") { + monitorUrl = monitorInfo.hostname; + if (monitorInfo.port) { + monitorUrl += ":" + monitorInfo.port; + } + } else if (monitorInfo.hostname != null) { + monitorUrl = monitorInfo.hostname; + } else { + monitorUrl = monitorInfo.url; + } + + const options = { + method: "POST", + url: notification.pagerdutyIntegrationUrl, + headers: { "Content-Type": "application/json" }, + data: { + payload: { + summary: `[${title}] [${monitorInfo.name}] ${body}`, + severity: notification.pagerdutyPriority || "warning", + source: monitorUrl, + }, + routing_key: notification.pagerdutyIntegrationKey, + event_action: eventAction, + dedup_key: "Uptime Kuma/" + monitorInfo.id, + } + }; + + const baseURL = await setting("primaryBaseURL"); + if (baseURL && monitorInfo) { + options.client = "Uptime Kuma"; + options.client_url = baseURL + getMonitorRelativeURL(monitorInfo.id); + } + + let result = await axios.request(options); + this.checkResult(result); + if (result.statusText != null) { + return "PagerDuty notification succeed: " + result.statusText; + } + + return successMessage; + } +} + +module.exports = PagerDuty; diff --git a/server/notification.js b/server/notification.js index 269e94440..d0b6f40d9 100644 --- a/server/notification.js +++ b/server/notification.js @@ -29,6 +29,7 @@ const SerwerSMS = require("./notification-providers/serwersms"); const Stackfield = require("./notification-providers/stackfield"); const WeCom = require("./notification-providers/wecom"); const GoogleChat = require("./notification-providers/google-chat"); +const PagerDuty = require("./notification-providers/pagerduty"); const Gorush = require("./notification-providers/gorush"); const Alerta = require("./notification-providers/alerta"); const OneBot = require("./notification-providers/onebot"); @@ -74,6 +75,7 @@ class Notification { new Stackfield(), new WeCom(), new GoogleChat(), + new PagerDuty(), new Gorush(), new Alerta(), new OneBot(), diff --git a/server/routers/api-router.js b/server/routers/api-router.js index 872d6d8cf..24a42069c 100644 --- a/server/routers/api-router.js +++ b/server/routers/api-router.js @@ -59,7 +59,7 @@ router.get("/api/push/:pushToken", async (request, response) => { let duration = 0; let bean = R.dispense("heartbeat"); - bean.time = R.isoDateTime(dayjs.utc()); + bean.time = R.isoDateTimeMillis(dayjs.utc()); if (previousHeartbeat) { isFirstBeat = false; @@ -67,6 +67,7 @@ router.get("/api/push/:pushToken", async (request, response) => { duration = dayjs(bean.time).diff(dayjs(previousHeartbeat.time), "second"); } + log.debug("router", `/api/push/ called at ${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")}`); log.debug("router", "PreviousStatus: " + previousStatus); log.debug("router", "Current Status: " + status); diff --git a/server/util-server.js b/server/util-server.js index 54974e148..db7f525c7 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -176,12 +176,16 @@ exports.mqttAsync = function (hostname, topic, okMessage, options = {}) { * Resolves a given record using the specified DNS server * @param {string} hostname The hostname of the record to lookup * @param {string} resolverServer The DNS server to use + * @param {string} resolverPort Port the DNS server is listening on * @param {string} rrtype The type of record to request * @returns {Promise<(string[]|Object[]|Object)>} */ -exports.dnsResolve = function (hostname, resolverServer, rrtype) { +exports.dnsResolve = function (hostname, resolverServer, resolverPort, rrtype) { const resolver = new Resolver(); - resolver.setServers([ resolverServer ]); + // Remove brackets from IPv6 addresses so we can re-add them to + // prevent issues with ::1:5300 (::1 port 5300) + resolverServer = resolverServer.replace("[", "").replace("]", ""); + resolver.setServers([`[${resolverServer}]:${resolverPort}`]); return new Promise((resolve, reject) => { if (rrtype === "PTR") { resolver.reverse(hostname, (err, records) => { diff --git a/src/components/CountUp.vue b/src/components/CountUp.vue index 41edc4a0e..df1d1ac6c 100644 --- a/src/components/CountUp.vue +++ b/src/components/CountUp.vue @@ -10,7 +10,10 @@ import { sleep } from "../util.ts"; export default { props: { - value: [ String, Number ], + value: { + type: [ String, Number ], + default: 0, + }, time: { type: Number, default: 0.3, diff --git a/src/components/Datetime.vue b/src/components/Datetime.vue index ed38c434d..fa68d02c7 100644 --- a/src/components/Datetime.vue +++ b/src/components/Datetime.vue @@ -13,7 +13,10 @@ dayjs.extend(relativeTime); export default { props: { - value: String, + value: { + type: String, + default: null, + }, dateOnly: { type: Boolean, default: false, diff --git a/src/components/HeartbeatBar.vue b/src/components/HeartbeatBar.vue index 245a8512c..ce888a989 100644 --- a/src/components/HeartbeatBar.vue +++ b/src/components/HeartbeatBar.vue @@ -168,7 +168,8 @@ export default { getBeatTitle(beat) { return `${this.$root.datetime(beat.time)}` + ((beat.msg) ? ` - ${beat.msg}` : ""); - } + }, + }, }; diff --git a/src/components/Status.vue b/src/components/Status.vue index a3916adce..1985d8518 100644 --- a/src/components/Status.vue +++ b/src/components/Status.vue @@ -5,7 +5,10 @@ diff --git a/src/components/notifications/PromoSMS.vue b/src/components/notifications/PromoSMS.vue index 61e61a93c..91b4d96df 100644 --- a/src/components/notifications/PromoSMS.vue +++ b/src/components/notifications/PromoSMS.vue @@ -1,8 +1,8 @@