Skip to main content

asterust_junos/system/services/
resource_monitor.rs

1//! `resource-monitor`
2//! `system { services { resource-monitor { ... } } }`.
3//!
4//! # See also
5//!
6//! - [resource-monitor](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/resource-monitor-edit-system-services.html)
7//! - [free-fw-memory-watermark](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/free-fw-memory-watermark-edit-system-services-resource-monitor.html)
8//! - [free-heap-memory-watermark](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/free-heap-memory-watermark-edit-system-services-resource-monitor.html)
9//! - [free-nh-memory-watermark](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/free-nh-memory-watermark-edit-system-services-resource-monitor.html)
10//! - [high-cos-queue-threshold](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/high-cos-queue-threshold-edit-system-services-resource-monitor.html)
11//! - [high-threshold](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/high-threshold-edit-system-services-resource-monitor.html)
12//! - [no-logging](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-logging-edit-system-services-resource-monitor.html)
13//! - [no-throttle](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-throttle-edit-system-services-resource-monitor.html)
14//! - [no-load-throttle](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/no-load-throttling-edit-system-services-resource-monitor.html)
15//! - [resource-category](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/resource-category-edit-system-services-resource-monitor.html)
16//! - [subscribers-limit](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/subscribers-limit-edit-system-services-resource-monitor.html)
17
18use crate::protocols::traceoptions::{parse_traceoptions, Traceoptions};
19use crate::{arg_of, child_block, has_leaf, named_children};
20use asterust::config::ConfigLine;
21
22/// A `resource-type` of the `jtree` resource category.
23#[derive(Clone, Debug, PartialEq, Eq)]
24#[non_exhaustive]
25pub struct JtreeResourceType {
26    /// `contiguous-pages`, `free-dwords` or `free-pages`.
27    pub resource_type: String,
28    pub low_watermark: Option<String>,
29    pub high_watermark: Option<String>,
30}
31
32/// A `port <number>` limit.
33#[derive(Clone, Debug, PartialEq, Eq)]
34#[non_exhaustive]
35pub struct PortLimit {
36    pub number: String,
37    pub limit: Option<String>,
38}
39
40/// A `pic <number>` limit.
41#[derive(Clone, Debug, PartialEq, Eq)]
42#[non_exhaustive]
43pub struct PicLimit {
44    pub number: String,
45    pub limit: Option<String>,
46    pub ports: Vec<PortLimit>,
47}
48
49/// An `fpc <slot>` limit.
50#[derive(Clone, Debug, PartialEq, Eq)]
51#[non_exhaustive]
52pub struct FpcLimit {
53    pub slot: String,
54    pub limit: Option<String>,
55    pub pics: Vec<PicLimit>,
56}
57
58/// A `client-type` of `subscribers-limit`.
59#[derive(Clone, Debug, PartialEq, Eq)]
60#[non_exhaustive]
61pub struct SubscriberLimit {
62    /// `any`, `dhcp`, `l2tp` or `pppoe`.
63    pub client_type: String,
64    /// From `chassis { limit }`.
65    pub chassis_limit: Option<String>,
66    pub fpcs: Vec<FpcLimit>,
67}
68
69/// The `system services resource-monitor` settings.
70#[derive(Clone, Debug, PartialEq, Eq)]
71#[non_exhaustive]
72pub struct ResourceMonitor {
73    pub free_fw_memory_watermark: Option<String>,
74    pub free_heap_memory_watermark: Option<String>,
75    pub free_nh_memory_watermark: Option<String>,
76    pub high_cos_queue_threshold: Option<String>,
77    pub high_threshold: Option<String>,
78    pub no_logging: bool,
79    pub no_throttle: bool,
80    pub no_load_throttle: bool,
81    /// The `resource-type` entries of `resource-category jtree`.
82    pub jtree_resource_types: Vec<JtreeResourceType>,
83    pub subscribers_limit: Vec<SubscriberLimit>,
84    pub traceoptions: Option<Traceoptions>,
85}
86
87fn limit_of(block: &ConfigLine) -> Option<String> {
88    arg_of(block, "limit").map(str::to_string)
89}
90
91fn parse_fpc(slot: &str, fpc: &ConfigLine) -> FpcLimit {
92    FpcLimit {
93        slot: slot.to_string(),
94        limit: limit_of(fpc),
95        pics: named_children(fpc, "pic")
96            .map(|(number, pic)| PicLimit {
97                number: number.to_string(),
98                limit: limit_of(pic),
99                ports: named_children(pic, "port")
100                    .map(|(number, port)| PortLimit {
101                        number: number.to_string(),
102                        limit: limit_of(port),
103                    })
104                    .collect(),
105            })
106            .collect(),
107    }
108}
109
110pub(super) fn parse_resource_monitor(monitor: &ConfigLine) -> ResourceMonitor {
111    let field = |keyword: &str| arg_of(monitor, keyword).map(str::to_string);
112    ResourceMonitor {
113        free_fw_memory_watermark: field("free-fw-memory-watermark"),
114        free_heap_memory_watermark: field("free-heap-memory-watermark"),
115        free_nh_memory_watermark: field("free-nh-memory-watermark"),
116        high_cos_queue_threshold: field("high-cos-queue-threshold"),
117        high_threshold: field("high-threshold"),
118        no_logging: has_leaf(monitor, "no-logging"),
119        no_throttle: has_leaf(monitor, "no-throttle"),
120        no_load_throttle: has_leaf(monitor, "no-load-throttle"),
121        jtree_resource_types: named_children(monitor, "resource-category")
122            .filter(|(category, _)| *category == "jtree")
123            .flat_map(|(_, jtree)| named_children(jtree, "resource-type"))
124            .map(|(resource_type, block)| JtreeResourceType {
125                resource_type: resource_type.to_string(),
126                low_watermark: arg_of(block, "low-watermark").map(str::to_string),
127                high_watermark: arg_of(block, "high-watermark").map(str::to_string),
128            })
129            .collect(),
130        subscribers_limit: child_block(monitor, "subscribers-limit")
131            .map(|limits| {
132                named_children(limits, "client-type")
133                    .map(|(client_type, client)| SubscriberLimit {
134                        client_type: client_type.to_string(),
135                        chassis_limit: child_block(client, "chassis").and_then(limit_of),
136                        fpcs: named_children(client, "fpc")
137                            .map(|(slot, fpc)| parse_fpc(slot, fpc))
138                            .collect(),
139                    })
140                    .collect()
141            })
142            .unwrap_or_default(),
143        traceoptions: parse_traceoptions(monitor),
144    }
145}
146
147#[cfg(test)]
148mod tests {
149    use crate::system::system;
150    use asterust::config::juniper;
151
152    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
153    #[test]
154    fn extracts_the_resource_monitor_settings() {
155        let monitor = system(&juniper::parse_braces(
156            r#"
157system {
158    services {
159        resource-monitor {
160            free-fw-memory-watermark 15;
161            free-heap-memory-watermark 20;
162            free-nh-memory-watermark 25;
163            high-cos-queue-threshold 80;
164            high-threshold 90;
165            no-logging;
166            no-throttle;
167            no-load-throttle;
168            resource-category jtree {
169                resource-type free-pages {
170                    low-watermark 10;
171                    high-watermark 30;
172                }
173                resource-type contiguous-pages;
174            }
175            subscribers-limit {
176                client-type pppoe {
177                    chassis {
178                        limit 8000;
179                    }
180                    fpc 1 {
181                        limit 4000;
182                        pic 0 {
183                            limit 2000;
184                            port 3 {
185                                limit 500;
186                            }
187                        }
188                    }
189                }
190            }
191            traceoptions {
192                file rsmon.log;
193                flag all;
194            }
195        }
196    }
197}
198"#,
199        ))
200        .unwrap()
201        .services
202        .unwrap()
203        .resource_monitor
204        .unwrap();
205        assert_eq!(monitor.free_fw_memory_watermark.as_deref(), Some("15"));
206        assert_eq!(monitor.free_heap_memory_watermark.as_deref(), Some("20"));
207        assert_eq!(monitor.free_nh_memory_watermark.as_deref(), Some("25"));
208        assert_eq!(monitor.high_cos_queue_threshold.as_deref(), Some("80"));
209        assert_eq!(monitor.high_threshold.as_deref(), Some("90"));
210        assert!(monitor.no_logging);
211        assert!(monitor.no_throttle);
212        assert!(monitor.no_load_throttle);
213
214        assert_eq!(monitor.jtree_resource_types.len(), 2);
215        assert_eq!(monitor.jtree_resource_types[0].resource_type, "free-pages");
216        assert_eq!(monitor.jtree_resource_types[0].low_watermark.as_deref(), Some("10"));
217        assert_eq!(monitor.jtree_resource_types[0].high_watermark.as_deref(), Some("30"));
218        assert_eq!(monitor.jtree_resource_types[1].low_watermark, None);
219
220        let pppoe = &monitor.subscribers_limit[0];
221        assert_eq!(pppoe.client_type, "pppoe");
222        assert_eq!(pppoe.chassis_limit.as_deref(), Some("8000"));
223        let fpc = &pppoe.fpcs[0];
224        assert_eq!(fpc.slot, "1");
225        assert_eq!(fpc.limit.as_deref(), Some("4000"));
226        assert_eq!(fpc.pics[0].number, "0");
227        assert_eq!(fpc.pics[0].limit.as_deref(), Some("2000"));
228        assert_eq!(fpc.pics[0].ports[0].number, "3");
229        assert_eq!(fpc.pics[0].ports[0].limit.as_deref(), Some("500"));
230        assert_eq!(monitor.traceoptions.unwrap().file.as_deref(), Some("rsmon.log"));
231    }
232
233    #[test]
234    fn bare_resource_monitor_has_no_settings() {
235        let monitor = system(&juniper::parse_braces(
236            "system {\n    services {\n        resource-monitor;\n    }\n}\n",
237        ))
238        .unwrap()
239        .services
240        .unwrap()
241        .resource_monitor
242        .unwrap();
243        assert!(!monitor.no_logging);
244        assert!(monitor.jtree_resource_types.is_empty());
245        assert!(monitor.subscribers_limit.is_empty());
246    }
247
248    #[test]
249    fn no_resource_monitor_is_none() {
250        let services = system(&juniper::parse_braces(
251            "system {\n    services {\n        telnet;\n    }\n}\n",
252        ))
253        .unwrap()
254        .services
255        .unwrap();
256        assert!(services.resource_monitor.is_none());
257    }
258}