uptime-kuma/src/pages/EditMonitor.vue

524 lines
24 KiB
Vue
Raw Normal View History

2021-06-25 13:55:49 +00:00
<template>
2021-08-19 18:37:59 +00:00
<transition name="slide-fade" appear>
<div>
<h1 class="mb-3">{{ pageName }}</h1>
<form @submit.prevent="submit">
<div class="shadow-box">
<div class="row">
<div class="col-md-6">
<h2 class="mb-2">{{ $t("General") }}</h2>
2021-08-19 18:37:59 +00:00
<div class="my-3">
<label for="type" class="form-label">{{ $t("Monitor Type") }}</label>
<select id="type" v-model="monitor.type" class="form-select">
2021-08-19 18:37:59 +00:00
<option value="http">
HTTP(s)
</option>
<option value="port">
TCP Port
</option>
<option value="ping">
Ping
</option>
<option value="keyword">
HTTP(s) - {{ $t("Keyword") }}
2021-08-19 18:37:59 +00:00
</option>
2021-08-22 22:05:48 +00:00
<option value="dns">
DNS
</option>
2021-09-30 16:09:43 +00:00
<option value="push">
Push
</option>
2021-08-19 18:37:59 +00:00
</select>
</div>
2021-09-30 16:09:43 +00:00
<!-- Friendly Name -->
2021-08-19 18:37:59 +00:00
<div class="my-3">
<label for="name" class="form-label">{{ $t("Friendly Name") }}</label>
2021-08-19 18:37:59 +00:00
<input id="name" v-model="monitor.name" type="text" class="form-control" required>
</div>
2021-09-30 16:09:43 +00:00
<!-- URL -->
2021-08-19 18:37:59 +00:00
<div v-if="monitor.type === 'http' || monitor.type === 'keyword' " class="my-3">
<label for="url" class="form-label">{{ $t("URL") }}</label>
2021-08-19 18:37:59 +00:00
<input id="url" v-model="monitor.url" type="url" class="form-control" pattern="https?://.+" required>
</div>
2021-09-30 16:09:43 +00:00
<!-- Push URL -->
<div v-if="monitor.type === 'push' " class="my-3">
<label for="push-url" class="form-label">{{ $t("PushUrl") }}</label>
2021-09-30 16:09:43 +00:00
<CopyableInput id="push-url" v-model="pushURL" type="url" disabled="disabled" />
2021-10-01 08:43:11 +00:00
<div class="form-text">
{{ $t("needPushEvery", [monitor.interval]) }}<br />
2021-10-13 07:36:07 +00:00
{{ $t("pushOptionalParams", ["msg, ping"]) }}
2021-10-01 08:43:11 +00:00
</div>
2021-09-30 16:09:43 +00:00
</div>
<!-- Keyword -->
2021-08-19 18:37:59 +00:00
<div v-if="monitor.type === 'keyword' " class="my-3">
<label for="keyword" class="form-label">{{ $t("Keyword") }}</label>
2021-08-19 18:37:59 +00:00
<input id="keyword" v-model="monitor.keyword" type="text" class="form-control" required>
<div class="form-text">
2021-08-27 16:41:58 +00:00
{{ $t("keywordDescription") }}
2021-08-19 18:37:59 +00:00
</div>
</div>
<!-- Hostname -->
<!-- TCP Port / Ping / DNS only -->
<div v-if="monitor.type === 'port' || monitor.type === 'ping' || monitor.type === 'dns' " class="my-3">
<label for="hostname" class="form-label">{{ $t("Hostname") }}</label>
2021-09-21 10:27:40 +00:00
<input id="hostname" v-model="monitor.hostname" type="text" class="form-control" :pattern="`${ipRegexPattern}|${hostnameRegexPattern}`" required>
2021-08-19 18:37:59 +00:00
</div>
<!-- For TCP Port Type -->
2021-08-19 18:37:59 +00:00
<div v-if="monitor.type === 'port' " class="my-3">
<label for="port" class="form-label">{{ $t("Port") }}</label>
2021-08-19 18:37:59 +00:00
<input id="port" v-model="monitor.port" type="number" class="form-control" required min="0" max="65535" step="1">
</div>
<!-- For DNS Type -->
<template v-if="monitor.type === 'dns'">
<div class="my-3">
<label for="dns_resolve_server" class="form-label">{{ $t("Resolver Server") }}</label>
<input id="dns_resolve_server" v-model="monitor.dns_resolve_server" type="text" class="form-control" :pattern="ipRegex" required>
<div class="form-text">
{{ $t("resoverserverDescription") }}
</div>
2021-08-22 22:05:48 +00:00
</div>
<div class="my-3">
<label for="dns_resolve_type" class="form-label">{{ $t("Resource Record Type") }}</label>
<!-- :allow-empty="false" is not working, set a default value instead https://github.com/shentao/vue-multiselect/issues/336 -->
<VueMultiselect
id="dns_resolve_type"
v-model="monitor.dns_resolve_type"
:options="dnsresolvetypeOptions"
:multiple="false"
:close-on-select="true"
:clear-on-select="false"
:preserve-search="false"
placeholder="Pick a RR-Type..."
:preselect-first="false"
:max-height="500"
:taggable="false"
></VueMultiselect>
<div class="form-text">
{{ $t("rrtypeDescription") }}
</div>
2021-08-22 22:05:48 +00:00
</div>
</template>
2021-08-22 22:05:48 +00:00
2021-10-01 08:43:11 +00:00
<!-- Interval -->
2021-08-19 18:37:59 +00:00
<div class="my-3">
<label for="interval" class="form-label">{{ $t("Heartbeat Interval") }} ({{ $t("checkEverySecond", [ monitor.interval ]) }})</label>
2021-08-19 18:37:59 +00:00
<input id="interval" v-model="monitor.interval" type="number" class="form-control" required min="20" step="1">
</div>
<div class="my-3">
<label for="maxRetries" class="form-label">{{ $t("Retries") }}</label>
2021-08-19 18:37:59 +00:00
<input id="maxRetries" v-model="monitor.maxretries" type="number" class="form-control" required min="0" step="1">
<div class="form-text">
{{ $t("retriesDescription") }}
2021-08-19 18:37:59 +00:00
</div>
</div>
<div class="my-3">
<label for="retry-interval" class="form-label">
2021-09-14 07:48:25 +00:00
{{ $t("Heartbeat Retry Interval") }}
<span>({{ $t("retryCheckEverySecond", [ monitor.retryInterval ]) }})</span>
</label>
2021-09-14 07:48:25 +00:00
<input id="retry-interval" v-model="monitor.retryInterval" type="number" class="form-control" required min="20" step="1">
</div>
<h2 v-if="monitor.type !== 'push'" class="mt-5 mb-2">{{ $t("Advanced") }}</h2>
2021-08-19 18:37:59 +00:00
<div v-if="monitor.type === 'http' || monitor.type === 'keyword' " class="my-3 form-check">
<input id="ignore-tls" v-model="monitor.ignoreTls" class="form-check-input" type="checkbox" value="">
<label class="form-check-label" for="ignore-tls">
{{ $t("ignoreTLSError") }}
2021-08-19 18:37:59 +00:00
</label>
</div>
<div class="my-3 form-check">
2021-08-19 18:37:59 +00:00
<input id="upside-down" v-model="monitor.upsideDown" class="form-check-input" type="checkbox">
<label class="form-check-label" for="upside-down">
{{ $t("Upside Down Mode") }}
2021-08-19 18:37:59 +00:00
</label>
<div class="form-text">
{{ $t("upsideDownModeDescription") }}
2021-08-19 18:37:59 +00:00
</div>
</div>
<!-- HTTP / Keyword only -->
<template v-if="monitor.type === 'http' || monitor.type === 'keyword' ">
<div class="my-3">
<label for="maxRedirects" class="form-label">{{ $t("Max. Redirects") }}</label>
<input id="maxRedirects" v-model="monitor.maxredirects" type="number" class="form-control" required min="0" step="1">
<div class="form-text">
{{ $t("maxRedirectDescription") }}
</div>
2021-08-19 18:37:59 +00:00
</div>
<div class="my-3">
<label for="acceptedStatusCodes" class="form-label">{{ $t("Accepted Status Codes") }}</label>
<VueMultiselect
id="acceptedStatusCodes"
v-model="monitor.accepted_statuscodes"
:options="acceptedStatusCodeOptions"
:multiple="true"
:close-on-select="false"
:clear-on-select="false"
:preserve-search="true"
placeholder="Pick Accepted Status Codes..."
:preselect-first="false"
:max-height="600"
:taggable="true"
></VueMultiselect>
<div class="form-text">
{{ $t("acceptedStatusCodesDescription") }}
</div>
2021-08-19 18:37:59 +00:00
</div>
</template>
2021-08-19 18:37:59 +00:00
<div class="my-3">
<tags-manager ref="tagsManager" :pre-selected-tags="monitor.tags"></tags-manager>
</div>
2021-08-19 18:37:59 +00:00
<div class="mt-5 mb-1">
<button class="btn btn-primary" type="submit" :disabled="processing">{{ $t("Save") }}</button>
2021-08-19 18:37:59 +00:00
</div>
2021-07-27 17:47:13 +00:00
</div>
2021-08-19 18:37:59 +00:00
<div class="col-md-6">
<div v-if="$root.isMobile" class="mt-3" />
2021-07-29 17:09:14 +00:00
2021-10-15 10:36:40 +00:00
<!-- Notifications -->
<h2 class="mb-2">{{ $t("Notifications") }}</h2>
2021-08-19 18:37:59 +00:00
<p v-if="$root.notificationList.length === 0">
{{ $t("Not available, please setup.") }}
2021-08-19 18:37:59 +00:00
</p>
2021-07-29 17:09:14 +00:00
2021-08-19 18:37:59 +00:00
<div v-for="notification in $root.notificationList" :key="notification.id" class="form-check form-switch my-3">
<input :id=" 'notification' + notification.id" v-model="monitor.notificationIDList[notification.id]" class="form-check-input" type="checkbox">
2021-07-29 17:09:14 +00:00
2021-08-19 18:37:59 +00:00
<label class="form-check-label" :for=" 'notification' + notification.id">
{{ notification.name }}
<a href="#" @click="$refs.notificationDialog.show(notification.id)">{{ $t("Edit") }}</a>
2021-08-19 18:37:59 +00:00
</label>
<span v-if="notification.isDefault == true" class="badge bg-primary ms-2">Default</span>
2021-08-19 18:37:59 +00:00
</div>
2021-08-19 18:37:59 +00:00
<button class="btn btn-primary me-2" type="button" @click="$refs.notificationDialog.show()">
{{ $t("Setup Notification") }}
2021-08-19 18:37:59 +00:00
</button>
2021-10-15 10:36:40 +00:00
<!-- HTTP Options -->
<template v-if="monitor.type === 'http' || monitor.type === 'keyword' ">
<h2 class="mt-5 mb-2">{{ $t("HTTP Options") }}</h2>
<!-- Method -->
<div class="my-3">
<label for="method" class="form-label">{{ $t("Method") }}</label>
<select id="method" v-model="monitor.method" class="form-select">
<option value="GET">
GET
</option>
<option value="POST">
POST
</option>
<option value="PUT">
PUT
</option>
<option value="PATCH">
PATCH
</option>
<option value="DELETE">
DELETE
</option>
<option value="HEAD">
HEAD
</option>
<option value="OPTIONS">
OPTIONS
</option>
</select>
</div>
<!-- Body -->
<div class="my-3">
<label for="body" class="form-label">{{ $t("Body") }}</label>
<textarea id="body" v-model="monitor.body" class="form-control" :placeholder="bodyPlaceholder"></textarea>
</div>
<!-- Headers -->
<div class="my-3">
<label for="headers" class="form-label">{{ $t("Headers") }}</label>
<textarea id="headers" v-model="monitor.headers" class="form-control" :placeholder="headersPlaceholder"></textarea>
</div>
</template>
</div>
</div>
2021-07-27 17:47:13 +00:00
</div>
2021-08-19 18:37:59 +00:00
</form>
2021-06-25 13:55:49 +00:00
<NotificationDialog ref="notificationDialog" @added="addedNotification" />
2021-06-25 13:55:49 +00:00
</div>
2021-08-19 18:37:59 +00:00
</transition>
2021-06-25 13:55:49 +00:00
</template>
<script>
2021-06-29 08:06:20 +00:00
import NotificationDialog from "../components/NotificationDialog.vue";
import TagsManager from "../components/TagsManager.vue";
2021-09-30 16:09:43 +00:00
import CopyableInput from "../components/CopyableInput.vue";
import { useToast } from "vue-toastification";
import VueMultiselect from "vue-multiselect";
import { genSecret, isDev } from "../util.ts";
const toast = useToast();
2021-06-25 13:55:49 +00:00
export default {
components: {
2021-09-30 16:09:43 +00:00
CopyableInput,
2021-07-27 17:47:13 +00:00
NotificationDialog,
TagsManager,
VueMultiselect,
2021-06-25 13:55:49 +00:00
},
2021-08-08 13:14:29 +00:00
2021-06-25 13:55:49 +00:00
data() {
return {
processing: false,
monitor: {
notificationIDList: {},
// Do not add default value here, please check init() method
},
2021-08-08 13:14:29 +00:00
acceptedStatusCodeOptions: [],
2021-08-22 22:05:48 +00:00
dnsresolvetypeOptions: [],
2021-08-24 07:00:30 +00:00
// Source: https://digitalfortress.tech/tips/top-15-commonly-used-regex/
2021-09-22 15:05:46 +00:00
ipRegexPattern: "((^\\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\\s*$)|(^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?\\s*$))",
2021-09-17 14:02:20 +00:00
// Source: https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address
2021-10-06 19:29:42 +00:00
hostnameRegexPattern: "^(([a-zA-Z0-9_]|[a-zA-Z0-9_][a-zA-Z0-9\\-_]*[a-zA-Z0-9_])\\.)*([A-Za-z0-9_]|[A-Za-z0-9_][A-Za-z0-9\\-_]*[A-Za-z0-9_])$"
2021-09-30 16:09:43 +00:00
};
2021-06-25 13:55:49 +00:00
},
2021-08-08 13:14:29 +00:00
2021-06-25 13:55:49 +00:00
computed: {
ipRegex() {
// Allow to test with simple dns server with port (127.0.0.1:5300)
if (! isDev) {
return this.ipRegexPattern;
}
return null;
},
2021-06-25 13:55:49 +00:00
pageName() {
return this.$t((this.isAdd) ? "Add New Monitor" : "Edit");
2021-06-25 13:55:49 +00:00
},
2021-09-30 16:09:43 +00:00
2021-06-25 13:55:49 +00:00
isAdd() {
return this.$route.path === "/add";
2021-06-27 08:10:55 +00:00
},
2021-09-30 16:09:43 +00:00
2021-06-27 08:10:55 +00:00
isEdit() {
return this.$route.path.startsWith("/edit");
2021-07-27 17:47:13 +00:00
},
2021-09-30 16:09:43 +00:00
pushURL() {
2021-10-01 08:43:11 +00:00
return this.$root.baseURL + "/api/push/" + this.monitor.pushToken + "?msg=OK&ping=";
},
bodyPlaceholder() {
return "{\n\t\"id\": 124357,\n\t\"username\": \"admin\",\n\t\"password\": \"myAdminPassword\"\n}";
},
headersPlaceholder() {
return "{\n\t\"Authorization\": \"Bearer abc123\",\n\t\"Content-Type\": \"application/json\"\n}";
2021-09-30 16:09:43 +00:00
}
2021-07-27 17:47:13 +00:00
},
watch: {
2021-09-30 16:09:43 +00:00
2021-08-19 18:37:59 +00:00
"$route.fullPath"() {
2021-07-27 17:47:13 +00:00
this.init();
},
2021-09-30 16:09:43 +00:00
2021-09-14 07:48:25 +00:00
"monitor.interval"(value, oldValue) {
// Link interval and retryInterval if they are the same value.
2021-09-14 07:48:25 +00:00
if (this.monitor.retryInterval === oldValue) {
this.monitor.retryInterval = value;
}
2021-09-30 16:09:43 +00:00
},
"monitor.type"() {
if (this.monitor.type === "push") {
if (! this.monitor.pushToken) {
this.monitor.pushToken = genSecret(10);
}
}
2021-09-14 07:48:25 +00:00
}
2021-09-30 16:09:43 +00:00
2021-07-27 17:47:13 +00:00
},
mounted() {
this.init();
2021-08-08 13:14:29 +00:00
let acceptedStatusCodeOptions = [
"100-199",
"200-299",
"300-399",
"400-499",
"500-599",
];
2021-08-22 22:05:48 +00:00
let dnsresolvetypeOptions = [
"A",
"AAAA",
"CAA",
"CNAME",
"MX",
"NS",
"PTR",
"SOA",
"SRV",
"TXT",
];
2021-08-08 13:14:29 +00:00
for (let i = 100; i <= 999; i++) {
acceptedStatusCodeOptions.push(i.toString());
}
this.acceptedStatusCodeOptions = acceptedStatusCodeOptions;
2021-08-22 22:05:48 +00:00
this.dnsresolvetypeOptions = dnsresolvetypeOptions;
2021-06-25 13:55:49 +00:00
},
methods: {
2021-06-27 08:10:55 +00:00
init() {
if (this.isAdd) {
2021-06-27 08:10:55 +00:00
this.monitor = {
type: "http",
name: "",
url: "https://",
method: "GET",
2021-06-27 08:10:55 +00:00
interval: 60,
2021-09-14 07:48:25 +00:00
retryInterval: this.interval,
maxretries: 0,
notificationIDList: {},
2021-07-30 11:18:26 +00:00
ignoreTls: false,
2021-07-29 17:09:14 +00:00
upsideDown: false,
maxredirects: 10,
accepted_statuscodes: ["200-299"],
dns_resolve_type: "A",
2021-08-22 22:05:48 +00:00
dns_resolve_server: "1.1.1.1",
2021-09-30 16:09:43 +00:00
};
2021-09-05 21:23:06 +00:00
for (let i = 0; i < this.$root.notificationList.length; i++) {
if (this.$root.notificationList[i].isDefault == true) {
this.monitor.notificationIDList[this.$root.notificationList[i].id] = true;
}
}
2021-06-27 08:10:55 +00:00
} else if (this.isEdit) {
this.$root.getSocket().emit("getMonitor", this.$route.params.id, (res) => {
if (res.ok) {
this.monitor = res.monitor;
// Handling for monitors that are created before 1.7.0
if (this.monitor.retryInterval === 0) {
this.monitor.retryInterval = this.monitor.interval;
}
2021-06-27 08:10:55 +00:00
} else {
2021-09-30 16:09:43 +00:00
toast.error(res.msg);
2021-06-27 08:10:55 +00:00
}
2021-09-30 16:09:43 +00:00
});
2021-06-27 08:10:55 +00:00
}
},
isInputValid() {
if (this.monitor.body) {
try {
JSON.parse(this.monitor.body);
} catch (err) {
toast.error(this.$t("BodyInvalidFormat") + err.message);
return false;
}
}
if (this.monitor.headers) {
try {
JSON.parse(this.monitor.headers);
} catch (err) {
toast.error(this.$t("HeadersInvalidFormat") + err.message);
return false;
}
}
return true;
},
async submit() {
2021-06-25 13:55:49 +00:00
this.processing = true;
if (!this.isInputValid()) {
this.processing = false;
return;
}
// Beautiful the JSON format
if (this.monitor.body) {
this.monitor.body = JSON.stringify(JSON.parse(this.monitor.body), null, 4);
}
if (this.monitor.headers) {
this.monitor.headers = JSON.stringify(JSON.parse(this.monitor.headers), null, 4);
}
2021-06-25 13:55:49 +00:00
if (this.isAdd) {
this.$root.add(this.monitor, async (res) => {
2021-06-25 13:55:49 +00:00
if (res.ok) {
await this.$refs.tagsManager.submit(res.monitorID);
2021-06-25 13:55:49 +00:00
toast.success(res.msg);
this.processing = false;
this.$root.getMonitorList();
2021-09-30 16:09:43 +00:00
this.$router.push("/dashboard/" + res.monitorID);
2021-06-25 13:55:49 +00:00
} else {
toast.error(res.msg);
this.processing = false;
2021-06-25 13:55:49 +00:00
}
2021-09-30 16:09:43 +00:00
});
2021-06-25 13:55:49 +00:00
} else {
await this.$refs.tagsManager.submit(this.monitor.id);
2021-06-27 08:10:55 +00:00
this.$root.getSocket().emit("editMonitor", this.monitor, (res) => {
this.processing = false;
2021-09-10 03:53:11 +00:00
this.$root.toastRes(res);
this.init();
2021-09-30 16:09:43 +00:00
});
2021-06-25 13:55:49 +00:00
}
2021-07-27 17:47:13 +00:00
},
// Added a Notification Event
// Enable it if the notification is added in EditMonitor.vue
addedNotification(id) {
this.monitor.notificationIDList[id] = true;
},
2021-06-27 08:10:55 +00:00
},
2021-09-30 16:09:43 +00:00
};
2021-06-25 13:55:49 +00:00
</script>
<style lang="scss" scoped>
2021-06-25 13:55:49 +00:00
.shadow-box {
padding: 20px;
}
textarea {
min-height: 200px;
}
2021-06-25 13:55:49 +00:00
</style>