Merge branch 'master' into feature/numeric-history-monitors-6024

This commit is contained in:
circlecrystalin 2026-01-18 20:11:08 -08:00 committed by GitHub
commit bdeea53142
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 1136 additions and 618 deletions

View File

@ -0,0 +1,11 @@
exports.up = async function (knex) {
await knex.schema.alterTable("monitor", (table) => {
table.string("snmp_v3_username", 255);
});
};
exports.down = async function (knex) {
await knex.schema.alterTable("monitor", (table) => {
table.dropColumn("snmp_v3_username");
});
};

1274
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -207,7 +207,7 @@
"stylelint-config-standard": "~25.0.0",
"terser": "~5.15.0",
"test": "~3.3.0",
"testcontainers": "^10.13.1",
"testcontainers": "^11.5.0",
"typescript": "~4.4.4",
"v-pagination-3": "~0.1.7",
"vite": "~5.4.15",

View File

@ -165,7 +165,7 @@ class Database {
* Read the database config
* @throws {Error} If the config is invalid
* @typedef {string|undefined} envString
* @returns {{type: "sqlite"} | {type:envString, hostname:envString, port:envString, database:envString, username:envString, password:envString}} Database config
* @returns {{type: "sqlite"} | {type:envString, hostname:envString, port:envString, database:envString, username:envString, password:envString, socketPath:envString}} Database config
*/
static readDBConfig() {
let dbConfig;
@ -185,7 +185,7 @@ class Database {
/**
* @typedef {string|undefined} envString
* @param {{type: "sqlite"} | {type:envString, hostname:envString, port:envString, database:envString, username:envString, password:envString}} dbConfig the database configuration that should be written
* @param {{type: "sqlite"} | {type:envString, hostname:envString, port:envString, database:envString, username:envString, password:envString, socketPath:envString}} dbConfig the database configuration that should be written
* @returns {void}
*/
static writeDBConfig(dbConfig) {
@ -284,6 +284,7 @@ class Database {
port: dbConfig.port,
user: dbConfig.username,
password: dbConfig.password,
socketPath: dbConfig.socketPath,
...(dbConfig.ssl
? {
ssl: {
@ -309,6 +310,7 @@ class Database {
user: dbConfig.username,
password: dbConfig.password,
database: dbConfig.dbName,
socketPath: dbConfig.socketPath,
timezone: "Z",
typeCast: function (field, next) {
if (field.type === "DATETIME") {

View File

@ -1508,8 +1508,6 @@ class Monitor extends BeanModel {
let msg = `[${monitor.name}] [${text}] ${bean.msg}`;
for (let notification of notificationList) {
try {
const heartbeatJSON = await bean.toJSONAsync({ decodeResponse: true });
const monitorData = [{ id: monitor.id, active: monitor.active, name: monitor.name }];
const preloadData = await Monitor.preparePreloadData(monitorData);
@ -1526,6 +1524,30 @@ class Monitor extends BeanModel {
.tz(heartbeatJSON["timezone"])
.format(SQL_DATETIME_FORMAT);
// Calculate downtime tracking information when service comes back up
// This makes downtime information available to all notification providers
if (bean.status === UP && monitor.id) {
try {
const lastDownHeartbeat = await R.getRow(
"SELECT time FROM heartbeat WHERE monitor_id = ? AND status = ? ORDER BY time DESC LIMIT 1",
[monitor.id, DOWN]
);
if (lastDownHeartbeat && lastDownHeartbeat.time) {
heartbeatJSON["lastDownTime"] = lastDownHeartbeat.time;
}
} catch (error) {
// If we can't calculate downtime, just continue without it
// Silently fail to avoid disrupting notification sending
log.debug(
"monitor",
`[${monitor.name}] Could not calculate downtime information: ${error.message}`
);
}
}
for (let notification of notificationList) {
try {
await Notification.send(
JSON.parse(notification.config),
msg,

View File

@ -17,7 +17,22 @@ class SNMPMonitorType extends MonitorType {
timeout: monitor.timeout * 1000,
version: snmp.Version[monitor.snmpVersion],
};
if (monitor.snmpVersion === "3") {
if (!monitor.snmp_v3_username) {
throw new Error("SNMPv3 username is required");
}
// SNMPv3 currently defaults to noAuthNoPriv.
// Supporting authNoPriv / authPriv requires additional inputs
// (auth/priv protocols, passwords), validation, secure storage,
// and database migrations, which is intentionally left for
// a follow-up PR to keep this change scoped.
sessionOptions.securityLevel = snmp.SecurityLevel.noAuthNoPriv;
sessionOptions.username = monitor.snmp_v3_username;
session = snmp.createV3Session(monitor.hostname, monitor.snmp_v3_username, sessionOptions);
} else {
session = snmp.createSession(monitor.hostname, monitor.radiusPassword, sessionOptions);
}
// Handle errors during session creation
session.on("error", (error) => {

View File

@ -56,6 +56,8 @@ class Discord extends NotificationProvider {
// If heartbeatJSON is not null, we go into the normal alerting loop.
let addess = this.extractAddress(monitorJSON);
if (heartbeatJSON["status"] === DOWN) {
const wentOfflineTimestamp = Math.floor(new Date(heartbeatJSON["time"]).getTime() / 1000);
let discorddowndata = {
username: discordDisplayName,
embeds: [
@ -76,6 +78,11 @@ class Discord extends NotificationProvider {
},
]
: []),
{
name: "Went Offline",
// F for full date/time
value: `<t:${wentOfflineTimestamp}:F>`,
},
{
name: `Time (${heartbeatJSON["timezone"]})`,
value: heartbeatJSON["localDateTime"],
@ -104,6 +111,14 @@ class Discord extends NotificationProvider {
await axios.post(webhookUrl.toString(), discorddowndata, config);
return okMsg;
} else if (heartbeatJSON["status"] === UP) {
const backOnlineTimestamp = Math.floor(new Date(heartbeatJSON["time"]).getTime() / 1000);
let downtimeDuration = null;
let wentOfflineTimestamp = null;
if (heartbeatJSON["lastDownTime"]) {
wentOfflineTimestamp = Math.floor(new Date(heartbeatJSON["lastDownTime"]).getTime() / 1000);
downtimeDuration = this.formatDuration(backOnlineTimestamp - wentOfflineTimestamp);
}
let discordupdata = {
username: discordDisplayName,
embeds: [
@ -124,10 +139,23 @@ class Discord extends NotificationProvider {
},
]
: []),
...(wentOfflineTimestamp
? [
{
name: `Time (${heartbeatJSON["timezone"]})`,
value: heartbeatJSON["localDateTime"],
name: "Went Offline",
// F for full date/time
value: `<t:${wentOfflineTimestamp}:F>`,
},
]
: []),
...(downtimeDuration
? [
{
name: "Downtime Duration",
value: downtimeDuration,
},
]
: []),
...(heartbeatJSON["ping"] != null
? [
{
@ -162,6 +190,32 @@ class Discord extends NotificationProvider {
this.throwGeneralAxiosError(error);
}
}
/**
* Format duration as human-readable string (e.g., "1h 23m", "45m 30s")
* TODO: Update below to `Intl.DurationFormat("en", { style: "short" }).format(duration)` once we are on a newer node version
* @param {number} timeInSeconds The time in seconds to format a duration for
* @returns {string} The formatted duration
*/
formatDuration(timeInSeconds) {
const hours = Math.floor(timeInSeconds / 3600);
const minutes = Math.floor((timeInSeconds % 3600) / 60);
const seconds = timeInSeconds % 60;
const durationParts = [];
if (hours > 0) {
durationParts.push(`${hours}h`);
}
if (minutes > 0) {
durationParts.push(`${minutes}m`);
}
if (seconds > 0 && hours === 0) {
// Only show seconds if less than an hour
durationParts.push(`${seconds}s`);
}
return durationParts.length > 0 ? durationParts.join(" ") : "0s";
}
}
module.exports = Discord;

View File

@ -18,7 +18,7 @@ class WeCom extends NotificationProvider {
},
};
config = this.getAxiosConfigWithProxy(config);
let body = this.composeMessage(heartbeatJSON, msg);
let body = this.composeMessage(notification, heartbeatJSON, msg);
await axios.post(
`https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${notification.weComBotKey}`,
body,
@ -32,11 +32,12 @@ class WeCom extends NotificationProvider {
/**
* Generate the message to send
* @param {object} notification Notification configuration
* @param {object} heartbeatJSON Heartbeat details (For Up/Down only)
* @param {string} msg General message
* @returns {object} Message
*/
composeMessage(heartbeatJSON, msg) {
composeMessage(notification, heartbeatJSON, msg) {
let title = "UptimeKuma Message";
if (msg != null && heartbeatJSON != null && heartbeatJSON["status"] === UP) {
title = "UptimeKuma Monitor Up";
@ -44,11 +45,26 @@ class WeCom extends NotificationProvider {
if (msg != null && heartbeatJSON != null && heartbeatJSON["status"] === DOWN) {
title = "UptimeKuma Monitor Down";
}
let textObj = {
content: title + "\n" + msg,
};
// Handle mentioned_mobile_list if configured
if (notification.weComMentionedMobileList?.trim()) {
let mentionedMobiles = notification.weComMentionedMobileList
.split(",")
.map((mobile) => mobile.trim())
.filter((mobile) => mobile.length > 0);
if (mentionedMobiles.length > 0) {
textObj.mentioned_mobile_list = mentionedMobiles;
}
}
return {
msgtype: "text",
text: {
content: title + "\n" + msg,
},
text: textObj,
};
}
}

View File

@ -102,6 +102,7 @@ class SetupDatabase {
dbConfig.dbName = process.env.UPTIME_KUMA_DB_NAME;
dbConfig.username = getEnvOrFile("UPTIME_KUMA_DB_USERNAME");
dbConfig.password = getEnvOrFile("UPTIME_KUMA_DB_PASSWORD");
dbConfig.socketPath = process.env.UPTIME_KUMA_DB_SOCKET?.trim();
dbConfig.ssl = getEnvOrFile("UPTIME_KUMA_DB_SSL")?.toLowerCase() === "true";
dbConfig.ca = getEnvOrFile("UPTIME_KUMA_DB_CA");
Database.writeDBConfig(dbConfig);
@ -160,6 +161,7 @@ class SetupDatabase {
runningSetup: this.runningSetup,
needSetup: this.needSetup,
isEnabledEmbeddedMariaDB: this.isEnabledEmbeddedMariaDB(),
isEnabledMariaDBSocket: process.env.UPTIME_KUMA_DB_SOCKET?.trim().length > 0,
});
});
@ -202,6 +204,11 @@ class SetupDatabase {
// External MariaDB
if (dbConfig.type === "mariadb") {
// If socketPath is provided and not empty, validate it
if (process.env.UPTIME_KUMA_DB_SOCKET?.trim().length > 0) {
dbConfig.socketPath = process.env.UPTIME_KUMA_DB_SOCKET.trim();
} else {
// socketPath not provided, hostname and port are required
if (!dbConfig.hostname) {
response.status(400).json("Hostname is required");
this.runningSetup = false;
@ -213,6 +220,7 @@ class SetupDatabase {
this.runningSetup = false;
return;
}
}
if (!dbConfig.dbName) {
response.status(400).json("Database name is required");
@ -241,6 +249,7 @@ class SetupDatabase {
user: dbConfig.username,
password: dbConfig.password,
database: dbConfig.dbName,
socketPath: dbConfig.socketPath,
...(dbConfig.ssl
? {
ssl: {

View File

@ -35,7 +35,13 @@
:aria-label="selectAll ? $t('deselectAllMonitorsAria') : $t('selectAllMonitorsAria')"
/>
<MonitorListFilter :filterState="filterState" @update-filter="updateFilter" />
<MonitorListFilter
:filterState="filterState"
:allCollapsed="allGroupsCollapsed"
:hasGroups="groupMonitors.length >= 2"
@update-filter="updateFilter"
@toggle-collapse-all="toggleCollapseAll"
/>
</div>
</div>
@ -100,8 +106,8 @@
</div>
<MonitorListItem
v-for="(item, index) in sortedMonitorList"
:key="index"
v-for="item in sortedMonitorList"
:key="`${item.id}-${collapseKey}`"
:monitor="item"
:isSelectMode="selectMode"
:isSelected="isSelected"
@ -154,6 +160,7 @@ export default {
active: null,
tags: null,
},
collapseKey: 0,
};
},
computed: {
@ -229,6 +236,38 @@ export default {
this.searchText !== ""
);
},
/**
* Gets all group monitors at root level that have children
* @returns {Array} Array of group monitors with children
*/
groupMonitors() {
const monitors = Object.values(this.$root.monitorList);
return monitors.filter(
(m) => m.type === "group" && m.parent === null && monitors.some((child) => child.parent === m.id)
);
},
/**
* Determines if all groups are collapsed.
* Note: collapseKey is included to force re-computation when toggleCollapseAll()
* updates localStorage, since Vue cannot detect localStorage changes.
* @returns {boolean} True if all groups are collapsed
*/
allGroupsCollapsed() {
// collapseKey forces this computed to re-evaluate after localStorage updates
if (this.collapseKey < 0 || this.groupMonitors.length === 0) {
return true;
}
const storage = window.localStorage.getItem("monitorCollapsed");
if (storage === null) {
return true; // Default is collapsed
}
const storageObject = JSON.parse(storage);
return this.groupMonitors.every((group) => storageObject[`monitor_${group.id}`] !== false);
},
},
watch: {
searchText() {
@ -303,6 +342,26 @@ export default {
updateFilter(newFilter) {
this.filterState = newFilter;
},
/**
* Toggle collapse state for all group monitors
* @returns {void}
*/
toggleCollapseAll() {
const shouldCollapse = !this.allGroupsCollapsed;
let storageObject = {};
const storage = window.localStorage.getItem("monitorCollapsed");
if (storage !== null) {
storageObject = JSON.parse(storage);
}
this.groupMonitors.forEach((group) => {
storageObject[`monitor_${group.id}`] = shouldCollapse;
});
window.localStorage.setItem("monitorCollapsed", JSON.stringify(storageObject));
this.collapseKey++;
},
/**
* Deselect a monitor
* @param {number} id ID of monitor
@ -731,6 +790,7 @@ export default {
.search-input {
width: 100%;
padding-right: 30px;
transition: none !important;
}
.monitor-item {

View File

@ -137,6 +137,15 @@
</li>
</template>
</MonitorListFilterDropdown>
<button
v-if="hasGroups"
type="button"
class="btn btn-outline-normal btn-collapse-all"
:title="allCollapsed ? $t('Expand All Groups') : $t('Collapse All Groups')"
@click="$emit('toggle-collapse-all')"
>
<font-awesome-icon :icon="allCollapsed ? 'folder' : 'folder-open'" fixed-width />
</button>
</template>
<script>
@ -155,8 +164,16 @@ export default {
type: Object,
required: true,
},
allCollapsed: {
type: Boolean,
default: true,
},
emits: ["updateFilter"],
hasGroups: {
type: Boolean,
default: false,
},
},
emits: ["updateFilter", "toggle-collapse-all"],
data() {
return {
tagsList: [],
@ -322,4 +339,8 @@ export default {
}
}
}
.btn-collapse-all {
transition: none !important;
}
</style>

View File

@ -23,4 +23,16 @@
</a>
</i18n-t>
</div>
<div class="mb-3">
<label for="wecom-mentioned-mobile-list" class="form-label">{{ $t("WeCom Mentioned Mobile List") }}</label>
<input
id="wecom-mentioned-mobile-list"
v-model="$parent.notification.weComMentionedMobileList"
type="text"
class="form-control"
placeholder="13800001111,13900002222,@all"
/>
<p class="form-text">{{ $t("WeCom Mentioned Mobile List Description") }}</p>
</div>
</template>

View File

@ -53,6 +53,8 @@ import {
faInfoCircle,
faClone,
faCertificate,
faFolder,
faFolderOpen,
} from "@fortawesome/free-solid-svg-icons";
library.add(
@ -103,6 +105,8 @@ library.add(
faInfoCircle,
faClone,
faCertificate,
faFolder,
faFolderOpen,
);
export { FontAwesomeIcon };

View File

@ -793,6 +793,8 @@
"Retry": "Retry",
"Topic": "Topic",
"WeCom Bot Key": "WeCom Bot Key",
"WeCom Mentioned Mobile List": "WeCom Mentioned Mobile List",
"WeCom Mentioned Mobile List Description": "Enter phone numbers to mention. Separate multiple numbers with commas. Use {'@'}all to mention everyone.",
"Setup Proxy": "Set Up Proxy",
"Proxy Protocol": "Proxy Protocol",
"Proxy Server": "Proxy Server",
@ -1109,6 +1111,7 @@
"snmpCommunityStringHelptext": "This string functions as a password to authenticate and control access to SNMP-enabled devices. Match it with your SNMP device's configuration.",
"OID (Object Identifier)": "OID (Object Identifier)",
"snmpOIDHelptext": "Enter the OID for the sensor or status you want to monitor. Use network management tools like MIB browsers or SNMP software if you're unsure about the OID.",
"snmpV3Username": "SNMPv3 Username",
"Condition": "Condition",
"SNMP Version": "SNMP Version",
"Please enter a valid OID.": "Please enter a valid OID.",
@ -1368,5 +1371,8 @@
"Expected TLS Alert": "Expected TLS Alert",
"None (Successful Connection)": "None (Successful Connection)",
"expectedTlsAlertDescription": "Select the TLS alert you expect the server to return. Use {code} to verify mTLS endpoints reject connections without client certificates. See {link} for details.",
"TLS Alert Spec": "RFC 8446"
"TLS Alert Spec": "RFC 8446",
"mariadbSocketPathDetectedHelptext": "Connecting to the database as specified via the {0} environment variable.",
"Expand All Groups": "Expand All Groups",
"Collapse All Groups": "Collapse All Groups"
}

View File

@ -1,7 +1,7 @@
<template>
<div class="container-fluid">
<div class="row">
<div v-if="!$root.isMobile" class="col-12 col-md-5 col-xl-4">
<div v-if="!$root.isMobile" class="col-12 col-md-5 col-xl-4 ps-0">
<div>
<router-link to="/add" class="btn btn-primary mb-3">
<font-awesome-icon icon="plus" />

View File

@ -5,7 +5,7 @@
{{ $t("Quick Stats") }}
</h1>
<div class="shadow-box big-padding text-center mb-4">
<div class="shadow-box big-padding text-center mb-3">
<div class="row">
<div class="col">
<h3>{{ $t("Up") }}</h3>

View File

@ -511,8 +511,23 @@
<select id="snmp_version" v-model="monitor.snmpVersion" class="form-select">
<option value="1">SNMPv1</option>
<option value="2c">SNMPv2c</option>
<option value="3">SNMPv3</option>
</select>
</div>
<div v-if="monitor.type === 'snmp' && monitor.snmpVersion === '3'" class="my-3">
<label for="snmp_v3_username" class="form-label">
{{ $t("snmpV3Username") }}
</label>
<input
id="snmp_v3_username"
v-model="monitor.snmpV3Username"
type="text"
class="form-control"
placeholder="SNMPv3 username"
required
/>
</div>
<div v-if="monitor.type === 'smtp'" class="my-3">
<label for="smtp_security" class="form-label">{{ $t("SMTP Security") }}</label>

View File

@ -79,7 +79,7 @@
</div>
<template v-if="dbConfig.type === 'mariadb'">
<div class="form-floating mt-3 short">
<div v-if="!isProvidedMariaDBSocket" class="form-floating mt-3 short">
<input
id="floatingInput"
v-model="dbConfig.hostname"
@ -90,11 +90,19 @@
<label for="floatingInput">{{ $t("Hostname") }}</label>
</div>
<div class="form-floating mt-3 short">
<div v-if="!isProvidedMariaDBSocket" class="form-floating mt-3 short">
<input id="floatingInput" v-model="dbConfig.port" type="text" class="form-control" required />
<label for="floatingInput">{{ $t("Port") }}</label>
</div>
<div v-if="isProvidedMariaDBSocket" class="mt-1 short text-start">
<i18n-t keypath="mariadbSocketPathDetectedHelptext" tag="div" class="form-text">
<code>UPTIME_KUMA_DB_SOCKET</code>
</i18n-t>
</div>
<hr v-if="isProvidedMariaDBSocket" class="mt-3 mb-2 short" />
<div class="form-floating mt-3 short">
<input
id="floatingInput"
@ -198,6 +206,9 @@ export default {
disabledButton() {
return this.dbConfig.type === undefined || this.info.runningSetup;
},
isProvidedMariaDBSocket() {
return this.info.isEnabledMariaDBSocket;
},
},
async mounted() {
let res = await axios.get("/setup-database-info");

View File

@ -0,0 +1,128 @@
const { describe, test } = require("node:test");
const assert = require("node:assert/strict");
const { GenericContainer } = require("testcontainers");
const { SNMPMonitorType } = require("../../server/monitor-types/snmp");
const { UP } = require("../../src/util");
const snmp = require("net-snmp");
describe("SNMPMonitorType", () => {
test(
"check() sets heartbeat to UP when SNMP agent responds",
{
skip: !!process.env.CI && (process.platform !== "linux" || process.arch !== "x64"),
},
async () => {
const container = await new GenericContainer("polinux/snmpd").withExposedPorts("161/udp").start();
try {
// Get the mapped UDP port
const hostPort = container.getMappedPort("161/udp");
const hostIp = container.getHost();
// UDP service small wait to ensure snmpd is ready inside container
await new Promise((r) => setTimeout(r, 2000));
const monitor = {
type: "snmp",
hostname: hostIp,
port: hostPort,
snmpVersion: "2c",
radiusPassword: "public",
snmpOid: "1.3.6.1.2.1.1.1.0",
timeout: 5,
maxretries: 1,
jsonPath: "$",
jsonPathOperator: "!=",
expectedValue: "",
};
const snmpMonitor = new SNMPMonitorType();
const heartbeat = {};
await snmpMonitor.check(monitor, heartbeat);
assert.strictEqual(heartbeat.status, UP);
assert.match(heartbeat.msg, /JSON query passes/);
} finally {
await container.stop();
}
}
);
test(
"check() throws when SNMP agent does not respond",
{
skip: !!process.env.CI && (process.platform !== "linux" || process.arch !== "x64"),
},
async () => {
const monitor = {
type: "snmp",
hostname: "127.0.0.1",
port: 65530, // Assuming no SNMP agent is running here
snmpVersion: "2c",
radiusPassword: "public",
snmpOid: "1.3.6.1.2.1.1.1.0",
timeout: 1,
maxretries: 1,
};
const snmpMonitor = new SNMPMonitorType();
const heartbeat = {};
await assert.rejects(() => snmpMonitor.check(monitor, heartbeat), /timeout|RequestTimedOutError/i);
}
);
test("check() uses SNMPv3 noAuthNoPriv session when version is 3", async () => {
const originalCreateV3Session = snmp.createV3Session;
const originalCreateSession = snmp.createSession;
let createV3Called = false;
let createSessionCalled = false;
let receivedOptions = null;
// Stub createV3Session
snmp.createV3Session = function (_host, _username, options) {
createV3Called = true;
receivedOptions = options;
return {
on: () => {},
close: () => {},
// Stop execution after session creation to avoid real network I/O.
get: (_oids, cb) => cb(new Error("stop test here")),
};
};
// Stub createSession
snmp.createSession = function () {
createSessionCalled = true;
return {};
};
const monitor = {
type: "snmp",
hostname: "127.0.0.1",
port: 161,
timeout: 5,
maxretries: 1,
snmpVersion: "3",
snmp_v3_username: "testuser",
snmpOid: "1.3.6.1.2.1.1.1.0",
};
const snmpMonitor = new SNMPMonitorType();
const heartbeat = {};
await assert.rejects(() => snmpMonitor.check(monitor, heartbeat), /stop test here/);
// Assertions
assert.strictEqual(createV3Called, true);
assert.strictEqual(createSessionCalled, false);
assert.strictEqual(receivedOptions.securityLevel, snmp.SecurityLevel.noAuthNoPriv);
// Restore originals
snmp.createV3Session = originalCreateV3Session;
snmp.createSession = originalCreateSession;
});
});