rigtop-core 0.4.0

Data collection core of rigtop: CPU, memory, GPU (NVML), sensors, storage health (SMART, btrfs, mdadm), network and processes as serializable snapshots
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! Temperatures, fans and CPU power on Windows, from LibreHardwareMonitor.
//!
//! Windows has no unprivileged interface for these readings, so rigtop reads
//! them from LibreHardwareMonitor (LHM), which runs elevated with its own
//! driver. LHM dropped its WMI provider in v0.9.5; what remains is the
//! `data.json` of its "Remote Web Server", a tree of
//! computer → hardware → sensor type → sensor nodes:
//!
//! ```json
//! {"Text": "AMD Ryzen 9 8945HX", "HardwareId": "/amdcpu/0", "Children": [
//!   {"Text": "Temperatures", "Children": [
//!     {"Text": "Core (Tctl/Tdie)", "SensorId": "/amdcpu/0/temperature/2",
//!      "Type": "Temperature", "Value": "52.4 °C", "RawValue": 52.375}]}]}
//! ```

use std::io::{BufRead, BufReader, Read, Write};
use std::net::{TcpStream, ToSocketAddrs};
use std::time::{Duration, Instant};

use serde::Deserialize;

use crate::snapshot::{Fan, SensorChip, Temperature};

/// How long to wait for LHM before giving up on this sample.
const TIMEOUT: Duration = Duration::from_millis(800);
/// How long to wait before trying again after LHM was unreachable.
const RETRY_AFTER: Duration = Duration::from_secs(10);
/// Upper bound for the response, against a misconfigured URL.
const MAX_RESPONSE: u64 = 16 * 1024 * 1024;

/// Prefix of the chip keys, to tell them apart from hwmon chips.
const KEY_PREFIX: &str = "lhm:";

/// What rigtop takes from LHM.
#[derive(Debug, Default, Clone)]
pub struct LhmReadings {
    /// One chip per hardware item with temperatures or fans.
    pub chips: Vec<SensorChip>,
    /// The CPU's temperatures, with short labels (`Tctl`, `Tccd1`, `Package`).
    pub cpu_temperatures: Vec<Temperature>,
    /// CPU package power.
    pub cpu_package_w: Option<f32>,
}

/// Polls LHM's web server, backing off while it is unreachable.
pub struct LhmReader {
    url: String,
    retry_at: Option<Instant>,
    /// Why the last attempt failed, kept while waiting to retry.
    problem: Option<String>,
}

impl LhmReader {
    /// Reads from `url` (the `data.json` address); an empty URL disables it.
    pub fn new(url: &str) -> Self {
        Self {
            url: url.trim().to_owned(),
            retry_at: None,
            problem: None,
        }
    }

    /// Why LHM could not be read, until it can.
    pub fn problem(&self) -> Option<&str> {
        self.problem.as_deref()
    }

    /// Current readings, or `None` if disabled or LHM is not reachable.
    pub fn read(&mut self, now: Instant) -> Option<LhmReadings> {
        if self.url.is_empty() || self.retry_at.is_some_and(|at| now < at) {
            return None;
        }
        match http_get(&self.url, TIMEOUT).and_then(|body| parse(&body)) {
            Ok(readings) => {
                self.retry_at = None;
                self.problem = None;
                Some(readings)
            }
            Err(err) => {
                // Usually LHM is not running or its web server is off; do
                // not slow every sample down with a failing request.
                self.retry_at = Some(now + RETRY_AFTER);
                self.problem = Some(err);
                None
            }
        }
    }
}

/// A node of `data.json`. Only the fields rigtop uses; older LHM versions
/// lack `SensorId`, `Type` and `RawValue`.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
struct Node {
    #[serde(rename = "Text")]
    text: String,
    #[serde(rename = "HardwareId")]
    hardware_id: Option<String>,
    #[serde(rename = "SensorId")]
    sensor_id: Option<String>,
    #[serde(rename = "Type")]
    kind: Option<String>,
    /// Formatted value such as `"52.4 °C"` (locale-dependent decimal mark).
    #[serde(rename = "Value")]
    value: String,
    /// Unformatted value; null when the sensor has no reading.
    #[serde(rename = "RawValue")]
    raw_value: serde_json::Value,
    #[serde(rename = "Children")]
    children: Vec<Node>,
}

/// Sensor types rigtop uses.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Kind {
    Temperature,
    Fan,
    Power,
}

impl Kind {
    /// From the `Type` field, the type in the sensor id
    /// (`/amdcpu/0/temperature/2`) or the group node (`Temperatures`).
    fn of(sensor: &Node, group: Option<&Node>) -> Option<Self> {
        let from_name = |name: &str| match name.to_ascii_lowercase().trim_end_matches('s') {
            "temperature" => Some(Kind::Temperature),
            "fan" => Some(Kind::Fan),
            "power" => Some(Kind::Power),
            _ => None,
        };
        if let Some(kind) = &sensor.kind {
            return from_name(kind);
        }
        if let Some(id) = &sensor.sensor_id {
            let mut segments = id.rsplit('/');
            segments.next();
            return segments.next().and_then(from_name);
        }
        group.and_then(|g| from_name(&g.text))
    }
}

/// Parses `data.json`.
fn parse(body: &[u8]) -> Result<LhmReadings, String> {
    let root: Node = serde_json::from_slice(body).map_err(|e| e.to_string())?;
    let mut readings = LhmReadings::default();
    let mut ancestors = Vec::new();
    walk(&root, &mut ancestors, &mut readings);
    readings
        .chips
        .retain(|c| !c.temperatures.is_empty() || !c.fans.is_empty());
    Ok(readings)
}

fn walk<'a>(node: &'a Node, ancestors: &mut Vec<&'a Node>, out: &mut LhmReadings) {
    if node.children.is_empty() {
        if !ancestors.is_empty() {
            add_sensor(node, ancestors, out);
        }
        return;
    }
    ancestors.push(node);
    for child in &node.children {
        walk(child, ancestors, out);
    }
    ancestors.pop();
}

fn add_sensor(sensor: &Node, ancestors: &[&Node], out: &mut LhmReadings) {
    let group = ancestors.last().copied();
    let Some(kind) = Kind::of(sensor, group) else {
        return;
    };
    let Some(value) = sensor_value(sensor) else {
        return;
    };
    // The nearest hardware node; old versions have no ids, so the parent of
    // the group node stands in.
    let hardware = ancestors
        .iter()
        .rev()
        .find(|n| n.hardware_id.is_some())
        .or_else(|| ancestors.len().checked_sub(2).map(|i| &ancestors[i]));
    let Some(hardware) = hardware else {
        return;
    };
    let hardware_id = hardware.hardware_id.as_deref().unwrap_or(&hardware.text);
    // NVML reports NVIDIA GPUs in more detail.
    if hardware_id.starts_with("/gpu-nvidia") {
        return;
    }
    let is_cpu = ["/amdcpu", "/intelcpu", "/cpu"]
        .iter()
        .any(|p| hardware_id.starts_with(p));
    let chip_key = format!("{KEY_PREFIX}{hardware_id}");
    let key = channel_key(&chip_key, sensor, kind);

    if kind == Kind::Power {
        if is_cpu && matches!(sensor.text.as_str(), "Package" | "CPU Package") {
            out.cpu_package_w = Some(value as f32);
        }
        return;
    }
    let index = match out.chips.iter().position(|c| c.key == chip_key) {
        Some(index) => index,
        None => {
            out.chips.push(SensorChip {
                key: chip_key,
                name: hardware.text.clone(),
                temperatures: Vec::new(),
                fans: Vec::new(),
            });
            out.chips.len() - 1
        }
    };
    let chip = &mut out.chips[index];
    match kind {
        Kind::Temperature => {
            let temperature = Temperature {
                key,
                label: sensor.text.clone(),
                celsius: value as f32,
                max_celsius: None,
                critical_celsius: None,
            };
            if is_cpu && let Some(label) = cpu_label(&sensor.text, hardware_id) {
                out.cpu_temperatures.push(Temperature {
                    label,
                    ..temperature.clone()
                });
            }
            chip.temperatures.push(temperature);
        }
        Kind::Fan => chip.fans.push(Fan {
            key,
            label: sensor.text.clone(),
            rpm: value.max(0.0).round() as u32,
        }),
        Kind::Power => {}
    }
}

/// `RawValue`, else the number at the start of the formatted `Value`.
fn sensor_value(sensor: &Node) -> Option<f64> {
    if let Some(value) = sensor.raw_value.as_f64() {
        return value.is_finite().then_some(value);
    }
    let number: String = sensor
        .value
        .trim()
        .chars()
        .take_while(|c| c.is_ascii_digit() || matches!(c, '.' | ',' | '-'))
        .map(|c| if c == ',' { '.' } else { c })
        .collect();
    number.parse().ok()
}

/// `"<chip key>/temp2"` from the sensor id `/amdcpu/0/temperature/2`, like
/// the hwmon keys; the sensor name when there is no id.
fn channel_key(chip_key: &str, sensor: &Node, kind: Kind) -> String {
    let prefix = match kind {
        Kind::Temperature => "temp",
        Kind::Fan => "fan",
        Kind::Power => "power",
    };
    match sensor
        .sensor_id
        .as_deref()
        .and_then(|id| id.rsplit('/').next())
    {
        Some(index) => format!("{chip_key}/{prefix}{index}"),
        None => format!("{chip_key}/{}", sensor.text),
    }
}

/// Short label for a CPU temperature, or `None` for readings left out of
/// the CPU line (derived maxima and averages, Intel's distance to TjMax,
/// and AMD's per-core readings next to the per-CCD ones).
fn cpu_label(name: &str, hardware_id: &str) -> Option<String> {
    if name.contains("Max") || name.contains("Average") || name.contains("Distance") {
        return None;
    }
    let label = match name {
        "Core (Tctl/Tdie)" | "Core (Tctl)" => "Tctl".to_owned(),
        "Core (Tdie)" => "Tdie".to_owned(),
        "CPU Package" => "Package".to_owned(),
        _ => {
            // "CCD1 (Tdie)" -> "Tccd1"
            if let Some(ccd) = name
                .strip_prefix("CCD")
                .and_then(|r| r.strip_suffix(" (Tdie)"))
            {
                format!("Tccd{ccd}")
            } else if let Some(core) = name.strip_prefix("Core #") {
                if hardware_id.starts_with("/amdcpu") {
                    return None;
                }
                format!("Core {core}")
            } else {
                name.to_owned()
            }
        }
    };
    Some(label)
}

/// A minimal HTTP/1.0 GET for `http://host[:port]/path`, enough for LHM's
/// local web server (no TLS, redirects or authentication).
fn http_get(url: &str, timeout: Duration) -> Result<Vec<u8>, String> {
    let rest = url
        .strip_prefix("http://")
        .ok_or_else(|| format!("only http:// URLs are supported: {url}"))?;
    let (authority, path) = match rest.find('/') {
        Some(slash) => (&rest[..slash], &rest[slash..]),
        None => (rest, "/"),
    };
    let address = if authority.contains(':') {
        authority.to_owned()
    } else {
        format!("{authority}:80")
    };
    let socket = address
        .to_socket_addrs()
        .map_err(|e| format!("{authority}: {e}"))?
        .next()
        .ok_or_else(|| format!("{authority}: no address"))?;
    let mut stream =
        TcpStream::connect_timeout(&socket, timeout).map_err(|e| format!("{authority}: {e}"))?;
    stream
        .set_read_timeout(Some(timeout))
        .and_then(|_| stream.set_write_timeout(Some(timeout)))
        .map_err(|e| e.to_string())?;
    // HTTP/1.0 keeps the response simple: no chunked encoding, and the
    // server closes the connection at the end.
    let request =
        format!("GET {path} HTTP/1.0\r\nHost: {authority}\r\nAccept: application/json\r\n\r\n");
    stream
        .write_all(request.as_bytes())
        .map_err(|e| e.to_string())?;

    let mut reader = BufReader::new(stream.take(MAX_RESPONSE));
    let mut status = String::new();
    reader.read_line(&mut status).map_err(|e| e.to_string())?;
    let code = status.split_whitespace().nth(1).unwrap_or("");
    if code != "200" {
        return Err(format!("HTTP status {:?}", status.trim()));
    }
    // Skip the headers.
    loop {
        let mut line = String::new();
        let read = reader.read_line(&mut line).map_err(|e| e.to_string())?;
        if read == 0 || line == "\r\n" || line == "\n" {
            break;
        }
    }
    let mut body = Vec::new();
    reader.read_to_end(&mut body).map_err(|e| e.to_string())?;
    Ok(body)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::net::TcpListener;
    use std::thread;

    /// Shaped like LHM 0.9.6 output, trimmed.
    const SAMPLE: &str = r#"{"id":0,"Text":"Sensor","Min":"Min","Value":"Value","Max":"Max","ImageURL":"","Children":[
      {"id":1,"Text":"CARMEL","Min":"","Value":"","Max":"","ImageURL":"images_icon/computer.png","Children":[
        {"id":2,"Text":"AMD Ryzen 9 8945HX","HardwareId":"/amdcpu/0","Value":"","Children":[
          {"id":3,"Text":"Temperatures","Value":"","Children":[
            {"id":4,"Text":"Core (Tctl/Tdie)","SensorId":"/amdcpu/0/temperature/2","Type":"Temperature","Value":"52.4 °C","RawValue":52.375,"Children":[]},
            {"id":5,"Text":"CCD1 (Tdie)","SensorId":"/amdcpu/0/temperature/3","Type":"Temperature","Value":"48.1 °C","RawValue":48.125,"Children":[]},
            {"id":6,"Text":"CCDs Max (Tdie)","SensorId":"/amdcpu/0/temperature/5","Type":"Temperature","Value":"48.1 °C","RawValue":48.125,"Children":[]},
            {"id":7,"Text":"Core #1","SensorId":"/amdcpu/0/temperature/7","Type":"Temperature","Value":"45.0 °C","RawValue":45.0,"Children":[]}]},
          {"id":8,"Text":"Powers","Value":"","Children":[
            {"id":9,"Text":"Package","SensorId":"/amdcpu/0/power/0","Type":"Power","Value":"35.2 W","RawValue":35.2,"Children":[]},
            {"id":10,"Text":"Core #1 (SMU)","SensorId":"/amdcpu/0/power/1","Type":"Power","Value":"1.2 W","RawValue":1.2,"Children":[]}]},
          {"id":11,"Text":"Load","Value":"","Children":[
            {"id":12,"Text":"CPU Total","SensorId":"/amdcpu/0/load/0","Type":"Load","Value":"3.0 %","RawValue":3.0,"Children":[]}]}]},
        {"id":13,"Text":"ASUS ROG STRIX","HardwareId":"/motherboard","Value":"","Children":[
          {"id":14,"Text":"Nuvoton NCT6799D","HardwareId":"/lpc/nct6799d/0","Value":"","Children":[
            {"id":15,"Text":"Fans","Value":"","Children":[
              {"id":16,"Text":"Fan #1","SensorId":"/lpc/nct6799d/0/fan/0","Type":"Fan","Value":"918 RPM","RawValue":918.4,"Children":[]},
              {"id":17,"Text":"Fan #2","SensorId":"/lpc/nct6799d/0/fan/1","Type":"Fan","Value":"-","RawValue":null,"Children":[]}]},
            {"id":18,"Text":"Temperatures","Value":"","Children":[
              {"id":19,"Text":"System","SensorId":"/lpc/nct6799d/0/temperature/1","Type":"Temperature","Value":"34.0 °C","RawValue":34.0,"Children":[]}]}]}]},
        {"id":20,"Text":"NVIDIA GeForce RTX 4090","HardwareId":"/gpu-nvidia/0","Value":"","Children":[
          {"id":21,"Text":"Temperatures","Value":"","Children":[
            {"id":22,"Text":"GPU Core","SensorId":"/gpu-nvidia/0/temperature/0","Type":"Temperature","Value":"59.0 °C","RawValue":59.0,"Children":[]}]}]},
        {"id":23,"Text":"Generic Memory","HardwareId":"/ram","Value":"","Children":[
          {"id":24,"Text":"Load","Value":"","Children":[
            {"id":25,"Text":"Memory","SensorId":"/ram/load/0","Type":"Load","Value":"46.0 %","RawValue":46.0,"Children":[]}]}]}]}]}"#;

    #[test]
    fn parses_cpu_motherboard_and_skips_nvidia() {
        let readings = parse(SAMPLE.as_bytes()).unwrap();
        let labels: Vec<&str> = readings
            .cpu_temperatures
            .iter()
            .map(|t| t.label.as_str())
            .collect();
        assert_eq!(labels, ["Tctl", "Tccd1"], "no maxima or AMD per-core");
        assert_eq!(readings.cpu_temperatures[0].key, "lhm:/amdcpu/0/temp2");
        assert_eq!(readings.cpu_package_w, Some(35.2));

        let keys: Vec<&str> = readings.chips.iter().map(|c| c.key.as_str()).collect();
        assert_eq!(
            keys,
            ["lhm:/amdcpu/0", "lhm:/lpc/nct6799d/0"],
            "NVIDIA comes from NVML; memory has no temperatures"
        );
        let board = &readings.chips[1];
        assert_eq!(board.name, "Nuvoton NCT6799D");
        assert_eq!(board.fans.len(), 1, "a fan without a reading is left out");
        assert_eq!(board.fans[0].key, "lhm:/lpc/nct6799d/0/fan0");
        assert_eq!(board.fans[0].rpm, 918);
        assert_eq!(board.temperatures[0].label, "System");
        // The CPU chip keeps LHM's full names.
        assert_eq!(readings.chips[0].temperatures[0].label, "Core (Tctl/Tdie)");
    }

    #[test]
    fn older_versions_without_ids_or_raw_values() {
        // No HardwareId / SensorId / Type / RawValue, and a decimal comma.
        let body = r#"{"Text":"Sensor","Children":[{"Text":"PC","Children":[
          {"Text":"Nuvoton NCT6796D","Children":[
            {"Text":"Temperatures","Children":[{"Text":"CPU Core","Value":"41,5 °C","Children":[]}]},
            {"Text":"Fans","Children":[{"Text":"Fan #1","Value":"1200 RPM","Children":[]}]}]}]}]}"#;
        let readings = parse(body.as_bytes()).unwrap();
        assert_eq!(readings.chips.len(), 1);
        let chip = &readings.chips[0];
        assert_eq!(chip.key, "lhm:Nuvoton NCT6796D");
        assert_eq!(chip.temperatures[0].celsius, 41.5);
        assert_eq!(chip.temperatures[0].key, "lhm:Nuvoton NCT6796D/CPU Core");
        assert_eq!(chip.fans[0].rpm, 1200);
    }

    #[test]
    fn intel_labels() {
        assert_eq!(
            cpu_label("CPU Package", "/intelcpu/0").as_deref(),
            Some("Package")
        );
        assert_eq!(
            cpu_label("Core #3", "/intelcpu/0").as_deref(),
            Some("Core 3")
        );
        assert_eq!(cpu_label("Core #3 Distance to TjMax", "/intelcpu/0"), None);
        assert_eq!(cpu_label("Core Average", "/intelcpu/0"), None);
        assert_eq!(
            cpu_label("CCD2 (Tdie)", "/amdcpu/0").as_deref(),
            Some("Tccd2")
        );
    }

    /// Serves `response` once on a local port; returns the URL.
    fn serve_once(response: &'static str) -> String {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let port = listener.local_addr().unwrap().port();
        thread::spawn(move || {
            let (mut stream, _) = listener.accept().unwrap();
            // Read the whole request: closing with unread data resets the
            // connection.
            let mut reader = BufReader::new(stream.try_clone().unwrap());
            let mut line = String::new();
            while reader.read_line(&mut line).unwrap() > 0 && line != "\r\n" {
                line.clear();
            }
            stream.write_all(response.as_bytes()).unwrap();
        });
        format!("http://127.0.0.1:{port}/data.json")
    }

    #[test]
    fn reads_over_http_and_backs_off_when_unreachable() {
        let body = r#"{"Text":"Sensor","Children":[{"Text":"PC","Children":[{"Text":"AMD Ryzen","HardwareId":"/amdcpu/0","Children":[{"Text":"Powers","Children":[{"Text":"Package","SensorId":"/amdcpu/0/power/0","Type":"Power","Value":"20 W","RawValue":20.0,"Children":[]}]}]}]}]}"#;
        let response: &'static str = Box::leak(
            format!(
                "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{body}",
                body.len()
            )
            .into_boxed_str(),
        );
        let mut reader = LhmReader::new(&serve_once(response));
        let now = Instant::now();
        assert_eq!(reader.read(now).unwrap().cpu_package_w, Some(20.0));

        // Authentication enabled in LHM: an error, then a pause.
        let mut reader = LhmReader::new(&serve_once("HTTP/1.1 401 Unauthorized\r\n\r\n"));
        assert!(reader.read(now).is_none());
        assert!(reader.retry_at.is_some_and(|at| at > now));
        assert!(reader.read(now + Duration::from_secs(1)).is_none());
        assert!(
            reader.problem().unwrap().contains("401"),
            "kept while waiting"
        );

        assert!(LhmReader::new("").read(now).is_none(), "disabled");
        assert!(http_get("https://localhost/data.json", TIMEOUT).is_err());
    }
}