diff --git a/data/custom-thresholds.json b/data/custom-thresholds.json index 9e26dfeeb..6debb34c6 100644 --- a/data/custom-thresholds.json +++ b/data/custom-thresholds.json @@ -1 +1,16 @@ -{} \ No newline at end of file +{ + "primary:103": { + "endpointId": "primary", + "nodeId": "auto-detect", + "vmid": "103", + "thresholds": { + "disk": { + "warning": 70, + "critical": 80 + } + }, + "enabled": true, + "createdAt": "2025-06-01T19:50:34.784Z", + "updatedAt": "2025-06-01T19:50:34.784Z" + } +} \ No newline at end of file diff --git a/server/thresholdRoutes.js b/server/thresholdRoutes.js index dcb747d98..59f1684e6 100644 --- a/server/thresholdRoutes.js +++ b/server/thresholdRoutes.js @@ -56,7 +56,7 @@ function setupThresholdRoutes(app) { // Set custom thresholds for VM/LXC app.post('/api/thresholds/:endpointId/:nodeId/:vmid', async (req, res) => { try { - const { endpointId, nodeId, vmid } = req.params; + let { endpointId, nodeId, vmid } = req.params; const { thresholds } = req.body; if (!thresholds) { @@ -66,6 +66,25 @@ function setupThresholdRoutes(app) { }); } + // Handle auto-detect node + if (nodeId === 'auto-detect') { + const state = require('./state'); + // Find the VM/LXC in the current state + const allGuests = [...(state.vms || []), ...(state.containers || [])]; + const guest = allGuests.find(g => + g.endpointId === endpointId && g.id === vmid + ); + + if (guest && guest.node) { + nodeId = guest.node; + console.log(`[ThresholdRoutes] Auto-detected node '${nodeId}' for ${endpointId}:${vmid}`); + } else { + // If we can't find the node, use a wildcard that will match any node + nodeId = '*'; + console.log(`[ThresholdRoutes] Could not auto-detect node for ${endpointId}:${vmid}, using wildcard`); + } + } + if (!customThresholdManager.initialized) { await customThresholdManager.init(); } @@ -84,7 +103,7 @@ function setupThresholdRoutes(app) { // Update existing custom thresholds app.put('/api/thresholds/:endpointId/:nodeId/:vmid', async (req, res) => { try { - const { endpointId, nodeId, vmid } = req.params; + let { endpointId, nodeId, vmid } = req.params; const { thresholds } = req.body; if (!thresholds) { @@ -94,6 +113,25 @@ function setupThresholdRoutes(app) { }); } + // Handle auto-detect node + if (nodeId === 'auto-detect') { + const state = require('./state'); + // Find the VM/LXC in the current state + const allGuests = [...(state.vms || []), ...(state.containers || [])]; + const guest = allGuests.find(g => + g.endpointId === endpointId && g.id === vmid + ); + + if (guest && guest.node) { + nodeId = guest.node; + console.log(`[ThresholdRoutes] Auto-detected node '${nodeId}' for ${endpointId}:${vmid}`); + } else { + // If we can't find the node, use a wildcard that will match any node + nodeId = '*'; + console.log(`[ThresholdRoutes] Could not auto-detect node for ${endpointId}:${vmid}, using wildcard`); + } + } + if (!customThresholdManager.initialized) { await customThresholdManager.init(); } diff --git a/src/public/js/ui/settings.js b/src/public/js/ui/settings.js index fc97dd5a6..e03381191 100644 --- a/src/public/js/ui/settings.js +++ b/src/public/js/ui/settings.js @@ -1712,14 +1712,24 @@ PulseApp.ui.settings = (() => { // Find the current node for this VM (for display purposes) const selectedGuest = allGuests.find(g => g.endpointId === selectedEndpoint && g.id === selectedVmid); - nodeField.value = selectedGuest ? selectedGuest.node : ''; + + // If we can't find the guest or it doesn't have a node, use a placeholder + // The server will handle finding the actual node + if (selectedGuest && selectedGuest.node) { + nodeField.value = selectedGuest.node; + } else { + // Use a placeholder value that will pass validation + // The server can determine the actual node from endpointId and vmid + nodeField.value = 'auto-detect'; + } // Debug logging console.log('[Settings] Guest selector changed:', { selectedEndpoint, selectedVmid, selectedGuest, - nodeValue: selectedGuest ? selectedGuest.node : 'MISSING' + nodeValue: nodeField.value, + allGuestsCount: allGuests.length }); } else { endpointField.value = '';