hardware_report 0.1.7

Hardware inventory reports for observability and cluster acceptance testing
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
/*
Copyright 2024 San Francisco Compute Company

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

//! macOS system information provider

use crate::domain::{
    parse_hostname_output, parse_macos_cpu_info, parse_macos_memory_info, parse_macos_network_info,
    parse_macos_storage_info, BiosInfo, ChassisInfo, CpuInfo, GpuInfo, MemoryInfo, MotherboardInfo,
    NetworkInfo, NumaNode, StorageInfo, SystemError, SystemInfo,
};
use crate::ports::{CommandExecutor, SystemCommand, SystemInfoProvider};
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

/// macOS system information provider using system_profiler and other macOS tools
pub struct MacOSSystemInfoProvider {
    command_executor: Arc<dyn CommandExecutor>,
}

impl MacOSSystemInfoProvider {
    /// Create a new macOS system information provider
    pub fn new(command_executor: Arc<dyn CommandExecutor>) -> Self {
        Self { command_executor }
    }

    /// Check if required commands are available
    pub async fn check_required_commands(&self) -> Vec<String> {
        let required_commands = ["system_profiler", "sysctl", "ioreg", "hostname", "df"];

        let mut missing = Vec::new();
        for cmd in &required_commands {
            if let Ok(false) = self.command_executor.is_command_available(cmd).await {
                missing.push(cmd.to_string());
            }
        }
        missing
    }
}

#[async_trait]
impl SystemInfoProvider for MacOSSystemInfoProvider {
    async fn get_cpu_info(&self) -> Result<CpuInfo, SystemError> {
        let system_profiler_cmd = SystemCommand::new("system_profiler")
            .args(&["SPHardwareDataType"])
            .timeout(Duration::from_secs(15));
        let output = self
            .command_executor
            .execute(&system_profiler_cmd)
            .await
            .map_err(|e| SystemError::CommandFailed {
                command: "system_profiler".to_string(),
                exit_code: None,
                stderr: e.to_string(),
            })?;

        parse_macos_cpu_info(&output.stdout).map_err(SystemError::ParseError)
    }

    async fn get_memory_info(&self) -> Result<MemoryInfo, SystemError> {
        let system_profiler_cmd = SystemCommand::new("system_profiler")
            .args(&["SPMemoryDataType"])
            .timeout(Duration::from_secs(15));
        let output = self
            .command_executor
            .execute(&system_profiler_cmd)
            .await
            .map_err(|e| SystemError::CommandFailed {
                command: "system_profiler".to_string(),
                exit_code: None,
                stderr: e.to_string(),
            })?;

        parse_macos_memory_info(&output.stdout).map_err(SystemError::ParseError)
    }

    async fn get_storage_info(&self) -> Result<StorageInfo, SystemError> {
        let system_profiler_cmd = SystemCommand::new("system_profiler")
            .args(&["SPStorageDataType", "-detailLevel", "full"])
            .timeout(Duration::from_secs(15));
        let output = self
            .command_executor
            .execute(&system_profiler_cmd)
            .await
            .map_err(|e| SystemError::CommandFailed {
                command: "system_profiler".to_string(),
                exit_code: None,
                stderr: e.to_string(),
            })?;

        let devices = parse_macos_storage_info(&output.stdout).map_err(SystemError::ParseError)?;

        Ok(StorageInfo { devices })
    }

    async fn get_gpu_info(&self) -> Result<GpuInfo, SystemError> {
        let system_profiler_cmd = SystemCommand::new("system_profiler")
            .args(&["SPDisplaysDataType"])
            .timeout(Duration::from_secs(15));
        let output = self
            .command_executor
            .execute(&system_profiler_cmd)
            .await
            .map_err(|e| SystemError::CommandFailed {
                command: "system_profiler".to_string(),
                exit_code: None,
                stderr: e.to_string(),
            })?;

        let mut devices = Vec::new();
        let mut gpu_index = 0;

        // Parse macOS GPU/display info
        for line in output.stdout.lines() {
            let trimmed = line.trim();
            if (trimmed.contains("M1")
                || trimmed.contains("M2")
                || trimmed.contains("M3")
                || trimmed.contains("M4"))
                && (trimmed.contains("Max") || trimmed.contains("Pro") || trimmed.contains("Ultra"))
            {
                let memory_cores = if trimmed.contains("M4 Max") {
                    "40 cores"
                } else if trimmed.contains("M4 Pro") {
                    "20 cores"
                } else if trimmed.contains("M3 Max") {
                    "40 cores"
                } else if trimmed.contains("M3 Pro") {
                    "18 cores"
                } else if trimmed.contains("M2 Max") {
                    "38 cores"
                } else if trimmed.contains("M2 Pro") {
                    "19 cores"
                } else if trimmed.contains("M1 Max") {
                    "32 cores"
                } else if trimmed.contains("M1 Pro") {
                    "16 cores"
                } else {
                    "Unknown"
                };

                devices.push(crate::domain::GpuDevice {
                    index: gpu_index,
                    name: format!("Apple {trimmed} (Metal 3)"),
                    uuid: format!("macOS-GPU-{gpu_index}"),
                    memory: format!("Unified Memory ({memory_cores} GPU cores)"),
                    pci_id: "Apple Fabric (Integrated)".to_string(),
                    vendor: "Apple".to_string(),
                    numa_node: None,
                    ..Default::default()
                });
                gpu_index += 1;
            }
        }

        // If no Apple Silicon GPU found, add a generic entry
        if devices.is_empty() {
            devices.push(crate::domain::GpuDevice {
                index: 0,
                name: "Integrated Graphics".to_string(),
                uuid: "macOS-GPU-0".to_string(),
                memory: "Unknown".to_string(),
                pci_id: "Apple Fabric (Integrated)".to_string(),
                vendor: "Apple".to_string(),
                numa_node: None,
                ..Default::default()
            });
        }

        Ok(GpuInfo { devices })
    }

    async fn get_network_info(&self) -> Result<NetworkInfo, SystemError> {
        let ifconfig_cmd = SystemCommand::new("ifconfig").timeout(Duration::from_secs(10));
        let output = self
            .command_executor
            .execute(&ifconfig_cmd)
            .await
            .map_err(|e| SystemError::CommandFailed {
                command: "ifconfig".to_string(),
                exit_code: None,
                stderr: e.to_string(),
            })?;

        let interfaces =
            parse_macos_network_info(&output.stdout).map_err(SystemError::ParseError)?;

        Ok(NetworkInfo {
            interfaces,
            infiniband: None, // macOS doesn't typically have InfiniBand
        })
    }

    async fn get_bios_info(&self) -> Result<BiosInfo, SystemError> {
        // For Apple Silicon, get firmware info
        let system_profiler_cmd = SystemCommand::new("system_profiler")
            .args(&["SPHardwareDataType"])
            .timeout(Duration::from_secs(15));
        let output = self
            .command_executor
            .execute(&system_profiler_cmd)
            .await
            .map_err(|e| SystemError::CommandFailed {
                command: "system_profiler".to_string(),
                exit_code: None,
                stderr: e.to_string(),
            })?;

        let vendor = "Apple Inc.".to_string();
        let mut version = "Unknown".to_string();
        let mut release_date = "Unknown".to_string();

        for line in output.stdout.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with("System Firmware Version:") {
                version = trimmed
                    .split(':')
                    .nth(1)
                    .unwrap_or("Unknown")
                    .trim()
                    .to_string();
            } else if trimmed.starts_with("OS Loader Version:") {
                release_date = trimmed
                    .split(':')
                    .nth(1)
                    .unwrap_or("Unknown")
                    .trim()
                    .to_string();
            }
        }

        Ok(BiosInfo {
            vendor,
            version: version.clone(),
            release_date,
            firmware_version: version,
        })
    }

    async fn get_chassis_info(&self) -> Result<ChassisInfo, SystemError> {
        let system_profiler_cmd = SystemCommand::new("system_profiler")
            .args(&["SPHardwareDataType"])
            .timeout(Duration::from_secs(15));
        let output = self
            .command_executor
            .execute(&system_profiler_cmd)
            .await
            .map_err(|e| SystemError::CommandFailed {
                command: "system_profiler".to_string(),
                exit_code: None,
                stderr: e.to_string(),
            })?;

        let manufacturer = "Apple Inc.".to_string();
        let mut type_ = "Laptop".to_string();
        let mut serial = "Unknown".to_string();

        for line in output.stdout.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with("Model Name:") {
                let model = trimmed.split(':').nth(1).unwrap_or("").trim();
                if model.contains("iMac")
                    || model.contains("Mac Pro")
                    || model.contains("Mac Studio")
                {
                    type_ = "Desktop".to_string();
                }
            } else if trimmed.starts_with("Serial Number (system):") {
                serial = trimmed
                    .split(':')
                    .nth(1)
                    .unwrap_or("Unknown")
                    .trim()
                    .to_string();
            }
        }

        Ok(ChassisInfo {
            manufacturer,
            type_,
            serial,
        })
    }

    async fn get_motherboard_info(&self) -> Result<MotherboardInfo, SystemError> {
        let system_profiler_cmd = SystemCommand::new("system_profiler")
            .args(&["SPHardwareDataType"])
            .timeout(Duration::from_secs(15));
        let output = self
            .command_executor
            .execute(&system_profiler_cmd)
            .await
            .map_err(|e| SystemError::CommandFailed {
                command: "system_profiler".to_string(),
                exit_code: None,
                stderr: e.to_string(),
            })?;

        let manufacturer = "Apple Inc.".to_string();
        let mut product_name = "Unknown Product".to_string();
        let mut version = "Unknown Version".to_string();
        let mut serial = "Unknown S/N".to_string();

        for line in output.stdout.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with("Model Identifier:") {
                product_name = trimmed
                    .split(':')
                    .nth(1)
                    .unwrap_or("Unknown Product")
                    .trim()
                    .to_string();
            } else if trimmed.starts_with("System Firmware Version:") {
                version = trimmed
                    .split(':')
                    .nth(1)
                    .unwrap_or("Unknown Version")
                    .trim()
                    .to_string();
            } else if trimmed.starts_with("Serial Number (system):") {
                serial = trimmed
                    .split(':')
                    .nth(1)
                    .unwrap_or("Unknown S/N")
                    .trim()
                    .to_string();
            }
        }

        Ok(MotherboardInfo {
            manufacturer,
            product_name,
            version,
            serial,
            features: "Integrated".to_string(),
            location: "System Board".to_string(),
            type_: "Motherboard".to_string(),
        })
    }

    async fn get_system_info(&self) -> Result<SystemInfo, SystemError> {
        let system_profiler_cmd = SystemCommand::new("system_profiler")
            .args(&["SPHardwareDataType"])
            .timeout(Duration::from_secs(15));
        let output = self
            .command_executor
            .execute(&system_profiler_cmd)
            .await
            .map_err(|e| SystemError::CommandFailed {
                command: "system_profiler".to_string(),
                exit_code: None,
                stderr: e.to_string(),
            })?;

        let mut uuid = "Unknown".to_string();
        let mut serial = "Unknown".to_string();
        let mut product_name = "Unknown".to_string();
        let manufacturer = "Apple Inc.".to_string();

        for line in output.stdout.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with("Hardware UUID:") {
                uuid = trimmed
                    .split(':')
                    .nth(1)
                    .unwrap_or("Unknown")
                    .trim()
                    .to_string();
            } else if trimmed.starts_with("Serial Number (system):") {
                serial = trimmed
                    .split(':')
                    .nth(1)
                    .unwrap_or("Unknown")
                    .trim()
                    .to_string();
            } else if trimmed.starts_with("Model Name:") {
                let model = trimmed.split(':').nth(1).unwrap_or("Unknown").trim();
                let chip = output
                    .stdout
                    .lines()
                    .find(|line| line.trim().starts_with("Chip:"))
                    .and_then(|line| line.split(':').nth(1))
                    .unwrap_or("")
                    .trim();
                product_name = if !chip.is_empty() {
                    format!("{model} ({chip})")
                } else {
                    model.to_string()
                };
            }
        }

        Ok(SystemInfo {
            uuid,
            serial,
            product_name,
            product_manufacturer: manufacturer,
        })
    }

    async fn get_numa_topology(&self) -> Result<HashMap<String, NumaNode>, SystemError> {
        // macOS doesn't expose NUMA topology in the same way as Linux
        Ok(HashMap::new())
    }

    async fn get_hostname(&self) -> Result<String, SystemError> {
        let hostname_cmd = SystemCommand::new("hostname").timeout(Duration::from_secs(5));
        let hostname_output = self
            .command_executor
            .execute(&hostname_cmd)
            .await
            .map_err(|e| SystemError::CommandFailed {
                command: "hostname".to_string(),
                exit_code: None,
                stderr: e.to_string(),
            })?;

        parse_hostname_output(&hostname_output.stdout).map_err(SystemError::ParseError)
    }

    async fn get_fqdn(&self) -> Result<String, SystemError> {
        // On macOS, hostname -f might not work, so fall back to regular hostname
        let hostname_cmd = SystemCommand::new("hostname").timeout(Duration::from_secs(5));
        let hostname_output = self
            .command_executor
            .execute(&hostname_cmd)
            .await
            .map_err(|e| SystemError::CommandFailed {
                command: "hostname".to_string(),
                exit_code: None,
                stderr: e.to_string(),
            })?;

        parse_hostname_output(&hostname_output.stdout).map_err(SystemError::ParseError)
    }

    async fn get_filesystems(&self) -> Result<Vec<String>, SystemError> {
        let df_cmd = SystemCommand::new("df")
            .args(&["-h"])
            .timeout(Duration::from_secs(5));
        let df_output = self.command_executor.execute(&df_cmd).await.map_err(|e| {
            SystemError::CommandFailed {
                command: "df".to_string(),
                exit_code: None,
                stderr: e.to_string(),
            }
        })?;

        let mut filesystems = Vec::new();
        for line in df_output.stdout.lines().skip(1) {
            // Skip header
            if !line.trim().is_empty() && !line.starts_with("map ") {
                filesystems.push(line.to_string());
            }
        }

        Ok(filesystems)
    }

    async fn has_required_privileges(&self) -> Result<bool, SystemError> {
        // Most macOS system_profiler commands don't require sudo
        Ok(true)
    }

    async fn get_missing_dependencies(&self) -> Result<Vec<String>, SystemError> {
        Ok(self.check_required_commands().await)
    }
}