From 90634dfacf3d761c0e2c0e3a5dca046d013d7d5d Mon Sep 17 00:00:00 2001 From: CN-JS-HuiBai Date: Fri, 10 Apr 2026 21:38:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=87=87=E9=9B=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/prometheus-service.js | 39 +++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/server/prometheus-service.js b/server/prometheus-service.js index c1897e6..2ec9bb4 100644 --- a/server/prometheus-service.js +++ b/server/prometheus-service.js @@ -559,9 +559,7 @@ async function getServerDetails(baseUrl, instance, job) { sockstatTcpMem: `node_sockstat_TCP_mem{instance="${node}",job="${job}"} * 4096`, // Get individual partitions (excluding virtual and FUSE mounts) partitions_size: `node_filesystem_size_bytes{instance="${node}", job="${job}", fstype!~"tmpfs|autofs|proc|sysfs|fuse.*", mountpoint!~"/tmp.*|/var/lib/docker/.*|/run/.*"}`, - partitions_free: `node_filesystem_free_bytes{instance="${node}", job="${job}", fstype!~"tmpfs|autofs|proc|sysfs|fuse.*", mountpoint!~"/tmp.*|/var/lib/docker/.*|/run/.*"}`, - ipv4: `node_network_address_info{instance="${node}", job="${job}", address!~"127\\\\..*|10\\\\..*|172\\\\.(1[6-9]|2[0-9]|3[0-1])\\\\..*|192\\\\.168\\\\..*", address=~"\\\\d+\\\\.\\\\d+\\\\.\\\\d+\\\\.\\\\d+"}`, - ipv6: `node_network_address_info{instance="${node}", job="${job}", address=~".*:.*", address!~"fe80:.*|::1"}` + partitions_free: `node_filesystem_free_bytes{instance="${node}", job="${job}", fstype!~"tmpfs|autofs|proc|sysfs|fuse.*", mountpoint!~"/tmp.*|/var/lib/docker/.*|/run/.*"}` }; const results = {}; @@ -573,8 +571,6 @@ async function getServerDetails(baseUrl, instance, job) { mountpoint: r.metric.mountpoint, value: parseFloat(r.value[1]) || 0 })); - } else if (key === 'ipv4' || key === 'ipv6') { - results[key] = res.map(r => r.metric.address).filter(a => a); } else { results[key] = res.length > 0 ? parseFloat(res[0].value[1]) : 0; } @@ -586,6 +582,39 @@ async function getServerDetails(baseUrl, instance, job) { await Promise.all(queryPromises); + // Add IP information from Prometheus target (discovered labels or scrape URL) + try { + const targets = await getTargets(baseUrl); + const matchedTarget = targets.find(t => t.labels && t.labels.instance === node && t.labels.job === job); + if (matchedTarget) { + // Extract address from scrapeUrl or instance label + const scrapeUrl = matchedTarget.scrapeUrl || ''; + try { + const urlObj = new URL(scrapeUrl); + const host = urlObj.hostname; + if (host.includes(':')) { + results.ipv6 = [host]; + results.ipv4 = []; + } else { + results.ipv4 = [host]; + results.ipv6 = []; + } + } catch (e) { + // Fallback to instance label without port + const inst = matchedTarget.labels.instance.split(':')[0]; + results.ipv4 = [inst]; + results.ipv6 = []; + } + } else { + results.ipv4 = []; + results.ipv6 = []; + } + } catch (e) { + console.error(`[Prometheus] Error fetching target info for ${node}:`, e.message); + results.ipv4 = []; + results.ipv6 = []; + } + // Group partitions const partitionsMap = {}; (results.partitions_size || []).forEach(p => {