PEARDATA_PROCESSES_MS = 3k
This commit is contained in:
@@ -78,16 +78,66 @@ function makeSensorPowerChart(chip, label) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {Array<{ chartId: string, context: string, values: Record<string, number> }>}
|
||||
*/
|
||||
/**
|
||||
* @returns {Array<{ chartId: string, context: string, values: Record<string, number> }>}
|
||||
*/
|
||||
export function sampleSensors() {
|
||||
/** @type {Array<{ chartId: string, context: string, values: Record<string, number> }>} */
|
||||
const out = []
|
||||
|
||||
for (const entry of getSensorPaths()) {
|
||||
let raw
|
||||
if (entry._type === 'temp') {
|
||||
raw = readFile(entry._inputPath)
|
||||
if (raw == null) continue
|
||||
const milli = Number(raw.trim())
|
||||
if (!Number.isFinite(milli)) continue
|
||||
out.push({ chartId: entry.chartId, context: entry.context, values: { temperature: milli / 1000 } })
|
||||
} else if (entry._type === 'fan') {
|
||||
raw = readFile(entry._inputPath)
|
||||
if (raw == null) continue
|
||||
const rpm = Number(raw.trim())
|
||||
if (!Number.isFinite(rpm)) continue
|
||||
out.push({ chartId: entry.chartId, context: entry.context, values: { rpm } })
|
||||
} else if (entry._type === 'volt') {
|
||||
raw = readFile(entry._inputPath)
|
||||
if (raw == null) continue
|
||||
const mv = Number(raw.trim())
|
||||
if (!Number.isFinite(mv)) continue
|
||||
out.push({ chartId: entry.chartId, context: entry.context, values: { millivolts: mv } })
|
||||
} else if (entry._type === 'power') {
|
||||
raw = readFile(entry._inputPath)
|
||||
if (raw == null) continue
|
||||
const uw = Number(raw.trim())
|
||||
if (!Number.isFinite(uw)) continue
|
||||
out.push({ chartId: entry.chartId, context: entry.context, values: { microwatts: uw } })
|
||||
} else if (entry._type === 'thermal') {
|
||||
raw = readFile(entry._inputPath)
|
||||
if (raw == null) continue
|
||||
const milli = Number(raw.trim())
|
||||
if (!Number.isFinite(milli) || milli === 0) continue
|
||||
const celsius = milli > 1000 ? milli / 1000 : milli
|
||||
out.push({ chartId: entry.chartId, context: entry.context, values: { temperature: celsius } })
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
let _sensorPaths = null
|
||||
let _sensorPathsTs = 0
|
||||
|
||||
function getSensorPaths() {
|
||||
const now = Date.now()
|
||||
if (_sensorPaths && now - _sensorPathsTs < 30000) return _sensorPaths
|
||||
_sensorPaths = []
|
||||
_sensorPathsTs = now
|
||||
|
||||
const hwmonRoot = '/sys/class/hwmon'
|
||||
try {
|
||||
const chips = fs.readdirSync(hwmonRoot)
|
||||
for (const chip of chips) {
|
||||
for (const chip of fs.readdirSync(hwmonRoot)) {
|
||||
const dir = path.join(hwmonRoot, chip)
|
||||
const name = (readFile(path.join(dir, 'name')) || chip).trim()
|
||||
let ents
|
||||
@@ -98,60 +148,40 @@ export function sampleSensors() {
|
||||
}
|
||||
for (const ent of ents) {
|
||||
const m = ent.match(/^temp(\d+)_input$/)
|
||||
if (!m) continue
|
||||
const n = m[1]
|
||||
const raw = readFile(path.join(dir, ent))
|
||||
if (raw == null) continue
|
||||
const milli = Number(raw.trim())
|
||||
if (!Number.isFinite(milli)) continue
|
||||
const label =
|
||||
(readFile(path.join(dir, `temp${n}_label`)) || `temp${n}`).trim() || `temp${n}`
|
||||
const def = makeSensorTempChart(name, label)
|
||||
registerChart(def)
|
||||
out.push({
|
||||
chartId: def.id,
|
||||
context: def.context,
|
||||
values: { temperature: milli / 1000 },
|
||||
})
|
||||
}
|
||||
const fan = ent.match(/^fan(\d+)_input$/)
|
||||
if (fan) {
|
||||
const n = fan[1]
|
||||
const raw = readFile(path.join(dir, ent))
|
||||
if (raw == null) continue
|
||||
const rpm = Number(raw.trim())
|
||||
if (!Number.isFinite(rpm)) continue
|
||||
const label =
|
||||
(readFile(path.join(dir, `fan${n}_label`)) || `fan${n}`).trim() || `fan${n}`
|
||||
const def = makeSensorFanChart(name, label)
|
||||
registerChart(def)
|
||||
out.push({ chartId: def.id, context: def.context, values: { rpm } })
|
||||
}
|
||||
const vin = ent.match(/^in(\d+)_input$/)
|
||||
if (vin) {
|
||||
const n = vin[1]
|
||||
const raw = readFile(path.join(dir, ent))
|
||||
if (raw == null) continue
|
||||
const mv = Number(raw.trim())
|
||||
if (!Number.isFinite(mv)) continue
|
||||
const label =
|
||||
(readFile(path.join(dir, `in${n}_label`)) || `in${n}`).trim() || `in${n}`
|
||||
const def = makeSensorVoltageChart(name, label)
|
||||
registerChart(def)
|
||||
out.push({ chartId: def.id, context: def.context, values: { millivolts: mv } })
|
||||
}
|
||||
const pwr = ent.match(/^power(\d+)_input$/)
|
||||
if (pwr) {
|
||||
const n = pwr[1]
|
||||
const raw = readFile(path.join(dir, ent))
|
||||
if (raw == null) continue
|
||||
const uw = Number(raw.trim())
|
||||
if (!Number.isFinite(uw)) continue
|
||||
const label =
|
||||
(readFile(path.join(dir, `power${n}_label`)) || `power${n}`).trim() || `power${n}`
|
||||
const def = makeSensorPowerChart(name, label)
|
||||
registerChart(def)
|
||||
out.push({ chartId: def.id, context: def.context, values: { microwatts: uw } })
|
||||
if (m) {
|
||||
const n = m[1]
|
||||
const label = (readFile(path.join(dir, `temp${n}_label`)) || `temp${n}`).trim() || `temp${n}`
|
||||
const def = makeSensorTempChart(name, label)
|
||||
registerChart(def)
|
||||
_sensorPaths.push({ chartId: def.id, context: def.context, _type: 'temp', _inputPath: path.join(dir, ent) })
|
||||
continue
|
||||
}
|
||||
const fan = ent.match(/^fan(\d+)_input$/)
|
||||
if (fan) {
|
||||
const n = fan[1]
|
||||
const label = (readFile(path.join(dir, `fan${n}_label`)) || `fan${n}`).trim() || `fan${n}`
|
||||
const def = makeSensorFanChart(name, label)
|
||||
registerChart(def)
|
||||
_sensorPaths.push({ chartId: def.id, context: def.context, _type: 'fan', _inputPath: path.join(dir, ent) })
|
||||
continue
|
||||
}
|
||||
const vin = ent.match(/^in(\d+)_input$/)
|
||||
if (vin) {
|
||||
const n = vin[1]
|
||||
const label = (readFile(path.join(dir, `in${n}_label`)) || `in${n}`).trim() || `in${n}`
|
||||
const def = makeSensorVoltageChart(name, label)
|
||||
registerChart(def)
|
||||
_sensorPaths.push({ chartId: def.id, context: def.context, _type: 'volt', _inputPath: path.join(dir, ent) })
|
||||
continue
|
||||
}
|
||||
const pwr = ent.match(/^power(\d+)_input$/)
|
||||
if (pwr) {
|
||||
const n = pwr[1]
|
||||
const label = (readFile(path.join(dir, `power${n}_label`)) || `power${n}`).trim() || `power${n}`
|
||||
const def = makeSensorPowerChart(name, label)
|
||||
registerChart(def)
|
||||
_sensorPaths.push({ chartId: def.id, context: def.context, _type: 'power', _inputPath: path.join(dir, ent) })
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
@@ -164,20 +194,15 @@ export function sampleSensors() {
|
||||
if (!zone.startsWith('thermal_zone')) continue
|
||||
const dir = path.join(thermalRoot, zone)
|
||||
const type = (readFile(path.join(dir, 'type')) || zone).trim()
|
||||
const raw = readFile(path.join(dir, 'temp'))
|
||||
if (raw == null) continue
|
||||
const milli = Number(raw.trim())
|
||||
if (!Number.isFinite(milli) || milli === 0) continue
|
||||
// some platforms report already in C
|
||||
const celsius = milli > 1000 ? milli / 1000 : milli
|
||||
const def = makeThermalZoneChart(zone, type)
|
||||
registerChart(def)
|
||||
out.push({ chartId: def.id, context: def.context, values: { temperature: celsius } })
|
||||
_sensorPaths.push({ chartId: def.id, context: def.context, _type: 'thermal', _inputPath: path.join(dir, 'temp') })
|
||||
}
|
||||
} catch {
|
||||
// no thermal
|
||||
}
|
||||
return out
|
||||
|
||||
return _sensorPaths
|
||||
}
|
||||
|
||||
export class SensorsCollector extends EventEmitter {
|
||||
|
||||
Reference in New Issue
Block a user