ironmonitor 7.1.0

IronMonitor: hardware monitoring for CPUs, GPUs, NPUs, memory, I/O and network silicon on Linux, Windows and macOS, which reports what it cannot read as well as what it can
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
//! Disk firmware and geometry, NVMe controller and health detail, RAID arrays and filesystems.

#[allow(unused_imports)]
use super::super::resolve::*;
#[allow(unused_imports)]
use super::super::{Domain as D, Entity, EntityKind as K, Provenance as P, Unit as U};

use crate::disk::{DiskDevice, SmartSource};
use crate::storage_controller::{RaidArrayInfo, RaidLevel, RaidStatus, StorageControllerMonitor};

#[allow(clippy::too_many_arguments)]
fn e(
    add: &mut dyn FnMut(Entity),
    id: &str,
    domain: D,
    kind: K,
    unit: Option<U>,
    prov: P,
    nullable: bool,
    description: &str,
) {
    add(Entity::new(
        id,
        domain,
        kind,
        unit,
        prov,
        nullable,
        description,
    ));
}

pub(super) fn declare(add: &mut dyn FnMut(Entity)) {
    // ── Disk identity and geometry (DiskInfo) ───────────────────────────────
    e(
        add,
        "disk.{n}.firmware",
        D::Disk,
        K::Identity,
        Some(U::Identifier),
        P::Measured,
        true,
        "Firmware revision the device reports for itself, read from the drive's identify data.",
    );
    e(
        add,
        "disk.{n}.vendor",
        D::Disk,
        K::Identity,
        Some(U::Text),
        P::Measured,
        true,
        "Vendor name the device or its driver reports.",
    );
    e(
        add,
        "disk.{n}.block_size",
        D::Disk,
        K::Limit,
        Some(U::Bytes),
        P::Measured,
        true,
        "Block size the device addresses in, in bytes, as the platform reports it.",
    );
    e(add, "disk.{n}.physical_sector_size", D::Disk, K::Limit, Some(U::Bytes), P::Measured, true,
      "Physical sector size in bytes. Differs from the logical size on Advanced Format drives, which is why both are published.");
    e(
        add,
        "disk.{n}.logical_sector_size",
        D::Disk,
        K::Limit,
        Some(U::Bytes),
        P::Measured,
        true,
        "Logical sector size in bytes, the unit the operating system addresses the device in.",
    );
    e(add, "disk.{n}.rotation_rate", D::Disk, K::Measurement, Some(U::Rpm), P::Measured, true,
      "Nominal spindle speed of a rotating drive, as the drive declares it. No platform reader currently obtains it, so this is unavailable with a reason.");
    e(add, "disk.{n}.interface_type", D::Disk, K::Identity, Some(U::Identifier), P::Measured, true,
      "Transport the device is attached over, such as NVMe, SATA or USB, as the platform names it.");

    // ── NVMe controller detail (NvmeInfo) ───────────────────────────────────
    e(add, "disk.{n}.nvme.unallocated_capacity", D::Disk, K::Measurement, Some(U::Bytes), P::Measured, true,
      "Capacity of the NVM the controller has not allocated to a namespace, in bytes, from the Identify Controller structure.");
    e(add, "disk.{n}.nvme.controller_id", D::Disk, K::Identity, Some(U::Count), P::Specification, true,
      "Numeric identifier the NVMe controller reports for itself in its Identify Controller structure.");
    e(
        add,
        "disk.{n}.nvme.num_namespaces",
        D::Disk,
        K::Measurement,
        Some(U::Count),
        P::Specification,
        true,
        "Number of namespaces the controller supports, from its Identify Controller structure.",
    );
    e(add, "disk.{n}.nvme.host_read_commands", D::Disk, K::Measurement, Some(U::Count), P::Measured, true,
      "Host read commands the controller has completed since it was last reset, from the SMART/Health log page.");
    e(add, "disk.{n}.nvme.host_write_commands", D::Disk, K::Measurement, Some(U::Count), P::Measured, true,
      "Host write commands the controller has completed since it was last reset, from the SMART/Health log page.");
    e(add, "disk.{n}.nvme.available_spare", D::Disk, K::Measurement, Some(U::Percent), P::Measured, true,
      "Remaining spare capacity the controller reports, percent, from the SMART/Health log page. Null where the health log was not read.");
    e(add, "disk.{n}.nvme.available_spare_threshold", D::Disk, K::Limit, Some(U::Percent), P::Measured, true,
      "Available spare level below which the controller raises a warning, percent, from the SMART/Health log page. Null where the health log was not read.");
    e(add, "disk.{n}.nvme.controller_busy_time", D::Disk, K::Measurement, Some(U::Seconds), P::Measured, true,
      "Time the controller has spent processing commands since it was last reset, in seconds. The SMART/Health log reports minutes; this is converted. Null where the health log was not read.");
    e(add, "disk.{n}.nvme.unsafe_shutdowns", D::Disk, K::Measurement, Some(U::Count), P::Measured, true,
      "Power losses the controller saw without a shutdown notification, from the SMART/Health log page. Null where the health log was not read.");
    e(
        add,
        "disk.{n}.nvme.sensor.<none>",
        D::Disk,
        K::Diagnostic,
        None,
        P::Unavailable,
        true,
        "Present when the NVMe controller exposed no temperature sensor, carrying why.",
    );
    e(add, "disk.{n}.nvme.sensor.{k}.temperature", D::Disk, K::Measurement, Some(U::Celsius), P::Measured, true,
      "Temperature reported by one of the controller's own sensors, from the SMART/Health log page. A drive can have several.");
    e(
        add,
        "disk.{n}.nvme.available_power_state.<none>",
        D::Disk,
        K::Diagnostic,
        None,
        P::Unavailable,
        true,
        "Present when the controller's Identify structure listed no power states, carrying why.",
    );
    e(add, "disk.{n}.nvme.available_power_state.{s}.max_power", D::Disk, K::Limit, Some(U::Watts), P::Specification, true,
      "Maximum power the controller draws in power state s, as declared in its Identify Controller power state descriptor.");
    e(add, "disk.{n}.nvme.available_power_state.{s}.entry_latency", D::Disk, K::Limit, Some(U::Milliseconds), P::Specification, true,
      "Time the controller takes to enter power state s, from its power state descriptor. Published in milliseconds from the declared microseconds.");
    e(add, "disk.{n}.nvme.available_power_state.{s}.exit_latency", D::Disk, K::Limit, Some(U::Milliseconds), P::Specification, true,
      "Time the controller takes to leave power state s, from its power state descriptor. Published in milliseconds from the declared microseconds.");

    // ── SMART attribute table (SmartInfo.attributes) ────────────────────────
    e(
        add,
        "disk.{n}.smart.attribute.<none>",
        D::Disk,
        K::Diagnostic,
        None,
        P::Unavailable,
        true,
        "Present when the drive produced no ATA SMART attribute table, carrying why.",
    );
    e(add, "disk.{n}.smart.attribute.{m}.id", D::Disk, K::Identity, Some(U::Count), P::Measured, true,
      "ATA attribute number of entry m in this drive's SMART attribute table. The index m is the table position, not this number.");
    e(add, "disk.{n}.smart.attribute.{m}.name", D::Disk, K::Identity, Some(U::Text), P::Specification, true,
      "Name of ATA SMART attribute m, looked up from its attribute number. The drive does not transmit names.");
    e(add, "disk.{n}.smart.attribute.{m}.value", D::Disk, K::Measurement, Some(U::Count), P::Measured, true,
      "Normalised current value of ATA SMART attribute m, from 0 to 255. Higher is healthier for most attributes.");
    e(
        add,
        "disk.{n}.smart.attribute.{m}.worst",
        D::Disk,
        K::Measurement,
        Some(U::Count),
        P::Measured,
        true,
        "Worst normalised value the drive has recorded for ATA SMART attribute m, from 0 to 255.",
    );
    e(add, "disk.{n}.smart.attribute.{m}.threshold", D::Disk, K::Limit, Some(U::Count), P::Measured, true,
      "Failure threshold the drive declares for ATA SMART attribute m, from 0 to 255. Absent where the platform did not read the threshold table.");
    e(add, "disk.{n}.smart.attribute.{m}.raw_value", D::Disk, K::Measurement, Some(U::Count), P::Measured, true,
      "Raw counter for ATA SMART attribute m. Its meaning depends on the attribute, and the drive's vendor defines it.");

    // ── Storage controllers (StorageControllerInfo and its NVMe detail) ─────
    e(
        add,
        "disk.controller.{n}.firmware",
        D::Disk,
        K::Identity,
        Some(U::Text),
        P::Measured,
        true,
        "Firmware revision of the NVMe controller. Absent for controllers that are not NVMe.",
    );
    e(add, "disk.controller.{n}.serial", D::Disk, K::Identity, Some(U::Identifier), P::Measured, true,
      "Serial number of the NVMe controller, from its Identify Controller structure. Absent for controllers that are not NVMe.");
    e(add, "disk.controller.{n}.pcie_speed", D::Disk, K::Limit, Some(U::Text), P::Measured, true,
      "PCIe link speed the NVMe controller trained to, such as 8 GT/s. Absent for controllers that are not NVMe.");
    e(add, "disk.controller.{n}.pcie_width", D::Disk, K::Limit, Some(U::Text), P::Measured, true,
      "PCIe link width the NVMe controller trained to, such as x4. Absent for controllers that are not NVMe.");
    e(add, "disk.controller.{n}.transport", D::Disk, K::Identity, Some(U::Identifier), P::Measured, true,
      "Transport the NVMe controller uses, such as pcie or tcp, from its sysfs transport file. Absent where that file is not present, and for controllers that are not NVMe.");
    e(add, "disk.controller.{n}.namespace_count", D::Disk, K::Measurement, Some(U::Count), P::Measured, true,
      "Number of namespaces the NVMe controller reports, counted from its sysfs directory. Absent where that directory could not be listed, and for controllers that are not NVMe.");

    // ── RAID arrays (RaidArrayInfo, software RAID on Linux) ─────────────────
    e(
        add,
        "disk.raid.<none>",
        D::Disk,
        K::Diagnostic,
        None,
        P::Unavailable,
        true,
        "Present when no RAID array was enumerated, carrying why.",
    );
    e(
        add,
        "disk.raid.{n}.name",
        D::Disk,
        K::Identity,
        Some(U::Identifier),
        P::Measured,
        true,
        "Name of the software RAID array, such as md0, as the kernel lists it.",
    );
    e(
        add,
        "disk.raid.{n}.level",
        D::Disk,
        K::Identity,
        Some(U::Identifier),
        P::Measured,
        true,
        "RAID level of the array, such as raid1 or raid5, as the kernel reports it.",
    );
    e(
        add,
        "disk.raid.{n}.status",
        D::Disk,
        K::Identity,
        Some(U::Identifier),
        P::Measured,
        true,
        "Status of the array: active, degraded, rebuilding, inactive or failed, as the kernel reports it. Inactive means listed but not started.",
    );
    e(
        add,
        "disk.raid.{n}.active_devices",
        D::Disk,
        K::Measurement,
        Some(U::Count),
        P::Measured,
        true,
        "Member devices that are neither failed nor spare, counted from the array's member list.",
    );
    e(
        add,
        "disk.raid.{n}.total_devices",
        D::Disk,
        K::Measurement,
        Some(U::Count),
        P::Measured,
        true,
        "Member devices listed on the array line, including failed and spare ones.",
    );
    e(add, "disk.raid.{n}.wanted_devices", D::Disk, K::Measurement, Some(U::Count), P::Measured, true,
      "Devices the array wants, the first number of the kernel's n/m device count. Absent where no blocks line was read.");
    e(add, "disk.raid.{n}.working_devices", D::Disk, K::Measurement, Some(U::Count), P::Measured, true,
      "Devices the array reports as working, the second number of the kernel's n/m device count. Absent where no blocks line was read.");
    e(
        add,
        "disk.raid.{n}.failed_devices",
        D::Disk,
        K::Measurement,
        Some(U::Count),
        P::Measured,
        true,
        "Member devices the kernel flags as failed on the array line.",
    );
    e(
        add,
        "disk.raid.{n}.spare_devices",
        D::Disk,
        K::Measurement,
        Some(U::Count),
        P::Measured,
        true,
        "Member devices the kernel flags as spares on the array line.",
    );
    e(
        add,
        "disk.raid.{n}.size",
        D::Disk,
        K::Limit,
        Some(U::Bytes),
        P::Measured,
        true,
        "Usable size of the array in bytes. Absent where the kernel's blocks line was not read.",
    );
    e(
        add,
        "disk.raid.{n}.members",
        D::Disk,
        K::Identity,
        None,
        P::Measured,
        true,
        "Names of the member devices of the array, as a JSON list.",
    );
    e(add, "disk.raid.{n}.resync_progress", D::Disk, K::Measurement, Some(U::Percent), P::Measured, true,
      "Progress of the array's resync or rebuild, from 0 to 100. Absent when the array is not resyncing.");

    // ── Filesystems and partitions (FilesystemInfo) ─────────────────────────
    // `disk.{n}.partition.{m}.mount_point` is declared in the base schema.
    e(
        add,
        "disk.{n}.partition.<none>",
        D::Disk,
        K::Diagnostic,
        None,
        P::Unavailable,
        true,
        "Present when no mounted filesystem was listed for this device, carrying why.",
    );
    e(
        add,
        "disk.{n}.partition.{m}.fs_type",
        D::Disk,
        K::Identity,
        Some(U::Identifier),
        P::Measured,
        true,
        "Filesystem type on this partition, such as ntfs, ext4 or apfs, as the platform names it.",
    );
    e(
        add,
        "disk.{n}.partition.{m}.total_size",
        D::Disk,
        K::Limit,
        Some(U::Bytes),
        P::Measured,
        true,
        "Total size of the filesystem in bytes. Absent where the platform did not report it.",
    );
    e(
        add,
        "disk.{n}.partition.{m}.used_size",
        D::Disk,
        K::Measurement,
        Some(U::Bytes),
        P::Measured,
        true,
        "Space in use on the filesystem, in bytes. Absent where the platform did not report it.",
    );
    e(add, "disk.{n}.partition.{m}.available_size", D::Disk, K::Measurement, Some(U::Bytes), P::Measured, true,
      "Space available to unprivileged users on the filesystem, in bytes. Absent where the platform did not report it.");
    e(
        add,
        "disk.{n}.partition.{m}.total_inodes",
        D::Disk,
        K::Limit,
        Some(U::Count),
        P::Measured,
        true,
        "Total inodes on the filesystem. Absent on platforms that do not use inodes.",
    );
    e(
        add,
        "disk.{n}.partition.{m}.used_inodes",
        D::Disk,
        K::Measurement,
        Some(U::Count),
        P::Measured,
        true,
        "Inodes in use on the filesystem. Absent on platforms that do not use inodes.",
    );
    e(add, "disk.{n}.partition.{m}.read_only", D::Disk, K::Identity, None, P::Measured, true,
      "Whether the filesystem is mounted read-only. Absent where the platform's volume query does not carry the flag.");
}

/// A reader's optional text, with an empty string treated as absent.
///
/// `push_opt` guards names that spell an absence, but an empty string passes
/// straight through it, so the emptiness check happens here.
fn text_opt(value: Option<&String>) -> Option<serde_json::Value> {
    value
        .filter(|v| !v.trim().is_empty())
        .map(|v| serde_json::json!(v))
}

fn bool_reading(out: &mut Vec<Reading>, id: impl Into<String>, value: Option<bool>, why: &str) {
    let id = id.into();
    match value {
        Some(b) => out.push(Reading::measured(id, serde_json::json!(b), None)),
        None => out.push(Reading::unavailable(id, None, why)),
    }
}

pub(super) fn resolve(out: &mut Vec<Reading>) {
    // Enumerated exactly as `resolve_disk` enumerates, so `disk.{n}` lines up with
    // the base rows. On failure the base resolver has already reported `disk.<none>`.
    if let Ok(disks) = crate::disk::enumerate_disks() {
        for (i, disk) in disks.iter().enumerate() {
            resolve_disk(out, &format!("disk.{i}"), disk.as_ref());
        }
    }

    // One monitor for both controllers and RAID arrays, constructed once.
    match StorageControllerMonitor::new() {
        Ok(monitor) => {
            resolve_controllers(out, &monitor);
            resolve_raid(out, monitor.raid_arrays());
        }
        Err(e) => {
            out.push(Reading::unavailable(
                "disk.raid.<none>",
                None,
                format!(
                    "RAID arrays were not read because storage controller enumeration failed: {e}"
                ),
            ));
        }
    }
}

fn resolve_disk(out: &mut Vec<Reading>, base: &str, disk: &dyn DiskDevice) {
    resolve_disk_info(out, base, disk);
    resolve_nvme(out, base, disk);
    resolve_smart_attributes(out, base, disk);
    resolve_filesystems(out, base, disk);
}

fn resolve_disk_info(out: &mut Vec<Reading>, base: &str, disk: &dyn DiskDevice) {
    match disk.info() {
        Ok(info) => {
            push_opt(
                out,
                format!("{base}.firmware"),
                text_opt(info.firmware.as_ref()),
                Some(U::Identifier),
                "the device did not report a firmware revision",
            );
            push_opt(
                out,
                format!("{base}.vendor"),
                text_opt(info.vendor.as_ref()),
                Some(U::Text),
                "the device did not report a vendor name",
            );
            push_opt(
                out,
                format!("{base}.block_size"),
                info.block_size.map(|v| serde_json::json!(v)),
                Some(U::Bytes),
                "the platform did not report a block size for this device",
            );
            push_opt(
                out,
                format!("{base}.physical_sector_size"),
                info.physical_sector_size.map(|v| serde_json::json!(v)),
                Some(U::Bytes),
                "the platform did not report a physical sector size for this device",
            );
            push_opt(
                out,
                format!("{base}.logical_sector_size"),
                info.logical_sector_size.map(|v| serde_json::json!(v)),
                Some(U::Bytes),
                "the platform did not report a logical sector size for this device",
            );
            push_opt(
                out,
                format!("{base}.rotation_rate"),
                info.rotation_rate.map(|v| serde_json::json!(v)),
                Some(U::Rpm),
                "the reader reported no rotation rate for this device",
            );
            push_opt(
                out,
                format!("{base}.interface_type"),
                text_opt(info.interface_type.as_ref()),
                Some(U::Identifier),
                "the platform did not name this device's interface",
            );
        }
        Err(e) => {
            let why = format!("device info read failed: {e}");
            for (suffix, unit) in [
                ("firmware", U::Identifier),
                ("vendor", U::Text),
                ("block_size", U::Bytes),
                ("physical_sector_size", U::Bytes),
                ("logical_sector_size", U::Bytes),
                ("rotation_rate", U::Rpm),
                ("interface_type", U::Identifier),
            ] {
                out.push(Reading::unavailable(
                    format!("{base}.{suffix}"),
                    Some(unit),
                    why.clone(),
                ));
            }
        }
    }
}

fn resolve_nvme(out: &mut Vec<Reading>, base: &str, disk: &dyn DiskDevice) {
    match disk.nvme_info() {
        Ok(n) => {
            push_opt(
                out,
                format!("{base}.nvme.unallocated_capacity"),
                n.unallocated_capacity.map(|v| serde_json::json!(v)),
                Some(U::Bytes),
                "the controller did not report unallocated capacity",
            );
            push_opt(
                out,
                format!("{base}.nvme.controller_id"),
                n.controller_id.map(|v| serde_json::json!(v)),
                Some(U::Count),
                "the controller did not report its identifier",
            );
            push_opt(
                out,
                format!("{base}.nvme.num_namespaces"),
                n.num_namespaces.map(|v| serde_json::json!(v)),
                Some(U::Count),
                "the controller did not report a namespace count",
            );
            push_opt(
                out,
                format!("{base}.nvme.host_read_commands"),
                n.host_read_commands.map(|v| serde_json::json!(v)),
                Some(U::Count),
                "the controller did not report a host read command count",
            );
            push_opt(
                out,
                format!("{base}.nvme.host_write_commands"),
                n.host_write_commands.map(|v| serde_json::json!(v)),
                Some(U::Count),
                "the controller did not report a host write command count",
            );
            push_opt(
                out,
                format!("{base}.nvme.available_spare"),
                n.available_spare.map(|v| serde_json::json!(v)),
                Some(U::Percent),
                "this reader did not obtain the available spare from the SMART/Health log",
            );
            push_opt(
                out,
                format!("{base}.nvme.available_spare_threshold"),
                n.available_spare_threshold.map(|v| serde_json::json!(v)),
                Some(U::Percent),
                "this reader did not obtain the spare threshold from the SMART/Health log",
            );
            // The log reports minutes; the ontology publishes seconds.
            push_opt(
                out,
                format!("{base}.nvme.controller_busy_time"),
                n.controller_busy_time_minutes
                    .map(|m| serde_json::json!(m.saturating_mul(60))),
                Some(U::Seconds),
                "this reader did not obtain the controller busy time from the SMART/Health log",
            );
            push_opt(
                out,
                format!("{base}.nvme.unsafe_shutdowns"),
                n.unsafe_shutdowns.map(|v| serde_json::json!(v)),
                Some(U::Count),
                "this reader did not obtain the unsafe shutdown count from the SMART/Health log",
            );

            if n.temperature_sensors.is_empty() {
                out.push(Reading::unavailable(
                    format!("{base}.nvme.sensor.<none>"),
                    None,
                    "the controller's SMART/Health log reported no temperature sensor",
                ));
            }
            for (k, t) in n.temperature_sensors.iter().enumerate() {
                let id = format!("{base}.nvme.sensor.{k}.temperature");
                if t.is_finite() {
                    out.push(Reading::measured(
                        id,
                        serde_json::json!(f64::from(*t)),
                        Some(U::Celsius),
                    ));
                } else {
                    out.push(Reading::unavailable(
                        id,
                        Some(U::Celsius),
                        "the sensor reported a value that is not a number",
                    ));
                }
            }

            if n.available_power_states.is_empty() {
                out.push(Reading::unavailable(
                    format!("{base}.nvme.available_power_state.<none>"),
                    None,
                    "the controller's Identify structure listed no power states, or was not read",
                ));
            }
            for p in &n.available_power_states {
                let pb = format!("{base}.nvme.available_power_state.{}", p.state);
                let watts = f64::from(p.max_power_watts);
                if watts.is_finite() {
                    out.push(Reading::spec(
                        format!("{pb}.max_power"),
                        serde_json::json!(watts),
                        Some(U::Watts),
                    ));
                } else {
                    out.push(Reading::unavailable(
                        format!("{pb}.max_power"),
                        Some(U::Watts),
                        "the power state descriptor reported a maximum power that is not a number",
                    ));
                }
                // The descriptor carries microseconds, which the unit list does not
                // have; milliseconds are the same quantity at a readable scale.
                out.push(Reading::spec(
                    format!("{pb}.entry_latency"),
                    serde_json::json!(f64::from(p.entry_latency_us) / 1000.0),
                    Some(U::Milliseconds),
                ));
                out.push(Reading::spec(
                    format!("{pb}.exit_latency"),
                    serde_json::json!(f64::from(p.exit_latency_us) / 1000.0),
                    Some(U::Milliseconds),
                ));
            }
        }
        Err(e) => {
            // A SATA or USB device declines the NVMe protocol; the reason uses the
            // same wording the base resolver uses for these rows.
            let why = format!("no NVMe controller data: {e}");
            for (suffix, unit) in [
                ("unallocated_capacity", U::Bytes),
                ("controller_id", U::Count),
                ("num_namespaces", U::Count),
                ("host_read_commands", U::Count),
                ("host_write_commands", U::Count),
                ("available_spare", U::Percent),
                ("available_spare_threshold", U::Percent),
                ("controller_busy_time", U::Seconds),
                ("unsafe_shutdowns", U::Count),
            ] {
                out.push(Reading::unavailable(
                    format!("{base}.nvme.{suffix}"),
                    Some(unit),
                    why.clone(),
                ));
            }
            out.push(Reading::unavailable(
                format!("{base}.nvme.sensor.<none>"),
                None,
                why.clone(),
            ));
            out.push(Reading::unavailable(
                format!("{base}.nvme.available_power_state.<none>"),
                None,
                why,
            ));
        }
    }
}

fn resolve_smart_attributes(out: &mut Vec<Reading>, base: &str, disk: &dyn DiskDevice) {
    let id_of = |suffix: &str| format!("{base}.smart.attribute.{suffix}");
    match disk.smart_info() {
        Ok(s) => {
            if s.attributes.is_empty() {
                let why = match s.source {
                    SmartSource::NvmeLogPage => {
                        "this drive reports its counters in the NVMe health log, which has no ATA attribute table"
                    }
                    SmartSource::AtaAttributes => {
                        "the drive returned an ATA attribute table with no entries"
                    }
                    SmartSource::StorageStack => {
                        "no ATA attribute table was obtained from this drive, because the storage stack answered instead"
                    }
                };
                out.push(Reading::unavailable(id_of("<none>"), None, why));
            }
            for (m, a) in s.attributes.iter().enumerate() {
                out.push(Reading::measured(
                    id_of(&format!("{m}.id")),
                    serde_json::json!(a.id),
                    Some(U::Count),
                ));
                push_spec_text(out, id_of(&format!("{m}.name")), &a.name);
                out.push(Reading::measured(
                    id_of(&format!("{m}.value")),
                    serde_json::json!(a.value),
                    Some(U::Count),
                ));
                out.push(Reading::measured(
                    id_of(&format!("{m}.worst")),
                    serde_json::json!(a.worst),
                    Some(U::Count),
                ));
                push_opt(
                    out,
                    id_of(&format!("{m}.threshold")),
                    a.threshold.map(|v| serde_json::json!(v)),
                    Some(U::Count),
                    "the platform's ATA reader did not read this drive's threshold table",
                );
                out.push(Reading::measured(
                    id_of(&format!("{m}.raw_value")),
                    serde_json::json!(a.raw_value),
                    Some(U::Count),
                ));
            }
        }
        Err(e) => {
            out.push(Reading::unavailable(
                id_of("<none>"),
                None,
                format!("SMART read failed: {e}"),
            ));
        }
    }
}

fn resolve_filesystems(out: &mut Vec<Reading>, base: &str, disk: &dyn DiskDevice) {
    match disk.filesystem_info() {
        Ok(filesystems) if filesystems.is_empty() => {
            out.push(Reading::unavailable(
                format!("{base}.partition.<none>"),
                None,
                "the platform listed no mounted filesystem on this device",
            ));
        }
        Ok(filesystems) => {
            for (m, fs) in filesystems.iter().enumerate() {
                let pb = format!("{base}.partition.{m}");
                push_text(
                    out,
                    format!("{pb}.mount_point"),
                    &fs.mount_point.to_string_lossy(),
                );
                push_id(out, format!("{pb}.fs_type"), &fs.fs_type);
                push_opt(
                    out,
                    format!("{pb}.total_size"),
                    fs.total_size.map(|v| serde_json::json!(v)),
                    Some(U::Bytes),
                    "the platform did not report the total size of this filesystem",
                );
                push_opt(
                    out,
                    format!("{pb}.used_size"),
                    fs.used_size.map(|v| serde_json::json!(v)),
                    Some(U::Bytes),
                    "the platform did not report how much of this filesystem is in use",
                );
                push_opt(
                    out,
                    format!("{pb}.available_size"),
                    fs.available_size.map(|v| serde_json::json!(v)),
                    Some(U::Bytes),
                    "the platform did not report the space available on this filesystem",
                );
                push_opt(
                    out,
                    format!("{pb}.total_inodes"),
                    fs.total_inodes.map(|v| serde_json::json!(v)),
                    Some(U::Count),
                    "the platform does not report inode counts for this filesystem",
                );
                push_opt(
                    out,
                    format!("{pb}.used_inodes"),
                    fs.used_inodes.map(|v| serde_json::json!(v)),
                    Some(U::Count),
                    "the platform does not report inode usage for this filesystem",
                );
                bool_reading(
                    out,
                    format!("{pb}.read_only"),
                    fs.read_only,
                    "the platform's volume query does not report whether this filesystem is read-only",
                );
            }
        }
        Err(e) => {
            out.push(Reading::unavailable(
                format!("{base}.partition.<none>"),
                None,
                format!("filesystem enumeration failed: {e}"),
            ));
        }
    }
}

fn resolve_controllers(out: &mut Vec<Reading>, monitor: &StorageControllerMonitor) {
    for (i, c) in monitor.controllers().iter().enumerate() {
        let base = format!("disk.controller.{i}");
        match &c.nvme_info {
            Some(n) => {
                push_text(out, format!("{base}.firmware"), &n.firmware);
                push_id(out, format!("{base}.serial"), &n.serial);
                push_text(out, format!("{base}.pcie_speed"), &n.pcie_speed);
                push_text(out, format!("{base}.pcie_width"), &n.pcie_width);
                push_opt(
                    out,
                    format!("{base}.transport"),
                    n.transport.as_deref().map(|t| serde_json::json!(t)),
                    Some(U::Identifier),
                    "the kernel exposes no transport file for this controller, so none was read",
                );
                push_opt(
                    out,
                    format!("{base}.namespace_count"),
                    n.namespace_count.map(|v| serde_json::json!(v)),
                    Some(U::Count),
                    "the controller's sysfs directory could not be listed, so its namespaces were not counted",
                );
            }
            None => {
                // An NVMe controller without `nvme_info` is one whose identity was
                // not read (the Windows reader reads none), not one that lacks it.
                // These rows used to say "not NVMe" beside an interface of NVMe.
                let why = match c.interface {
                    crate::storage_controller::StorageInterface::NVMe => {
                        "this NVMe controller's identity data was not read on this platform"
                    }
                    crate::storage_controller::StorageInterface::Unknown => concat!(
                        "the controller's interface was not identified, ",
                        "so whether NVMe identity fields apply is unknown"
                    ),
                    _ => "this controller is not NVMe, so NVMe identity fields do not apply",
                };
                for (suffix, unit) in [
                    ("firmware", U::Text),
                    ("serial", U::Identifier),
                    ("pcie_speed", U::Text),
                    ("pcie_width", U::Text),
                    ("transport", U::Identifier),
                    ("namespace_count", U::Count),
                ] {
                    out.push(Reading::unavailable(
                        format!("{base}.{suffix}"),
                        Some(unit),
                        why,
                    ));
                }
            }
        }
    }
}

fn raid_level_name(level: &RaidLevel) -> String {
    match level {
        RaidLevel::Raid0 => "raid0".to_string(),
        RaidLevel::Raid1 => "raid1".to_string(),
        RaidLevel::Raid5 => "raid5".to_string(),
        RaidLevel::Raid6 => "raid6".to_string(),
        RaidLevel::Raid10 => "raid10".to_string(),
        RaidLevel::Raid50 => "raid50".to_string(),
        RaidLevel::Raid60 => "raid60".to_string(),
        RaidLevel::JBOD => "jbod".to_string(),
        RaidLevel::Linear => "linear".to_string(),
        RaidLevel::Unknown(name) => name.to_ascii_lowercase(),
    }
}

fn raid_status_name(status: &RaidStatus) -> &'static str {
    match status {
        RaidStatus::Active => "active",
        RaidStatus::Degraded => "degraded",
        RaidStatus::Rebuilding => "rebuilding",
        RaidStatus::Failed => "failed",
        RaidStatus::Inactive => "inactive",
        RaidStatus::Unknown => "unknown",
    }
}

fn resolve_raid(out: &mut Vec<Reading>, arrays: &[RaidArrayInfo]) {
    if arrays.is_empty() {
        let why = if cfg!(target_os = "linux") {
            "no software RAID array is listed in /proc/mdstat"
        } else {
            "software RAID arrays are read from /proc/mdstat, which exists only on Linux, so none were read on this platform"
        };
        out.push(Reading::unavailable("disk.raid.<none>", None, why));
        return;
    }

    for (n, a) in arrays.iter().enumerate() {
        let base = format!("disk.raid.{n}");
        push_id(out, format!("{base}.name"), &a.name);
        push_opt(
            out,
            format!("{base}.level"),
            a.level.as_ref().map(|l| serde_json::json!(raid_level_name(l))),
            Some(U::Identifier),
            "the kernel's state line names no RAID level for this array, which is what an inactive array looks like",
        );
        push_id(out, format!("{base}.status"), raid_status_name(&a.status));
        out.push(Reading::measured(
            format!("{base}.active_devices"),
            serde_json::json!(a.active_devices),
            Some(U::Count),
        ));
        out.push(Reading::measured(
            format!("{base}.total_devices"),
            serde_json::json!(a.total_devices),
            Some(U::Count),
        ));
        let no_blocks = "no blocks line was read for this array";
        push_opt(
            out,
            format!("{base}.wanted_devices"),
            a.wanted_devices.map(|v| serde_json::json!(v)),
            Some(U::Count),
            no_blocks,
        );
        push_opt(
            out,
            format!("{base}.working_devices"),
            a.working_devices.map(|v| serde_json::json!(v)),
            Some(U::Count),
            no_blocks,
        );
        out.push(Reading::measured(
            format!("{base}.failed_devices"),
            serde_json::json!(a.failed_devices),
            Some(U::Count),
        ));
        out.push(Reading::measured(
            format!("{base}.spare_devices"),
            serde_json::json!(a.spare_devices),
            Some(U::Count),
        ));
        push_opt(
            out,
            format!("{base}.size"),
            a.size_bytes.map(|v| serde_json::json!(v)),
            Some(U::Bytes),
            "no blocks line was read for this array, so its size is unknown",
        );

        let members: Vec<&String> = a.members.iter().filter(|m| !m.trim().is_empty()).collect();
        if members.is_empty() {
            out.push(Reading::unavailable(
                format!("{base}.members"),
                None,
                "the kernel listed no member devices for this array",
            ));
        } else {
            out.push(Reading::measured(
                format!("{base}.members"),
                serde_json::json!(members),
                None,
            ));
        }

        push_opt(
            out,
            format!("{base}.resync_progress"),
            a.resync_progress.map(|v| serde_json::json!(v)),
            Some(U::Percent),
            "the array is not resyncing, so no resync progress applies",
        );
    }
}