wip
This commit is contained in:
98
examples/continuous_monitoring.rs
Normal file
98
examples/continuous_monitoring.rs
Normal file
@@ -0,0 +1,98 @@
|
||||
// Exemple de surveillance continue
|
||||
use discord_client::DiscordNotifier;
|
||||
use system_monitor::{SystemMonitor, Severity};
|
||||
use std::time::Duration;
|
||||
use std::thread;
|
||||
|
||||
fn format_violation_message(violation: &system_monitor::ThresholdViolation, check_count: u32) -> String {
|
||||
let emoji = match violation.severity {
|
||||
Severity::Warning => "⚠️",
|
||||
Severity::Critical => "🚨",
|
||||
};
|
||||
|
||||
let level = match violation.severity {
|
||||
Severity::Warning => "ALERTE",
|
||||
Severity::Critical => "CRITIQUE",
|
||||
};
|
||||
|
||||
format!(
|
||||
"{} **{}** - {}\n\n**Valeur:** {:.2}%\n**Seuil:** {:.2}%\n\n*Vérification #{} à {}*",
|
||||
emoji,
|
||||
level,
|
||||
violation.metric_name,
|
||||
violation.current_value,
|
||||
violation.threshold,
|
||||
check_count,
|
||||
chrono::Utc::now().format("%Y-%m-%d %H:%M:%S UTC")
|
||||
)
|
||||
}
|
||||
|
||||
fn format_report_message(metrics: &system_monitor::SystemMetrics, check_count: u32) -> String {
|
||||
format!(
|
||||
"📊 **Rapport système** (Vérification #{})\n\n```\nCPU: {:.1}%\nMémoire: {:.1}%\nSwap: {:.1}%\nDisque: {:.1}%\n```\n\n*Système OK - Surveillance continue*",
|
||||
check_count,
|
||||
metrics.cpu_usage,
|
||||
metrics.memory_usage,
|
||||
metrics.swap_usage,
|
||||
metrics.disk_usage
|
||||
)
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("🔄 Surveillance continue démarrée\n");
|
||||
|
||||
// Configuration
|
||||
let webhook_url = std::env::var("DISCORD_WEBHOOK")
|
||||
.expect("DISCORD_WEBHOOK environment variable not set");
|
||||
|
||||
let check_interval = Duration::from_secs(30); // Vérification toutes les 30 secondes
|
||||
let report_interval = 10; // Rapport complet toutes les 10 vérifications (5 minutes)
|
||||
|
||||
// Initialisation
|
||||
let notifier = DiscordNotifier::new(
|
||||
webhook_url,
|
||||
"🔍 System Monitor".to_string(),
|
||||
"https://cdn.shopify.com/s/files/1/0262/1423/6212/files/Lord_of_the_Rings_eye_of_Sauron_-_Ghtic.com_-_Blog.png?v=1579680018".to_string(),
|
||||
);
|
||||
|
||||
let mut monitor = SystemMonitor::new();
|
||||
let mut check_count = 0;
|
||||
|
||||
// Message de démarrage
|
||||
notifier.send_notification("🟢 **Surveillance système démarrée**\n\nMonitoring actif avec vérifications toutes les 30 secondes.")?;
|
||||
|
||||
println!("✅ Surveillance démarrée - Appuyez sur Ctrl+C pour arrêter");
|
||||
|
||||
// Boucle de surveillance
|
||||
loop {
|
||||
check_count += 1;
|
||||
|
||||
// Actualiser les données système
|
||||
monitor.refresh();
|
||||
|
||||
// Vérifier les seuils
|
||||
let violations = monitor.check_thresholds();
|
||||
|
||||
// Envoyer les alertes s'il y en a
|
||||
for violation in violations {
|
||||
let message = format_violation_message(&violation, check_count);
|
||||
|
||||
println!("🚨 Alerte envoyée: {} {:.2}%", violation.metric_name, violation.current_value);
|
||||
notifier.send_notification(&message)?;
|
||||
}
|
||||
|
||||
// Rapport périodique complet
|
||||
if check_count % report_interval == 0 {
|
||||
let metrics = monitor.get_metrics();
|
||||
let report = format_report_message(&metrics, check_count);
|
||||
|
||||
println!("📊 Envoi du rapport périodique (vérification #{})", check_count);
|
||||
notifier.send_notification(&report)?;
|
||||
} else {
|
||||
println!("✅ Vérification #{} - Système OK", check_count);
|
||||
}
|
||||
|
||||
// Attendre avant la prochaine vérification
|
||||
thread::sleep(check_interval);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user