docs: structs

pull/7/head
prplV 2024-11-13 13:04:09 +03:00
parent 985ddc6065
commit e5437fb877
2 changed files with 156 additions and 26 deletions

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,17 +161,36 @@ 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 {
@ -90,6 +198,7 @@ pub struct Metrics {
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

@ -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` : vec of processes
// 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` : network activity
///
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![];