added MQTT monitor type

pull/875/head
Tarun Singh 3 years ago
parent d4c9431142
commit 670754b697

@ -0,0 +1,10 @@
-- You should not modify if this have pushed to Github, unless it does serious wrong with the db.
BEGIN TRANSACTION;
ALTER TABLE monitor
ADD topic VARCHAR(50);
ALTER TABLE monitor
ADD success_message VARCHAR(255);
COMMIT;

15091
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -78,6 +78,7 @@
"jsonwebtoken": "~8.5.1",
"jwt-decode": "^3.1.2",
"limiter": "^2.1.0",
"mqtt": "^4.2.8",
"nodemailer": "~6.6.5",
"notp": "~2.0.3",
"password-hash": "~1.2.2",

@ -52,6 +52,7 @@ class Database {
"patch-http-monitor-method-body-and-headers.sql": true,
"patch-2fa-invalidate-used-token.sql": true,
"patch-notification_sent_history.sql": true,
"patch-added-mqtt-monitor.sql": true,
}
/**

@ -1,5 +1,6 @@
const https = require("https");
const dayjs = require("dayjs");
const mqtt = require("mqtt");
const utc = require("dayjs/plugin/utc");
let timezone = require("dayjs/plugin/timezone");
dayjs.extend(utc);
@ -7,7 +8,7 @@ dayjs.extend(timezone);
const axios = require("axios");
const { Prometheus } = require("../prometheus");
const { debug, UP, DOWN, PENDING, flipStatus, TimeLogger } = require("../../src/util");
const { tcping, ping, dnsResolve, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, errorLog } = require("../util-server");
const { tcping, ping, dnsResolve, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, errorLog, mqttAsync } = require("../util-server");
const { R } = require("redbean-node");
const { BeanModel } = require("redbean-node/dist/bean-model");
const { Notification } = require("../notification");
@ -319,6 +320,14 @@ class Monitor extends BeanModel {
throw new Error("Server not found on Steam");
}
} else if (this.type === "mqtt") {
try {
bean.msg = await mqttAsync(this.url, this.topic, this.successMessage);
bean.status = UP;
} catch (error) {
bean.status = DOWN;
bean.msg = error.message;
}
} else {
bean.msg = "Unknown Monitor Type";
bean.status = PENDING;

@ -10,6 +10,8 @@ const iconv = require("iconv-lite");
const chardet = require("chardet");
const fs = require("fs");
const nodeJsUtil = require("util");
const mqtt = require("mqtt");
// From ping-lite
exports.WIN = /^win/.test(process.platform);
@ -88,6 +90,30 @@ exports.pingAsync = function (hostname, ipv6 = false) {
});
};
exports.mqttAsync = function (hostname, topic, okMessage) {
return new Promise((resolve, reject) => {
try {
let client = mqtt.connect(hostname);
client.on("connect", () => {
client.subscribe(topic);
}
);
client.on("message", (messageTopic, message) => {
console.log(messageTopic);
if (messageTopic == topic && message.toString() !== okMessage) {
client.end();
reject(new Error(`Error; Topic: ${messageTopic}; Message: ${message.toString()}`));
} else {
client.end();
resolve(`Topic: ${messageTopic}; Message: ${message.toString()}`);
}
});
} catch (error) {
reject(new Error(error));
}
});
};
exports.dnsResolve = function (hostname, resolver_server, rrtype) {
const resolver = new Resolver();
resolver.setServers([resolver_server]);

@ -306,4 +306,9 @@ export default {
"One record": "One record",
steamApiKeyDescription: "For monitoring a Steam Game Server you need a Steam Web-API key. You can register your API key here: ",
"Current User": "Current User",
topic: "Topic",
topicExplanation: "MQTT topic to monitor",
successMessage: "Success Message",
successMessageExplanation: "MQTT message that will be considered as success",
url: "Server URL",
};

@ -32,6 +32,9 @@
<option value="steam">
Steam Game Server
</option>
<option value="mqtt">
MQTT
</option>
</select>
</div>
@ -115,6 +118,32 @@
</div>
</template>
<!-- MQTT -->
<!-- For MQTT Type -->
<template v-if="monitor.type === 'mqtt'">
<div class="my-3">
<label for="url" class="form-label">{{ $t("url") }}</label>
<input id="url" v-model="monitor.url" type="text" class="form-control" pattern="https?://.+" required>
</div>
<div class="my-3">
<label for="topic" class="form-label">{{ $t("topic") }}</label>
<input id="topic" v-model="monitor.topic" type="text" class="form-control" required>
<div class="form-text">
{{ $t("topicExplanation") }}
</div>
</div>
<div class="my-3">
<label for="successMessage" class="form-label">{{ $t("successMessage") }}</label>
<input id="successMessage" v-model="monitor.successMessage" type="text" class="form-control" required>
<div class="form-text">
{{ $t("successMessageExplanation") }}
</div>
</div>
</template>
<!-- Interval -->
<div class="my-3">
<label for="interval" class="form-label">{{ $t("Heartbeat Interval") }} ({{ $t("checkEverySecond", [ monitor.interval ]) }})</label>
@ -426,6 +455,8 @@ export default {
accepted_statuscodes: ["200-299"],
dns_resolve_type: "A",
dns_resolve_server: "1.1.1.1",
topic: "",
successMessage: "",
};
for (let i = 0; i < this.$root.notificationList.length; i++) {

Loading…
Cancel
Save