chore: create RuleSchedule as enum

This commit is contained in:
Jesús Pérez Lorenzo 2021-09-14 11:53:58 +01:00
parent f0e1d3ce6b
commit 50eb1c23bd

View File

@ -21,6 +21,7 @@ use crate::clouds::monitor_rules::{
MonitorRules,
MonitorRule,
RuleContext,
RuleSchedule,
RuleOperator,
MonitorAction,
};
@ -99,6 +100,7 @@ pub async fn on_service_rule(srvc: &str, out_cmd: &str, rule: &MonitorRule) -> R
// println!("target: {}",&target);
for action in &rule.actions {
on_monitor_action(action, &target, &rule).await?;
}
};
}
@ -183,12 +185,7 @@ pub fn run_on_selector(rule: &MonitorRule) -> Result<String> {
Err(anyhow!("Selector: failed:\n {}",&rule.id))
}
}
pub async fn run_monitor(monitor_rules: MonitorRules, _cloud: Cloud, _app_env: AppEnv) -> Result<()> {
println!("Run {}: {}",&monitor_rules.name,&monitor_rules.description);
for (idx,rule) in monitor_rules.rules.iter().enumerate() {
match rule.schedule.as_str() {
"inmediate" => {
// dbg!(&rule);
pub async fn run_monitor_rule(idx: usize, rule: &MonitorRule ) -> Result<()> {
println!("{} [{}] {}: {}",&idx,&rule.id,&rule.name,&rule.description);
let out_cmd: String;
if rule.command.is_empty() {
@ -203,34 +200,58 @@ pub async fn run_monitor(monitor_rules: MonitorRules, _cloud: Cloud, _app_env: A
});
}
if out_cmd.as_str() == "?" {
continue;
return Ok(());
}
let env_name = format!("MONITOR_{}",&rule.id);
let env_state = envmnt::get_isize(&env_name,0);
if ! on_monitor_operator(&rule.operator,&out_cmd, &rule.value) {
continue;
if env_state > 0 {
println!("{} Ok in state: {}",&env_name,&env_state);
envmnt::set_isize(&env_name, 0);
}
return Ok(());
}
if env_state > 0 && env_state < rule.wait_checks {
println!("{} is already set: {}",&env_name,&env_state);
envmnt::increment(&env_name);
return Ok(());
}
match &rule.context {
RuleContext::Server(v) => {
on_server_rule(&v, &out_cmd, &rule).await.unwrap_or_else(|e|{
eprintln!("Error rule {} on {}: {}",&rule.id,&v,e);
});
on_server_rule(&v, &out_cmd, &rule).await.unwrap_or_else(|e|
eprintln!("Error rule {} on {}: {}",&rule.id,&v,e)
);
},
RuleContext::None => { continue
RuleContext::None => {
return Ok(());
},
RuleContext::Service(v) => {
on_service_rule(&v, &out_cmd, &rule).await.unwrap_or_else(|e|{
eprintln!("Error rule {} on {}: {}",&rule.id,&v,e);
});
on_service_rule(&v, &out_cmd, &rule).await.unwrap_or_else(|e|
eprintln!("Error rule {} on {}: {}",&rule.id,&v,e)
);
},
RuleContext::Ip(v) => {
on_ip_rule(&v, &out_cmd, &rule).await.unwrap_or_else(|e|{
eprintln!("Error rule {} on {}: {}",&rule.id,&v,e);
});
on_ip_rule(&v, &out_cmd, &rule).await.unwrap_or_else(|e|
eprintln!("Error rule {} on {}: {}",&rule.id,&v,e)
);
},
}
},
_ => continue,
};
}
envmnt::set_isize(&env_name,1);
Ok(())
}
pub async fn run_monitor(monitor_rules: MonitorRules, _cloud: Cloud, _app_env: AppEnv) -> Result<()> {
// println!("Run {}: {}",&monitor_rules.name,&monitor_rules.description);
for (idx,rule) in monitor_rules.rules.iter().enumerate() {
match rule.schedule {
RuleSchedule::Check => {
// dbg!(&rule);
run_monitor_rule(idx,rule).await.unwrap_or_else(|e|
eprintln!("Error rule {}: {}",&rule.id,e)
);
},
RuleSchedule::OnDemand => continue,
RuleSchedule::None => continue,
};
}
Ok(())
}