加入自定义指标支持
This commit is contained in:
@@ -535,7 +535,7 @@ function resolveToken(token) {
|
||||
/**
|
||||
* Get detailed metrics for a specific server (node)
|
||||
*/
|
||||
async function getServerDetails(baseUrl, instance, job) {
|
||||
async function getServerDetails(baseUrl, instance, job, settings = {}) {
|
||||
const url = normalizeUrl(baseUrl);
|
||||
const node = resolveToken(instance);
|
||||
|
||||
@@ -582,12 +582,38 @@ async function getServerDetails(baseUrl, instance, job) {
|
||||
|
||||
await Promise.all(queryPromises);
|
||||
|
||||
// Add IP information from Prometheus target (discovered labels or scrape URL)
|
||||
// Add IP information
|
||||
try {
|
||||
let foundIp = false;
|
||||
|
||||
// 1. Try Custom Node Exporter Metric if configured
|
||||
if (settings.ip_metric_name) {
|
||||
try {
|
||||
const expr = `${settings.ip_metric_name}{instance="${node}",job="${job}"}`;
|
||||
const res = await query(url, expr);
|
||||
if (res && res.length > 0) {
|
||||
const address = res[0].metric[settings.ip_label_name || 'address'];
|
||||
if (address) {
|
||||
if (address.includes(':')) {
|
||||
results.ipv6 = [address];
|
||||
results.ipv4 = [];
|
||||
} else {
|
||||
results.ipv4 = [address];
|
||||
results.ipv6 = [];
|
||||
}
|
||||
foundIp = true;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`[Prometheus] Error querying custom IP metric ${settings.ip_metric_name}:`, e.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Fallback to Prometheus Targets API
|
||||
if (!foundIp) {
|
||||
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);
|
||||
@@ -599,20 +625,29 @@ async function getServerDetails(baseUrl, instance, job) {
|
||||
results.ipv4 = [host];
|
||||
results.ipv6 = [];
|
||||
}
|
||||
foundIp = true;
|
||||
} catch (e) {
|
||||
// Fallback to instance label without port
|
||||
const inst = matchedTarget.labels.instance.split(':')[0];
|
||||
results.ipv4 = [inst];
|
||||
results.ipv6 = [];
|
||||
results.ipv4 = [];
|
||||
results.ipv6 = [];
|
||||
}
|
||||
} else {
|
||||
} else {
|
||||
results.ipv4 = [];
|
||||
results.ipv6 = [];
|
||||
}
|
||||
} else {
|
||||
results.ipv4 = [];
|
||||
results.ipv6 = [];
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`[Prometheus] Error fetching target info for ${node}:`, e.message);
|
||||
}
|
||||
|
||||
if (!foundIp) {
|
||||
results.ipv4 = [];
|
||||
results.ipv6 = [];
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`[Prometheus] Critical error resolving IPs for ${node}:`, err.message);
|
||||
results.ipv4 = [];
|
||||
results.ipv6 = [];
|
||||
}
|
||||
|
||||
// Group partitions
|
||||
|
||||
Reference in New Issue
Block a user