Skip to main content

asterust_junos/system/
processes.rs

1//! `processes`
2//! `system { processes { ... } }`.
3//!
4//! # See also
5//!
6//! - [processes](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/processes-edit-system.html)
7//! - [traceoptions (DHCP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/traceoptions-edit-system-processes-dhcp-service.html)
8//! - [interface-traceoptions (DHCP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-traceoptions-edit-system-processes-dhcp-service.html)
9//! - [dhcp-snooping-file](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dhcp-snooping-file-edit-system-processess-dhcp-service.html)
10//! - [dhcpv6-snooping-file](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dhcpv6-snooping-file-edit-system-processess-dhcp-service.html)
11//! - [accept-max-tcp-connections](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/accept-max-tcp-connections-edit-system-process.html)
12//! - [request-max-tcp-connections](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/request-max-tcp-connections-edit-system-process.html)
13//! - [ltv-syslog-interval](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/ltv-syslog-interval-edit-system-process.html)
14//! - [routing (System Processes)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/routing-edit-system-processes.html)
15//! - [watchdog](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/watchdog-edit-system-processes.html)
16//! - [process-monitor](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/process-monitor-edit-system.html)
17//! - [resource-cleanup](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/resource-cleanup-edit-system.html)
18//! - [log (DHCP service)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dhcp-service-log-edit-systems.html)
19//! - [pic-services-logging](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pic-services-logging-system-processes.html)
20//! - [rib-sharding](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rib-sharding-edit-protocols-bgp.html)
21//! - [update-threading](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/update-threading.html)
22//! - [smg-service](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/smg-service-edit-system-processes.html)
23
24use crate::protocols::traceoptions::{parse_named_traceoptions, parse_traceoptions, Traceoptions};
25use crate::{active_children, arg_of, child_block, has_leaf};
26use asterust::config::ConfigLine;
27
28/// A `dhcp-snooping-file` or `dhcpv6-snooping-file` of `dhcp-service`.
29#[derive(Clone, Debug, Default, PartialEq, Eq)]
30#[non_exhaustive]
31pub struct SnoopingFile {
32    /// A local path or a remote URL.
33    pub location: Option<String>,
34    /// Seconds between writes of the database.
35    pub write_interval: Option<String>,
36    pub timeout: Option<String>,
37}
38
39/// The `log session` block of `dhcp-service`: which DHCP session events are logged.
40#[derive(Clone, Debug, Default, PartialEq, Eq)]
41#[non_exhaustive]
42pub struct DhcpSessionLog {
43    pub client: bool,
44    pub server: bool,
45    pub relay: bool,
46    pub all: bool,
47    pub dhcpv6_client: bool,
48    pub dhcpv6_server: bool,
49    pub dhcpv6_relay: bool,
50    pub dhcpv6_dynamic_server: bool,
51    pub dhcpv6_all: bool,
52}
53
54/// A process under `system processes`, e.g. `routing` or `dhcp-service`; `all-processes` appears
55/// here too. The options only some processes have are `None` for the others.
56#[derive(Clone, Debug, PartialEq, Eq)]
57#[non_exhaustive]
58pub struct Process {
59    pub name: String,
60    /// `Some(true)` for `enable`, `Some(false)` for `disable`, `None` if neither is written.
61    pub enabled: Option<bool>,
62    /// `alternate-media` or `other-routing-engine`.
63    pub failover: Option<String>,
64    /// Seconds.
65    pub timeout: Option<String>,
66    pub no_livecore_dump_on_crash: bool,
67    /// Path of the process binary, from `command`.
68    pub command: Option<String>,
69    pub traceoptions: Option<Traceoptions>,
70    /// `force-32-bit`, `force-64-bit` or `auto-64-bit`, on `routing`.
71    pub address_mode: Option<String>,
72    /// `dhcp-service` `interface-traceoptions`.
73    pub interface_traceoptions: Option<Traceoptions>,
74    pub accept_max_tcp_connections: Option<String>,
75    pub request_max_tcp_connections: Option<String>,
76    /// Seconds, on `dhcp-service`.
77    pub ltv_syslog_interval: Option<String>,
78    pub dhcp_snooping_file: Option<SnoopingFile>,
79    pub dhcpv6_snooping_file: Option<SnoopingFile>,
80    /// `dhcp-service` `log session`.
81    pub dhcp_session_log: Option<DhcpSessionLog>,
82    /// `routing` `bgp { rib-sharding { number-of-shards } }`.
83    pub bgp_rib_sharding_shards: Option<String>,
84    /// `routing` `bgp { update-threading { number-of-threads } }`.
85    pub bgp_update_threading_threads: Option<String>,
86    /// `routing` `bgp { update-threading { group-split-size } }`.
87    pub bgp_update_threading_group_split_size: Option<String>,
88}
89
90/// `<keyword> <location>;`, or a block with `location`, `write-interval` and `timeout`.
91fn snooping_file(process: &ConfigLine, keyword: &str) -> Option<SnoopingFile> {
92    let statement = active_children(process).find(|c| c.text == keyword || c.arg(keyword).is_some())?;
93    Some(SnoopingFile {
94        location: statement
95            .arg(keyword)
96            .or_else(|| arg_of(statement, "location"))
97            .map(|location| location.trim_matches('"').to_string()),
98        write_interval: arg_of(statement, "write-interval").map(str::to_string),
99        timeout: arg_of(statement, "timeout").map(str::to_string),
100    })
101}
102
103/// The `log { session { ... } }` block of `dhcp-service`.
104fn session_log(process: &ConfigLine) -> Option<DhcpSessionLog> {
105    let session = child_block(child_block(process, "log")?, "session")?;
106    let v6 = child_block(session, "dhcpv6");
107    let v6_flag = |name: &str| v6.is_some_and(|v6| has_leaf(v6, name));
108    Some(DhcpSessionLog {
109        client: has_leaf(session, "client"),
110        server: has_leaf(session, "server"),
111        relay: has_leaf(session, "relay"),
112        all: has_leaf(session, "all"),
113        dhcpv6_client: v6_flag("client"),
114        dhcpv6_server: v6_flag("server"),
115        dhcpv6_relay: v6_flag("relay"),
116        dhcpv6_dynamic_server: v6_flag("dynamic-server"),
117        dhcpv6_all: v6_flag("all"),
118    })
119}
120
121/// Processes written either as `<name> disable;` or as `<name> { disable; failover ...; }`.
122pub(super) fn parse_processes(processes: &ConfigLine) -> Vec<Process> {
123    active_children(processes)
124        .filter_map(|child| {
125            let mut name_and_rest = child.text.split_whitespace();
126            let name = name_and_rest.next()?.to_string();
127            let mut words = name_and_rest.chain(active_children(child).flat_map(|c| c.text.split_whitespace()));
128            let bgp = child_block(child, "bgp");
129            let threading = bgp.and_then(|bgp| child_block(bgp, "update-threading"));
130            let mut process = Process {
131                name,
132                enabled: None,
133                failover: None,
134                timeout: None,
135                no_livecore_dump_on_crash: false,
136                command: None,
137                traceoptions: parse_traceoptions(child),
138                address_mode: None,
139                interface_traceoptions: parse_named_traceoptions(child, "interface-traceoptions"),
140                accept_max_tcp_connections: None,
141                request_max_tcp_connections: None,
142                ltv_syslog_interval: None,
143                dhcp_snooping_file: snooping_file(child, "dhcp-snooping-file"),
144                dhcpv6_snooping_file: snooping_file(child, "dhcpv6-snooping-file"),
145                dhcp_session_log: session_log(child),
146                bgp_rib_sharding_shards: bgp
147                    .and_then(|bgp| child_block(bgp, "rib-sharding"))
148                    .and_then(|sharding| arg_of(sharding, "number-of-shards"))
149                    .map(str::to_string),
150                bgp_update_threading_threads: threading
151                    .and_then(|t| arg_of(t, "number-of-threads"))
152                    .map(str::to_string),
153                bgp_update_threading_group_split_size: threading
154                    .and_then(|t| arg_of(t, "group-split-size"))
155                    .map(str::to_string),
156            };
157            while let Some(word) = words.next() {
158                match word {
159                    "enable" => process.enabled = Some(true),
160                    "disable" => process.enabled = Some(false),
161                    "failover" => process.failover = words.next().map(str::to_string),
162                    "timeout" => process.timeout = words.next().map(str::to_string),
163                    "no-livecore-dump-on-crash" => process.no_livecore_dump_on_crash = true,
164                    "command" => process.command = words.next().map(str::to_string),
165                    "force-32-bit" | "force-64-bit" | "auto-64-bit" => process.address_mode = Some(word.to_string()),
166                    "accept-max-tcp-connections" => {
167                        process.accept_max_tcp_connections = words.next().map(str::to_string)
168                    }
169                    "request-max-tcp-connections" => {
170                        process.request_max_tcp_connections = words.next().map(str::to_string)
171                    }
172                    "ltv-syslog-interval" => process.ltv_syslog_interval = words.next().map(str::to_string),
173                    _ => {}
174                }
175            }
176            Some(process)
177        })
178        .collect()
179}
180
181#[cfg(test)]
182mod tests {
183    use crate::system::system;
184    use asterust::config::juniper;
185
186    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
187    const PROCESSES: &str = r#"
188system {
189    processes {
190        dhcp-service disable;
191        routing enable failover other-routing-engine;
192        mib-process {
193            enable;
194            timeout 60;
195            traceoptions {
196                file mib.log;
197                flag all;
198            }
199        }
200        inactive: ntp disable;
201        all-processes {
202            no-livecore-dump-on-crash;
203        }
204    }
205}
206"#;
207
208    #[test]
209    fn extracts_processes_in_line_and_block_form() {
210        let processes = system(&juniper::parse_braces(PROCESSES)).unwrap().processes;
211        let names: Vec<_> = processes.iter().map(|p| p.name.as_str()).collect();
212        assert_eq!(names, ["dhcp-service", "routing", "mib-process", "all-processes"]);
213
214        assert_eq!(processes[0].enabled, Some(false));
215        assert_eq!(processes[1].enabled, Some(true));
216        assert_eq!(processes[1].failover.as_deref(), Some("other-routing-engine"));
217        assert_eq!(processes[2].enabled, Some(true));
218        assert_eq!(processes[2].timeout.as_deref(), Some("60"));
219    }
220
221    #[test]
222    fn extracts_process_flags() {
223        let processes = system(&juniper::parse_braces(PROCESSES)).unwrap().processes;
224        assert!(processes[3].no_livecore_dump_on_crash);
225        assert_eq!(processes[3].enabled, None);
226        assert!(!processes[0].no_livecore_dump_on_crash);
227    }
228
229    #[test]
230    fn extracts_process_commands_and_traceoptions() {
231        let processes = system(&juniper::parse_braces(
232            r#"
233system {
234    processes {
235        usb-control {
236            command /usr/sbin/usbd;
237            disable;
238        }
239        process-monitor {
240            traceoptions {
241                file pmond.log files 3 size 1m world-readable;
242                flag all;
243                level verbose;
244                no-remote-trace;
245            }
246        }
247        tcp-forwarding {
248            traceoptions {
249                file tcpfwd {
250                    files 4;
251                    size 2m;
252                    match "error";
253                }
254                flag configuration;
255            }
256        }
257        routing {
258            force-64-bit;
259        }
260    }
261}
262"#,
263        ))
264        .unwrap()
265        .processes;
266        assert_eq!(processes[0].command.as_deref(), Some("/usr/sbin/usbd"));
267        assert_eq!(processes[0].enabled, Some(false));
268
269        let trace = processes[1].traceoptions.as_ref().unwrap();
270        assert_eq!(trace.file.as_deref(), Some("pmond.log"));
271        assert_eq!(trace.files.as_deref(), Some("3"));
272        assert_eq!(trace.size.as_deref(), Some("1m"));
273        assert_eq!(trace.world_readable, Some(true));
274        assert_eq!(trace.level.as_deref(), Some("verbose"));
275        assert!(trace.no_remote_trace);
276        assert_eq!(trace.flags[0].name, "all");
277
278        let block_form = processes[2].traceoptions.as_ref().unwrap();
279        assert_eq!(block_form.file.as_deref(), Some("tcpfwd"));
280        assert_eq!(block_form.files.as_deref(), Some("4"));
281        assert_eq!(block_form.size.as_deref(), Some("2m"));
282        assert_eq!(block_form.match_regex.as_deref(), Some("error"));
283
284        assert_eq!(processes[3].address_mode.as_deref(), Some("force-64-bit"));
285        assert!(processes[0].traceoptions.is_none());
286    }
287
288    #[test]
289    fn extracts_dhcp_service_options() {
290        let processes = system(&juniper::parse_braces(
291            r#"
292system {
293    processes {
294        dhcp-service {
295            accept-max-tcp-connections 8;
296            request-max-tcp-connections 4;
297            ltv-syslog-interval 900;
298            dhcp-snooping-file /var/db/snoop.db {
299                write-interval 600;
300            }
301            dhcpv6-snooping-file {
302                location "ftp://192.0.2.9/snoop6.db";
303                timeout 30;
304                write-interval 120;
305            }
306            interface-traceoptions {
307                file dhcp-if.log;
308                flag all;
309            }
310        }
311    }
312}
313"#,
314        ))
315        .unwrap()
316        .processes;
317        let dhcp = &processes[0];
318        assert_eq!(dhcp.accept_max_tcp_connections.as_deref(), Some("8"));
319        assert_eq!(dhcp.request_max_tcp_connections.as_deref(), Some("4"));
320        assert_eq!(dhcp.ltv_syslog_interval.as_deref(), Some("900"));
321        let v4 = dhcp.dhcp_snooping_file.as_ref().unwrap();
322        assert_eq!(v4.location.as_deref(), Some("/var/db/snoop.db"));
323        assert_eq!(v4.write_interval.as_deref(), Some("600"));
324        assert_eq!(v4.timeout, None);
325        let v6 = dhcp.dhcpv6_snooping_file.as_ref().unwrap();
326        assert_eq!(v6.location.as_deref(), Some("ftp://192.0.2.9/snoop6.db"));
327        assert_eq!(v6.timeout.as_deref(), Some("30"));
328        assert_eq!(v6.write_interval.as_deref(), Some("120"));
329        assert_eq!(
330            dhcp.interface_traceoptions.as_ref().unwrap().file.as_deref(),
331            Some("dhcp-if.log")
332        );
333        // The snooping file's `timeout` is not the process timeout.
334        assert_eq!(dhcp.timeout, None);
335    }
336
337    #[test]
338    fn extracts_the_dhcp_session_log() {
339        let processes = system(&juniper::parse_braces(
340            "system {\n    processes {\n        dhcp-service {\n            log {\n                session {\n                    client;\n                    dhcpv6 {\n                        relay;\n                        dynamic-server;\n                    }\n                }\n            }\n        }\n        pic-services-logging disable;\n    }\n}\n",
341        ))
342        .unwrap()
343        .processes;
344        let log = processes[0].dhcp_session_log.as_ref().unwrap();
345        assert!(log.client);
346        assert!(!log.server);
347        assert!(log.dhcpv6_relay);
348        assert!(log.dhcpv6_dynamic_server);
349        assert!(!log.dhcpv6_client);
350        assert_eq!(processes[1].name, "pic-services-logging");
351        assert!(processes[1].dhcp_session_log.is_none());
352    }
353
354    #[test]
355    fn extracts_the_routing_process_bgp_threading() {
356        let routing = system(&juniper::parse_braces(
357            "system {\n    processes {\n        routing {\n            bgp {\n                rib-sharding {\n                    number-of-shards 4;\n                }\n                update-threading {\n                    number-of-threads 8;\n                    group-split-size 50;\n                }\n            }\n        }\n    }\n}\n",
358        ))
359        .unwrap()
360        .processes
361        .remove(0);
362        assert_eq!(routing.bgp_rib_sharding_shards.as_deref(), Some("4"));
363        assert_eq!(routing.bgp_update_threading_threads.as_deref(), Some("8"));
364        assert_eq!(routing.bgp_update_threading_group_split_size.as_deref(), Some("50"));
365    }
366
367    #[test]
368    fn no_processes_is_empty() {
369        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
370        assert!(system.processes.is_empty());
371    }
372}