From 9bfd6d3393adec6742fe5f53a1cc96938dc21e67 Mon Sep 17 00:00:00 2001 From: Taiga <53041471+TakanoTaiga@users.noreply.github.com> Date: Tue, 12 Mar 2024 19:44:29 +0900 Subject: [PATCH] Add files via upload --- Cargo.lock | 7 +++++++ Cargo.toml | 8 ++++++++ src/main.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..68b27e9 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "mem_checker" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..16b6515 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "mem_checker" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..225ad65 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,45 @@ +use std::process::{Command, Stdio}; +use std::fs::OpenOptions; +use std::io::Write; +use std::thread; +use std::time::Duration; +use std::path::Path; + + +fn main() { + loop { + let output = Command::new("ps") + .args(["aux"]) + .stdout(Stdio::piped()) + .output() + .expect("Failed to execute command"); + + let output_str = String::from_utf8_lossy(&output.stdout); + + for line in output_str.lines().skip(1) { + let columns: Vec<&str> = line.split_whitespace().collect(); + let user = columns.get(0).unwrap_or(&""); + let pid = columns.get(1).unwrap_or(&""); + + if *user == "taiga" { + let rss_str = columns.get(5).unwrap_or(&"0"); + let rss_value: i32 = rss_str.parse().unwrap_or(0); + let cpu = columns.get(2).unwrap_or(&""); + + let file_name = format!("./data/{}.csv",pid); + let path = Path::new(&file_name); + let mut file = OpenOptions::new() + .write(true) + .append(true) + .create(true) + .open(path) + .expect("Failed to open file"); + + writeln!(file, "{},{}", cpu, rss_value) + .expect("Failed to write to file"); + } + } + + thread::sleep(Duration::from_secs(1)); + } +}