-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement events command for cgroup v1 stats
- Loading branch information
Showing
7 changed files
with
72 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,6 +157,6 @@ impl CgroupManager for Manager { | |
} | ||
|
||
fn stats(&self) -> Result<Stats> { | ||
todo!(); | ||
Ok(Stats::default()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crate::{cgroups::common, utils}; | ||
use clap::Clap; | ||
use std::{path::PathBuf, thread, time::Duration}; | ||
|
||
use anyhow::{bail, Context, Result}; | ||
|
||
use crate::container::{Container, ContainerStatus}; | ||
|
||
#[derive(Clap, Debug)] | ||
pub struct Events { | ||
/// Sets the stats collection interval in seconds (default: 5s) | ||
#[clap(long, default_value = "5")] | ||
pub interval: u32, | ||
/// Display the container stats only once | ||
#[clap(long)] | ||
pub stats: bool, | ||
/// Name of the container instance | ||
pub container_id: String, | ||
} | ||
|
||
impl Events { | ||
pub fn exec(&self, root_path: PathBuf) -> Result<()> { | ||
let container_dir = root_path.join(&self.container_id); | ||
if !container_dir.exists() { | ||
log::debug!("{:?}", container_dir); | ||
bail!("{} doesn't exist.", self.container_id) | ||
} | ||
|
||
let container = Container::load(container_dir)?.refresh_status()?; | ||
if !container.state.status.eq(&ContainerStatus::Running) { | ||
bail!("{} is not in running state", self.container_id); | ||
} | ||
|
||
let cgroups_path = utils::get_cgroup_path( | ||
&container | ||
.spec()? | ||
.linux | ||
.context("no linux in spec")? | ||
.cgroups_path, | ||
&self.container_id, | ||
); | ||
let use_systemd = container | ||
.systemd() | ||
.context("Could not determine cgroup manager")?; | ||
|
||
let cgroup_manager = common::create_cgroup_manager(cgroups_path, use_systemd)?; | ||
match self.stats { | ||
true => println!("{:#?}", cgroup_manager.stats()), | ||
false => loop { | ||
println!("{:#?}", cgroup_manager.stats()); | ||
thread::sleep(Duration::from_secs(self.interval as u64)); | ||
}, | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod create; | ||
pub mod delete; | ||
pub mod events; | ||
pub mod exec; | ||
pub mod info; | ||
pub mod kill; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters