From 9631f211dc3d0aacdd4ccb7b68b114e5e380a450 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Hanuszczak?= Date: Wed, 22 Nov 2023 08:23:45 +0100 Subject: [PATCH] Make `get_system_metadata` errors non-critical. --- crates/rrg/src/action/get_system_metadata.rs | 77 ++++++++++++++++---- 1 file changed, 62 insertions(+), 15 deletions(-) diff --git a/crates/rrg/src/action/get_system_metadata.rs b/crates/rrg/src/action/get_system_metadata.rs index 4b7abe1e..f8ec8cdd 100644 --- a/crates/rrg/src/action/get_system_metadata.rs +++ b/crates/rrg/src/action/get_system_metadata.rs @@ -2,34 +2,71 @@ // // Use of this source code is governed by an MIT-style license that can be found // in the LICENSE file or at https://opensource.org/licenses/MIT. +use log::error; /// A result of the the `get_system_metadata` action. struct Item { /// The kind of the operating system the agent is running on. kind: ospect::os::Kind, /// Version string of the operating system the agent is running on. - version: String, + version: Option, /// CPU architecture of the operating system the agent is running on. - arch: String, + arch: Option, /// Hostname of the operating system the agent is running on. - hostname: std::ffi::OsString, + hostname: Option, /// FQDN of the operating system the agent is running on. - fqdn: std::ffi::OsString, + fqdn: Option, /// Estimated time at which the operating system was installed. - installed: std::time::SystemTime, + installed: Option, } impl Item { /// Returns metadata of the operating system the agent is running on. fn new() -> std::io::Result { + let version = match ospect::os::version() { + Ok(version) => Some(version), + Err(error) => { + error!("failed to collect system version: {error}"); + None + } + }; + let arch = match ospect::os::arch() { + Ok(arch) => Some(arch), + Err(error) => { + error!("failed to collect system architecture: {error}"); + None + } + }; + let hostname = match ospect::os::hostname() { + Ok(hostname) => Some(hostname), + Err(error) => { + error!("failed to collect system hostname: {error}"); + None + } + }; + let fqdn = match ospect::os::fqdn() { + Ok(fqdn) => Some(fqdn), + Err(error) => { + error!("failed to collect system FQDN: {error}"); + None + } + }; + let installed = match ospect::os::installed() { + Ok(installed) => Some(installed), + Err(error) => { + error!("failed to collect system installation time: {error}"); + None + } + }; + Ok(Item { kind: ospect::os::kind(), - version: ospect::os::version()?, - arch: ospect::os::arch()?, - hostname: ospect::os::hostname()?, - fqdn: ospect::os::fqdn()?, - installed: ospect::os::installed()?, + version, + arch, + hostname, + fqdn, + installed, }) } } @@ -43,11 +80,21 @@ impl crate::response::Item for Item { let mut proto = rrg_proto::get_system_metadata::Result::new(); proto.set_field_type(self.kind.into()); - proto.set_version(self.version); - proto.set_arch(self.arch); - proto.set_hostname(self.hostname.to_string_lossy().into_owned()); - proto.set_fqdn(self.fqdn.to_string_lossy().into_owned()); - proto.set_install_time(into_timestamp(self.installed)); + if let Some(version) = self.version { + proto.set_version(version); + } + if let Some(arch) = self.arch { + proto.set_arch(arch); + } + if let Some(hostname) = self.hostname { + proto.set_hostname(hostname.to_string_lossy().into_owned()); + } + if let Some(fqdn) = self.fqdn { + proto.set_fqdn(fqdn.to_string_lossy().into_owned()); + } + if let Some(installed) = self.installed { + proto.set_install_time(into_timestamp(installed)); + } proto }