mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-08-09 03:28:45 +08:00
Blend json-query and snmp monitors
Utilizes the JSON Query library to handle comparison logic.
This commit is contained in:
@@ -2,7 +2,7 @@ const dayjs = require("dayjs");
|
||||
const axios = require("axios");
|
||||
const { Prometheus } = require("../prometheus");
|
||||
const { log, UP, DOWN, PENDING, MAINTENANCE, flipStatus, MAX_INTERVAL_SECOND, MIN_INTERVAL_SECOND,
|
||||
SQL_DATETIME_FORMAT
|
||||
SQL_DATETIME_FORMAT, evaluateJsonQuery
|
||||
} = require("../../src/util");
|
||||
const { tcping, ping, checkCertificate, checkStatusCode, getTotalClientInRoom, setting, mssqlQuery, postgresQuery, mysqlQuery, setSetting, httpNtlm, radius, grpcQuery,
|
||||
redisPingAsync, mongodbPing, kafkaProducerAsync, getOidcTokenClientCredentials, rootCertificatesFingerprints, axiosAbortSignal
|
||||
@@ -17,7 +17,6 @@ const apicache = require("../modules/apicache");
|
||||
const { UptimeKumaServer } = require("../uptime-kuma-server");
|
||||
const { DockerHost } = require("../docker");
|
||||
const Gamedig = require("gamedig");
|
||||
const jsonata = require("jsonata");
|
||||
const jwt = require("jsonwebtoken");
|
||||
const crypto = require("crypto");
|
||||
const { UptimeCalculator } = require("../uptime-calculator");
|
||||
@@ -610,15 +609,13 @@ class Monitor extends BeanModel {
|
||||
}
|
||||
}
|
||||
|
||||
let expression = jsonata(this.jsonPath);
|
||||
const result = await evaluateJsonQuery(data, this.jsonPath, this.jsonPathOperator, this.expectedValue);
|
||||
|
||||
let result = await expression.evaluate(data);
|
||||
|
||||
if (result.toString() === this.expectedValue) {
|
||||
if (result) {
|
||||
bean.msg += ", expected value is found";
|
||||
bean.status = UP;
|
||||
} else {
|
||||
throw new Error(bean.msg + ", but value is not equal to expected value, value was: [" + result + "]");
|
||||
throw new Error(`${bean.msg}, but value is not equal to expected value, value was: [${result}]`);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,7 +1,6 @@
|
||||
const { MonitorType } = require("./monitor-type");
|
||||
const { UP, DOWN, log } = require("../../src/util");
|
||||
const { UP, DOWN, log, evaluateJsonQuery } = require("../../src/util");
|
||||
const snmp = require("net-snmp");
|
||||
const jsonata = require("jsonata");
|
||||
|
||||
class SNMPMonitorType extends MonitorType {
|
||||
name = "snmp";
|
||||
@@ -45,46 +44,23 @@ class SNMPMonitorType extends MonitorType {
|
||||
// We restrict querying to one OID per monitor, therefore `varbinds[0]` will always contain the value we're interested in.
|
||||
const value = varbinds[0].value;
|
||||
|
||||
// Check if inputs are numeric. If not, re-parse as strings. This ensures comparisons are handled correctly.
|
||||
const expectedValue = isNaN(monitor.expectedValue) ? monitor.expectedValue.toString() : parseFloat(monitor.expectedValue);
|
||||
let snmpResponse = isNaN(value) ? value.toString() : parseFloat(value);
|
||||
const result = await evaluateJsonQuery(value, monitor.jsonPath, monitor.jsonPathOperator, monitor.expectedValue);
|
||||
|
||||
let jsonQueryExpression;
|
||||
switch (monitor.jsonPathOperator) {
|
||||
case ">":
|
||||
case ">=":
|
||||
case "<":
|
||||
case "<=":
|
||||
jsonQueryExpression = `$.value ${monitor.jsonPathOperator} $.control`;
|
||||
break;
|
||||
case "==":
|
||||
jsonQueryExpression = "$string($.value) = $string($.control)";
|
||||
break;
|
||||
case "contains":
|
||||
jsonQueryExpression = "$contains($string($.value), $string($.control))";
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Invalid condition ${monitor.jsonPathOperator}`);
|
||||
}
|
||||
|
||||
const expression = jsonata(jsonQueryExpression);
|
||||
const evaluation = await expression.evaluate({
|
||||
value: snmpResponse,
|
||||
control: expectedValue
|
||||
});
|
||||
heartbeat.status = result ? UP : DOWN;
|
||||
heartbeat.msg = `SNMP value ${result ? "passes" : "does not pass"} comparison: ${snmpValue} ${monitor.snmpCondition} ${snmpControlValue}`;
|
||||
heartbeat.msg = `SNMP value ${result ? "passes" : "does not pass"} `;
|
||||
heartbeat.msg += (monitor.jsonPathOperator === "custom")
|
||||
? `custom query. Query result: ${result}. Expected Value: ${monitor.expectedValue}.`
|
||||
: `comparison: ${value.toString()} ${monitor.jsonPathOperator} ${monitor.expectedValue}.`;
|
||||
|
||||
} catch (err) {
|
||||
heartbeat.status = DOWN;
|
||||
heartbeat.msg = `SNMP Error: ${err.message}`;
|
||||
heartbeat.msg = `Error: ${err.message}`;
|
||||
} finally {
|
||||
if (session) {
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
Reference in New Issue
Block a user