Skip to main content

opcua_server/diagnostics/
server.rs

1use opcua_types::{DataValue, ServerDiagnosticsSummaryDataType, VariableId};
2
3use super::LocalValue;
4
5/// The server diagnostics struct, containing shared
6/// types for various forms of server diagnostics.
7#[derive(Default)]
8pub struct ServerDiagnostics {
9    /// Server diagnostics summary.
10    pub summary: ServerDiagnosticsSummary,
11    /// Whether diagnostics are enabled or not.
12    /// Set on server startup.
13    pub enabled: bool,
14}
15
16impl ServerDiagnostics {
17    /// Check if the given variable ID is managed by this object.
18    pub fn is_mapped(&self, variable_id: VariableId) -> bool {
19        self.enabled && self.summary.is_mapped(variable_id)
20    }
21
22    /// Get the value of a diagnostics element by its ID.
23    pub fn get(&self, variable_id: VariableId) -> Option<DataValue> {
24        self.summary.get(variable_id)
25    }
26
27    /// Set the current session count.
28    pub fn set_current_session_count(&self, count: u32) {
29        if self.enabled {
30            self.summary.current_session_count.set(count);
31        }
32    }
33
34    /// Set the current subscription count.
35    pub fn set_current_subscription_count(&self, count: u32) {
36        if self.enabled {
37            self.summary.current_subscription_count.set(count);
38        }
39    }
40
41    /// Increment the cumulated session count.
42    pub fn inc_session_count(&self) {
43        if self.enabled {
44            self.summary.cumulated_session_count.increment();
45        }
46    }
47
48    /// Increment the cumulated subscription count.
49    pub fn inc_subscription_count(&self) {
50        if self.enabled {
51            self.summary.cumulated_subscription_count.increment();
52        }
53    }
54
55    /// Increment the rejected requests count.
56    pub fn inc_rejected_requests(&self) {
57        if self.enabled {
58            self.summary.rejected_requests_count.increment();
59        }
60    }
61
62    /// Increment the security rejected requests count.
63    pub fn inc_security_rejected_requests(&self) {
64        if self.enabled {
65            self.summary.security_rejected_requests_count.increment();
66        }
67    }
68
69    /// Increment the security rejected session count.
70    pub fn inc_security_rejected_session_count(&self) {
71        if self.enabled {
72            self.summary.security_rejected_session_count.increment();
73        }
74    }
75
76    /// Set the number of server-created views.
77    pub fn set_server_view_count(&self, count: u32) {
78        if self.enabled {
79            self.summary.server_view_count.set(count);
80        }
81    }
82
83    /// Increment the session abort count.
84    pub fn inc_session_abort_count(&self) {
85        if self.enabled {
86            self.summary.session_abort_count.increment();
87        }
88    }
89
90    /// Increment the session timeout count.
91    pub fn inc_session_timeout_count(&self) {
92        if self.enabled {
93            self.summary.session_timeout_count.increment();
94        }
95    }
96
97    /// Set the number of publishing intervals supported by the server.
98    pub fn set_publishing_interval_count(&self, count: u32) {
99        if self.enabled {
100            self.summary.publishing_interval_count.set(count);
101        }
102    }
103}
104
105/// The server diagnostics summary type. Users with approparite
106/// permissions can read these values.
107#[derive(Default)]
108pub struct ServerDiagnosticsSummary {
109    /// The number of sessions that have been created since the server started.
110    cumulated_session_count: LocalValue<u32>,
111    /// The number of subscriptions that have been created since the server started.
112    cumulated_subscription_count: LocalValue<u32>,
113    /// The number of sessions that are currently active.
114    current_session_count: LocalValue<u32>,
115    /// The number of subscriptions that are currently active.
116    current_subscription_count: LocalValue<u32>,
117    /// The number of publishing intervals that have been created since the server started.
118    publishing_interval_count: LocalValue<u32>,
119    /// The number of rejected requests since the server started.
120    rejected_requests_count: LocalValue<u32>,
121    /// The number of rejected sessions since the server started.
122    rejected_session_count: LocalValue<u32>,
123    /// The number of security rejected requests since the server started.
124    security_rejected_requests_count: LocalValue<u32>,
125    /// The number of security rejected sessions since the server started.
126    security_rejected_session_count: LocalValue<u32>,
127    /// The number of server-created views in the server.
128    server_view_count: LocalValue<u32>,
129    /// The number of sessions that were closed due to errors since the server started.
130    session_abort_count: LocalValue<u32>,
131    /// The number of sessions that timed out since the server started.
132    session_timeout_count: LocalValue<u32>,
133}
134
135impl ServerDiagnosticsSummary {
136    /// Check if the given variable ID is managed by this object.
137    pub fn is_mapped(&self, variable_id: VariableId) -> bool {
138        matches!(variable_id,
139            VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_ServerViewCount
140            | VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_CurrentSessionCount
141            | VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_CumulatedSessionCount
142            | VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_SecurityRejectedSessionCount
143            | VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_RejectedSessionCount
144            | VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_SessionTimeoutCount
145            | VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_SessionAbortCount
146            | VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_CurrentSubscriptionCount
147            | VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_CumulatedSubscriptionCount
148            | VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_PublishingIntervalCount
149            | VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_SecurityRejectedRequestsCount
150            | VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_RejectedRequestsCount
151            | VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary
152        )
153    }
154
155    /// Get the value of a variable by its ID.
156    pub fn get(&self, variable_id: VariableId) -> Option<DataValue> {
157        match variable_id {
158            VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_ServerViewCount => Some(self.server_view_count.sample()),
159            VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_CurrentSessionCount => Some(self.current_session_count.sample()),
160            VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_CumulatedSessionCount => Some(self.cumulated_session_count.sample()),
161            VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_SecurityRejectedSessionCount => Some(self.security_rejected_session_count.sample()),
162            VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_RejectedSessionCount => Some(self.rejected_session_count.sample()),
163            VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_SessionTimeoutCount => Some(self.session_timeout_count.sample()),
164            VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_SessionAbortCount => Some(self.session_abort_count.sample()),
165            VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_CurrentSubscriptionCount => Some(self.current_subscription_count.sample()),
166            VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_CumulatedSubscriptionCount => Some(self.cumulated_subscription_count.sample()),
167            VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_PublishingIntervalCount => Some(self.publishing_interval_count.sample()),
168            VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_SecurityRejectedRequestsCount => Some(self.security_rejected_requests_count.sample()),
169            VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary_RejectedRequestsCount => Some(self.rejected_requests_count.sample()),
170            VariableId::Server_ServerDiagnostics_ServerDiagnosticsSummary => Some(self.sample()),
171            _ => None,
172        }
173    }
174
175    /// Get the current value of the server diagnostics summary.
176    pub fn sample(&self) -> DataValue {
177        let values = [
178            self.server_view_count.get_with_time(),
179            self.current_session_count.get_with_time(),
180            self.cumulated_session_count.get_with_time(),
181            self.security_rejected_session_count.get_with_time(),
182            self.rejected_session_count.get_with_time(),
183            self.session_timeout_count.get_with_time(),
184            self.session_abort_count.get_with_time(),
185            self.current_subscription_count.get_with_time(),
186            self.cumulated_subscription_count.get_with_time(),
187            self.publishing_interval_count.get_with_time(),
188            self.security_rejected_requests_count.get_with_time(),
189            self.rejected_requests_count.get_with_time(),
190        ];
191        let ts = values.iter().map(|v| v.1).max().unwrap();
192
193        DataValue::new_at(
194            ServerDiagnosticsSummaryDataType {
195                server_view_count: values[0].0,
196                current_session_count: values[1].0,
197                cumulated_session_count: values[2].0,
198                security_rejected_session_count: values[3].0,
199                rejected_session_count: values[4].0,
200                session_timeout_count: values[5].0,
201                session_abort_count: values[6].0,
202                current_subscription_count: values[7].0,
203                cumulated_subscription_count: values[8].0,
204                publishing_interval_count: values[9].0,
205                security_rejected_requests_count: values[10].0,
206                rejected_requests_count: values[11].0,
207            },
208            ts,
209        )
210    }
211}