Configurar Top-Rack como ajuste global

This commit is contained in:
2026-06-05 23:23:30 +02:00
parent 5eb7964a7b
commit c4af121597
5 changed files with 68 additions and 23 deletions
+28 -4
View File
@@ -49,6 +49,7 @@ async function readConfig() {
parsed.temperatureThresholdsC = normalizeTemperatureThresholds(parsed.temperatureThresholdsC);
parsed.metricThresholdsPercent = normalizeMetricThresholds(parsed.metricThresholdsPercent);
parsed.devices = Array.isArray(parsed.devices) ? parsed.devices.map(normalizeDeviceConfig) : [];
normalizeRackTemperatureConfig(parsed);
return parsed;
}
@@ -65,6 +66,7 @@ async function writeConfig(nextConfig) {
metricThresholdsPercent: normalizeMetricThresholds(nextConfig.metricThresholdsPercent),
devices: Array.isArray(nextConfig.devices) ? nextConfig.devices.map(normalizeDeviceConfig) : []
};
normalizeRackTemperatureConfig(normalized);
await fs.mkdir(path.dirname(CONFIG_PATH), { recursive: true });
await fs.writeFile(CONFIG_PATH, JSON.stringify(normalized, null, 2) + "\n", "utf8");
config = normalized;
@@ -80,6 +82,19 @@ function normalizeDeviceConfig(device = {}) {
};
}
function normalizeRackTemperatureConfig(nextConfig) {
nextConfig.rackTemperatureSourceDeviceId = String(nextConfig.rackTemperatureSourceDeviceId || "");
nextConfig.rackTemperatureFile = String(nextConfig.rackTemperatureFile || "");
if (!nextConfig.rackTemperatureSourceDeviceId || !nextConfig.rackTemperatureFile) {
const legacyDevice = nextConfig.devices.find((device) => device.rackTemperatureFile);
if (legacyDevice) {
nextConfig.rackTemperatureSourceDeviceId = legacyDevice.id || "";
nextConfig.rackTemperatureFile = legacyDevice.rackTemperatureFile || "";
}
}
}
function classifyTemp(tempC) {
const thresholds = config.temperatureThresholdsC || {};
if (tempC >= (thresholds.critical || 80)) return "critical";
@@ -130,6 +145,7 @@ function mockMetrics(device) {
const temp = seededNumber(`${device.id}:temp`, baseTemp - 4, baseTemp + 18);
const dockerRole = /docker|gitea|nginx/i.test(device.role || "");
const haRole = /home assistant/i.test(device.role || "");
const rackTemperatureFile = rackTemperatureFileForDevice(device);
return {
cpuTempC: Number(temp.toFixed(1)),
@@ -166,13 +182,13 @@ function mockMetrics(device) {
rxBytesPerSecond: Math.round(seededNumber(`${device.id}:rx`, 1200, 95000)),
txBytesPerSecond: Math.round(seededNumber(`${device.id}:tx`, 700, 55000))
},
rackTemperature: device.rackTemperatureFile
rackTemperature: rackTemperatureFile
? {
temperatureC: Number(seededNumber(`${device.id}:rack-temp`, 25, 37).toFixed(1)),
sensor: "rack-top",
updatedAt: new Date().toISOString(),
ageSeconds: 0,
sourceFile: device.rackTemperatureFile
sourceFile: rackTemperatureFile
}
: null
};
@@ -340,6 +356,13 @@ function shellQuote(value) {
return `'${String(value || "").replace(/'/g, `'\\''`)}'`;
}
function rackTemperatureFileForDevice(device) {
const sourceId = config.rackTemperatureSourceDeviceId || "";
const sourceMatches = sourceId && [device.id, device.host, device.name].includes(sourceId);
if (sourceMatches) return config.rackTemperatureFile || "";
return device.rackTemperatureFile || "";
}
function networkRates(device, values) {
const sampleKey = device.id || `${device.host}:${device.port || 22}`;
const now = Date.now();
@@ -470,7 +493,8 @@ async function sshMetrics(device) {
];
}
const remoteScript = `RACK_TEMPERATURE_FILE=${shellQuote(device.rackTemperatureFile || "")}\n${REMOTE_METRICS_SCRIPT}`;
const rackTemperatureFile = rackTemperatureFileForDevice(device);
const remoteScript = `RACK_TEMPERATURE_FILE=${shellQuote(rackTemperatureFile)}\n${REMOTE_METRICS_SCRIPT}`;
const { stdout } = await runCommandWithInput(command, args, remoteScript, timeoutMs);
const values = parseKeyValueOutput(stdout);
const temp = toNumber(values.cpuTempC, 0);
@@ -511,7 +535,7 @@ async function sshMetrics(device) {
sensor: values.rackTemperatureSensor || "",
updatedAt: values.rackTemperatureUpdatedAt || "",
ageSeconds: toNumber(values.rackTemperatureAgeSeconds, null),
sourceFile: device.rackTemperatureFile || ""
sourceFile: rackTemperatureFile
}
: null
};