Skip to content

Commit

Permalink
添加hwmon
Browse files Browse the repository at this point in the history
  • Loading branch information
xml committed Apr 15, 2024
1 parent 656ca55 commit 31836c6
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
103 changes: 103 additions & 0 deletions src/hwmon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// /sys/class/hwmon/hwmon
use std::collections::HashMap;

static ROOTPATH: &str = "/sys/class/hwmon";

#[derive(Clone, Debug)]
pub struct Hwmon {
pub node: String,
pub name: String,
}

impl Hwmon {
pub fn new(path: &str) -> std::io::Result<Hwmon> {
let path = std::path::Path::new(path);
let node = path.file_name().unwrap().to_str().unwrap().to_string();
let name = crate::utils::read_line(&path.join("name").to_str().unwrap().to_string())?;

Ok(Hwmon{
node,
name,
})
}

pub fn fans(&self) -> HashMap<String, u32> {
let mut node_vals :HashMap<String, u32>= HashMap::new();
for entry in walkdir::WalkDir::new(format!("{}/{}", ROOTPATH, self.node))
.sort_by_file_name()
.max_depth(1)
.into_iter()
.filter_map(Result::ok)
.filter(|e| (e.file_type().is_file() || e.file_type().is_symlink())) {
let re = regex::Regex::new(r"^fan\d{1,3}_input$").unwrap();
if re.is_match(entry.path().file_name().unwrap().to_str().unwrap()) {
if let Ok(raw_val) = crate::utils::read_line(entry.path().to_str().unwrap()) {
if let Ok(val) = raw_val.parse::<u32>() {
let key = entry.path().file_name().unwrap().to_str().unwrap().to_string();
node_vals.insert(key, val);
}
}
}
}

node_vals
}

pub fn temps(&self) -> HashMap<String, u32> {
let mut node_vals: HashMap<String, u32> = HashMap::new();
for entry in walkdir::WalkDir::new(format!("{}/{}", ROOTPATH, self.node))
.sort_by_file_name()
.max_depth(1)
.into_iter()
.filter_map(Result::ok)
.filter(|e| (e.file_type().is_file() || e.file_type().is_symlink())) {
//println!("path: {:#?}", entry.path());
let re = regex::Regex::new(r"^temp(?<num>\d{1,3})_input$").unwrap();
if let Some(caps) = re.captures(entry.path().file_name().unwrap().to_str().unwrap()) {
let num = caps["num"].to_string();


let val = if let Ok(raw_val) = crate::utils::read_line(entry.path().to_str().unwrap()) {
if let Ok(val) = raw_val.parse::<u32>() {
val
} else {
continue;
}
} else {
continue;
};

let key = if let Ok(val) = crate::utils::read_line(&format!("{}/{}/temp{}_label", ROOTPATH, self.node, num)) {
val
} else {
format!("temp{}_label", num)
};
node_vals.insert(key, val);
}
}

node_vals
}
}

pub fn enumerate() -> Vec<Hwmon> {
let mut hwmons = Vec::new();
for entry in walkdir::WalkDir::new(ROOTPATH)
.sort_by_file_name()
.max_depth(1)
.into_iter()
.filter_map(Result::ok)
.filter(|e| (e.file_type().is_dir() || e.file_type().is_symlink())){
if entry.path().to_str().unwrap() != ROOTPATH {
//println!("node: {:#?}", entry.path());
let re = regex::Regex::new(r"^hwmon\d{1,3}$").unwrap();
if re.is_match(entry.path().file_name().unwrap().to_str().unwrap().trim()) {
if let Ok(hwmon) = Hwmon::new(entry.path().to_str().unwrap().trim()) {
hwmons.push(hwmon)
}
}
}
}

hwmons
}
15 changes: 15 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod visualization;
mod cpu;
mod battery;
mod thermal;
mod hwmon;

use walkdir;
use regex;
Expand Down Expand Up @@ -287,6 +288,20 @@ fn main() -> std::io::Result<()> {
println!("{:#?}", bats);
let thermals = thermal::enumerate();
println!("{:#?}", thermals);
let hwmons = hwmon::enumerate();
for hwmon in hwmons {
let fans = hwmon.fans();
let temps = hwmon.temps();
if !fans.is_empty() || !temps.is_empty() {
println!("name: {}", hwmon.name);
if !fans.is_empty() {
println!("{:#?}", hwmon.fans());
}
if !temps.is_empty() {
println!("{:#?}", hwmon.temps());
}
}
}
},
Command::Collect { time } => {
let mut collectors: Vec<Box<dyn collector::Collector>> = Vec::new();
Expand Down

0 comments on commit 31836c6

Please sign in to comment.