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//! - [smg-service](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/smg-service-edit-system-processes.html)
19
20use crate::protocols::traceoptions::{parse_named_traceoptions, parse_traceoptions, Traceoptions};
21use crate::{active_children, arg_of};
22use asterust::config::ConfigLine;
23
24/// A `dhcp-snooping-file` or `dhcpv6-snooping-file` of `dhcp-service`.
25#[derive(Clone, Debug, Default, PartialEq, Eq)]
26#[non_exhaustive]
27pub struct SnoopingFile {
28    /// A local path or a remote URL.
29    pub location: Option<String>,
30    /// Seconds between writes of the database.
31    pub write_interval: Option<String>,
32    pub timeout: Option<String>,
33}
34
35/// A process under `system processes`, e.g. `routing` or `dhcp-service`; `all-processes` appears
36/// here too. The options only some processes have are `None` for the others.
37#[derive(Clone, Debug, PartialEq, Eq)]
38#[non_exhaustive]
39pub struct Process {
40    pub name: String,
41    /// `Some(true)` for `enable`, `Some(false)` for `disable`, `None` if neither is written.
42    pub enabled: Option<bool>,
43    /// `alternate-media` or `other-routing-engine`.
44    pub failover: Option<String>,
45    /// Seconds.
46    pub timeout: Option<String>,
47    pub no_livecore_dump_on_crash: bool,
48    /// Path of the process binary, from `command`.
49    pub command: Option<String>,
50    pub traceoptions: Option<Traceoptions>,
51    /// `force-32-bit`, `force-64-bit` or `auto-64-bit`, on `routing`.
52    pub address_mode: Option<String>,
53    /// `dhcp-service` `interface-traceoptions`.
54    pub interface_traceoptions: Option<Traceoptions>,
55    pub accept_max_tcp_connections: Option<String>,
56    pub request_max_tcp_connections: Option<String>,
57    /// Seconds, on `dhcp-service`.
58    pub ltv_syslog_interval: Option<String>,
59    pub dhcp_snooping_file: Option<SnoopingFile>,
60    pub dhcpv6_snooping_file: Option<SnoopingFile>,
61}
62
63/// `<keyword> <location>;`, or a block with `location`, `write-interval` and `timeout`.
64fn snooping_file(process: &ConfigLine, keyword: &str) -> Option<SnoopingFile> {
65    let statement = active_children(process).find(|c| c.text == keyword || c.arg(keyword).is_some())?;
66    Some(SnoopingFile {
67        location: statement
68            .arg(keyword)
69            .or_else(|| arg_of(statement, "location"))
70            .map(|location| location.trim_matches('"').to_string()),
71        write_interval: arg_of(statement, "write-interval").map(str::to_string),
72        timeout: arg_of(statement, "timeout").map(str::to_string),
73    })
74}
75
76/// Processes written either as `<name> disable;` or as `<name> { disable; failover ...; }`.
77pub(super) fn parse_processes(processes: &ConfigLine) -> Vec<Process> {
78    active_children(processes)
79        .filter_map(|child| {
80            let mut name_and_rest = child.text.split_whitespace();
81            let name = name_and_rest.next()?.to_string();
82            let mut words = name_and_rest.chain(active_children(child).flat_map(|c| c.text.split_whitespace()));
83            let mut process = Process {
84                name,
85                enabled: None,
86                failover: None,
87                timeout: None,
88                no_livecore_dump_on_crash: false,
89                command: None,
90                traceoptions: parse_traceoptions(child),
91                address_mode: None,
92                interface_traceoptions: parse_named_traceoptions(child, "interface-traceoptions"),
93                accept_max_tcp_connections: None,
94                request_max_tcp_connections: None,
95                ltv_syslog_interval: None,
96                dhcp_snooping_file: snooping_file(child, "dhcp-snooping-file"),
97                dhcpv6_snooping_file: snooping_file(child, "dhcpv6-snooping-file"),
98            };
99            while let Some(word) = words.next() {
100                match word {
101                    "enable" => process.enabled = Some(true),
102                    "disable" => process.enabled = Some(false),
103                    "failover" => process.failover = words.next().map(str::to_string),
104                    "timeout" => process.timeout = words.next().map(str::to_string),
105                    "no-livecore-dump-on-crash" => process.no_livecore_dump_on_crash = true,
106                    "command" => process.command = words.next().map(str::to_string),
107                    "force-32-bit" | "force-64-bit" | "auto-64-bit" => process.address_mode = Some(word.to_string()),
108                    "accept-max-tcp-connections" => {
109                        process.accept_max_tcp_connections = words.next().map(str::to_string)
110                    }
111                    "request-max-tcp-connections" => {
112                        process.request_max_tcp_connections = words.next().map(str::to_string)
113                    }
114                    "ltv-syslog-interval" => process.ltv_syslog_interval = words.next().map(str::to_string),
115                    _ => {}
116                }
117            }
118            Some(process)
119        })
120        .collect()
121}
122
123#[cfg(test)]
124mod tests {
125    use crate::system::system;
126    use asterust::config::juniper;
127
128    // Not from documentation: written from the CLI Reference syntax page. See the README "Sources".
129    const PROCESSES: &str = r#"
130system {
131    processes {
132        dhcp-service disable;
133        routing enable failover other-routing-engine;
134        mib-process {
135            enable;
136            timeout 60;
137            traceoptions {
138                file mib.log;
139                flag all;
140            }
141        }
142        inactive: ntp disable;
143        all-processes {
144            no-livecore-dump-on-crash;
145        }
146    }
147}
148"#;
149
150    #[test]
151    fn extracts_processes_in_line_and_block_form() {
152        let processes = system(&juniper::parse_braces(PROCESSES)).unwrap().processes;
153        let names: Vec<_> = processes.iter().map(|p| p.name.as_str()).collect();
154        assert_eq!(names, ["dhcp-service", "routing", "mib-process", "all-processes"]);
155
156        assert_eq!(processes[0].enabled, Some(false));
157        assert_eq!(processes[1].enabled, Some(true));
158        assert_eq!(processes[1].failover.as_deref(), Some("other-routing-engine"));
159        assert_eq!(processes[2].enabled, Some(true));
160        assert_eq!(processes[2].timeout.as_deref(), Some("60"));
161    }
162
163    #[test]
164    fn extracts_process_flags() {
165        let processes = system(&juniper::parse_braces(PROCESSES)).unwrap().processes;
166        assert!(processes[3].no_livecore_dump_on_crash);
167        assert_eq!(processes[3].enabled, None);
168        assert!(!processes[0].no_livecore_dump_on_crash);
169    }
170
171    #[test]
172    fn extracts_process_commands_and_traceoptions() {
173        let processes = system(&juniper::parse_braces(
174            r#"
175system {
176    processes {
177        usb-control {
178            command /usr/sbin/usbd;
179            disable;
180        }
181        process-monitor {
182            traceoptions {
183                file pmond.log files 3 size 1m world-readable;
184                flag all;
185                level verbose;
186                no-remote-trace;
187            }
188        }
189        tcp-forwarding {
190            traceoptions {
191                file tcpfwd {
192                    files 4;
193                    size 2m;
194                    match "error";
195                }
196                flag configuration;
197            }
198        }
199        routing {
200            force-64-bit;
201        }
202    }
203}
204"#,
205        ))
206        .unwrap()
207        .processes;
208        assert_eq!(processes[0].command.as_deref(), Some("/usr/sbin/usbd"));
209        assert_eq!(processes[0].enabled, Some(false));
210
211        let trace = processes[1].traceoptions.as_ref().unwrap();
212        assert_eq!(trace.file.as_deref(), Some("pmond.log"));
213        assert_eq!(trace.files.as_deref(), Some("3"));
214        assert_eq!(trace.size.as_deref(), Some("1m"));
215        assert_eq!(trace.world_readable, Some(true));
216        assert_eq!(trace.level.as_deref(), Some("verbose"));
217        assert!(trace.no_remote_trace);
218        assert_eq!(trace.flags[0].name, "all");
219
220        let block_form = processes[2].traceoptions.as_ref().unwrap();
221        assert_eq!(block_form.file.as_deref(), Some("tcpfwd"));
222        assert_eq!(block_form.files.as_deref(), Some("4"));
223        assert_eq!(block_form.size.as_deref(), Some("2m"));
224        assert_eq!(block_form.match_regex.as_deref(), Some("error"));
225
226        assert_eq!(processes[3].address_mode.as_deref(), Some("force-64-bit"));
227        assert!(processes[0].traceoptions.is_none());
228    }
229
230    #[test]
231    fn extracts_dhcp_service_options() {
232        let processes = system(&juniper::parse_braces(
233            r#"
234system {
235    processes {
236        dhcp-service {
237            accept-max-tcp-connections 8;
238            request-max-tcp-connections 4;
239            ltv-syslog-interval 900;
240            dhcp-snooping-file /var/db/snoop.db {
241                write-interval 600;
242            }
243            dhcpv6-snooping-file {
244                location "ftp://192.0.2.9/snoop6.db";
245                timeout 30;
246                write-interval 120;
247            }
248            interface-traceoptions {
249                file dhcp-if.log;
250                flag all;
251            }
252        }
253    }
254}
255"#,
256        ))
257        .unwrap()
258        .processes;
259        let dhcp = &processes[0];
260        assert_eq!(dhcp.accept_max_tcp_connections.as_deref(), Some("8"));
261        assert_eq!(dhcp.request_max_tcp_connections.as_deref(), Some("4"));
262        assert_eq!(dhcp.ltv_syslog_interval.as_deref(), Some("900"));
263        let v4 = dhcp.dhcp_snooping_file.as_ref().unwrap();
264        assert_eq!(v4.location.as_deref(), Some("/var/db/snoop.db"));
265        assert_eq!(v4.write_interval.as_deref(), Some("600"));
266        assert_eq!(v4.timeout, None);
267        let v6 = dhcp.dhcpv6_snooping_file.as_ref().unwrap();
268        assert_eq!(v6.location.as_deref(), Some("ftp://192.0.2.9/snoop6.db"));
269        assert_eq!(v6.timeout.as_deref(), Some("30"));
270        assert_eq!(v6.write_interval.as_deref(), Some("120"));
271        assert_eq!(
272            dhcp.interface_traceoptions.as_ref().unwrap().file.as_deref(),
273            Some("dhcp-if.log")
274        );
275        // The snooping file's `timeout` is not the process timeout.
276        assert_eq!(dhcp.timeout, None);
277    }
278
279    #[test]
280    fn no_processes_is_empty() {
281        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
282        assert!(system.processes.is_empty());
283    }
284}