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
//! Storage controller monitoring — RAID arrays, HBAs, NVMe controllers.
//!
//! # Platform Support
//!
//! - **Linux**: Reads `/sys/class/scsi_host/`, `/sys/class/nvme/`, `/proc/mdstat`
//! - **Windows**: Uses WMI (`Win32_SCSIController`, `Win32_IDEController`, `MSFT_StorageSubSystem`)
//! - **macOS**: Uses `system_profiler SPStorageDataType`, `SPNVMeDataType`
//!
//! # Examples
//!
//! ```no_run
//! use ironmonitor::storage_controller::StorageControllerMonitor;
//!
//! let monitor = StorageControllerMonitor::new().unwrap();
//! for ctrl in monitor.controllers() {
//! println!("{}: {} ({:?})", ctrl.name, ctrl.driver, ctrl.interface);
//! }
//! for array in monitor.raid_arrays() {
//! println!("RAID {}: {} - {:?}", array.level, array.name, array.status);
//! }
//! ```
use serde::{Deserialize, Serialize};
use crate::error::IronError;
/// Storage interface type
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum StorageInterface {
/// NVMe (Non-Volatile Memory Express)
NVMe,
/// AHCI / SATA controller
AHCI,
/// SAS (Serial Attached SCSI)
SAS,
/// SCSI (legacy parallel)
SCSI,
/// IDE / PATA (legacy)
IDE,
/// USB mass storage
USB,
/// Fibre Channel
FibreChannel,
/// iSCSI (network)
ISCSI,
/// Virtio (virtualized)
Virtio,
/// Unknown interface
Unknown,
}
/// RAID level
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RaidLevel {
Raid0,
Raid1,
Raid5,
Raid6,
Raid10,
Raid50,
Raid60,
JBOD,
Linear,
Unknown(String),
}
/// RAID array status
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RaidStatus {
/// Healthy / active
Active,
/// Degraded (missing member)
Degraded,
/// Rebuilding / resyncing
Rebuilding,
/// Failed / inactive
Failed,
/// Unknown status
Unknown,
}
/// NVMe controller details
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NvmeControllerInfo {
/// Controller name (e.g., "nvme0")
pub name: String,
/// Model name
pub model: String,
/// Serial number
pub serial: String,
/// Firmware revision
pub firmware: String,
/// PCIe link speed (e.g., "8 GT/s")
pub pcie_speed: String,
/// PCIe link width (e.g., "x4")
pub pcie_width: String,
/// Number of namespaces
pub namespace_count: u32,
/// Transport type (e.g., "pcie", "tcp")
pub transport: String,
}
/// Storage controller information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageControllerInfo {
/// Controller name or identifier
pub name: String,
/// Vendor or manufacturer
pub vendor: String,
/// Model or product name
pub model: String,
/// Driver in use
pub driver: String,
/// Controller interface type
pub interface: StorageInterface,
/// PCI address (if applicable)
pub pci_address: String,
/// Number of ports
pub ports: u32,
/// NVMe-specific info (if NVMe controller)
pub nvme_info: Option<NvmeControllerInfo>,
}
/// Software RAID array information (mdadm, etc.)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RaidArrayInfo {
/// Array name (e.g., "md0")
pub name: String,
/// RAID level
pub level: RaidLevel,
/// Current status
pub status: RaidStatus,
/// Number of active devices
pub active_devices: u32,
/// Total expected devices
pub total_devices: u32,
/// Failed devices count
pub failed_devices: u32,
/// Spare devices count
pub spare_devices: u32,
/// Array size in bytes
pub size_bytes: u64,
/// Member device names
pub members: Vec<String>,
/// Resync progress percentage (0-100, None if not resyncing)
pub resync_progress: Option<f32>,
}
/// Monitor for storage controllers and RAID arrays
pub struct StorageControllerMonitor {
controllers: Vec<StorageControllerInfo>,
raid_arrays: Vec<RaidArrayInfo>,
}
impl StorageControllerMonitor {
/// Create a new StorageControllerMonitor and detect controllers.
pub fn new() -> Result<Self, IronError> {
let mut monitor = Self {
controllers: Vec::new(),
raid_arrays: Vec::new(),
};
monitor.refresh()?;
Ok(monitor)
}
/// Refresh storage controller detection.
/// Re-enumerate storage controllers and RAID arrays.
///
/// Returns `Err` when the enumeration failed, and `Ok` with an empty list
/// only when it succeeded and found nothing -- which the resolver publishes
/// as `disk.controller.<none>`. See [`crate::core::command`].
pub fn refresh(&mut self) -> Result<(), IronError> {
self.controllers.clear();
self.raid_arrays.clear();
#[cfg(target_os = "linux")]
{
self.refresh_nvme_linux()?;
self.refresh_scsi_linux()?;
self.refresh_mdstat_linux()?;
}
#[cfg(target_os = "windows")]
self.refresh_windows()?;
#[cfg(target_os = "macos")]
self.refresh_macos()?;
Ok(())
}
/// All detected storage controllers.
pub fn controllers(&self) -> &[StorageControllerInfo] {
&self.controllers
}
/// All detected RAID arrays.
pub fn raid_arrays(&self) -> &[RaidArrayInfo] {
&self.raid_arrays
}
/// Get NVMe controllers only.
pub fn nvme_controllers(&self) -> Vec<&StorageControllerInfo> {
self.controllers
.iter()
.filter(|c| c.interface == StorageInterface::NVMe)
.collect()
}
/// Get controllers by interface type.
pub fn controllers_by_interface(&self, iface: StorageInterface) -> Vec<&StorageControllerInfo> {
self.controllers
.iter()
.filter(|c| c.interface == iface)
.collect()
}
/// Check if any RAID arrays are degraded.
pub fn has_degraded_raids(&self) -> bool {
self.raid_arrays
.iter()
.any(|a| a.status == RaidStatus::Degraded || a.status == RaidStatus::Failed)
}
// ── Linux implementations ──
#[cfg(target_os = "linux")]
fn refresh_nvme_linux(&mut self) -> Result<(), IronError> {
let nvme_base = std::path::Path::new("/sys/class/nvme");
if !nvme_base.exists() {
// A real answer: this kernel exposes no such class.
return Ok(());
}
let entries = std::fs::read_dir(nvme_base)
.map_err(|e| IronError::System(format!("cannot read /sys/class/nvme: {e}")))?;
{
for entry in entries.flatten() {
let name = entry.file_name().to_string_lossy().to_string();
if !name.starts_with("nvme") {
continue;
}
let base = entry.path();
let model = Self::read_trimmed(&base.join("model"));
let serial = Self::read_trimmed(&base.join("serial"));
let firmware = Self::read_trimmed(&base.join("firmware_rev"));
let transport = Self::read_trimmed(&base.join("transport"));
// Count namespaces
let ns_count = std::fs::read_dir(&base)
.map(|rd| {
rd.flatten()
.filter(|e| {
e.file_name()
.to_string_lossy()
.starts_with(&format!("{}n", name))
})
.count() as u32
})
.unwrap_or(0);
// PCIe info from device/
let pcie_speed = Self::read_trimmed(&base.join("device/current_link_speed"));
let pcie_width = Self::read_trimmed(&base.join("device/current_link_width"));
let nvme_info = NvmeControllerInfo {
name: name.clone(),
model: model.clone(),
serial: serial.clone(),
firmware,
pcie_speed,
pcie_width,
namespace_count: ns_count,
transport: if transport.is_empty() {
"pcie".to_string()
} else {
transport
},
};
self.controllers.push(StorageControllerInfo {
name: name.clone(),
vendor: String::new(),
model,
driver: "nvme".to_string(),
interface: StorageInterface::NVMe,
pci_address: Self::read_pci_address(&base),
ports: ns_count,
nvme_info: Some(nvme_info),
});
}
}
Ok(())
}
#[cfg(target_os = "linux")]
fn refresh_scsi_linux(&mut self) -> Result<(), IronError> {
let scsi_base = std::path::Path::new("/sys/class/scsi_host");
if !scsi_base.exists() {
// A real answer: this kernel exposes no such class.
return Ok(());
}
let entries = std::fs::read_dir(scsi_base)
.map_err(|e| IronError::System(format!("cannot read /sys/class/scsi_host: {e}")))?;
{
for entry in entries.flatten() {
let name = entry.file_name().to_string_lossy().to_string();
let base = entry.path();
let proc_name = Self::read_trimmed(&base.join("proc_name"));
let interface = match proc_name.as_str() {
"ahci" => StorageInterface::AHCI,
"mpt3sas" | "mpt2sas" | "megaraid_sas" => StorageInterface::SAS,
"uas" | "usb-storage" => StorageInterface::USB,
"virtio_scsi" => StorageInterface::Virtio,
_ => {
if proc_name.contains("iscsi") {
StorageInterface::ISCSI
} else {
StorageInterface::SCSI
}
}
};
let model_name = Self::read_trimmed(&base.join("model_name"));
self.controllers.push(StorageControllerInfo {
name,
vendor: String::new(),
model: model_name,
driver: proc_name,
interface,
pci_address: String::new(),
ports: 0,
nvme_info: None,
});
}
}
Ok(())
}
#[cfg(target_os = "linux")]
fn refresh_mdstat_linux(&mut self) -> Result<(), IronError> {
// A kernel with no md subsystem has no /proc/mdstat, which is a real
// answer; a file that will not open is not.
let content = match std::fs::read_to_string("/proc/mdstat") {
Ok(c) => c,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
Err(e) => return Err(IronError::System(format!("cannot read /proc/mdstat: {e}"))),
};
let mut current: Option<RaidArrayInfo> = None;
for line in content.lines() {
let line = line.trim();
// "md0 : active raid1 sda1[0] sdb1[1]"
if line.starts_with("md") && line.contains(" : ") {
// Save previous array
if let Some(array) = current.take() {
self.raid_arrays.push(array);
}
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() < 4 {
continue;
}
let name = parts[0].to_string();
let status_str = parts[2]; // "active" or "inactive"
let level_str = if parts.len() > 3 { parts[3] } else { "" };
let status = match status_str {
"active" => RaidStatus::Active,
"inactive" => RaidStatus::Failed,
_ => RaidStatus::Unknown,
};
let level = match level_str {
"raid0" => RaidLevel::Raid0,
"raid1" => RaidLevel::Raid1,
"raid5" => RaidLevel::Raid5,
"raid6" => RaidLevel::Raid6,
"raid10" => RaidLevel::Raid10,
"linear" => RaidLevel::Linear,
other => RaidLevel::Unknown(other.to_string()),
};
// Extract member devices (e.g., "sda1[0]", "sdb1[1]")
let members: Vec<String> = parts[4..]
.iter()
.filter_map(|p| p.split('[').next().map(|s| s.to_string()))
.collect();
current = Some(RaidArrayInfo {
name,
level,
status,
active_devices: members.len() as u32,
total_devices: members.len() as u32,
failed_devices: 0,
spare_devices: 0,
size_bytes: 0,
members,
resync_progress: None,
});
} else if let Some(ref mut array) = current {
// "123456 blocks super 1.2 [2/2] [UU]"
if line.contains("blocks") {
if let Some(blocks_str) = line.split_whitespace().next() {
if let Ok(blocks) = blocks_str.parse::<u64>() {
array.size_bytes = blocks * 1024;
}
}
// Parse [U_] or [UU] status
if let Some(bracket_pos) = line.rfind('[') {
let remainder = &line[bracket_pos + 1..];
if let Some(end) = remainder.find(']') {
let status_chars = &remainder[..end];
let failed = status_chars.chars().filter(|c| *c == '_').count() as u32;
if failed > 0 {
array.status = RaidStatus::Degraded;
array.failed_devices = failed;
}
}
}
}
// "recovery = 45.2% ..."
if line.contains("recovery") || line.contains("resync") {
if let Some(pct_str) = line
.split('=')
.nth(1)
.and_then(|s| s.trim().split('%').next())
{
if let Ok(pct) = pct_str.trim().parse::<f32>() {
array.resync_progress = Some(pct);
array.status = RaidStatus::Rebuilding;
}
}
}
}
}
if let Some(array) = current {
self.raid_arrays.push(array);
}
Ok(())
}
#[cfg(target_os = "linux")]
fn read_trimmed(path: &std::path::Path) -> String {
std::fs::read_to_string(path)
.unwrap_or_default()
.trim()
.to_string()
}
#[cfg(target_os = "linux")]
fn read_pci_address(base: &std::path::Path) -> String {
// Resolve the symlink to get PCI BDF
let device_link = base.join("device");
std::fs::read_link(&device_link)
.ok()
.and_then(|p| p.file_name().map(|n| n.to_string_lossy().to_string()))
.unwrap_or_default()
}
// ── Windows implementation ──
/// Query WMI directly rather than shelling out to PowerShell.
///
/// This collector used to spawn three separate `powershell.exe` processes. Each
/// costs roughly 1.3s in interpreter startup alone, so enumeration took about
/// four seconds and dominated `ironmon profile` — 4000ms of its 4900ms total, for
/// twelve settings. The queries themselves are trivial.
///
/// `wmi` is already a dependency and is used this way elsewhere in the crate.
#[cfg(target_os = "windows")]
/// One WMI query, re-serialized to the JSON shape the parsers expect.
///
/// This returned `Option<String>` and reached it through three `.ok()?` --
/// COM initialization, the namespace connection and the query itself all
/// collapsed into `None`, which the caller read as "this namespace has no
/// rows". Each now says which step failed.
fn query_wmi_json(namespace: &str, query: &str) -> Result<String, IronError> {
use std::collections::HashMap;
use wmi::{COMLibrary, Variant, WMIConnection};
// COM may already be initialized on this thread (the collector thread holds a
// guard); `COMLibrary::new` handles that case.
let com = COMLibrary::new()
.map_err(|e| IronError::System(format!("COM initialization failed: {e}")))?;
let conn = WMIConnection::with_namespace_path(namespace, com).map_err(|e| {
IronError::System(format!("cannot connect to WMI namespace {namespace}: {e}"))
})?;
let rows: Vec<HashMap<String, Variant>> = conn
.raw_query(query)
.map_err(|e| IronError::System(format!("WMI query failed in {namespace}: {e}")))?;
// Re-serialize to the JSON shape the existing parsers expect, so the parsing
// logic below is unchanged.
let json_rows: Vec<serde_json::Value> = rows
.into_iter()
.map(|row| {
let obj: serde_json::Map<String, serde_json::Value> = row
.into_iter()
.map(|(k, v)| {
let value = match v {
Variant::String(s) => serde_json::Value::String(s),
Variant::I2(i) => serde_json::Value::from(i),
Variant::I4(i) => serde_json::Value::from(i),
Variant::UI4(i) => serde_json::Value::from(i),
Variant::Bool(b) => serde_json::Value::Bool(b),
_ => serde_json::Value::Null,
};
(k, value)
})
.collect();
serde_json::Value::Object(obj)
})
.collect();
serde_json::to_string(&json_rows)
.map_err(|e| IronError::Parse(format!("cannot re-serialize WMI rows: {e}")))
}
#[cfg(target_os = "windows")]
fn refresh_windows(&mut self) -> Result<(), IronError> {
// Three independent queries. A machine with no SCSI controller and a
// machine whose WMI is unreachable both used to arrive here with an
// empty list, so the rule is the one in `usb::refresh_windows`: any
// source succeeding is enough to trust an empty result, and every
// source failing is not an empty machine.
let mut failures: Vec<String> = Vec::new();
let mut any_ok = false;
// SCSI controllers
match Self::query_wmi_json(
"root\\CIMV2",
"SELECT Name, Manufacturer, DriverName, DeviceID, Status FROM Win32_SCSIController",
) {
Ok(text) => {
any_ok = true;
self.parse_windows_controllers(&text, StorageInterface::SCSI);
}
Err(e) => failures.push(format!("Win32_SCSIController: {e}")),
}
// IDE controllers
match Self::query_wmi_json(
"root\\CIMV2",
"SELECT Name, Manufacturer, DriverName, DeviceID, Status FROM Win32_IDEController",
) {
Ok(text) => {
any_ok = true;
self.parse_windows_controllers(&text, StorageInterface::AHCI);
}
Err(e) => failures.push(format!("Win32_IDEController: {e}")),
}
// There is deliberately no `MSFT_PhysicalDisk` query here.
//
// One used to run, `WHERE BusType = 17`, and push each row as a
// controller. `MSFT_PhysicalDisk` is what `Get-PhysicalDisk` wraps: it
// enumerates **disks**. So every NVMe device appeared twice -- once as
// its actual controller from `Win32_SCSIController`, and once as the
// drive itself -- and this host reported nine controllers where six
// exist, three of them named `Samsung SSD 990 PRO 4TB` and carrying no
// PCI address, because a disk does not have a controller's address.
//
// Everything that branch published about those drives -- model, serial,
// firmware -- is disk identity, and `disk.{n}` already reports it from
// a reader that knows it is describing a disk. The `nvme_info` it
// attached was never resolved into the ontology at all, and its
// `namespace_count: 1` was a constant.
if any_ok {
Ok(())
} else {
Err(IronError::System(format!(
"no storage controller enumeration succeeded: {}",
failures.join("; ")
)))
}
}
#[cfg(target_os = "windows")]
fn parse_windows_controllers(&mut self, json_text: &str, default_iface: StorageInterface) {
if let Ok(val) = serde_json::from_str::<serde_json::Value>(json_text) {
let items = match &val {
serde_json::Value::Array(arr) => arr.clone(),
obj @ serde_json::Value::Object(_) => vec![obj.clone()],
_ => return,
};
for item in &items {
let name = item["Name"].as_str().unwrap_or("").to_string();
let vendor = item["Manufacturer"].as_str().unwrap_or("").to_string();
let driver = item["DriverName"].as_str().unwrap_or("").to_string();
// The bound driver names the transport, and the row already
// carries it. `default_iface` is the class of the WMI query
// that returned the row, which is not a property of the
// device: Windows exposes NVMe controllers through
// `Win32_SCSIController`, so all three on this host were
// published as `SCSI`.
//
// The name check below it did not save them either. It looked
// for `nvme`, and Windows calls them `Standard NVM Express
// Controller` -- the substring the reader wanted is the one
// string Windows does not use.
let lower_name = name.to_lowercase();
let interface = match driver.to_lowercase().as_str() {
"stornvme" => StorageInterface::NVMe,
"storahci" => StorageInterface::AHCI,
_ if lower_name.contains("nvme") || lower_name.contains("nvm express") => {
StorageInterface::NVMe
}
_ if lower_name.contains("sas") => StorageInterface::SAS,
_ if lower_name.contains("usb") => StorageInterface::USB,
_ if lower_name.contains("ahci") || lower_name.contains("sata") => {
StorageInterface::AHCI
}
_ => default_iface.clone(),
};
// `DeviceID` is a device instance path, not an address. For a
// controller on a PCI bus Windows records the address in the
// same registry value the PCI enumeration reads, so ask for it:
// all three NVMe controllers here resolve to `0000:02:00.0`,
// `0000:06:00.0` and `0000:0d:00.0`, each of which is a node in
// `pci.*` in the same snapshot. The ontology was refusing the
// instance path -- correctly -- under a reason claiming the
// address could not be joined against `pci.*`.
//
// The path is kept when there is no address to be had: a
// `ROOT\SPACEPORT\0000` or `SWD\XVDDENUM\...` controller is not
// on a PCI bus, the ontology declines to publish it, and the
// absence reason quotes the path, which says exactly that.
let device_id = item["DeviceID"].as_str().unwrap_or("").to_string();
let pci_address = crate::pci_devices::pci_address_of(&device_id)
.unwrap_or_else(|| device_id.clone());
self.controllers.push(StorageControllerInfo {
name: name.clone(),
vendor,
model: name,
driver,
interface,
pci_address,
ports: 0,
nvme_info: None,
});
}
}
}
// ── macOS implementation ──
#[cfg(target_os = "macos")]
fn refresh_macos(&mut self) -> Result<(), IronError> {
// NVMe controllers
let nvme =
crate::core::command::capture_json("system_profiler", &["SPNVMeDataType", "-json"])?;
if let Some(val) = nvme {
if let Some(items) = val["SPNVMeDataType"].as_array() {
for (i, item) in items.iter().enumerate() {
let name = item["_name"].as_str().unwrap_or("NVMe").to_string();
let model = item["device_model"].as_str().unwrap_or("").to_string();
let serial = item["device_serial"].as_str().unwrap_or("").to_string();
let firmware = item["device_revision"].as_str().unwrap_or("").to_string();
let nvme_info = NvmeControllerInfo {
name: format!("nvme{}", i),
model: model.clone(),
serial,
firmware,
pcie_speed: item["spnvme_linkspeed"].as_str().unwrap_or("").to_string(),
pcie_width: item["spnvme_linkwidth"].as_str().unwrap_or("").to_string(),
namespace_count: 1,
transport: "pcie".to_string(),
};
self.controllers.push(StorageControllerInfo {
name,
vendor: String::new(),
model,
driver: "nvme".to_string(),
interface: StorageInterface::NVMe,
pci_address: String::new(),
// Not read here either; see above.
ports: 0,
nvme_info: Some(nvme_info),
});
}
}
}
// SATA controllers
let sata = crate::core::command::capture_json(
"system_profiler",
&["SPSerialATADataType", "-json"],
)?;
if let Some(val) = sata {
if let Some(items) = val["SPSerialATADataType"].as_array() {
for item in items {
let name = item["_name"].as_str().unwrap_or("SATA").to_string();
let vendor = item["spsata_vendor"].as_str().unwrap_or("").to_string();
let port_count = item["_items"]
.as_array()
.map(|a| a.len() as u32)
.unwrap_or(0);
self.controllers.push(StorageControllerInfo {
name,
vendor,
model: String::new(),
driver: "ahci".to_string(),
interface: StorageInterface::AHCI,
pci_address: String::new(),
ports: port_count,
nvme_info: None,
});
}
}
}
Ok(())
}
}
impl Default for StorageControllerMonitor {
fn default() -> Self {
Self::new().unwrap_or(Self {
controllers: Vec::new(),
raid_arrays: Vec::new(),
})
}
}
impl std::fmt::Display for StorageInterface {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NVMe => write!(f, "NVMe"),
Self::AHCI => write!(f, "AHCI/SATA"),
Self::SAS => write!(f, "SAS"),
Self::SCSI => write!(f, "SCSI"),
Self::IDE => write!(f, "IDE"),
Self::USB => write!(f, "USB"),
Self::FibreChannel => write!(f, "Fibre Channel"),
Self::ISCSI => write!(f, "iSCSI"),
Self::Virtio => write!(f, "Virtio"),
Self::Unknown => write!(f, "Unknown"),
}
}
}
impl std::fmt::Display for RaidLevel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Raid0 => write!(f, "RAID 0"),
Self::Raid1 => write!(f, "RAID 1"),
Self::Raid5 => write!(f, "RAID 5"),
Self::Raid6 => write!(f, "RAID 6"),
Self::Raid10 => write!(f, "RAID 10"),
Self::Raid50 => write!(f, "RAID 50"),
Self::Raid60 => write!(f, "RAID 60"),
Self::JBOD => write!(f, "JBOD"),
Self::Linear => write!(f, "Linear"),
Self::Unknown(s) => write!(f, "{}", s),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Constructing the monitor either enumerates or says why it could not.
///
/// This asserted `is_ok()`, which was true by construction until 6.0.0
/// because `refresh` could not fail. Now that it can, the contract is
/// weaker and more useful: **whatever happens, the caller can tell which
/// happened.** A failure has to carry a reason, because a reason is the
/// whole difference between "this machine has none" and "nobody looked".
#[test]
fn test_storage_controller_creation() {
match StorageControllerMonitor::new() {
Ok(_monitor) => {}
Err(e) => {
let why = e.to_string();
assert!(
why.len() > 10,
"enumeration failed without saying why: {why:?}"
);
}
}
}
#[test]
fn test_storage_controller_default() {
let monitor = StorageControllerMonitor::default();
let _ = monitor.controllers();
let _ = monitor.raid_arrays();
let _ = monitor.nvme_controllers();
let _ = monitor.has_degraded_raids();
}
#[test]
fn test_nvme_info_serialization() {
let info = NvmeControllerInfo {
name: "nvme0".into(),
model: "Samsung 980 PRO".into(),
serial: "ABC123".into(),
firmware: "5B2QGXA7".into(),
pcie_speed: "8 GT/s".into(),
pcie_width: "x4".into(),
namespace_count: 1,
transport: "pcie".into(),
};
let json = serde_json::to_string(&info).unwrap();
assert!(json.contains("Samsung"));
let _: NvmeControllerInfo = serde_json::from_str(&json).unwrap();
}
#[test]
fn test_raid_serialization() {
let array = RaidArrayInfo {
name: "md0".into(),
level: RaidLevel::Raid1,
status: RaidStatus::Active,
active_devices: 2,
total_devices: 2,
failed_devices: 0,
spare_devices: 0,
size_bytes: 1024 * 1024 * 1024,
members: vec!["sda1".into(), "sdb1".into()],
resync_progress: None,
};
let json = serde_json::to_string(&array).unwrap();
assert!(json.contains("md0"));
let _: RaidArrayInfo = serde_json::from_str(&json).unwrap();
}
#[test]
fn test_interface_display() {
assert_eq!(StorageInterface::NVMe.to_string(), "NVMe");
assert_eq!(StorageInterface::AHCI.to_string(), "AHCI/SATA");
assert_eq!(RaidLevel::Raid1.to_string(), "RAID 1");
}
}