osvm 0.8.3

OpenSVM CLI tool for managing SVM nodes and deployments
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
//! System health assessment utilities
//!
//! This module provides detailed system health assessment capabilities
//! and health status management.

use super::{HealthIssue, HealthStatus, IssueCategory, IssueSeverity, SystemHealth};
use std::collections::HashMap;
use std::process::Command;

/// System health analyzer
pub struct SystemHealthAnalyzer;

impl SystemHealthAnalyzer {
    /// Create a new system health analyzer
    pub fn new() -> Self {
        Self
    }

    /// Analyze system health from collected data
    pub fn analyze_health(
        &self,
        system_deps: &[crate::utils::self_repair::system_deps::DependencyInfo],
        user_config: &crate::utils::self_repair::user_deps::SolanaConfigInfo,
        network_health: &super::NetworkHealth,
    ) -> SystemHealth {
        let mut issues = Vec::new();
        let mut recommendations = Vec::new();

        // Check system tuning parameters FIRST (new)
        self.check_system_tuning(&mut issues, &mut recommendations);

        // Analyze system dependencies
        for dep in system_deps {
            if !dep.installed {
                issues.push(HealthIssue {
                    severity: IssueSeverity::Error,
                    category: IssueCategory::SystemDependencies,
                    title: format!("Missing dependency: {}", dep.name),
                    description: format!(
                        "Required system dependency '{}' is not installed",
                        dep.name
                    ),
                    suggested_fix: Some(format!("Install {} using your package manager", dep.name)),
                });
            } else if dep.update_available {
                issues.push(HealthIssue {
                    severity: IssueSeverity::Warning,
                    category: IssueCategory::SystemDependencies,
                    title: format!("Update available: {}", dep.name),
                    description: format!("An update is available for {}", dep.name),
                    suggested_fix: Some("Run system updates".to_string()),
                });
            }
        }

        // Analyze user configuration
        if !user_config.cli_installed {
            issues.push(HealthIssue {
                severity: IssueSeverity::Error,
                category: IssueCategory::UserConfiguration,
                title: "Solana CLI not installed".to_string(),
                description: "Solana CLI is required for OSVM operations".to_string(),
                suggested_fix: Some("Install Solana CLI using the official installer".to_string()),
            });
            recommendations
                .push("Run 'osvm doctor --fix' to automatically install Solana CLI".to_string());
        }

        if !user_config.config_dir_exists {
            issues.push(HealthIssue {
                severity: IssueSeverity::Warning,
                category: IssueCategory::UserConfiguration,
                title: "Solana config directory missing".to_string(),
                description: "Solana configuration directory does not exist".to_string(),
                suggested_fix: Some("Create configuration directory".to_string()),
            });
        }

        if !user_config.keypair_exists {
            issues.push(HealthIssue {
                severity: IssueSeverity::Warning,
                category: IssueCategory::UserConfiguration,
                title: "Solana keypair missing".to_string(),
                description: "No Solana keypair found".to_string(),
                suggested_fix: Some("Generate a new keypair with 'solana-keygen new'".to_string()),
            });
            recommendations
                .push("Run 'osvm doctor --fix' to automatically generate a keypair".to_string());
        }

        // Analyze network connectivity
        if !network_health.internet_connected {
            issues.push(HealthIssue {
                severity: IssueSeverity::Error,
                category: IssueCategory::NetworkConnectivity,
                title: "No internet connection".to_string(),
                description: "Internet connectivity is required for Solana operations".to_string(),
                suggested_fix: Some("Check network connection and firewall settings".to_string()),
            });
        }

        let solana_endpoints_down = !network_health.solana_mainnet_accessible
            && !network_health.solana_testnet_accessible
            && !network_health.solana_devnet_accessible;

        if solana_endpoints_down {
            issues.push(HealthIssue {
                severity: IssueSeverity::Error,
                category: IssueCategory::NetworkConnectivity,
                title: "Solana endpoints unreachable".to_string(),
                description: "All Solana network endpoints are unreachable".to_string(),
                suggested_fix: Some("Check firewall settings and network connectivity".to_string()),
            });
        } else {
            // Check individual endpoints
            if !network_health.solana_mainnet_accessible {
                issues.push(HealthIssue {
                    severity: IssueSeverity::Warning,
                    category: IssueCategory::NetworkConnectivity,
                    title: "Solana mainnet unreachable".to_string(),
                    description: "Solana mainnet endpoint is not accessible".to_string(),
                    suggested_fix: Some("Check network connectivity to mainnet".to_string()),
                });
            }

            if !network_health.solana_testnet_accessible {
                issues.push(HealthIssue {
                    severity: IssueSeverity::Warning,
                    category: IssueCategory::NetworkConnectivity,
                    title: "Solana testnet unreachable".to_string(),
                    description: "Solana testnet endpoint is not accessible".to_string(),
                    suggested_fix: Some("Check network connectivity to testnet".to_string()),
                });
            }

            if !network_health.solana_devnet_accessible {
                issues.push(HealthIssue {
                    severity: IssueSeverity::Warning,
                    category: IssueCategory::NetworkConnectivity,
                    title: "Solana devnet unreachable".to_string(),
                    description: "Solana devnet endpoint is not accessible".to_string(),
                    suggested_fix: Some("Check network connectivity to devnet".to_string()),
                });
            }
        }

        // Performance checks
        self.check_performance_issues(&network_health.response_times, &mut issues);

        // Security checks
        self.check_security_issues(user_config, &mut issues);

        // Determine overall status
        let overall_status = self.determine_overall_status(&issues);

        // Add general recommendations
        if !issues.is_empty() {
            recommendations.push(
                "Use 'osvm doctor --verbose' for detailed diagnostic information".to_string(),
            );
        }

        SystemHealth {
            overall_status,
            system_dependencies: system_deps.to_vec(),
            user_configuration: user_config.clone(),
            network_connectivity: network_health.clone(),
            issues,
            recommendations,
        }
    }

    /// Check system tuning parameters for Solana validator requirements
    fn check_system_tuning(
        &self,
        issues: &mut Vec<HealthIssue>,
        recommendations: &mut Vec<String>,
    ) {
        // Define required kernel parameters
        let required_params = vec![
            ("net.core.rmem_max", 134217728u64, "134217728"),
            ("net.core.rmem_default", 134217728u64, "134217728"),
            ("net.core.wmem_max", 134217728u64, "134217728"),
            ("net.core.wmem_default", 134217728u64, "134217728"),
            ("vm.max_map_count", 2000000u64, "2000000"),
        ];

        for (param, min_value, recommended) in required_params {
            match self.check_kernel_parameter(param) {
                Ok(current_value) => {
                    if current_value < min_value {
                        issues.push(HealthIssue {
                            severity: IssueSeverity::Error,
                            category: IssueCategory::SystemDependencies,
                            title: format!("System tuning: {} too low", param),
                            description: format!(
                                "Kernel parameter {} is set to {}, but Solana requires at least {}",
                                param, current_value, min_value
                            ),
                            suggested_fix: Some(format!(
                                "Run: sudo sysctl -w {}={}",
                                param, recommended
                            )),
                        });
                        recommendations.push(
                            "Run 'osvm doctor --fix' to automatically tune system parameters"
                                .to_string(),
                        );
                    }
                }
                Err(_) => {
                    issues.push(HealthIssue {
                        severity: IssueSeverity::Warning,
                        category: IssueCategory::SystemDependencies,
                        title: format!("Cannot check system parameter: {}", param),
                        description: format!("Unable to read kernel parameter {}", param),
                        suggested_fix: Some(
                            "Check system permissions or kernel configuration".to_string(),
                        ),
                    });
                }
            }
        }

        // Check file descriptor limits
        match self.check_file_descriptor_limit() {
            Ok(limit) => {
                if limit < 1000000 {
                    issues.push(HealthIssue {
                        severity: IssueSeverity::Warning,
                        category: IssueCategory::SystemDependencies,
                        title: "File descriptor limit too low".to_string(),
                        description: format!(
                            "Current file descriptor limit is {}, Solana recommends at least 1,000,000",
                            limit
                        ),
                        suggested_fix: Some("Update /etc/security/limits.conf or systemd limits".to_string()),
                    });
                }
            }
            Err(_) => {
                // Silently ignore if we can't check
            }
        }
    }

    /// Check a specific kernel parameter value
    fn check_kernel_parameter(&self, param: &str) -> Result<u64, String> {
        let output = Command::new("sysctl")
            .arg("-n")
            .arg(param)
            .output()
            .map_err(|e| format!("Failed to run sysctl: {}", e))?;

        if !output.status.success() {
            return Err("sysctl command failed".to_string());
        }

        let value_str = String::from_utf8_lossy(&output.stdout).trim().to_string();
        value_str
            .parse::<u64>()
            .map_err(|e| format!("Failed to parse value: {}", e))
    }

    /// Check file descriptor limits
    fn check_file_descriptor_limit(&self) -> Result<u64, String> {
        let output = Command::new("ulimit")
            .arg("-n")
            .output()
            .map_err(|e| format!("Failed to run ulimit: {}", e))?;

        if !output.status.success() {
            return Err("ulimit command failed".to_string());
        }

        let value_str = String::from_utf8_lossy(&output.stdout).trim().to_string();
        value_str
            .parse::<u64>()
            .map_err(|e| format!("Failed to parse value: {}", e))
    }

    /// Check for performance-related issues
    fn check_performance_issues(
        &self,
        response_times: &HashMap<String, u64>,
        issues: &mut Vec<HealthIssue>,
    ) {
        for (endpoint, &response_time) in response_times {
            if response_time > 5000 {
                // 5 seconds
                issues.push(HealthIssue {
                    severity: IssueSeverity::Warning,
                    category: IssueCategory::Performance,
                    title: format!("Slow network response: {}", endpoint),
                    description: format!(
                        "Network response time to {} is {} ms",
                        endpoint, response_time
                    ),
                    suggested_fix: Some(
                        "Check network connectivity and consider using a different RPC endpoint"
                            .to_string(),
                    ),
                });
            } else if response_time > 10000 {
                // 10 seconds
                issues.push(HealthIssue {
                    severity: IssueSeverity::Error,
                    category: IssueCategory::Performance,
                    title: format!("Very slow network response: {}", endpoint),
                    description: format!(
                        "Network response time to {} is {} ms",
                        endpoint, response_time
                    ),
                    suggested_fix: Some(
                        "Network performance is severely degraded - check connection".to_string(),
                    ),
                });
            }
        }
    }

    /// Check for security-related issues
    fn check_security_issues(
        &self,
        user_config: &crate::utils::self_repair::user_deps::SolanaConfigInfo,
        issues: &mut Vec<HealthIssue>,
    ) {
        // Check if keypair file has appropriate permissions
        if let Some(ref keypair_path) = user_config.keypair_path {
            if std::path::Path::new(keypair_path).exists() {
                if let Ok(metadata) = std::fs::metadata(keypair_path) {
                    // On Unix systems, check file permissions
                    #[cfg(unix)]
                    {
                        use std::os::unix::fs::PermissionsExt;
                        let mode = metadata.permissions().mode();
                        let others_readable = (mode & 0o044) != 0;
                        let group_writable = (mode & 0o020) != 0;
                        let others_writable = (mode & 0o002) != 0;

                        if others_readable || group_writable || others_writable {
                            issues.push(HealthIssue {
                                severity: IssueSeverity::Warning,
                                category: IssueCategory::Security,
                                title: "Keypair file permissions too permissive".to_string(),
                                description: format!(
                                    "Keypair file {} has overly permissive permissions",
                                    keypair_path
                                ),
                                suggested_fix: Some(format!("Run: chmod 600 {}", keypair_path)),
                            });
                        }
                    }
                }
            }
        }

        // Check if using default/development networks inappropriately
        if let Some(ref network) = user_config.current_network {
            if network == "devnet" {
                issues.push(HealthIssue {
                    severity: IssueSeverity::Info,
                    category: IssueCategory::Security,
                    title: "Using development network".to_string(),
                    description: "Currently configured to use Solana devnet".to_string(),
                    suggested_fix: Some(
                        "Consider switching to mainnet for production use".to_string(),
                    ),
                });
            }
        }
    }

    /// Determine overall system status based on issues
    fn determine_overall_status(&self, issues: &[HealthIssue]) -> HealthStatus {
        if issues.iter().any(|i| i.severity == IssueSeverity::Critical) {
            HealthStatus::Critical
        } else if issues.iter().any(|i| i.severity == IssueSeverity::Error) {
            HealthStatus::Critical
        } else if issues.iter().any(|i| i.severity == IssueSeverity::Warning) {
            HealthStatus::Warning
        } else if issues.is_empty() {
            HealthStatus::Healthy
        } else {
            HealthStatus::Unknown
        }
    }

    /// Get health status as a color-coded emoji
    pub fn get_status_emoji(status: &HealthStatus) -> &'static str {
        match status {
            HealthStatus::Healthy => "🟢",
            HealthStatus::Warning => "🟡",
            HealthStatus::Critical => "🔴",
            HealthStatus::Unknown => "⚪",
        }
    }

    /// Get health status as a descriptive string
    pub fn get_status_description(status: &HealthStatus) -> &'static str {
        match status {
            HealthStatus::Healthy => "All systems operational",
            HealthStatus::Warning => "Minor issues detected",
            HealthStatus::Critical => "Critical issues require attention",
            HealthStatus::Unknown => "System status unknown",
        }
    }

    /// Generate a health score (0-100)
    pub fn calculate_health_score(&self, health: &SystemHealth) -> u8 {
        let mut score = 100u8;

        for issue in &health.issues {
            let deduction = match issue.severity {
                IssueSeverity::Critical => 25,
                IssueSeverity::Error => 15,
                IssueSeverity::Warning => 5,
                IssueSeverity::Info => 1,
            };
            score = score.saturating_sub(deduction);
        }

        score
    }

    /// Get recommendations for improving system health
    pub fn get_improvement_recommendations(&self, health: &SystemHealth) -> Vec<String> {
        let mut recommendations = health.recommendations.clone();

        // Add category-specific recommendations
        let has_system_issues = health
            .issues
            .iter()
            .any(|i| matches!(i.category, IssueCategory::SystemDependencies));
        let has_user_issues = health
            .issues
            .iter()
            .any(|i| matches!(i.category, IssueCategory::UserConfiguration));
        let has_network_issues = health
            .issues
            .iter()
            .any(|i| matches!(i.category, IssueCategory::NetworkConnectivity));

        if has_system_issues {
            recommendations.push("Consider updating system packages regularly".to_string());
        }

        if has_user_issues {
            recommendations.push(
                "Review user configuration and ensure all required tools are installed".to_string(),
            );
        }

        if has_network_issues {
            recommendations.push("Check firewall settings and network connectivity".to_string());
        }

        // Add proactive recommendations
        if health.overall_status == HealthStatus::Healthy {
            recommendations.push(
                "System is healthy - consider running 'osvm doctor' periodically".to_string(),
            );
            recommendations.push("Keep system dependencies up to date".to_string());
        }

        recommendations
    }
}

impl Default for SystemHealthAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::utils::self_repair::{system_deps::DependencyInfo, user_deps::SolanaConfigInfo};

    #[test]
    fn test_system_health_analyzer_creation() {
        let analyzer = SystemHealthAnalyzer::new();
        // Should not panic
    }

    #[test]
    fn test_health_status_emoji() {
        assert_eq!(
            SystemHealthAnalyzer::get_status_emoji(&HealthStatus::Healthy),
            "🟢"
        );
        assert_eq!(
            SystemHealthAnalyzer::get_status_emoji(&HealthStatus::Warning),
            "🟡"
        );
        assert_eq!(
            SystemHealthAnalyzer::get_status_emoji(&HealthStatus::Critical),
            "🔴"
        );
        assert_eq!(
            SystemHealthAnalyzer::get_status_emoji(&HealthStatus::Unknown),
            "⚪"
        );
    }

    #[test]
    fn test_health_status_description() {
        assert_eq!(
            SystemHealthAnalyzer::get_status_description(&HealthStatus::Healthy),
            "All systems operational"
        );
        assert_eq!(
            SystemHealthAnalyzer::get_status_description(&HealthStatus::Warning),
            "Minor issues detected"
        );
        assert_eq!(
            SystemHealthAnalyzer::get_status_description(&HealthStatus::Critical),
            "Critical issues require attention"
        );
    }

    #[test]
    fn test_health_score_calculation() {
        let analyzer = SystemHealthAnalyzer::new();

        // Healthy system
        let healthy_system = SystemHealth {
            overall_status: HealthStatus::Healthy,
            system_dependencies: Vec::new(),
            user_configuration: SolanaConfigInfo {
                cli_installed: true,
                cli_version: Some("1.18.0".to_string()),
                config_dir_exists: true,
                config_file_exists: true,
                keypair_exists: true,
                keypair_path: Some("/test/path".to_string()),
                current_network: Some("mainnet".to_string()),
            },
            network_connectivity: crate::utils::diagnostics::NetworkHealth {
                internet_connected: true,
                solana_mainnet_accessible: true,
                solana_testnet_accessible: true,
                solana_devnet_accessible: true,
                response_times: std::collections::HashMap::new(),
            },
            issues: Vec::new(),
            recommendations: Vec::new(),
        };

        assert_eq!(analyzer.calculate_health_score(&healthy_system), 100);

        // System with warning
        let mut warning_system = healthy_system.clone();
        warning_system.issues.push(HealthIssue {
            severity: IssueSeverity::Warning,
            category: IssueCategory::SystemDependencies,
            title: "Test warning".to_string(),
            description: "Test".to_string(),
            suggested_fix: None,
        });

        assert_eq!(analyzer.calculate_health_score(&warning_system), 95);
    }

    #[test]
    fn test_overall_status_determination() {
        let analyzer = SystemHealthAnalyzer::new();

        // No issues
        let no_issues = Vec::new();
        assert_eq!(
            analyzer.determine_overall_status(&no_issues),
            HealthStatus::Healthy
        );

        // Warning issue
        let warning_issues = vec![HealthIssue {
            severity: IssueSeverity::Warning,
            category: IssueCategory::SystemDependencies,
            title: "Test".to_string(),
            description: "Test".to_string(),
            suggested_fix: None,
        }];
        assert_eq!(
            analyzer.determine_overall_status(&warning_issues),
            HealthStatus::Warning
        );

        // Critical issue
        let critical_issues = vec![HealthIssue {
            severity: IssueSeverity::Critical,
            category: IssueCategory::SystemDependencies,
            title: "Test".to_string(),
            description: "Test".to_string(),
            suggested_fix: None,
        }];
        assert_eq!(
            analyzer.determine_overall_status(&critical_issues),
            HealthStatus::Critical
        );
    }
}