Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom mongodb commands #4445

Merged
merged 6 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Migrate to new monitor-type
  • Loading branch information
Sebastian Lang committed Feb 2, 2024
commit 6ee18efcd073bc1a7281740a7eeb25ac67aeb338
39 changes: 1 addition & 38 deletions server/model/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { log, UP, DOWN, PENDING, MAINTENANCE, flipStatus, MAX_INTERVAL_SECOND, MI
SQL_DATETIME_FORMAT
} = require("../../src/util");
const { tcping, ping, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, mssqlQuery, postgresQuery, mysqlQuery, setSetting, httpNtlm, radius, grpcQuery,
redisPingAsync, mongodbCommand, kafkaProducerAsync, getOidcTokenClientCredentials, rootCertificatesFingerprints, axiosAbortSignal
redisPingAsync, kafkaProducerAsync, getOidcTokenClientCredentials, rootCertificatesFingerprints, axiosAbortSignal
} = require("../util-server");
const { R } = require("redbean-node");
const { BeanModel } = require("redbean-node/dist/bean-model");
Expand Down Expand Up @@ -814,43 +814,6 @@ class Monitor extends BeanModel {
bean.msg = await mysqlQuery(this.databaseConnectionString, this.databaseQuery || "SELECT 1", mysqlPassword);
bean.status = UP;
bean.ping = dayjs().valueOf() - startTime;
} else if (this.type === "mongodb") {
let startTime = dayjs().valueOf();

let command = { "ping": 1 };

if (this.databaseQuery) {
command = JSON.parse(this.databaseQuery);
}

let result = await mongodbCommand(this.databaseConnectionString, command);

if (result["ok"] !== 1) {
throw new Error("MongoDB command failed");
}

if (this.jsonPath) {
let expression = jsonata(this.jsonPath);
result = await expression.evaluate(result);
if (!result) {
throw new Error("Queried value not found.");
}
}

if (this.expectedValue) {
if (result.toString() === this.expectedValue) {
bean.msg = "Expected value found";
bean.status = UP;
} else {
throw new Error("Query executed, but value is not equal to expected value, value was: [" + JSON.stringify(result) + "]");
}
} else {
bean.msg = "";
bean.status = UP;
}

bean.ping = dayjs().valueOf() - startTime;

} else if (this.type === "radius") {
let startTime = dayjs().valueOf();

Expand Down
64 changes: 64 additions & 0 deletions server/monitor-types/mongodb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { MonitorType } = require("./monitor-type");
const { UP } = require("../../src/util");
const { MongoClient } = require("mongodb");
const jsonata = require("jsonata");

class MongodbMonitorType extends MonitorType {

name = "mongodb";

/**
* @inheritdoc
*/
async check(monitor, heartbeat, _server) {
let command = { "ping": 1 };

SebastianLng marked this conversation as resolved.
Show resolved Hide resolved
if (monitor.databaseQuery) {
command = JSON.parse(monitor.databaseQuery);
}

let result = await this.runMongodbCommand(monitor.databaseConnectionString, command);

if (result["ok"] !== 1) {
throw new Error("MongoDB command failed");
CommanderStorm marked this conversation as resolved.
Show resolved Hide resolved
}

if (monitor.jsonPath) {
let expression = jsonata(monitor.jsonPath);
result = await expression.evaluate(result);
if (!result) {
throw new Error("Queried value not found.");
}
}

if (monitor.expectedValue) {
if (result.toString() === monitor.expectedValue) {
heartbeat.msg = "Expected value found";
heartbeat.status = UP;
} else {
throw new Error("Query executed, but value is not equal to expected value, value was: [" + JSON.stringify(result) + "]");
}
} else {
heartbeat.msg = "";
SebastianLng marked this conversation as resolved.
Show resolved Hide resolved
heartbeat.status = UP;
}
}

/**
* Connect to and ping a MongoDB database
* @param {string} connectionString The database connection string
* @param {object} command MongoDB command to run on the database
* @returns {Promise<(string[] | object[] | object)>} Response from
* server
*/
async runMongodbCommand(connectionString, command) {
let client = await MongoClient.connect(connectionString);
let result = await client.db().command(command);
await client.close();
return result;
}
}

module.exports = {
MongodbMonitorType,
};
2 changes: 2 additions & 0 deletions server/uptime-kuma-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class UptimeKumaServer {
UptimeKumaServer.monitorTypeList["tailscale-ping"] = new TailscalePing();
UptimeKumaServer.monitorTypeList["dns"] = new DnsMonitorType();
UptimeKumaServer.monitorTypeList["mqtt"] = new MqttMonitorType();
UptimeKumaServer.monitorTypeList["mongodb"] = new MongodbMonitorType();

// Allow all CORS origins (polling) in development
let cors = undefined;
Expand Down Expand Up @@ -516,3 +517,4 @@ const { RealBrowserMonitorType } = require("./monitor-types/real-browser-monitor
const { TailscalePing } = require("./monitor-types/tailscale-ping");
const { DnsMonitorType } = require("./monitor-types/dns");
const { MqttMonitorType } = require("./monitor-types/mqtt");
const { MongodbMonitorType } = require("./monitor-types/mongodb");
15 changes: 0 additions & 15 deletions server/util-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const mssql = require("mssql");
const { Client } = require("pg");
const postgresConParse = require("pg-connection-string").parse;
const mysql = require("mysql2");
const { MongoClient } = require("mongodb");
const { NtlmClient } = require("axios-ntlm");
const { Settings } = require("./settings");
const grpc = require("@grpc/grpc-js");
Expand Down Expand Up @@ -437,20 +436,6 @@ exports.mysqlQuery = function (connectionString, query, password = undefined) {
});
};

/**
* Connect to and ping a MongoDB database
* @param {string} connectionString The database connection string
* @param {object} command MongoDB command to run on the database
* @returns {Promise<(string[] | object[] | object)>} Response from
* server
*/
exports.mongodbCommand = async function (connectionString, command) {
let client = await MongoClient.connect(connectionString);
let result = await client.db().command(command);
await client.close();
return result;
};

/**
* Query radius server
* @param {string} hostname Hostname of radius server
Expand Down
Loading