asterust_junos/system/
health_monitor.rs1use crate::statements;
9use asterust::config::ConfigLine;
10
11#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct HealthMonitor {
15 pub ifstate_clients: Option<String>,
17 pub threshold_level: Option<String>,
19 pub action: Option<String>,
21}
22
23pub(super) fn parse_health_monitor(monitor: &ConfigLine) -> HealthMonitor {
24 let first = |keyword: &str| statements(monitor, keyword).first().map(|s| s.to_string());
25 HealthMonitor {
26 ifstate_clients: first("ifstate-clients"),
27 threshold_level: first("threshold-level"),
28 action: first("action"),
29 }
30}
31
32#[cfg(test)]
33mod tests {
34 use crate::system::system;
35 use asterust::config::juniper;
36
37 #[test]
39 fn extracts_the_health_monitor() {
40 let monitor = system(&juniper::parse_braces(
41 "system {\n health-monitor {\n ifstate-clients {\n peer-stuck;\n }\n threshold-level {\n medium;\n }\n action {\n alarm-with-cores;\n }\n }\n}\n",
42 ))
43 .unwrap()
44 .health_monitor
45 .unwrap();
46 assert_eq!(monitor.ifstate_clients.as_deref(), Some("peer-stuck"));
47 assert_eq!(monitor.threshold_level.as_deref(), Some("medium"));
48 assert_eq!(monitor.action.as_deref(), Some("alarm-with-cores"));
49 }
50
51 #[test]
52 fn health_monitor_accepts_the_inline_form() {
53 let monitor = system(&juniper::parse_braces(
54 "system {\n health-monitor {\n action restart;\n }\n}\n",
55 ))
56 .unwrap()
57 .health_monitor
58 .unwrap();
59 assert_eq!(monitor.action.as_deref(), Some("restart"));
60 assert_eq!(monitor.ifstate_clients, None);
61 }
62
63 #[test]
64 fn no_health_monitor_is_none() {
65 let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
66 assert!(system.health_monitor.is_none());
67 }
68}