master to dev #9

Merged
VladislavD merged 13 commits from master into dev 2024-11-19 12:10:33 +03:00
10 changed files with 724 additions and 44 deletions

View File

@ -11,7 +11,19 @@ use tokio::time::Duration;
const CONFIG_PATH: &str = "settings.json"; const CONFIG_PATH: &str = "settings.json";
// 4ever sync /// # Fn `load_processes`
/// ## for reading and parsing *local* storing config
///
/// *input* : `&str`
///
/// *output* : `None` if local conf file doesn't exist or invalid | `Some(conf)` on finish reading and parsing
///
/// *initiator* : func `get_actual_config`
///
/// *managing* : conf file name in `&str` format
///
/// *depends on* : struct `Processes`
///
fn load_processes(json_filename: &str) -> Option<Processes> { fn load_processes(json_filename: &str) -> Option<Processes> {
if let Ok(res) = fs::read_to_string(json_filename) { if let Ok(res) = fs::read_to_string(json_filename) {
if let Ok(conf) = serde_json::from_str::<Processes>(&res) { if let Ok(conf) = serde_json::from_str::<Processes>(&res) {
@ -21,6 +33,19 @@ fn load_processes(json_filename: &str) -> Option<Processes> {
None None
} }
/// # Fn `get_actual_config`
/// ## for getting actual Monitor's config from local and remote storages
///
/// *input* : -
///
/// *output* : `None` on fatal error in mechanisms | `Some(conf)` on finish reading and parsing
///
/// *initiator* : main thread
///
/// *managing* : -
///
/// *depends on* : struct `Processes`
///
pub async fn get_actual_config() -> Option<Processes> { pub async fn get_actual_config() -> Option<Processes> {
// * if no local conf -> loop and +inf getting conf from redis server // * if no local conf -> loop and +inf getting conf from redis server
// * if local conf -> once getting conf from redis server // * if local conf -> once getting conf from redis server
@ -64,6 +89,19 @@ pub async fn get_actual_config() -> Option<Processes> {
} }
} }
/// # Fn `get_remote_conf_watcher`
/// ## for infinitive pulling remote config
///
/// *input* : `&mut Connection`
///
/// *output* : `None` on fatal error | `Some(conf)` on succesfull pulling
///
/// *initiator* : fn `get_actual_config`
///
/// *managing* : mut ref `Connection` object
///
/// *depends on* : struct `Processes`
///
async fn get_remote_conf_watcher(conn : &mut Connection) -> Option<Processes> { async fn get_remote_conf_watcher(conn : &mut Connection) -> Option<Processes> {
let mut conn = conn.as_pubsub(); let mut conn = conn.as_pubsub();
let cont = crate::utils::get_container_id(); let cont = crate::utils::get_container_id();
@ -105,8 +143,22 @@ async fn get_remote_conf_watcher(conn : &mut Connection) -> Option<Processes> {
} }
None None
} }
// ! once iter exec
// ! only for situation when local isn't None (no need to fck redis server) /// # Fn `get_remote_conf_watcher`
/// ## for trying to pull remote config
///
/// > only for situation when local isn't None (no need to fck redis server)
///
/// *input* : `&str`
///
/// *output* : `None` on empty pubsub or error | `Some(conf)` on succesfull pulling
///
/// *initiator* : fn `get_actual_config`
///
/// *managing* : &str of Redis Server credentials
///
/// *depends on* : struct `Processes`
///
fn once_get_remote_configuration(serv_info: &str) -> Option<Processes> { fn once_get_remote_configuration(serv_info: &str) -> Option<Processes> {
let cont = crate::utils::get_container_id(); let cont = crate::utils::get_container_id();
match Client::open(serv_info) { match Client::open(serv_info) {
@ -165,6 +217,21 @@ fn once_get_remote_configuration(serv_info: &str) -> Option<Processes> {
// ! watchers // ! watchers
/// # Fn `open_watcher`
/// ## for infinitive opening Redis client
///
/// > only for situation when local isn't None (no need to fck redis server)
///
/// *input* : `Option<Processes>`
///
/// *output* : redis::Client on successful opening client
///
/// *initiator* : fn `get_actual_config`
///
/// *managing* : &str of Redis Server credentials
///
/// *depends on* : struct `redis::Client`
///
fn open_watcher(serv_info: &str) -> Client { fn open_watcher(serv_info: &str) -> Client {
loop { loop {
match Client::open(serv_info) { match Client::open(serv_info) {
@ -180,6 +247,21 @@ fn open_watcher(serv_info: &str) -> Client {
} }
} }
/// # Fn `get_connection_watcher`
/// ## for infinitive establishing Redis connection on existing client
///
/// > only for situation when local isn't None (no need to fck redis server)
///
/// *input* : `&Client`
///
/// *output* : `Connection`
///
/// *initiator* : fn `get_actual_config`
///
/// *managing* : &Client for opening connection
///
/// *depends on* : struct `redis::Connection`
///
fn get_connection_watcher(client: &Client) -> Connection { fn get_connection_watcher(client: &Client) -> Connection {
loop { loop {
match client.get_connection() { match client.get_connection() {
@ -197,11 +279,38 @@ fn get_connection_watcher(client: &Client) -> Connection {
} }
} }
/// # Fn `restart_main_thread`
/// ## for restart monitor with new config
///
/// *input* : -
///
/// *output* : `Ok(())` on valid restart | `Err(er)` on error
///
/// *initiator* : fn `subscribe_config_stream`
///
/// *managing* : -
///
/// *depends on* : -
///
fn restart_main_thread() -> std::io::Result<()> { fn restart_main_thread() -> std::io::Result<()> {
let current_exe = env::current_exe()?; let current_exe = env::current_exe()?;
Command::new(current_exe).exec(); Command::new(current_exe).exec();
Ok(()) Ok(())
} }
/// # Fn `subscribe_config_stream`
/// ## for subscribe on changes, pulling to Redis pubsub to get more actual config
///
/// *input* : `Arc<Processes>`
///
/// *output* : `Ok(())` on end of work | `Err(er)` on error with subscribing mechanism
///
/// *initiator* : fn `subscribe_config_stream`
///
/// *managing* : `Arc<Processes>` to compare old config with new pulled
///
/// *depends on* : `Processes`
///
pub async fn subscribe_config_stream(actual_prcs: Arc<Processes>) -> Result<(), CustomError> { pub async fn subscribe_config_stream(actual_prcs: Arc<Processes>) -> Result<(), CustomError> {
if let Ok(client) = Client::open(format!("redis://{}/", &actual_prcs.config_server)) { if let Ok(client) = Client::open(format!("redis://{}/", &actual_prcs.config_server)) {
if let Ok(mut conn) = client.get_connection() { if let Ok(mut conn) = client.get_connection() {
@ -259,6 +368,19 @@ pub async fn subscribe_config_stream(actual_prcs: Arc<Processes>) -> Result<(),
Err(CustomError::Fatal) Err(CustomError::Fatal)
} }
/// # Fn `config_comparing`
/// ## for compare old and new configs
///
/// *input* : local: `&Processes`, remote: `&Processes`
///
/// *output* : `ConfigActuality::Local` or `ConfigActuality::Remote`
///
/// *initiator* : fn `subscribe_config_stream`, fn `get_actual_config`
///
/// *managing* : two objects `&Processes`
///
/// *depends on* : `Processes`, `ConfigActuality`
///
fn config_comparing(local: &Processes, remote: &Processes) -> ConfigActuality { fn config_comparing(local: &Processes, remote: &Processes) -> ConfigActuality {
let local_date: u64 = local.date_of_creation.parse().unwrap(); let local_date: u64 = local.date_of_creation.parse().unwrap();
let remote_date: u64 = remote.date_of_creation.parse().unwrap(); let remote_date: u64 = remote.date_of_creation.parse().unwrap();
@ -277,6 +399,19 @@ fn config_comparing(local: &Processes, remote: &Processes) -> ConfigActuality {
// } // }
// } // }
/// # Fn `save_new_config`
/// ## mechanism for saving new config in local storage
///
/// *input* : `&Processes`, `&str`
///
/// *output* : `Ok(())` on succesfull saving | Err(er) on fs error
///
/// *initiator* : fn `subscribe_config_stream`, fn `get_actual_config`
///
/// *managing* : new config object: `&Processes` and config file name: `&str`
///
/// *depends on* : `Processes`
///
fn save_new_config(config: &Processes, config_file: &str) -> Result<(), CustomError> { fn save_new_config(config: &Processes, config_file: &str) -> Result<(), CustomError> {
match serde_json::to_string_pretty(&config) { match serde_json::to_string_pretty(&config) {
// Ok(st) => match fs::write(config_file, st) { // Ok(st) => match fs::write(config_file, st) {
@ -305,6 +440,19 @@ fn save_new_config(config: &Processes, config_file: &str) -> Result<(), CustomEr
} }
} }
/// # Fn `parse_extern_config`
/// ## for parsing &str to Processes
///
/// *input* : `&str`
///
/// *output* : parsed config in Some(Processes) | None on error with parsing
///
/// *initiator* : fn `subscribe_config_stream`, fn `once_get_remote_configuration`, fn `get_remote_conf`
///
/// *managing* : unparsed config `&str`
///
/// *depends on* : `Processes`
///
fn parse_extern_config(json_string: &str) -> Option<Processes> { fn parse_extern_config(json_string: &str) -> Option<Processes> {
if let Ok(des) = serde_json::from_str::<Processes>(json_string) { if let Ok(des) = serde_json::from_str::<Processes>(json_string) {
return Some(des); return Some(des);

View File

@ -13,6 +13,19 @@ use crate::utils::get_container_id;
// file_writer: BufWriter<File>, // file_writer: BufWriter<File>,
// } // }
/// # Fn `setup_logger`
/// ## for initializing process of unstoppable grubbing metrics.
///
/// *input* : `Result<()>`
///
/// *output* : `Err` if it cant create logger | `Ok` after logger initialing
///
/// *initiator* : main thread
///
/// *managing* : -
///
/// *depends on* : -
///
pub fn setup_logger() -> Result<(), crate::options::structs::CustomError> { pub fn setup_logger() -> Result<(), crate::options::structs::CustomError> {
// if Command::new("sh").args(["-c", "mkdir logs"]).output().is_err() { // if Command::new("sh").args(["-c", "mkdir logs"]).output().is_err() {
// println!("Error: Cannot init logs directory"); // println!("Error: Cannot init logs directory");

View File

@ -9,6 +9,19 @@ use tokio::{
type SendersVec = Arc<Vec<Arc<mpsc::Sender<u8>>>>; type SendersVec = Arc<Vec<Arc<mpsc::Sender<u8>>>>;
/// # Fn set_valid_destructor
/// ## for initializing process of unstoppable grubbing metrics.
///
/// *input* : `Result<()>`
///
/// *output* : `Err` if it cant create signals listeners | `Ok` on returning Monitor
///
/// *initiator* : main thread
///
/// *managing* : `Arc<Vec<Arc<mpsc::Sender<u8>>>>`
///
/// *depends on* : Sig, Signals
///
pub async fn set_valid_destructor(senders: SendersVec) -> Result<(), CustomError> { pub async fn set_valid_destructor(senders: SendersVec) -> Result<(), CustomError> {
let (mut int, mut term, mut stop) = ( let (mut int, mut term, mut stop) = (
Sig::new(Signals::Sigint, senders.clone()), Sig::new(Signals::Sigint, senders.clone()),
@ -23,16 +36,30 @@ pub async fn set_valid_destructor(senders: SendersVec) -> Result<(), CustomError
} }
Ok(()) Ok(())
} }
/// # Enum Signals
/// ## for instancing each managed system signals (such as SIGINT)
///
/// > (element needed in Sig constructor's signature)
///
/// *depends on* : -
enum Signals { enum Signals {
Sigint, Sigint,
Sigterm, Sigterm,
Sigstop, Sigstop,
} }
/// # Struct Signals
/// ## for instancing each managed system signals (such as SIGINT)
///
/// > (needed to construct system signals listener)
///
/// *depends on* : Signals
struct Sig { struct Sig {
signal: Signal, signal: Signal,
sig_type: Signals, sig_type: Signals,
senders: SendersVec, senders: SendersVec,
} }
/// ## default Sig's constructor
impl Sig { impl Sig {
fn new(signal_type: Signals, sends: SendersVec) -> Self { fn new(signal_type: Signals, sends: SendersVec) -> Self {
Sig { Sig {
@ -42,6 +69,9 @@ impl Sig {
} }
} }
} }
/// ## trait Display realization for returning String-name of signal
///
/// > (needed in logs)
impl std::fmt::Display for Signals { impl std::fmt::Display for Signals {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self { match self {
@ -51,6 +81,7 @@ impl std::fmt::Display for Signals {
} }
} }
} }
/// ## associated func to init signals listener
impl Signals { impl Signals {
fn get_signal(&self) -> io::Result<Signal> { fn get_signal(&self) -> io::Result<Signal> {
match self { match self {
@ -60,9 +91,20 @@ impl Signals {
} }
} }
} }
/// # Trait SigPostProcessing
/// ## to handle post-processing jobs after getting system signal
///
/// ## > (needed in signals post-processing)
///
trait SigPostProcessing { trait SigPostProcessing {
async fn post_processing(&mut self) -> io::Result<()>; async fn post_processing(&mut self) -> io::Result<()>;
} }
/// # Trait SigPostProcessing realization for Sig struct
/// ## to deinitialize Monitor correctly after getting signal
///
/// ## > (needed in signals post-processing)
///
impl SigPostProcessing for Sig { impl SigPostProcessing for Sig {
async fn post_processing(&mut self) -> io::Result<()> { async fn post_processing(&mut self) -> io::Result<()> {
// manipulations ... // manipulations ...

View File

@ -11,8 +11,20 @@ pub enum ConfigActuality {
Remote, Remote,
} }
/// # struct for the 1st level in json conf file /// # Struct for the 1st level in json conf file
/// ## for storing main config data
///
/// > (needed in serialization and deserialization) /// > (needed in serialization and deserialization)
///
/// *depends on* : `TrackingProcess`
///
/// ```
/// {
/// -> "dateOfCreation": "1721381809104",
/// -> "configServer": "localhost",
/// -> "processes": [
/// { ...
/// ```
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Processes { pub struct Processes {
// #[serde(rename="id")] // #[serde(rename="id")]
@ -25,8 +37,24 @@ pub struct Processes {
pub processes: Vec<TrackingProcess>, pub processes: Vec<TrackingProcess>,
} }
/// # struct for each process to contain info, such as name, path and dependencies /// # Struct for the 2nd level in json conf file
/// ## for each process to contain info, such as name, path and dependencies
///
/// > (needed in serialization and deserialization) /// > (needed in serialization and deserialization)
///
/// *depends on* : `Dependencies`
///
/// ```
/// ...
/// "processes": [
/// -> {
/// -> "name": "temp-process",
/// -> "path": "/home/user/monitor/runner-rs/temp-process",
/// -> "dependencies": { ... }
/// -> }, ...
/// ]
/// ...
/// ```
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TrackingProcess { pub struct TrackingProcess {
pub name: String, pub name: String,
@ -34,8 +62,22 @@ pub struct TrackingProcess {
pub dependencies: Dependencies, pub dependencies: Dependencies,
} }
/// # struct for processes' dependencies including files and services /// # Struct for the 3d level in json conf file
/// ## for processes' dependencies including files and services
///
/// > (needed in serialization and deserialization) /// > (needed in serialization and deserialization)
///
/// *depends on* : `Files`, `Services`
///
/// ```
/// ...
/// "path": "/home/user/monitor/runner-rs/temp-process",
/// -> "dependencies": {
/// -> "files": [ ... ],
/// -> "services": [ ... ]
/// -> }
/// ...
/// ```
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Dependencies { pub struct Dependencies {
#[serde(default)] #[serde(default)]
@ -44,17 +86,49 @@ pub struct Dependencies {
pub services: Vec<Services>, pub services: Vec<Services>,
} }
/// # struct for containing file object with its triggers to manipulate in daemons /// # Struct for the 4th level in json conf file
/// ## for containing file object with its triggers to manipulate in daemons
///
/// > (needed in serialization and deserialization) /// > (needed in serialization and deserialization)
///
/// *depends on* : `FileTriggers`
///
/// ```
/// ...
/// "files": [
/// -> {
/// -> "filename": "dep-file",
/// -> "src": "/home/user/monitor/runner-rs/tests/examples/",
/// -> "triggers": { ... }
/// -> } ,
/// ...
/// ], ...
/// ```
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Files { pub struct Files {
pub filename: String, pub filename: String,
pub src: String, pub src: String,
pub triggers: FIleTriggers, pub triggers: FileTriggers,
} }
/// # struct for containing service object with its triggers to manipulate in daemons /// # Struct for the 4th level in json conf file
/// ## for containing service object with its triggers to manipulate in daemons
///
/// > (needed in serialization and deserialization) /// > (needed in serialization and deserialization)
///
/// *depends on* : `ServiceTriggers`
///
/// ```
/// ...
/// "services": [
/// -> {
/// -> "hostname" : "ya.ru",
/// -> "port" : 443,
/// -> "triggers": { ... }
/// -> } ,
/// ...
/// ], ...
/// ```
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Services { pub struct Services {
pub hostname: String, pub hostname: String,
@ -62,8 +136,23 @@ pub struct Services {
pub triggers: ServiceTriggers, pub triggers: ServiceTriggers,
} }
/// # struct for instancing each service's policies such as on lost or time to wait till reachable /// # Struct for the 5th level in json conf file
/// ## for instancing each service's policies such as on lost or time to wait till reachable
///
/// > (needed in serialization and deserialization) /// > (needed in serialization and deserialization)
///
/// *depends on* : -
///
/// ```
/// ...
/// "port": 443,
/// -> "triggers": {
/// -> "wait": 10,
/// -> "delay": 2,
/// -> "onLost": "hold"
/// -> }
/// ...
/// ```
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ServiceTriggers { pub struct ServiceTriggers {
pub wait: u32, pub wait: u32,
@ -72,24 +161,44 @@ pub struct ServiceTriggers {
pub on_lost: String, pub on_lost: String,
} }
/// # struct for instancing each file's policies such as on-delete or onupdate events /// # Struct for the 5th level in json conf file
/// ## for instancing each file's policies such as on-delete or onupdate events
///
/// > (needed in serialization and deserialization) /// > (needed in serialization and deserialization)
///
/// *depends on* : -
///
/// ```
/// ...
/// "src": "/home/user/monitor/runner-rs/tests/examples/",
/// -> "triggers": {
/// -> "onDelete": "stop",
/// -> "onChange": "stay"
/// -> }
/// ...
/// ```
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct FIleTriggers { pub struct FileTriggers {
#[serde(rename = "onDelete")] #[serde(rename = "onDelete")]
pub on_delete: String, pub on_delete: String,
#[serde(rename = "onChange")] #[serde(rename = "onChange")]
pub on_change: String, pub on_change: String,
} }
/// /// # Metrics struct
/// /// ## for gathering all system metrics (from container + each process)
///
/// > (needed in hagent communication, `?...?`)
///
/// *depends on* : `ContainerMetrics`, `ProcessMetrics`
///
#[derive(Debug, Clone, Serialize,)] #[derive(Debug, Clone, Serialize,)]
pub struct Metrics { pub struct Metrics {
pub container_metrics : ContainerMetrics, pub container_metrics : ContainerMetrics,
pub processes_metrics : Vec<ProcessMetrics>, pub processes_metrics : Vec<ProcessMetrics>,
// pub net_metrics : Vec<PacketInfo>, // pub net_metrics : Vec<PacketInfo>,
} }
/// ## Metrics struct's constructor
impl Metrics { impl Metrics {
pub fn new(cm: ContainerMetrics, prm: Vec<ProcessMetrics>) -> Self { pub fn new(cm: ContainerMetrics, prm: Vec<ProcessMetrics>) -> Self {
Metrics { Metrics {
@ -101,7 +210,12 @@ impl Metrics {
} }
/// # Container metrics struct
/// ## for gathering all container metrics
/// ///
/// > (needed in gathering metrics)
///
/// *depends on* : -
/// ///
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct ContainerMetrics { pub struct ContainerMetrics {
@ -111,7 +225,7 @@ pub struct ContainerMetrics {
// pub net_activity : ??? // pub net_activity : ???
processes : Vec<String>, processes : Vec<String>,
} }
/// ## Container struct's constructor
impl ContainerMetrics { impl ContainerMetrics {
pub fn new(container_id : &str, cpu: f32, ram: f32, subsystems: Vec<String>,) -> Self{ pub fn new(container_id : &str, cpu: f32, ram: f32, subsystems: Vec<String>,) -> Self{
ContainerMetrics { ContainerMetrics {
@ -123,7 +237,12 @@ impl ContainerMetrics {
} }
} }
/// # Process metrics struct
/// ## for gathering each process's all metrics
/// ///
/// > (needed in gathering metrics)
///
/// *depends on* : -
/// ///
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct ProcessMetrics { pub struct ProcessMetrics {
@ -131,7 +250,7 @@ pub struct ProcessMetrics {
cpu_load : f32, cpu_load : f32,
ram_load : f32, ram_load : f32,
} }
/// ## Process struct's constructor
impl ProcessMetrics { impl ProcessMetrics {
pub fn new(process_name :&str, cpu: f32, ram: f32) -> Self { pub fn new(process_name :&str, cpu: f32, ram: f32) -> Self {
ProcessMetrics { ProcessMetrics {
@ -142,6 +261,13 @@ impl ProcessMetrics {
} }
} }
/// # Packet info struct
/// ## for gathering info about container's net activity
///
/// > (needed in gathering metrics)
///
/// *depends on* : -
///
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct PacketInfo { pub struct PacketInfo {
protocol : String, protocol : String,
@ -149,6 +275,7 @@ pub struct PacketInfo {
src_ip : Ipv4Addr, src_ip : Ipv4Addr,
size : usize, size : usize,
} }
/// ## PacketInfo's constructor
impl PacketInfo { impl PacketInfo {
pub fn new(prt: String, dest: Ipv4Addr, src: Ipv4Addr, size_of_packet: usize) -> Self { pub fn new(prt: String, dest: Ipv4Addr, src: Ipv4Addr, size_of_packet: usize) -> Self {
PacketInfo { PacketInfo {

View File

@ -24,9 +24,21 @@ use tokio::time::Duration;
const GET_ID_CMD: &str = "hostname"; const GET_ID_CMD: &str = "hostname";
/// # async func to run 3 main daemons (now it's more like tree-form than classical 0.1.0 form ) /// # Fn `run_daemons`
/// > hint : give mpsc with capacity 1 to jump over potential errors during running process /// ## async func to run 3 main daemons: process, service and file monitors and manage process state according to given messages into channel
/// > ** in [developing](https://github.com/prplV/runner-rs "REPOSITORY") ** ///
/// *input* : `Arc<TrackingProcess>`, `Arc<mpsc::Sender<u8>>`, `&mut mpsc::Receiver<u8>`,
///
/// *output* : ()
///
/// *initiator* : main thread
///
/// *managing* : Arc to current process struct, Arc to managing channel writer, mut ref to managing channel reader
///
/// *depends on* : all module `prcs`'s functions, fn `running_handler`, fn `utils::files::create_watcher`
///
/// > *hint* : give mpsc with capacity 1 to jump over potential errors during running process
///
pub async fn run_daemons( pub async fn run_daemons(
proc: Arc<TrackingProcess>, proc: Arc<TrackingProcess>,
tx: Arc<mpsc::Sender<u8>>, tx: Arc<mpsc::Sender<u8>>,
@ -169,6 +181,19 @@ pub async fn run_daemons(
tokio::task::yield_now().await; tokio::task::yield_now().await;
} }
// check process status daemon // check process status daemon
/// # Fn `run_daemons`
/// ## func to async exec subjobs of checking process, services and files states
///
/// *input* : `Arc<TrackingProcess>`, `Arc<mpsc::Sender<u8>>`, `Arc<tokio::sync::Mutex<Vec<Inotify>>>`
///
/// *output* : ()
///
/// *initiator* : fn `run_daemons`
///
/// *managing* : Arc to current process struct, Arc to Mutex to list of file watchers
///
/// *depends on* : fn `utils::files::file_handler`, fn `utils::services::service_handler`, fn `utils::prcs::{is_active, is_frozen, start_process}`
///
pub async fn running_handler( pub async fn running_handler(
prc: Arc<TrackingProcess>, prc: Arc<TrackingProcess>,
tx: Arc<mpsc::Sender<u8>>, tx: Arc<mpsc::Sender<u8>>,
@ -201,6 +226,19 @@ pub async fn running_handler(
} }
// todo: cmd across cat /proc/self/mountinfo | grep "/docker/containers/" | head -1 | awk -F '/' '{print $5}' // todo: cmd across cat /proc/self/mountinfo | grep "/docker/containers/" | head -1 | awk -F '/' '{print $5}'
/// # Fn `get_container_id`
/// ## for getting container id used in logs
///
/// *input* : -
///
/// *output* : Some(String) if cont-id was grubbed | None - if not
///
/// *initiator* : fn `options::logger::setup_logger`
///
/// *managing* : -
///
/// *depends on* : -
///
pub fn get_container_id() -> Option<String> { pub fn get_container_id() -> Option<String> {
match Command::new(GET_ID_CMD).output() { match Command::new(GET_ID_CMD).output() {
Ok(output) => { Ok(output) => {

View File

@ -7,6 +7,19 @@ use std::sync::Arc;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio::time::Duration; use tokio::time::Duration;
/// # Fn `create_watcher`
/// ## for creating watcher on file's delete | update events
///
/// *input* : `&str`, `&str`
///
/// *output* : `Err` if it cant create file watcher | `Ok(watcher)` on successfull construction
///
/// *initiator* : fn `file_handler`, fn `utils::run_daemons`
///
/// *managing* : current file's name: &str, path in local storage to current file: &str
///
/// *depends on* : -
///
pub async fn create_watcher(filename: &str, path: &str) -> Result<Inotify, std::io::Error> { pub async fn create_watcher(filename: &str, path: &str) -> Result<Inotify, std::io::Error> {
let src = format!("{}{}", path, filename); let src = format!("{}{}", path, filename);
let inotify: Inotify = Inotify::init()?; let inotify: Inotify = Inotify::init()?;
@ -14,6 +27,19 @@ pub async fn create_watcher(filename: &str, path: &str) -> Result<Inotify, std::
Ok(inotify) Ok(inotify)
} }
/// # Fn `create_watcher`
/// ## for managing processes by checking dep files' states
///
/// *input* : `&str`, `&[Files]`, `Arc<mpsc::Sender<u8>>`, `Arc<tokio::sync::Mutex<Vec<Inotify>>>`
///
/// *output* : `Err` if something with dep file is wrong | `Ok(())` on successfull dep file check
///
/// *initiator* : fn `utils::running_handler`
///
/// *managing* : current process's name: &str, list of dep files : `&[Files]`, atomic ref counter on sender main channel for current process `Arc<mpsc::Sender<u8>>`, mut list of file watchers`Arc<tokio::sync::Mutex<Vec<Inotify>>>`
///
/// *depends on* : Files
///
pub async fn file_handler( pub async fn file_handler(
name: &str, name: &str,
files: &[Files], files: &[Files],
@ -97,6 +123,19 @@ pub async fn file_handler(
Ok(()) Ok(())
} }
/// # Fn `check_file`
/// ## for checking existance of current file
///
/// *input* : `&str`, `&str`
///
/// *output* : `Ok(())` if file exists | `Err(_)` if not | panic on fs error
///
/// *initiator* : fn `file_handler`
///
/// *managing* : current file's name: `&str` and current file's path in local storage: `&str`
///
/// *depends on* : network activity
///
pub async fn check_file(filename: &str, path: &str) -> Result<(), CustomError> { pub async fn check_file(filename: &str, path: &str) -> Result<(), CustomError> {
let arc_name = Arc::new(filename.to_string()); let arc_name = Arc::new(filename.to_string());
let arc_path = Arc::new(path.to_string()); let arc_path = Arc::new(path.to_string());

View File

@ -1,12 +1,37 @@
// module needed to check host-agent health condition and to communicate with it // module needed to check host-agent health condition and to communicate with it
/// asdasdasds
use tokio::{io::Interest, net::UnixStream}; use tokio::{io::Interest, net::UnixStream};
/// # Fn `open_unix_socket`
/// ## opening unix-socket for host-agent communication
///
/// *input* : -
///
/// *output* : `Ok(socket)` if socket was successfully opened | `Err(er)` if not
///
/// *initiator* : main thread `(??)`
///
/// *managing* : -
///
/// *depends on* : -
///
async fn open_unix_socket() -> Result<UnixStream, std::io::Error> { async fn open_unix_socket() -> Result<UnixStream, std::io::Error> {
let socket = UnixStream::connect("/var/run/enode/hostagent.sock").await?; let socket = UnixStream::connect("/var/run/enode/hostagent.sock").await?;
Ok(socket) Ok(socket)
} }
/// # Fn `ha_healthcheck`
/// ## for checking host-agent state
///
/// *input* : `&UnixStream`
///
/// *output* : `Ok(()))` if host-agent is running | `Err(er)` if not
///
/// *initiator* : main thread `(??)`
///
/// *managing* : ref on unix-socket object
///
/// *depends on* : -
///
async fn ha_healthcheck(socket: &UnixStream) -> Result<(), std::io::Error >{ async fn ha_healthcheck(socket: &UnixStream) -> Result<(), std::io::Error >{
socket.ready(Interest::WRITABLE).await?; socket.ready(Interest::WRITABLE).await?;
if socket.writable().await.is_ok() { if socket.writable().await.is_ok() {
@ -19,7 +44,19 @@ async fn ha_healthcheck(socket: &UnixStream) -> Result<(), std::io::Error >{
Ok(()) Ok(())
} }
/// # Fn `ha_healthcheck`
/// ## for sending data to host-agent using unix-socket
///
/// *input* : `&UnixStream`, `&str`
///
/// *output* : `Ok(()))` if data was sent| `Err(er)` if not
///
/// *initiator* : main thread `(??)`
///
/// *managing* : socket: `&UnixStream`, data: `&str`
///
/// *depends on* : -
///
async fn ha_send_data(socket: &UnixStream, data: &str) -> Result<(), std::io::Error > { async fn ha_send_data(socket: &UnixStream, data: &str) -> Result<(), std::io::Error > {
socket.ready(Interest::WRITABLE).await?; socket.ready(Interest::WRITABLE).await?;
if socket.writable().await.is_ok() { if socket.writable().await.is_ok() {

View File

@ -1,29 +1,32 @@
// submodule needed to get metrics such as // submodule needed to get metrics such as
// cpu load, ram/rom load and net activity // cpu load, ram/rom load and net activity
use std::sync::Mutex; // use std::sync::Mutex;
use std::sync::Arc; use std::sync::Arc;
use crate::options::structs::TrackingProcess; use crate::options::structs::TrackingProcess;
use sysinfo::{Process, System}; use sysinfo::{Process, System};
use tokio::join; use tokio::join;
use crate::options::structs::{ProcessMetrics, ContainerMetrics, PacketInfo}; use crate::options::structs::{ProcessMetrics, ContainerMetrics};
use crate::utils::get_container_id; use crate::utils::get_container_id;
// use pcap::{Device, Capture, Active}; // use pcap::{Device, Capture, Active};
// use std::net::Ipv4Addr; // use std::net::Ipv4Addr;
// use anyhow::{Result, Ok}; // use anyhow::{Result, Ok};
type PacketBuffer = Arc<Mutex<Vec<PacketInfo>>>; // type PacketBuffer = Arc<Mutex<Vec<PacketInfo>>>;
// init_metrics_grubber - fn for initializing /// # Fn `init_metrics_grubber`
// loop for unstoppable grubbing metrics. /// ## for initializing process of unstoppable grubbing metrics.
// ///
// input : vec of processes /// *input* : `Arc<Mutex<UnixSocket>>` ??
// output : Err if it cant create grubbers | Ok on finish ///
// initiator : main thread /// *output* : `Err` if it cant create grubbers | `Ok` on finish
// managing : object of unix-socket reader ///
// depends on : network activity /// *initiator* : main thread ??
// !! FOR PROCESS !! ///
// pub async fn init_metrics_grubber(prcs: Arc<Vec<TrackingProcess>>) { /// *managing* : object of unix-socket reader
///
/// *depends on* : -
///
pub async fn init_metrics_grubber() { pub async fn init_metrics_grubber() {
let mut system = System::new(); let mut system = System::new();
// let mut buffer: Vec<PacketInfo> = vec![]; // let mut buffer: Vec<PacketInfo> = vec![];
@ -76,8 +79,19 @@ async fn gather_metrics(proc: Arc<Process>) {
// } // }
// !!! /// # Fn `get_all_container_metrics`
// for container (whole system metrics) /// ## for gathering all container (whole system metrics)
///
/// *input* : `Arc<System>`, `Arc<Vec<TrackingProcess>>`
///
/// *output* : `ContainerMetrics`
///
/// *initiator* : main thread ??
///
/// *managing* : ref counter to `System` object, ref counter to list of processes
///
/// *depends on* : `TrackingProcess`
///
async fn get_all_container_metrics(sys: Arc<System>, prcs: Arc<Vec<TrackingProcess>>) -> ContainerMetrics { async fn get_all_container_metrics(sys: Arc<System>, prcs: Arc<Vec<TrackingProcess>>) -> ContainerMetrics {
let metrics = join!( let metrics = join!(
get_cpu_metrics_container(sys.clone()), get_cpu_metrics_container(sys.clone()),
@ -91,22 +105,74 @@ async fn get_all_container_metrics(sys: Arc<System>, prcs: Arc<Vec<TrackingProce
metrics.2 metrics.2
) )
} }
/// # Fn `get_cpu_metrics_container`
/// ## for gathering container cpu metrics
///
/// *input* : `Arc<System>`
///
/// *output* : `f32`
///
/// *initiator* : main thread ??
///
/// *managing* : ref counter to `System` object
///
/// *depends on* : -
///
async fn get_cpu_metrics_container(sys: Arc<System>) -> f32 { async fn get_cpu_metrics_container(sys: Arc<System>) -> f32 {
sys.global_cpu_usage() sys.global_cpu_usage()
} }
/// # Fn `get_ram_metrics_container`
/// ## for gathering container ram metrics
///
/// *input* : `Arc<System>`
///
/// *output* : `f32`
///
/// *initiator* : main thread ??
///
/// *managing* : ref counter to `System` object
///
/// *depends on* : -
///
async fn get_ram_metrics_container(sys: Arc<System>) -> f32 { async fn get_ram_metrics_container(sys: Arc<System>) -> f32 {
(sys.used_memory() / sys.total_memory()) as f32 * 100.0 (sys.used_memory() / sys.total_memory()) as f32 * 100.0
} }
// async fn get_mem_metrics_container(sys: Arc<System>) -> f32 { // async fn get_mem_metrics_container(sys: Arc<System>) -> f32 {
// sys. // sys.
// } // }
/// # Fn `get_subsystems`
/// ## for gathering info about container subsystems (processes)
///
/// *input* : `Arc<Vec<TrackingProcess>>`
///
/// *output* : `Vec<String>`
///
/// *initiator* : main thread ??
///
/// *managing* : ref counter to list of `TrackingProcess`
///
/// *depends on* : `TrackingProcess`
///
async fn get_subsystems(prcs: Arc<Vec<TrackingProcess>>) -> Vec<String> { async fn get_subsystems(prcs: Arc<Vec<TrackingProcess>>) -> Vec<String> {
prcs.iter().map(|process| process.name.clone()).collect() prcs.iter().map(|process| process.name.clone()).collect()
} }
// !!! /// # Fn `get_all_metrics_process`
// for process (process metrics) /// ## for gathering all process' metrics
// % ///
/// *input* : `Arc<Process>`, `Arc<System>`
///
/// *output* : `ProcessMetrics`
///
/// *initiator* : main thread ??
///
/// *managing* : two ref counters to `Process` and `System`
///
/// *depends on* : -
///
async fn get_all_metrics_process(proc: Arc<Process>, sys: Arc<System>) -> ProcessMetrics { async fn get_all_metrics_process(proc: Arc<Process>, sys: Arc<System>) -> ProcessMetrics {
let metrics = join!( let metrics = join!(
get_cpu_metrics_process(proc.clone()), get_cpu_metrics_process(proc.clone()),
@ -118,10 +184,37 @@ async fn get_all_metrics_process(proc: Arc<Process>, sys: Arc<System>) -> Proces
metrics.1 metrics.1
) )
} }
/// # Fn `get_cpu_metrics_process`
/// ## for gathering process cpu metrics
///
/// *input* : `Arc<Process>`
///
/// *output* : `f32`
///
/// *initiator* : main thread ??
///
/// *managing* : ref counter to `Process` object
///
/// *depends on* : -
///
async fn get_cpu_metrics_process(proc: Arc<Process>) -> f32 { async fn get_cpu_metrics_process(proc: Arc<Process>) -> f32 {
proc.cpu_usage() proc.cpu_usage()
} }
// %
/// # Fn `get_ram_metrics_process`
/// ## for gathering process ram metrics
///
/// *input* : `Arc<Process>`
///
/// *output* : `f32`
///
/// *initiator* : main thread ??
///
/// *managing* : ref counter to `Process` object
///
/// *depends on* : -
///
async fn get_ram_metrics_process(proc: Arc<Process>, sys: Arc<System>) -> f32 { async fn get_ram_metrics_process(proc: Arc<Process>, sys: Arc<System>) -> f32 {
(proc.memory() as f64 / sys.total_memory() as f64) as f32 * 100.0 as f32 (proc.memory() as f64 / sys.total_memory() as f64) as f32 * 100.0 as f32
} }

View File

@ -1,10 +1,22 @@
use crate::options::structs::CustomError; use crate::options::structs::CustomError;
use log::{error, warn}; use log::{error, warn};
use std::io;
use std::process::{Command, Output}; use std::process::{Command, Output};
use std::sync::Arc; use std::sync::Arc;
use tokio::time::Duration; use tokio::time::Duration;
/// # Fn `get_pid`
/// ## for initializing process of unstoppable grubbing metrics.
///
/// *input* : `&str`
///
/// *output* : `None` if cant get process PID | `Some(Output)` on success
///
/// *initiator* : fn `is_frozen`, fn `utils::files::file_handler`, fn `utils::files::service_handler`
///
/// *managing* : process name
///
/// *depends on* : -
///
pub async fn get_pid(name: &str) -> Option<Output> { pub async fn get_pid(name: &str) -> Option<Output> {
let name = Arc::new(name.to_string()); let name = Arc::new(name.to_string());
let res = let res =
@ -20,8 +32,20 @@ pub async fn get_pid(name: &str) -> Option<Output> {
None None
} }
} }
// ! can be with bug !!!
// * APPROVED /// # Fn `is_active`
/// ## for checking process's activity state
///
/// *input* : `&str`
///
/// *output* : `true` if process running | `false` if not
///
/// *initiator* : fn `utils::files::file_handler`, fn `utils::files::service_handler`
///
/// *managing* : process name
///
/// *depends on* : -
///
pub async fn is_active(name: &str) -> bool { pub async fn is_active(name: &str) -> bool {
let arc_name = Arc::new(name.to_string()); let arc_name = Arc::new(name.to_string());
tokio::task::spawn_blocking(move || { tokio::task::spawn_blocking(move || {
@ -38,7 +62,19 @@ pub async fn is_active(name: &str) -> bool {
.unwrap() .unwrap()
} }
// T is for stopped processes /// # Fn `is_frozen`
/// ## for checking process's hibernation state
///
/// *input* : `&str`
///
/// *output* : `true` if process is frozen | `false` if not
///
/// *initiator* : fn `utils::files::file_handler`, fn `utils::files::service_handler`
///
/// *managing* : process name
///
/// *depends on* : fn `get_pid`
///
pub async fn is_frozen(name: &str) -> bool { pub async fn is_frozen(name: &str) -> bool {
let temp: Output; let temp: Output;
if let Some(output) = get_pid(name).await { if let Some(output) = get_pid(name).await {
@ -66,6 +102,20 @@ pub async fn is_frozen(name: &str) -> bool {
.unwrap() .unwrap()
} }
} }
/// # Fn `terminate_process`
/// ## for stop current process
///
/// *input* : `&str`
///
/// *output* : ()
///
/// *initiator* : fn `utils::files::file_handler`, fn `utils::files::service_handler`, fn `utils::run_daemons`
///
/// *managing* : process name
///
/// *depends on* : -
///
pub async fn terminate_process(name: &str) { pub async fn terminate_process(name: &str) {
let _ = Command::new("pkill") let _ = Command::new("pkill")
.arg(name) .arg(name)
@ -76,6 +126,19 @@ pub async fn terminate_process(name: &str) {
}); });
} }
/// # Fn `terminate_process`
/// ## for freeze/hibernate current process
///
/// *input* : `&str`
///
/// *output* : ()
///
/// *initiator* : fn `utils::run_daemons`
///
/// *managing* : process name
///
/// *depends on* : -
///
pub async fn freeze_process(name: &str) { pub async fn freeze_process(name: &str) {
let _ = Command::new("pkill") let _ = Command::new("pkill")
.args(["-STOP", name]) .args(["-STOP", name])
@ -85,6 +148,20 @@ pub async fn freeze_process(name: &str) {
std::process::exit(101); std::process::exit(101);
}); });
} }
/// # Fn `unfreeze_process`
/// ## for unfreeze/hibernate current process
///
/// *input* : `&str`
///
/// *output* : ()
///
/// *initiator* : fn `utils::run_daemons`
///
/// *managing* : process name
///
/// *depends on* : -
///
pub async fn unfreeze_process(name: &str) { pub async fn unfreeze_process(name: &str) {
let _ = Command::new("pkill") let _ = Command::new("pkill")
.args(["-CONT", name]) .args(["-CONT", name])
@ -94,12 +171,39 @@ pub async fn unfreeze_process(name: &str) {
std::process::exit(101); std::process::exit(101);
}); });
} }
/// # Fn `restart_process`
/// ## for restarting current process
///
/// *input* : `&str`, &str
///
/// *output* : ()
///
/// *initiator* : fn `utils::run_daemons`
///
/// *managing* : process name and path to its exec file
///
/// *depends on* : fn `start_process`, fn `terminate_process`
///
pub async fn restart_process(name: &str, path: &str) -> Result<(), CustomError> { pub async fn restart_process(name: &str, path: &str) -> Result<(), CustomError> {
terminate_process(name).await; terminate_process(name).await;
tokio::time::sleep(Duration::from_millis(100)).await; tokio::time::sleep(Duration::from_millis(100)).await;
start_process(name, path).await start_process(name, path).await
} }
/// # Fn `start_process`
/// ## for starting current process
///
/// *input* : `&str`, &str
///
/// *output* : ()
///
/// *initiator* : fn `restart_process`
///
/// *managing* : process name and path to its exec file
///
/// *depends on* : -
///
pub async fn start_process(name: &str, path: &str) -> Result<(), CustomError> { pub async fn start_process(name: &str, path: &str) -> Result<(), CustomError> {
// let runsh = format!("{} {}", "exec", path); // let runsh = format!("{} {}", "exec", path);
let mut command = Command::new(path); let mut command = Command::new(path);

View File

@ -6,6 +6,19 @@ use std::sync::Arc;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio::time::{Duration, Instant}; use tokio::time::{Duration, Instant};
/// # Fn `service_handler`
/// ## function to realize mechanism of current process' dep services monitoring
///
/// *input* : `&str`, `&Vec<Services>`, `Arc<mpsc::Sender<u8>>`
///
/// *output* : ()
///
/// *initiator* : fn `utils::running_handler`
///
/// *managing* : process name, ref of vec of dep services, ref counter to managing channel writer
///
/// *depends on* : fn `check_service`, fn `utils::prcs::is_active`, fn `utils::prcs::is_frozen`, fn `looped_service_connecting`
///
pub async fn service_handler( pub async fn service_handler(
name: &str, name: &str,
services: &Vec<Services>, services: &Vec<Services>,
@ -54,6 +67,19 @@ pub async fn service_handler(
Ok(()) Ok(())
} }
/// # Fn `looped_service_connecting`
/// ## for service's state check in loop (with delay and restriction of attempts)
///
/// *input* : `&str`, `&Services`
///
/// *output* : Ok(()) if service now available | Err(er) if still not
///
/// *initiator* : fn `service_handler`
///
/// *managing* : process name, current service struct
///
/// *depends on* : fn `check_service`
///
async fn looped_service_connecting(name: &str, serv: &Services) -> Result<(), CustomError> { async fn looped_service_connecting(name: &str, serv: &Services) -> Result<(), CustomError> {
if serv.triggers.wait == 0 { if serv.triggers.wait == 0 {
loop { loop {
@ -103,6 +129,19 @@ async fn looped_service_connecting(name: &str, serv: &Services) -> Result<(), Cu
} }
} }
/// # Fn `check_service`
/// ## for check current service's availiability
///
/// *input* : `&str`, `&u32`
///
/// *output* : Ok(()) if service now available | Err(er) if still not
///
/// *initiator* : fn `service_handler`, fn `looped_service_connecting`
///
/// *managing* : hostname, port
///
/// *depends on* : -
///
// ! have to be rewritten // ! have to be rewritten
// todo: rewrite use // todo: rewrite use
async fn check_service(hostname: &str, port: &u32) -> Result<(), CustomError> { async fn check_service(hostname: &str, port: &u32) -> Result<(), CustomError> {