Merge branch 'master' into tls-expiry

# Conflicts:
#	server/model/monitor.js
This commit is contained in:
LouisLam 2021-07-26 20:35:50 +08:00
commit 5b18a6a518
18 changed files with 134 additions and 30 deletions

View file

@ -11,3 +11,4 @@
LICENSE LICENSE
README.md README.md
.editorconfig .editorconfig
.vscode

1
.gitignore vendored
View file

@ -7,3 +7,4 @@ dist-ssr
/data /data
!/data/.gitkeep !/data/.gitkeep
.vscode

Binary file not shown.

37
db/patch3.sql Normal file
View file

@ -0,0 +1,37 @@
-- You should not modify if this have pushed to Github, unless it does serious wrong with the db.
-- Add maxretries column to monitor
PRAGMA foreign_keys=off;
BEGIN TRANSACTION;
create table monitor_dg_tmp
(
id INTEGER not null
primary key autoincrement,
name VARCHAR(150),
active BOOLEAN default 1 not null,
user_id INTEGER
references user
on update cascade on delete set null,
interval INTEGER default 20 not null,
url TEXT,
type VARCHAR(20),
weight INTEGER default 2000,
hostname VARCHAR(255),
port INTEGER,
created_date DATETIME,
keyword VARCHAR(255),
maxretries INTEGER NOT NULL DEFAULT 0
);
insert into monitor_dg_tmp(id, name, active, user_id, interval, url, type, weight, hostname, port, created_date, keyword) select id, name, active, user_id, interval, url, type, weight, hostname, port, created_date, keyword from monitor;
drop table monitor;
alter table monitor_dg_tmp rename to monitor;
create index user_id on monitor (user_id);
COMMIT;
PRAGMA foreign_keys=on;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4 KiB

After

Width:  |  Height:  |  Size: 2.5 KiB

View file

@ -6,6 +6,7 @@ var timezone = require('dayjs/plugin/timezone')
dayjs.extend(utc) dayjs.extend(utc)
dayjs.extend(timezone) dayjs.extend(timezone)
const axios = require("axios"); const axios = require("axios");
const {UP, DOWN, PENDING} = require("../util");
const {debug} = require("../util"); const {debug} = require("../util");
const {tcping, ping, checkCertificate} = require("../util-server"); const {tcping, ping, checkCertificate} = require("../util-server");
const {R} = require("redbean-node"); const {R} = require("redbean-node");
@ -24,7 +25,6 @@ const customAgent = new https.Agent({
* 1 = UP * 1 = UP
*/ */
class Monitor extends BeanModel { class Monitor extends BeanModel {
async toJSON() { async toJSON() {
let notificationIDList = {}; let notificationIDList = {};
@ -43,6 +43,7 @@ class Monitor extends BeanModel {
url: this.url, url: this.url,
hostname: this.hostname, hostname: this.hostname,
port: this.port, port: this.port,
maxretries: this.maxretries,
weight: this.weight, weight: this.weight,
active: this.active, active: this.active,
type: this.type, type: this.type,
@ -54,21 +55,25 @@ class Monitor extends BeanModel {
start(io) { start(io) {
let previousBeat = null; let previousBeat = null;
let retries = 0;
const beat = async () => { const beat = async () => {
if (! previousBeat) { if (! previousBeat) {
previousBeat = await R.findOne("heartbeat", " monitor_id = ? ORDER BY time DESC", [ previousBeat = await R.findOne("heartbeat", " monitor_id = ? ORDER BY time DESC", [
this.id this.id
]) ])
} }
const isFirstBeat = !previousBeat;
let bean = R.dispense("heartbeat") let bean = R.dispense("heartbeat")
bean.monitor_id = this.id; bean.monitor_id = this.id;
bean.time = R.isoDateTime(dayjs.utc()); bean.time = R.isoDateTime(dayjs.utc());
bean.status = 0; bean.status = DOWN;
// Duration // Duration
if (previousBeat) { if (! isFirstBeat) {
bean.duration = dayjs(bean.time).diff(dayjs(previousBeat.time), 'second'); bean.duration = dayjs(bean.time).diff(dayjs(previousBeat.time), 'second');
} else { } else {
bean.duration = 0; bean.duration = 0;
@ -98,7 +103,7 @@ class Monitor extends BeanModel {
debug("Cert Info Query Time: " + (dayjs().valueOf() - certInfoStartTime) + "ms") debug("Cert Info Query Time: " + (dayjs().valueOf() - certInfoStartTime) + "ms")
if (this.type === "http") { if (this.type === "http") {
bean.status = 1; bean.status = UP;
} else { } else {
let data = res.data; let data = res.data;
@ -110,7 +115,7 @@ class Monitor extends BeanModel {
if (data.includes(this.keyword)) { if (data.includes(this.keyword)) {
bean.msg += ", keyword is found" bean.msg += ", keyword is found"
bean.status = 1; bean.status = UP;
} else { } else {
throw new Error(bean.msg + ", but keyword is not found") throw new Error(bean.msg + ", but keyword is not found")
} }
@ -121,30 +126,52 @@ class Monitor extends BeanModel {
} else if (this.type === "port") { } else if (this.type === "port") {
bean.ping = await tcping(this.hostname, this.port); bean.ping = await tcping(this.hostname, this.port);
bean.msg = "" bean.msg = ""
bean.status = 1; bean.status = UP;
} else if (this.type === "ping") { } else if (this.type === "ping") {
bean.ping = await ping(this.hostname); bean.ping = await ping(this.hostname);
bean.msg = "" bean.msg = ""
bean.status = 1; bean.status = UP;
} }
retries = 0;
} catch (error) { } catch (error) {
if ((this.maxretries > 0) && (retries < this.maxretries)) {
retries++;
bean.status = PENDING;
}
bean.msg = error.message; bean.msg = error.message;
} }
// Mark as important if status changed // * ? -> ANY STATUS = important [isFirstBeat]
if (! previousBeat || previousBeat.status !== bean.status) { // UP -> PENDING = not important
// * UP -> DOWN = important
// UP -> UP = not important
// PENDING -> PENDING = not important
// * PENDING -> DOWN = important
// PENDING -> UP = not important
// DOWN -> PENDING = this case not exists
// DOWN -> DOWN = not important
// * DOWN -> UP = important
let isImportant = isFirstBeat ||
(previousBeat.status === UP && bean.status === DOWN) ||
(previousBeat.status === DOWN && bean.status === UP) ||
(previousBeat.status === PENDING && bean.status === DOWN);
// Mark as important if status changed, ignore pending pings,
// Don't notify if disrupted changes to up
if (isImportant) {
bean.important = true; bean.important = true;
// Do not send if first beat is UP // Send only if the first beat is DOWN
if (previousBeat || bean.status !== 1) { if (!isFirstBeat || bean.status === DOWN) {
let notificationList = await R.getAll(`SELECT notification.* FROM notification, monitor_notification WHERE monitor_id = ? AND monitor_notification.notification_id = notification.id `, [ let notificationList = await R.getAll(`SELECT notification.* FROM notification, monitor_notification WHERE monitor_id = ? AND monitor_notification.notification_id = notification.id `, [
this.id this.id
]) ])
let text; let text;
if (bean.status === 1) { if (bean.status === UP) {
text = "✅ Up" text = "✅ Up"
} else { } else {
text = "🔴 Down" text = "🔴 Down"
@ -165,8 +192,10 @@ class Monitor extends BeanModel {
bean.important = false; bean.important = false;
} }
if (bean.status === 1) { if (bean.status === UP) {
console.info(`Monitor #${this.id} '${this.name}': Successful Response: ${bean.ping} ms | Interval: ${this.interval} seconds | Type: ${this.type}`) console.info(`Monitor #${this.id} '${this.name}': Successful Response: ${bean.ping} ms | Interval: ${this.interval} seconds | Type: ${this.type}`)
} else if (bean.status === PENDING) {
console.warn(`Monitor #${this.id} '${this.name}': Pending: ${bean.msg} | Type: ${this.type}`)
} else { } else {
console.warn(`Monitor #${this.id} '${this.name}': Failing: ${bean.msg} | Type: ${this.type}`) console.warn(`Monitor #${this.id} '${this.name}': Failing: ${bean.msg} | Type: ${this.type}`)
} }
@ -293,7 +322,7 @@ class Monitor extends BeanModel {
} }
total += value; total += value;
if (row.status === 0) { if (row.status === 0 || row.status === 2) {
downtime += value; downtime += value;
} }
} }

View file

@ -204,7 +204,7 @@ class Notification {
} }
let data = { let data = {
"message": "<b>Uptime Kuma Alert</b>\n\n<b>Message</b>:" +msg + '\n<b>Time (UTC)</b>:' +time, "message": "<b>Uptime Kuma Alert</b>\n\n<b>Message</b>:"+msg+ '\n<b>Time (UTC)</b>:' +heartbeatJSON["time"],
"user":notification.pushoveruserkey, "user":notification.pushoveruserkey,
"token": notification.pushoverapptoken, "token": notification.pushoverapptoken,
"sound": notification.pushoversounds, "sound": notification.pushoversounds,

View file

@ -233,6 +233,7 @@ let needSetup = false;
bean.url = monitor.url bean.url = monitor.url
bean.interval = monitor.interval bean.interval = monitor.interval
bean.hostname = monitor.hostname; bean.hostname = monitor.hostname;
bean.maxretries = monitor.maxretries;
bean.port = monitor.port; bean.port = monitor.port;
bean.keyword = monitor.keyword; bean.keyword = monitor.keyword;

View file

@ -1,6 +1,10 @@
// Common JS cannot be used in frontend sadly // Common JS cannot be used in frontend sadly
// sleep, ucfirst is duplicated in ../src/util-frontend.js // sleep, ucfirst is duplicated in ../src/util-frontend.js
exports.DOWN = 0;
exports.UP = 1;
exports.PENDING = 2;
exports.sleep = function (ms) { exports.sleep = function (ms) {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }

View file

@ -1,5 +1,6 @@
$primary: #5CDD8B; $primary: #5CDD8B;
$danger: #DC3545; $danger: #DC3545;
$warning: #f8a306;
$link-color: #111; $link-color: #111;
$border-radius: 50rem; $border-radius: 50rem;

View file

@ -3,7 +3,7 @@
<div class="hp-bar-big" :style="barStyle"> <div class="hp-bar-big" :style="barStyle">
<div <div
class="beat" class="beat"
:class="{ 'empty' : (beat === 0), 'down' : (beat.status === 0) }" :class="{ 'empty' : (beat === 0), 'down' : (beat.status === 0), 'pending' : (beat.status === 2) }"
:style="beatStyle" :style="beatStyle"
v-for="(beat, index) in shortBeatList" v-for="(beat, index) in shortBeatList"
:key="index" :key="index"
@ -166,6 +166,10 @@ export default {
background-color: $danger; background-color: $danger;
} }
&.pending {
background-color: $warning;
}
&:not(.empty):hover { &:not(.empty):hover {
transition: all ease-in-out 0.15s; transition: all ease-in-out 0.15s;
opacity: 0.8; opacity: 0.8;

View file

@ -223,16 +223,22 @@
<template v-if="notification.type === 'pushover'"> <template v-if="notification.type === 'pushover'">
<div class="mb-3"> <div class="mb-3">
<label for="pushover-app-token" class="form-label">Application Token<span style="color:red;"><sup>*</sup></span></label>
<input type="text" class="form-control" id="pushover-app-token" required v-model="notification.pushoverapptoken">
<label for="pushover-user" class="form-label">User Key<span style="color:red;"><sup>*</sup></span></label> <label for="pushover-user" class="form-label">User Key<span style="color:red;"><sup>*</sup></span></label>
<input type="text" class="form-control" id="pushover-user" required v-model="notification.pushoveruserkey"> <input type="text" class="form-control" id="pushover-user" required v-model="notification.pushoveruserkey">
<label for="pushover-app-token" class="form-label">Application Token<span style="color:red;"><sup>*</sup></span></label>
<input type="text" class="form-control" id="pushover-app-token" required v-model="notification.pushoverapptoken">
<label for="pushover-device" class="form-label">Device</label> <label for="pushover-device" class="form-label">Device</label>
<input type="text" class="form-control" id="pushover-device" v-model="notification.pushoverdevice"> <input type="text" class="form-control" id="pushover-device" v-model="notification.pushoverdevice">
<label for="pushover-device" class="form-label">Message Title</label> <label for="pushover-device" class="form-label">Message Title</label>
<input type="text" class="form-control" id="pushover-title" v-model="notification.pushovertitle"> <input type="text" class="form-control" id="pushover-title" v-model="notification.pushovertitle">
<label for="pushover-priority" class="form-label">Priority</label> <label for="pushover-priority" class="form-label">Priority</label>
<input type="text" class="form-control" id="pushover-priority" v-model="notification.pushoverpriority"> <select class="form-select" id="pushover-priority" v-model="notification.pushoverpriority">
<option>-2</option>
<option>-1</option>
<option>0</option>
<option>1</option>
<option>2</option>
</select>
<label for="pushover-sound" class="form-label">Notification Sound</label> <label for="pushover-sound" class="form-label">Notification Sound</label>
<select class="form-select" id="pushover-sound" v-model="notification.pushoversounds"> <select class="form-select" id="pushover-sound" v-model="notification.pushoversounds">
<option>pushover</option> <option>pushover</option>
@ -265,16 +271,18 @@
</p> </p>
<p style="margin-top: 8px;"> <p style="margin-top: 8px;">
Emergency priority (2) has default 30 second timeout between retries and will expire after 1 hour. Emergency priority (2) has default 30 second timeout between retries and will expire after 1 hour.
</p>
<p style="margin-top: 8px;">
If you want to send notifications to different devices, fill out Device field.
</p> </p>
</div> </div>
</div> </div>
</template> </template>
<template v-if="notification.type === 'apprise'"> <template v-if="notification.type === 'apprise'">
<div class="mb-3"> <div class="mb-3">
<label for="gotify-application-token" class="form-label">Apprise URL</label> <label for="apprise-url" class="form-label">Apprise URL</label>
<input type="text" class="form-control" id="gotify-application-token" required v-model="notification.appriseURL"> <input type="text" class="form-control" id="apprise-url" required v-model="notification.appriseURL">
<div class="form-text"> <div class="form-text">
<p>Example: twilio://AccountSid:AuthToken@FromPhoneNo</p> <p>Example: twilio://AccountSid:AuthToken@FromPhoneNo</p>
<p> <p>
@ -282,7 +290,6 @@
</p> </p>
</div> </div>
</div> </div>
<div class="mb-3"> <div class="mb-3">
<p> <p>
Status: Status:
@ -290,7 +297,6 @@
<span class="text-danger" v-else>Apprise is not installed. <a href="https://github.com/caronc/apprise">Read more</a></span> <span class="text-danger" v-else>Apprise is not installed. <a href="https://github.com/caronc/apprise">Read more</a></span>
</p> </p>
</div> </div>
</template> </template>
</div> </div>

View file

@ -14,6 +14,8 @@ export default {
return "danger" return "danger"
} else if (this.status === 1) { } else if (this.status === 1) {
return "primary" return "primary"
} else if (this.status === 2) {
return "warning"
} else { } else {
return "secondary" return "secondary"
} }
@ -24,6 +26,8 @@ export default {
return "Down" return "Down"
} else if (this.status === 1) { } else if (this.status === 1) {
return "Up" return "Up"
} else if (this.status === 2) {
return "Pending"
} else { } else {
return "Unknown" return "Unknown"
} }
@ -34,6 +38,6 @@ export default {
<style scoped> <style scoped>
span { span {
width: 45px; width: 54px;
} }
</style> </style>

View file

@ -30,6 +30,8 @@ export default {
return "danger" return "danger"
} else if (this.lastHeartBeat.status === 1) { } else if (this.lastHeartBeat.status === 1) {
return "primary" return "primary"
} else if (this.lastHeartBeat.status === 2) {
return "warning"
} else { } else {
return "secondary" return "secondary"
} }

View file

@ -294,6 +294,11 @@ export default {
text: "Down", text: "Down",
color: "danger" color: "danger"
}; };
} else if (lastHeartBeat.status === 2) {
result[monitorID] = {
text: "Pending",
color: "warning"
};
} else { } else {
result[monitorID] = unknown; result[monitorID] = unknown;
} }

View file

@ -110,6 +110,8 @@ export default {
result.up++; result.up++;
} else if (beat.status === 0) { } else if (beat.status === 0) {
result.down++; result.down++;
} else if (beat.status === 2) {
result.up++;
} else { } else {
result.unknown++; result.unknown++;
} }

View file

@ -48,6 +48,12 @@
<input type="number" class="form-control" id="interval" v-model="monitor.interval" required min="20" step="1"> <input type="number" class="form-control" id="interval" v-model="monitor.interval" required min="20" step="1">
</div> </div>
<div class="mb-3">
<label for="maxRetries" class="form-label">Retries</label>
<input type="number" class="form-control" id="maxRetries" v-model="monitor.maxretries" required min="0" step="1">
<div class="form-text">Maximum retries before the service is marked as down and a notification is sent</div>
</div>
<div> <div>
<button class="btn btn-primary" type="submit" :disabled="processing">Save</button> <button class="btn btn-primary" type="submit" :disabled="processing">Save</button>
</div> </div>
@ -61,7 +67,7 @@
<h2>Notifications</h2> <h2>Notifications</h2>
<p v-if="$root.notificationList.length === 0">Not available, please setup.</p> <p v-if="$root.notificationList.length === 0">Not available, please setup.</p>
<div class="form-check form-switch mb-3" v-for="(notification, index) in $root.notificationList" :key="index"> <div class="form-check form-switch mb-3" :key="notification.id" v-for="notification in $root.notificationList">
<input class="form-check-input" type="checkbox" :id=" 'notification' + notification.id" v-model="monitor.notificationIDList[notification.id]"> <input class="form-check-input" type="checkbox" :id=" 'notification' + notification.id" v-model="monitor.notificationIDList[notification.id]">
<label class="form-check-label" :for=" 'notification' + notification.id"> <label class="form-check-label" :for=" 'notification' + notification.id">
@ -119,6 +125,7 @@ export default {
name: "", name: "",
url: "https://", url: "https://",
interval: 60, interval: 60,
maxretries: 0,
notificationIDList: {}, notificationIDList: {},
} }
} else if (this.isEdit) { } else if (this.isEdit) {

View file

@ -36,7 +36,7 @@
<label for="repeat-new-password" class="form-label">Repeat New Password</label> <label for="repeat-new-password" class="form-label">Repeat New Password</label>
<input type="password" class="form-control" :class="{ 'is-invalid' : invalidPassword }" id="repeat-new-password" required v-model="password.repeatNewPassword"> <input type="password" class="form-control" :class="{ 'is-invalid' : invalidPassword }" id="repeat-new-password" required v-model="password.repeatNewPassword">
<div class="invalid-feedback"> <div class="invalid-feedback">
The repeat password is not match. The repeat password does not match.
</div> </div>
</div> </div>
@ -56,7 +56,7 @@
<h2>Notifications</h2> <h2>Notifications</h2>
<p v-if="$root.notificationList.length === 0">Not available, please setup.</p> <p v-if="$root.notificationList.length === 0">Not available, please setup.</p>
<p v-else>Please assign the notification to monitor(s) to get it works.</p> <p v-else>Please assign a notification to monitor(s) to get it to work.</p>
<ul class="list-group mb-3" style="border-radius: 1rem;"> <ul class="list-group mb-3" style="border-radius: 1rem;">
<li class="list-group-item" v-for="(notification, index) in $root.notificationList" :key="index"> <li class="list-group-item" v-for="(notification, index) in $root.notificationList" :key="index">