getting data programmatically (#56)
* getting loadavg programmatically * added missing uptime in rsblock.yml * using clock_gettime syscall to get uptime
This commit is contained in:
parent
674f6811fe
commit
b1d81bf8c9
13
rsblocks.yml
13
rsblocks.yml
@ -11,6 +11,13 @@ time:
|
|||||||
icon: ''
|
icon: ''
|
||||||
|
|
||||||
|
|
||||||
|
# Time of the system has been running
|
||||||
|
uptime:
|
||||||
|
delay: 15.0
|
||||||
|
enabled: false
|
||||||
|
icon: ''
|
||||||
|
|
||||||
|
|
||||||
# public ip address
|
# public ip address
|
||||||
pub_ip:
|
pub_ip:
|
||||||
delay: 120.0
|
delay: 120.0
|
||||||
@ -18,7 +25,7 @@ pub_ip:
|
|||||||
icon: ''
|
icon: ''
|
||||||
|
|
||||||
|
|
||||||
# ethernet/wifi bandwith
|
# ethernet/wifi bandwith (no delay for this since it will calculate the bandwith every second)
|
||||||
netspeed:
|
netspeed:
|
||||||
enabled: false
|
enabled: false
|
||||||
interface: 'wlan0'
|
interface: 'wlan0'
|
||||||
@ -50,9 +57,9 @@ cpu_temperature:
|
|||||||
icon: ''
|
icon: ''
|
||||||
|
|
||||||
|
|
||||||
# cpu load average
|
# cpu load average from the last minute
|
||||||
loadavg:
|
loadavg:
|
||||||
delay: 2.0
|
delay: 15.0
|
||||||
enabled: false
|
enabled: false
|
||||||
icon: ''
|
icon: ''
|
||||||
|
|
||||||
|
@ -1,12 +1,16 @@
|
|||||||
use crate::config::CONFIG;
|
use crate::config::CONFIG;
|
||||||
use crate::types::ThreadsData;
|
use crate::types::ThreadsData;
|
||||||
use nix::sys::sysinfo;
|
|
||||||
|
use nix::libc::{c_double, c_int, getloadavg};
|
||||||
|
|
||||||
pub fn get_load_avg() -> ThreadsData {
|
pub fn get_load_avg() -> ThreadsData {
|
||||||
let load = sysinfo::sysinfo().unwrap().load_average().0;
|
let mut data: [c_double; 3] = [0f64; 3];
|
||||||
|
unsafe { getloadavg(data.as_mut_ptr(), data.len() as c_int) };
|
||||||
|
let [load, _, _] = data;
|
||||||
let data = format!(
|
let data = format!(
|
||||||
" {} {:.2} {}",
|
" {} {:.2} {}",
|
||||||
CONFIG.loadavg.icon, load, CONFIG.seperator
|
CONFIG.loadavg.icon, load, CONFIG.seperator
|
||||||
);
|
);
|
||||||
|
|
||||||
ThreadsData::LoadAvg(data)
|
ThreadsData::LoadAvg(data)
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,33 @@
|
|||||||
use crate::config::CONFIG;
|
use crate::config::CONFIG;
|
||||||
use crate::types::ThreadsData;
|
use crate::types::ThreadsData;
|
||||||
use chrono::Duration;
|
use nix::time::clock_gettime;
|
||||||
use nix::sys::sysinfo;
|
|
||||||
|
|
||||||
pub fn get_uptime() -> ThreadsData {
|
pub fn get_uptime() -> ThreadsData {
|
||||||
let duration = sysinfo::sysinfo().unwrap().uptime();
|
let (_, hour, minutes, seconds) = get_uptime_data();
|
||||||
let uptime_sec = Duration::from_std(duration).unwrap().num_seconds();
|
|
||||||
|
|
||||||
let hour = uptime_sec / 3600;
|
|
||||||
let rem = uptime_sec - hour * 3600;
|
|
||||||
let minutes = rem / 60;
|
|
||||||
|
|
||||||
let uptime = if hour > 0 {
|
let uptime = if hour > 0 {
|
||||||
format!("{}:{}", hour, minutes)
|
format!("{}:{}:{}", hour, minutes, seconds)
|
||||||
} else {
|
} else {
|
||||||
format!("{} min", minutes)
|
format!("{} min, {} sec", minutes, seconds)
|
||||||
};
|
};
|
||||||
let result = format!(" {} {} {}", CONFIG.uptime.icon, uptime, CONFIG.seperator);
|
let result = format!(" {} {} {}", CONFIG.uptime.icon, uptime, CONFIG.seperator);
|
||||||
ThreadsData::Uptime(result)
|
ThreadsData::Uptime(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This helper function will use the system call clock_gettime
|
||||||
|
// it will return a tuple of (days, hours, minutes, seconds)
|
||||||
|
fn get_uptime_data() -> (i64, i64, i64, i64) {
|
||||||
|
let mut uptime = clock_gettime(nix::time::ClockId::CLOCK_MONOTONIC)
|
||||||
|
.unwrap()
|
||||||
|
.tv_sec();
|
||||||
|
|
||||||
|
if uptime > 60 {
|
||||||
|
uptime += 30;
|
||||||
|
}
|
||||||
|
let days = uptime / 86400;
|
||||||
|
uptime %= 86400;
|
||||||
|
let hours = uptime / 3600;
|
||||||
|
uptime %= 3600;
|
||||||
|
let minutes = uptime / 60;
|
||||||
|
let seconds = uptime % 60;
|
||||||
|
(days, hours, minutes, seconds)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user