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
36 changes: 32 additions & 4 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, mongodbPing, kafkaProducerAsync, getOidcTokenClientCredentials, rootCertificatesFingerprints, axiosAbortSignal
redisPingAsync, mongodbCommand, kafkaProducerAsync, getOidcTokenClientCredentials, rootCertificatesFingerprints, axiosAbortSignal
} = require("../util-server");
const { R } = require("redbean-node");
const { BeanModel } = require("redbean-node/dist/bean-model");
Expand Down Expand Up @@ -817,10 +817,38 @@ class Monitor extends BeanModel {
} else if (this.type === "mongodb") {
let startTime = dayjs().valueOf();

await mongodbPing(this.databaseConnectionString);
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.msg = "";
bean.status = UP;
bean.ping = dayjs().valueOf() - startTime;

} else if (this.type === "radius") {
Expand Down
12 changes: 4 additions & 8 deletions server/util-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,19 +440,15 @@ 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.mongodbPing = async function (connectionString) {
exports.mongodbCommand = async function (connectionString, command) {
let client = await MongoClient.connect(connectionString);
let dbPing = await client.db().command({ ping: 1 });
let result = await client.db().command(command);
await client.close();

if (dbPing["ok"] === 1) {
return "UP";
} else {
throw Error("failed");
}
return result;
};

/**
Expand Down
21 changes: 21 additions & 0 deletions src/pages/EditMonitor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,27 @@
</div>
</template>

<!-- MongoDB -->
<template v-if="monitor.type === 'mongodb'">
<div class="my-3">
<label for="mongodbCommand" class="form-label">{{ $t("Command") }}</label>
SebastianLng marked this conversation as resolved.
Show resolved Hide resolved
<textarea id="mongodbCommand" v-model="monitor.databaseQuery" class="form-control" :placeholder="$t('Example:', [ '{ &quot;ping&quot;: 1 }' ])"></textarea>
SebastianLng marked this conversation as resolved.
Show resolved Hide resolved
SebastianLng marked this conversation as resolved.
Show resolved Hide resolved
</div>
<div class="my-3">
<label for="jsonPath" class="form-label">{{ $t("Json Query") }}</label>
<input id="jsonPath" v-model="monitor.jsonPath" type="text" class="form-control">
SebastianLng marked this conversation as resolved.
Show resolved Hide resolved

<i18n-t tag="div" class="form-text" keypath="jsonQueryDescription">
<a href="https://jsonata.org/">jsonata.org</a>
<a href="https://try.jsonata.org/">{{ $t('here') }}</a>
</i18n-t>
<br>

SebastianLng marked this conversation as resolved.
Show resolved Hide resolved
<label for="expectedValue" class="form-label">{{ $t("Expected Value") }}</label>
<input id="expectedValue" v-model="monitor.expectedValue" type="text" class="form-control">
SebastianLng marked this conversation as resolved.
Show resolved Hide resolved
</div>
</template>

<!-- Interval -->
<div class="my-3">
<label for="interval" class="form-label">{{ $t("Heartbeat Interval") }} ({{ $t("checkEverySecond", [ monitor.interval ]) }})</label>
Expand Down
Loading