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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions server/monitor-types/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class MongodbMonitorType extends MonitorType {
*/
async check(monitor, heartbeat, _server) {
let command = { "ping": 1 };

if (monitor.databaseQuery) {
command = JSON.parse(monitor.databaseQuery);
}
Expand All @@ -21,27 +20,29 @@ class MongodbMonitorType extends MonitorType {

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

if (monitor.jsonPath) {
let expression = jsonata(monitor.jsonPath);
result = await expression.evaluate(result);
if (!result) {
if (result) {
heartbeat.msg = "Command executed successfully and the jsonata expression produces a result.";
} else {
throw new Error("Queried value not found.");
}
}

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

heartbeat.status = UP;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -883,5 +883,7 @@
"deleteRemoteBrowserMessage": "Are you sure want to delete this Remote Browser for all monitors?",
"GrafanaOncallUrl": "Grafana Oncall URL",
"Browser Screenshot": "Browser Screenshot",
"What is a Remote Browser?": "What is a Remote Browser?"
"What is a Remote Browser?": "What is a Remote Browser?",
"Command": "Command",
"mongodbCommandDescription": "Run a MongoDB command against the database. For information about the available commands check out the MongoDB documentation {0}."
CommanderStorm marked this conversation as resolved.
Show resolved Hide resolved
}
7 changes: 5 additions & 2 deletions src/pages/EditMonitor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@
<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
<i18n-t tag="div" class="form-text" keypath="mongodbCommandDescription">
<a href="https://www.mongodb.com/docs/manual/reference/command/">{{ $t('here') }}</a>
CommanderStorm marked this conversation as resolved.
Show resolved Hide resolved
</i18n-t>
</div>
<div class="my-3">
<label for="jsonPath" class="form-label">{{ $t("Json Query") }}</label>
Expand All @@ -448,8 +451,8 @@
<a href="https://jsonata.org/">jsonata.org</a>
<a href="https://try.jsonata.org/">{{ $t('here') }}</a>
</i18n-t>
<br>

</div>
<div class="my-3">
<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>
Expand Down