chore: create RuleSchedule as enum
This commit is contained in:
parent
f0e1d3ce6b
commit
50eb1c23bd
107
src/monitor.rs
107
src/monitor.rs
@ -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,54 +185,73 @@ pub fn run_on_selector(rule: &MonitorRule) -> Result<String> {
|
||||
Err(anyhow!("Selector: failed:\n {}",&rule.id))
|
||||
}
|
||||
}
|
||||
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() {
|
||||
out_cmd=run_on_selector(&rule).unwrap_or_else(|e| {
|
||||
eprintln!("Error on selector {}: {}",&rule.id,e);
|
||||
String::from("?")
|
||||
});
|
||||
} else {
|
||||
out_cmd=run_command(&rule.command).unwrap_or_else(|e| {
|
||||
eprintln!("Error on command {}: {}",&rule.command,e);
|
||||
String::from("?")
|
||||
});
|
||||
}
|
||||
if out_cmd.as_str() == "?" {
|
||||
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) {
|
||||
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)
|
||||
);
|
||||
},
|
||||
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)
|
||||
);
|
||||
},
|
||||
RuleContext::Ip(v) => {
|
||||
on_ip_rule(&v, &out_cmd, &rule).await.unwrap_or_else(|e|
|
||||
eprintln!("Error rule {} on {}: {}",&rule.id,&v,e)
|
||||
);
|
||||
},
|
||||
}
|
||||
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);
|
||||
// println!("Run {}: {}",&monitor_rules.name,&monitor_rules.description);
|
||||
for (idx,rule) in monitor_rules.rules.iter().enumerate() {
|
||||
match rule.schedule.as_str() {
|
||||
"inmediate" => {
|
||||
match rule.schedule {
|
||||
RuleSchedule::Check => {
|
||||
// dbg!(&rule);
|
||||
println!("{} [{}] {}: {}",&idx,&rule.id,&rule.name,&rule.description);
|
||||
let out_cmd: String;
|
||||
if rule.command.is_empty() {
|
||||
out_cmd=run_on_selector(&rule).unwrap_or_else(|e| {
|
||||
eprintln!("Error on selector {}: {}",&rule.id,e);
|
||||
String::from("?")
|
||||
});
|
||||
} else {
|
||||
out_cmd=run_command(&rule.command).unwrap_or_else(|e| {
|
||||
eprintln!("Error on command {}: {}",&rule.command,e);
|
||||
String::from("?")
|
||||
});
|
||||
}
|
||||
if out_cmd.as_str() == "?" {
|
||||
continue;
|
||||
}
|
||||
if ! on_monitor_operator(&rule.operator,&out_cmd, &rule.value) {
|
||||
continue;
|
||||
}
|
||||
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);
|
||||
});
|
||||
},
|
||||
RuleContext::None => { continue
|
||||
},
|
||||
RuleContext::Service(v) => {
|
||||
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);
|
||||
});
|
||||
},
|
||||
}
|
||||
run_monitor_rule(idx,rule).await.unwrap_or_else(|e|
|
||||
eprintln!("Error rule {}: {}",&rule.id,e)
|
||||
);
|
||||
},
|
||||
_ => continue,
|
||||
RuleSchedule::OnDemand => continue,
|
||||
RuleSchedule::None => continue,
|
||||
};
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in New Issue
Block a user