Skip to main content

icinga2_api/api/action/
process_check_result.rs

1//! API Action process-check-result
2//!
3//! [Official Documentation](https://icinga.com/docs/icinga-2/latest/doc/12-icinga2-api/#process-check-result)
4
5use serde::{Deserialize, Serialize};
6
7use crate::serde::{
8    deserialize_optional_icinga_timestamp, deserialize_optional_seconds_as_duration,
9    serialize_optional_duration_as_seconds, serialize_optional_icinga_timestamp,
10};
11use crate::types::action::StatusResponse;
12use crate::types::common::{command::IcingaCommandLine, performance_data::IcingaPerformanceData};
13use crate::types::enums::object_type::IcingaObjectType;
14use crate::types::filter::IcingaFilter;
15use crate::types::query::ResultsWrapper;
16use crate::types::rest::{RestApiEndpoint, RestApiResponse};
17
18/// REST API Endpoint for the process-check-result call
19#[derive(Debug, Clone, derive_builder::Builder, Serialize, Deserialize)]
20#[builder(
21    build_fn(error = "crate::error::Error", validate = "Self::validate"),
22    derive(Debug)
23)]
24pub struct ProcessCheckResult {
25    ///  For services: 0=OK, 1=WARNING, 2=CRITICAL, 3=UNKNOWN
26    ///  For hosts: 0=UP, 1=DOWN
27    pub exit_status: u64,
28    /// the plugin output without the performance data
29    pub plugin_output: String,
30    /// the performance data
31    #[builder(default)]
32    #[serde(default, skip_serializing_if = "Option::is_none")]
33    pub performance_data: Option<Vec<IcingaPerformanceData>>,
34    /// the check command
35    #[builder(default)]
36    #[serde(default, skip_serializing_if = "Option::is_none")]
37    pub check_command: Option<IcingaCommandLine>,
38    /// usually the name of the command endpoint
39    #[builder(default)]
40    #[serde(default, skip_serializing_if = "Option::is_none")]
41    pub check_source: Option<String>,
42    /// the start time of the check command execution
43    #[serde(
44        serialize_with = "serialize_optional_icinga_timestamp",
45        deserialize_with = "deserialize_optional_icinga_timestamp"
46    )]
47    #[serde(default, skip_serializing_if = "Option::is_none")]
48    pub execution_start: Option<time::OffsetDateTime>,
49    /// the end time of the check command execution
50    #[serde(
51        serialize_with = "serialize_optional_icinga_timestamp",
52        deserialize_with = "deserialize_optional_icinga_timestamp"
53    )]
54    #[serde(default, skip_serializing_if = "Option::is_none")]
55    pub execution_end: Option<time::OffsetDateTime>,
56    /// Time-to-live duration in seconds for this check result. The next expected check result is now + ttl where freshness checks are executed.
57    #[serde(
58        serialize_with = "serialize_optional_duration_as_seconds",
59        deserialize_with = "deserialize_optional_seconds_as_duration"
60    )]
61    #[serde(default, skip_serializing_if = "Option::is_none")]
62    pub ttl: Option<time::Duration>,
63    /// filter to target which host and/or service this check result applies to
64    #[builder(default, setter(strip_option, into))]
65    #[serde(flatten)]
66    filter: Option<IcingaFilter>,
67}
68
69impl ProcessCheckResult {
70    /// create a new builder for this endpoint
71    ///
72    /// this is usually the first step to calling this REST API endpoint
73    #[must_use]
74    pub fn builder() -> ProcessCheckResultBuilder {
75        ProcessCheckResultBuilder::default()
76    }
77}
78
79impl ProcessCheckResultBuilder {
80    /// makes sure the filter object type is valid for this call (either Host or Service)
81    ///
82    /// # Errors
83    ///
84    /// this returns an error if the filter field object type is not Host or Service
85    pub fn validate(&self) -> Result<(), crate::error::Error> {
86        if let Some(Some(filter)) = &self.filter {
87            if filter.object_type != IcingaObjectType::Host
88                && filter.object_type != IcingaObjectType::Service
89            {
90                Err(crate::error::Error::FilterObjectTypeMismatch(
91                    vec![IcingaObjectType::Host, IcingaObjectType::Service],
92                    filter.object_type.to_owned(),
93                ))
94            } else {
95                Ok(())
96            }
97        } else {
98            Ok(())
99        }
100    }
101}
102
103impl RestApiEndpoint for ProcessCheckResult {
104    type RequestBody = Self;
105
106    fn method(&self) -> Result<reqwest::Method, crate::error::Error> {
107        Ok(reqwest::Method::POST)
108    }
109
110    fn url(&self, base_url: &url::Url) -> Result<url::Url, crate::error::Error> {
111        base_url
112            .join("v1/actions/process-check-result")
113            .map_err(crate::error::Error::CouldNotParseUrlFragment)
114    }
115
116    fn request_body(
117        &self,
118    ) -> Result<Option<std::borrow::Cow<'_, Self::RequestBody>>, crate::error::Error>
119    where
120        Self::RequestBody: Clone + serde::Serialize + std::fmt::Debug,
121    {
122        Ok(Some(std::borrow::Cow::Borrowed(self)))
123    }
124}
125
126impl RestApiResponse<ProcessCheckResult> for ResultsWrapper<StatusResponse> {}