diff --git a/agent/crates/public/src/consts.rs b/agent/crates/public/src/consts.rs index db007a61d72..ac5d91cb076 100644 --- a/agent/crates/public/src/consts.rs +++ b/agent/crates/public/src/consts.rs @@ -22,6 +22,8 @@ pub const PROCESS_NAME: &str = "deepflow-agent"; pub const PROCESS_NAME_SECONDARY: &str = "trident"; #[cfg(target_os = "windows")] pub const PROCESS_NAME: &str = "deepflow-agent.exe"; +#[cfg(target_os = "macos")] +pub const PROCESS_NAME: &str = "deepflow-agent"; pub const DAEMONSET_NAME: &str = "deepflow-agent"; pub const CONTAINER_NAME: &str = "deepflow-agent"; @@ -79,6 +81,15 @@ mod platform_consts { pub const COREFILE_FORMAT: &'static str = "dump"; } +#[cfg(target_os = "macos")] +mod platform_consts { + pub const DEFAULT_LOG_FILE: &'static str = "/var/log/deepflow-agent/deepflow-agent.log"; + pub const DEFAULT_CONF_FILE: &'static str = "/etc/deepflow-agent.yaml"; + pub const DEFAULT_TRIDENT_CONF_FILE: &'static str = "/etc/trident.yaml"; + pub const COREFILE_FORMAT: &'static str = "core"; + pub const DEFAULT_COREFILE_PATH: &'static str = "/tmp"; +} + pub use platform_consts::*; pub const FIELD_OFFSET_DA: usize = 0; diff --git a/agent/src/dispatcher/recv_engine/mod.rs b/agent/src/dispatcher/recv_engine/mod.rs index eb216709fd0..a19e6aba99c 100644 --- a/agent/src/dispatcher/recv_engine/mod.rs +++ b/agent/src/dispatcher/recv_engine/mod.rs @@ -157,7 +157,7 @@ impl Default for RecvEngine { } } -#[cfg(target_os = "windows")] +#[cfg(any(target_os = "windows", target_os = "macos"))] impl Default for RecvEngine { fn default() -> Self { Self::Libpcap(None) diff --git a/agent/src/platform/platform_synchronizer/mod.rs b/agent/src/platform/platform_synchronizer/mod.rs index 288f7f29bb1..0bea4278eb4 100644 --- a/agent/src/platform/platform_synchronizer/mod.rs +++ b/agent/src/platform/platform_synchronizer/mod.rs @@ -26,6 +26,8 @@ cfg_if::cfg_if! { pub use linux_process::{ProcessData, ProcessDataOp, get_os_app_tag_by_exec, OsAppTag}; } else if #[cfg(target_os = "windows")] { pub struct ProcessData {} + } else if #[cfg(target_os = "macos")] { + pub struct ProcessData {} } } diff --git a/agent/src/platform/querier.rs b/agent/src/platform/querier.rs index eebf4750b37..b573ab1d643 100644 --- a/agent/src/platform/querier.rs +++ b/agent/src/platform/querier.rs @@ -24,5 +24,8 @@ cfg_if::cfg_if! { } else if #[cfg(target_os = "windows")] { mod windows; pub use windows::Querier; + } else if #[cfg(target_os = "macos")] { + mod macos; + pub use macos::Querier; } } diff --git a/agent/src/platform/querier/macos.rs b/agent/src/platform/querier/macos.rs new file mode 100644 index 00000000000..d5c5ef2ecb4 --- /dev/null +++ b/agent/src/platform/querier/macos.rs @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2024 Yunshan Networks + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use std::hash::Hasher; + +use ahash::AHasher; +use log::{debug, trace}; + +use crate::{ + config::handler::PlatformConfig, + utils::command::{get_hostname, get_ip_address}, +}; + +use public::proto::agent as pb; + +pub struct Querier { + override_os_hostname: Option, + + digest: u64, + + raw_hostname: Option, + raw_ip_addr: String, +} + +impl Querier { + pub fn new(override_os_hostname: Option) -> Self { + Self { + override_os_hostname, + + digest: Default::default(), + + raw_hostname: Default::default(), + raw_ip_addr: Default::default(), + } + } + + pub fn digest(&self) -> u64 { + self.digest + } + + // returns digest + pub fn update(&mut self, _: &PlatformConfig) -> u64 { + let mut hasher = AHasher::default(); + + self.update_raw_hostname(&mut hasher); + self.update_raw_ip_addr(&mut hasher); + + self.digest = hasher.finish(); + self.digest() + } + + pub fn generate_message(&self, config: &PlatformConfig) -> pb::GenesisSyncRequest { + let platform_data = pb::GenesisPlatformData { + platform_enabled: Some(config.enabled), + raw_hostname: self.raw_hostname.clone(), + raw_ip_netns: vec!["default".into()], + raw_ip_addrs: vec![self.raw_ip_addr.clone()], + ..Default::default() + }; + + pb::GenesisSyncRequest { + platform_data: Some(platform_data), + ..Default::default() + } + } + + fn update_raw_hostname(&mut self, hasher: &mut AHasher) { + if let Some(hostname) = self.override_os_hostname.as_ref() { + if self.raw_hostname.is_none() { + self.raw_hostname = Some(hostname.clone()); + } + return; + } + match get_hostname() { + Ok(hostname) => { + debug!("get_hostname() = {}", hostname); + hasher.write(hostname.as_bytes()); + trace!("digest={:016x}", hasher.finish()); + self.raw_hostname = Some(hostname); + } + Err(e) => debug!("get_hostname failed: {}", e), + } + } + + fn update_raw_ip_addr(&mut self, hasher: &mut AHasher) { + let raw_host_ip_addr = get_ip_address() + .map_err(|err| debug!("get_ip_address error:{}", err)) + .ok(); + if let Some(ip_addr) = raw_host_ip_addr.as_ref() { + for line in ip_addr.lines() { + // 忽略可能变化的行避免version频繁更新 + if line.contains("valid_lft") { + continue; + } + hasher.write(line.as_bytes()); + } + } + self.raw_ip_addr = raw_host_ip_addr.unwrap_or_default(); + debug!("updated ip addresses"); + trace!("digest={:016x}", hasher.finish()); + } +}