M1: core engine, config, plugin model and core plugins

- Cargo workspace (core, plugins, tui, cli) with drop-in compatible CLI
- Python-compatible config file handling, limits, views, rates, MMM, history
- process collection via sysinfo + procfs, filters, thresholds, events, actions
- 20 core plugins (cpu, percpu, mem, memswap, load, fs, network, diskio, ...)
- stdout, stdout-csv, stdout-json, --issue, --modules-list output modes
- differential test harness against the Python implementation
This commit is contained in:
glances-rs
2026-10-09 23:25:35 +00:00
commit 975385da5d
61 changed files with 12049 additions and 0 deletions
@@ -0,0 +1,53 @@
//! CPU core count plugin.
use crate::fields;
use glances_core::plugin::{Field, Plugin, PluginModel, UpdateContext};
use glances_core::{Args, Config};
use serde_json::{Value, json};
pub struct CorePlugin {
pub model: PluginModel,
}
impl CorePlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let fields = fields! {
"phys" => Field::new("Number of physical cores (hyper thread CPUs are excluded).").unit("number"),
"log" => Field::new(
"Number of logical CPU cores. A logical CPU is the number of physical cores multiplied by the number of threads that can run on each core."
).unit("number"),
};
let model = PluginModel::new(
"core",
fields,
Vec::new(),
json!({}),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
Self { model }
}
}
impl Plugin for CorePlugin {
fn name(&self) -> &str {
"core"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<Value> {
Ok(json!({
"phys": crate::collectors::physical_core_count(),
"log": crate::collectors::logical_core_count(),
}))
}
}
+141
View File
@@ -0,0 +1,141 @@
//! CPU plugin.
use crate::collectors::{CpuTimes, read_cpu_times};
use crate::cpu_percent::cpu_percent;
use crate::fields;
use glances_core::plugin::{Field, HistoryItem, Plugin, PluginModel, UpdateContext};
use glances_core::{Args, Config};
use serde_json::{Value, json};
pub struct CpuPlugin {
pub model: PluginModel,
previous_times: Option<CpuTimes>,
pub nb_log_core: usize,
}
impl CpuPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let fields = fields! {
"total" => Field::new("Sum of all CPU percentages (except idle).").unit("percent").log().mmm(),
"system" => Field::new("Percent time spent in kernel space. System CPU time is the time spent running code in the Operating System kernel.").unit("percent").log(),
"user" => Field::new("CPU percent time spent in user space. User CPU time is the time spent on the processor running your program's code (or code in libraries).").unit("percent").log(),
"iowait" => Field::new("*(Linux)*: percent time spent by the CPU waiting for I/O operations to complete.").unit("percent").log(),
"dpc" => Field::new("*(Windows)*: time spent servicing deferred procedure calls (DPCs)").unit("percent").log(),
"idle" => Field::new("percent of CPU used by any program. Every program or task that runs on a computer system occupies a certain amount of processing time on the CPU. If the CPU has completed all tasks it is idle.").unit("percent").optional(),
"irq" => Field::new("*(Linux and BSD)*: percent time spent servicing/handling hardware/software interrupts. Time servicing interrupts (hardware + software).").unit("percent").optional(),
"nice" => Field::new("*(Unix)*: percent time occupied by user level processes with a positive nice value. The time the CPU has spent running users' processes that are *niced*.").unit("percent").optional(),
"steal" => Field::new("*(Linux)*: percentage of time a virtual CPU waits for a real CPU while the hypervisor is servicing another virtual processor.").unit("percent").alert().optional(),
"guest" => Field::new("*(Linux)*: time spent running a virtual CPU for guest operating systems under the control of the Linux kernel.").unit("percent").optional(),
"ctx_switches" => Field::new("number of context switches (voluntary + involuntary) per second. A context switch is a procedure that a computer's CPU (central processing unit) follows to change from one task to another while ensuring that the tasks do not conflict.").unit("number").rate().min_symbol("K").short_name("ctx_sw").optional(),
"interrupts" => Field::new("number of interrupts per second.").unit("number").rate().min_symbol("K").short_name("inter").optional(),
"soft_interrupts" => Field::new("number of software interrupts per second. Always set to 0 on Windows and SunOS.").unit("number").rate().min_symbol("K").short_name("sw_int").optional(),
"syscalls" => Field::new("number of system calls per second. Always 0 on Linux OS.").unit("number").rate().min_symbol("K").short_name("sys_call").optional(),
"cpucore" => Field::new("Total number of CPU core.").unit("number"),
"time_since_update" => Field::new("Number of seconds since last update.").unit("seconds"),
};
let items_history = vec![
HistoryItem::new("user", "User CPU usage"),
HistoryItem::new("system", "System CPU usage"),
];
let model = PluginModel::new(
"cpu",
fields,
items_history,
json!({}),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
let nb_log_core = crate::collectors::logical_core_count();
Self { model, previous_times: None, nb_log_core }
}
fn times_percent(&mut self) -> CpuTimes {
let current = read_cpu_times().unwrap_or_default();
match self.previous_times {
Some(previous) => {
self.previous_times = Some(current);
current.times_percent(&previous)
}
None => {
let second = read_cpu_times().unwrap_or(current);
self.previous_times = Some(second);
second.times_percent(&current)
}
}
}
}
impl Plugin for CpuPlugin {
fn name(&self) -> &str {
"cpu"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<Value> {
let percent = self.times_percent();
let total = {
let mut state = cpu_percent().lock().unwrap();
state.get_cpu()
};
let mut stats = serde_json::Map::new();
stats.insert("total".to_string(), json!(total));
stats.insert("user".to_string(), json!(percent.user));
stats.insert("nice".to_string(), json!(percent.nice));
stats.insert("system".to_string(), json!(percent.system));
stats.insert("idle".to_string(), json!(percent.idle));
stats.insert("iowait".to_string(), json!(percent.iowait));
stats.insert("irq".to_string(), json!(percent.irq));
stats.insert("steal".to_string(), json!(percent.steal));
stats.insert("guest".to_string(), json!(percent.guest));
// Additional CPU stats from /proc/stat.
if let Ok(content) = std::fs::read_to_string("/proc/stat") {
let mut ctx_switches = 0u64;
let mut interrupts = 0u64;
let mut soft_interrupts = 0u64;
for line in content.lines() {
if let Some(rest) = line.strip_prefix("ctxt ") {
ctx_switches = rest.split_whitespace().next().and_then(|v| v.parse().ok()).unwrap_or(0);
} else if let Some(rest) = line.strip_prefix("intr ") {
interrupts = rest.split_whitespace().next().and_then(|v| v.parse().ok()).unwrap_or(0);
} else if let Some(rest) = line.strip_prefix("softirq ") {
soft_interrupts = rest.split_whitespace().next().and_then(|v| v.parse().ok()).unwrap_or(0);
}
}
stats.insert("ctx_switches".to_string(), json!(ctx_switches));
stats.insert("interrupts".to_string(), json!(interrupts));
stats.insert("soft_interrupts".to_string(), json!(soft_interrupts));
}
stats.insert("syscalls".to_string(), json!(0));
stats.insert("cpucore".to_string(), json!(self.nb_log_core));
Ok(Value::Object(stats))
}
fn update_views(&mut self) {
let key = self.get_key();
self.model.update_views(key);
if self.model.stats.get("time_since_update").and_then(Value::as_f64) != Some(0.0) {
if let Some(ctx_switches) = self.model.stats.get("ctx_switches").and_then(Value::as_f64) {
let maximum = 100.0 * self.nb_log_core as f64;
let decoration =
self.model.get_alert(ctx_switches, 0.0, maximum, true, false, Some("ctx_switches"), None, false);
self.model.set_view_decoration(None, "ctx_switches", decoration);
}
}
}
fn display_curse(&self) -> bool {
true
}
}
@@ -0,0 +1,163 @@
//! Disk I/O plugin.
use crate::collectors::{SECTOR_SIZE, read_diskstats};
use crate::fields;
use glances_core::plugin::{Field, HistoryItem, Plugin, PluginModel, UpdateContext};
use glances_core::{Args, Config, Decoration};
use serde_json::{Value, json};
pub struct DiskioPlugin {
pub model: PluginModel,
}
impl DiskioPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let fields = fields! {
"disk_name" => Field::new("Disk name."),
"read_count" => Field::new("Number of reads.").rate().unit("number"),
"write_count" => Field::new("Number of writes.").rate().unit("number"),
"read_bytes" => Field::new("Number of bytes read.").rate().unit("byte"),
"write_bytes" => Field::new("Number of bytes written.").rate().unit("byte"),
"read_time" => Field::new("Time spent reading.").rate().unit("millisecond"),
"write_time" => Field::new("Time spent writing.").rate().unit("millisecond"),
"read_latency" => Field::new("Mean time spent reading per operation.").unit("millisecond"),
"write_latency" => Field::new("Mean time spent writing per operation.").unit("millisecond"),
};
let items_history = vec![
HistoryItem::new("read_bytes_rate_per_sec", "Bytes read per second"),
HistoryItem::new("write_bytes_rate_per_sec", "Bytes write per second"),
];
let mut model = PluginModel::new(
"diskio",
fields,
items_history,
Value::Array(Vec::new()),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
model.set_item_key("disk_name");
model.hide_zero = config.get_bool_value("diskio", "hide_zero", false);
model.hide_threshold_bytes = config.get_int_value("diskio", "hide_threshold_bytes", 0) as f64;
model.hide_zero_fields =
vec!["read_bytes_rate_per_sec".to_string(), "write_bytes_rate_per_sec".to_string()];
Self { model }
}
}
impl Plugin for DiskioPlugin {
fn name(&self) -> &str {
"diskio"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn get_key(&self) -> Option<&'static str> {
Some("disk_name")
}
fn collect(&mut self, ctx: &mut UpdateContext) -> anyhow::Result<Value> {
let mut stats = Vec::new();
for (disk_name, disk_stat) in read_diskstats() {
if !ctx.args.diskio_show_ramfs && disk_name.starts_with("ram") {
continue;
}
if !self.model.is_display(&disk_name, "") {
continue;
}
let mut item = serde_json::Map::new();
item.insert("read_count".to_string(), json!(disk_stat.read_count));
item.insert("write_count".to_string(), json!(disk_stat.write_count));
item.insert("read_bytes".to_string(), json!(disk_stat.sectors_read * SECTOR_SIZE));
item.insert("write_bytes".to_string(), json!(disk_stat.sectors_written * SECTOR_SIZE));
item.insert("read_time".to_string(), json!(disk_stat.read_time));
item.insert("write_time".to_string(), json!(disk_stat.write_time));
item.insert("key".to_string(), json!("disk_name"));
item.insert("disk_name".to_string(), json!(disk_name));
if let Some(alias) = self.model.has_alias(&disk_name) {
item.insert("alias".to_string(), json!(alias));
}
stats.push(Value::Object(item));
}
Ok(Value::Array(stats))
}
fn post_collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<()> {
if let Value::Array(items) = &mut self.model.stats {
for item in items.iter_mut() {
let Some(item) = item.as_object_mut() else {
continue;
};
let read_time = item.get("read_time_rate_per_sec").and_then(Value::as_f64).unwrap_or(0.0);
let read_count = item.get("read_count_rate_per_sec").and_then(Value::as_f64).unwrap_or(0.0);
let write_time = item.get("write_time_rate_per_sec").and_then(Value::as_f64).unwrap_or(0.0);
let write_count = item.get("write_count_rate_per_sec").and_then(Value::as_f64).unwrap_or(0.0);
let read_latency = if item.contains_key("read_time_rate_per_sec") && read_count > 0.0 {
(read_time / read_count).floor() as i64
} else {
0
};
let write_latency = if item.contains_key("write_time_rate_per_sec") && write_count > 0.0 {
(write_time / write_count).floor() as i64
} else {
0
};
item.insert("read_latency".to_string(), json!(read_latency));
item.insert("write_latency".to_string(), json!(write_latency));
}
}
Ok(())
}
fn update_views(&mut self) {
let key = self.get_key();
self.model.update_views(key);
let items: Vec<(String, f64, f64, f64, f64)> = match &self.model.stats {
Value::Array(list) => list
.iter()
.filter_map(|item| {
let name = item.get("disk_name")?.as_str()?.to_string();
let read_rate = item.get("read_bytes_rate_per_sec").and_then(Value::as_f64).unwrap_or(0.0);
let write_rate = item.get("write_bytes_rate_per_sec").and_then(Value::as_f64).unwrap_or(0.0);
let read_latency = item.get("read_latency").and_then(Value::as_f64).unwrap_or(0.0);
let write_latency = item.get("write_latency").and_then(Value::as_f64).unwrap_or(0.0);
Some((name, read_rate, write_rate, read_latency, write_latency))
})
.collect(),
_ => Vec::new(),
};
for (disk_name, read_rate, write_rate, read_latency, write_latency) in items {
let alert_rx = self.model.get_alert(read_rate, 0.0, 100.0, true, false, Some("rx"), Some(&disk_name), false);
let alert_tx = self.model.get_alert(write_rate, 0.0, 100.0, true, false, Some("tx"), Some(&disk_name), false);
for field in ["read_bytes", "read_bytes_rate_per_sec"] {
self.model.set_view_decoration(Some(&disk_name), field, alert_rx);
}
for field in ["write_bytes", "write_bytes_rate_per_sec"] {
self.model.set_view_decoration(Some(&disk_name), field, alert_tx);
}
let mut alert_latency_rx =
self.model.get_alert(read_latency, 0.0, 100.0, true, false, Some("rx_latency"), Some(&disk_name), false);
let mut alert_latency_tx =
self.model.get_alert(write_latency, 0.0, 100.0, true, false, Some("tx_latency"), Some(&disk_name), false);
if alert_latency_rx == Decoration::Default {
alert_latency_rx = self.model.get_alert(read_latency, 0.0, 100.0, true, false, Some("rx_latency"), None, false);
}
if alert_latency_tx == Decoration::Default {
alert_latency_tx = self.model.get_alert(write_latency, 0.0, 100.0, true, false, Some("tx_latency"), None, false);
}
self.model.set_view_decoration(Some(&disk_name), "read_latency", alert_latency_rx);
self.model.set_view_decoration(Some(&disk_name), "write_latency", alert_latency_tx);
}
}
fn display_curse(&self) -> bool {
true
}
}
+120
View File
@@ -0,0 +1,120 @@
//! File system plugin.
use crate::fields;
use glances_core::plugin::{Field, HistoryItem, Plugin, PluginModel, UpdateContext};
use glances_core::{Args, Config};
use serde_json::{Value, json};
pub struct FsPlugin {
pub model: PluginModel,
}
impl FsPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let fields = fields! {
"device_name" => Field::new("Device name."),
"fs_type" => Field::new("File system type."),
"mnt_point" => Field::new("Mount point."),
"options" => Field::new("Mount options."),
"size" => Field::new("Total size.").unit("byte"),
"used" => Field::new("Used size.").unit("byte"),
"free" => Field::new("Free size.").unit("byte"),
"percent" => Field::new("File system usage in percent.").unit("percent"),
};
let items_history = vec![HistoryItem::new("percent", "File system usage in percent")];
let mut model = PluginModel::new(
"fs",
fields,
items_history,
Value::Array(Vec::new()),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
model.set_item_key("mnt_point");
Self { model }
}
}
impl Plugin for FsPlugin {
fn name(&self) -> &str {
"fs"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn get_key(&self) -> Option<&'static str> {
Some("mnt_point")
}
fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<Value> {
let allowed_fs_types = self.model.get_conf_value("allow", "");
let mut stats = Vec::new();
for mount in crate::collectors::read_mounts() {
let is_physical = mount.device.starts_with("/dev/");
let is_allowed = allowed_fs_types.iter().any(|fs_type| mount.fs_type.contains(fs_type.as_str()));
if !is_physical && !is_allowed {
continue;
}
if !self.model.is_display_any(&[&mount.mount_point, &mount.device], "") {
continue;
}
let Some(usage) = crate::collectors::disk_usage(&mount.mount_point) else {
continue;
};
let mnt_point = mount.mount_point.replace('\u{00a0}', " ");
let mut item = serde_json::Map::new();
item.insert("device_name".to_string(), json!(mount.device));
item.insert("fs_type".to_string(), json!(mount.fs_type));
item.insert("mnt_point".to_string(), json!(mnt_point));
item.insert("options".to_string(), json!(mount.options));
item.insert("size".to_string(), json!(usage.total));
item.insert("used".to_string(), json!(usage.used));
item.insert("free".to_string(), json!(usage.free));
item.insert("percent".to_string(), json!((usage.percent * 10.0).round() / 10.0));
item.insert("key".to_string(), json!("mnt_point"));
if let Some(alias) = self.model.has_alias(&mnt_point) {
item.insert("alias".to_string(), json!(alias));
}
stats.push(Value::Object(item));
}
Ok(Value::Array(stats))
}
fn update_views(&mut self) {
let key = self.get_key();
self.model.update_views(key);
let items: Vec<(String, f64, f64, String)> = match &self.model.stats {
Value::Array(list) => list
.iter()
.filter_map(|item| {
let mnt = item.get("mnt_point")?.as_str()?.to_string();
let options = item.get("options").and_then(Value::as_str).unwrap_or("").to_string();
let size = item.get("size")?.as_f64()?;
let free = item.get("free")?.as_f64()?;
Some((mnt, size, free, options))
})
.collect(),
_ => Vec::new(),
};
for (mnt_point, size, free, options) in items {
// Do not display thresholds for volumes mounted in 'ro' (#3143).
if options.split(',').any(|o| o == "ro") {
continue;
}
let decoration = self.model.get_alert(size - free, 0.0, size, true, false, Some(&mnt_point), None, false);
self.model.set_view_decoration(Some(&mnt_point), "used", decoration);
}
}
fn display_curse(&self) -> bool {
true
}
}
+101
View File
@@ -0,0 +1,101 @@
//! IRQ plugin (Linux only).
use crate::fields;
use glances_core::plugin::{Field, Plugin, PluginModel, UpdateContext};
use glances_core::{Args, Config};
use indexmap::IndexMap;
use serde_json::{Value, json};
pub struct IrqPlugin {
pub model: PluginModel,
lasts: IndexMap<String, u64>,
}
impl IrqPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let fields = fields! {
"irq_line" => Field::new("IRQ line name"),
"irq_rate" => Field::new("IRQ rate per second").unit("numberpersecond"),
};
let mut model = PluginModel::new(
"irq",
fields,
Vec::new(),
Value::Array(Vec::new()),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
model.set_item_key("irq_line");
Self { model, lasts: IndexMap::new() }
}
}
impl Plugin for IrqPlugin {
fn name(&self) -> &str {
"irq"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn get_key(&self) -> Option<&'static str> {
Some("irq_line")
}
fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<Value> {
let time_since_update = glances_core::timer::get_time_since_last_update("irq");
let mut stats = Vec::new();
// /proc/interrupts may not exist in containers.
let Ok(content) = std::fs::read_to_string("/proc/interrupts") else {
return Ok(Value::Array(stats));
};
let mut lines = content.lines();
let cpu_number = lines.next().map(|header| header.split_whitespace().count()).unwrap_or(0);
for line in lines {
let fields: Vec<&str> = line.split_whitespace().collect();
if fields.len() < 2 {
continue;
}
let first = fields[0].trim_end_matches(':');
let irq_line = if first.chars().all(|c| c.is_ascii_digit()) {
format!("{first}_{}", fields.last().copied().unwrap_or(""))
} else {
first.to_string()
};
let current: u64 = fields[1..1 + cpu_number].iter().filter_map(|v| v.parse::<u64>().ok()).sum();
let irq_rate = match self.lasts.get(&irq_line) {
Some(last) if *last > 0 && time_since_update > 0.0 => {
((current.saturating_sub(*last)) as f64 / time_since_update) as i64
}
_ => 0,
};
stats.push(json!({
"irq_line": irq_line,
"irq_rate": irq_rate,
"key": "irq_line",
"time_since_update": time_since_update,
}));
self.lasts.insert(irq_line, current);
}
// Keep the top 5 by rate.
stats.sort_by(|a, b| {
b.get("irq_rate")
.and_then(Value::as_i64)
.unwrap_or(0)
.cmp(&a.get("irq_rate").and_then(Value::as_i64).unwrap_or(0))
});
stats.truncate(5);
Ok(Value::Array(stats))
}
fn display_curse(&self) -> bool {
true
}
}
@@ -0,0 +1,81 @@
//! Load average plugin.
use crate::fields;
use glances_core::plugin::{Field, HistoryItem, Plugin, PluginModel, UpdateContext};
use glances_core::{Args, Config};
use serde_json::{Value, json};
pub struct LoadPlugin {
pub model: PluginModel,
}
impl LoadPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let fields = fields! {
"min1" => Field::new("Average sum of the number of processes waiting in the run-queue plus the number currently executing over 1 minute.").unit("float").mmm(),
"min5" => Field::new("Average sum of the number of processes waiting in the run-queue plus the number currently executing over 5 minutes.").unit("float"),
"min15" => Field::new("Average sum of the number of processes waiting in the run-queue plus the number currently executing over 15 minutes.").unit("float"),
"cpucore" => Field::new("Total number of CPU core.").unit("number"),
};
let items_history = vec![
HistoryItem::new("min1", "1 minute load"),
HistoryItem::new("min5", "5 minutes load"),
HistoryItem::new("min15", "15 minutes load"),
];
let model = PluginModel::new(
"load",
fields,
items_history,
json!({}),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
Self { model }
}
}
impl Plugin for LoadPlugin {
fn name(&self) -> &str {
"load"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<Value> {
match crate::collectors::read_loadavg() {
Some((min1, min5, min15)) => Ok(json!({
"min1": min1,
"min5": min5,
"min15": min15,
"cpucore": crate::collectors::logical_core_count(),
})),
None => Ok(json!({})),
}
}
fn update_views(&mut self) {
let key = self.get_key();
self.model.update_views(key);
let max = self.model.stats.get("cpucore").and_then(Value::as_f64).unwrap_or(0.0) * 100.0;
if let Some(min15) = self.model.stats.get("min15").and_then(Value::as_f64) {
let decoration = self.model.get_alert_log(min15, max, None);
self.model.set_view_decoration(None, "min15", decoration);
}
if let Some(min5) = self.model.stats.get("min5").and_then(Value::as_f64) {
let decoration = self.model.get_alert(min5, 0.0, max, true, false, None, None, false);
self.model.set_view_decoration(None, "min5", decoration);
}
}
fn display_curse(&self) -> bool {
true
}
}
+116
View File
@@ -0,0 +1,116 @@
//! Memory plugin.
use crate::fields;
use glances_core::plugin::{Field, HistoryItem, Plugin, PluginModel, UpdateContext};
use glances_core::{Args, Config};
use serde_json::{Value, json};
pub struct MemPlugin {
pub model: PluginModel,
/// Display `available` instead of `used` in the TUI.
pub available: bool,
}
impl MemPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let fields = fields! {
"total" => Field::new("Total physical memory available.").unit("bytes").min_symbol("K"),
"available" => Field::new("The actual amount of available memory that can be given instantly to processes that request more memory in bytes; this is calculated by summing different memory values depending on the platform (e.g. free + buffers + cached on Linux) and it is supposed to be used to monitor actual memory usage in a cross platform fashion.").unit("bytes").min_symbol("K").short_name("avail"),
"percent" => Field::new("The percentage usage calculated as (total - available) / total * 100.").unit("percent").mmm(),
"used" => Field::new("Memory used, calculated differently depending on the platform and designed for informational purposes only.").unit("bytes").min_symbol("K"),
"free" => Field::new("Memory not being used at all (zeroed) that is readily available; note that this doesn't reflect the actual memory available (use 'available' instead).").unit("bytes").min_symbol("K"),
"active" => Field::new("*(UNIX)*: memory currently in use or very recently used, and so it is in RAM.").unit("bytes").min_symbol("K").optional(),
"inactive" => Field::new("*(UNIX)*: memory that is marked as not used.").unit("bytes").min_symbol("K").short_name("inacti").optional(),
"buffers" => Field::new("*(Linux, BSD)*: cache for things like file system metadata.").unit("bytes").min_symbol("K").short_name("buffer").optional(),
"cached" => Field::new("*(Linux, BSD)*: cache for various things (including ZFS cache).").unit("bytes").min_symbol("K").optional(),
"wired" => Field::new("*(BSD, macOS)*: memory that is marked to always stay in RAM. It is never moved to disk.").unit("bytes").min_symbol("K"),
"shared" => Field::new("*(BSD)*: memory that may be simultaneously accessed by multiple processes.").unit("bytes").min_symbol("K"),
};
let items_history = vec![HistoryItem::new("percent", "RAM memory usage")];
let model = PluginModel::new(
"mem",
fields,
items_history,
json!({}),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
let available = model
.get_conf_value("available", "")
.first()
.map(|v| v.to_lowercase() == "true")
.unwrap_or(false);
let mut model = model;
model.mmm_tracked = true;
Self { model, available }
}
}
impl Plugin for MemPlugin {
fn name(&self) -> &str {
"mem"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<Value> {
let meminfo = crate::collectors::read_meminfo();
let get = |key: &str| meminfo.get(key).copied().unwrap_or(0);
let total = get("MemTotal");
let free = get("MemFree");
let buffers = get("Buffers");
// procps-ng "free" sums reclaimable slab to cached.
let cached = get("Cached") + get("SReclaimable");
let shared = if meminfo.contains_key("Shmem") { get("Shmem") } else { get("MemShared") };
let active = get("Active");
let inactive = get("Inactive");
let mut available = get("MemAvailable");
if available == 0 {
// Kernel bug or old kernel: estimate like the "free" tool.
available = free + buffers + cached;
}
if available > total {
// Container with distorted values.
available = free;
}
let used = total.saturating_sub(available);
let percent = if total > 0 {
((used as f64 * 100.0 / total as f64) * 10.0).round() / 10.0
} else {
0.0
};
let mut stats = serde_json::Map::new();
stats.insert("total".to_string(), json!(total));
stats.insert("available".to_string(), json!(available));
stats.insert("percent".to_string(), json!(percent));
stats.insert("used".to_string(), json!(used));
stats.insert("free".to_string(), json!(free));
stats.insert("active".to_string(), json!(active));
stats.insert("inactive".to_string(), json!(inactive));
stats.insert("buffers".to_string(), json!(buffers));
stats.insert("cached".to_string(), json!(cached));
stats.insert("shared".to_string(), json!(shared));
Ok(Value::Object(stats))
}
fn update_views(&mut self) {
let key = self.get_key();
self.model.update_views(key);
let total = self.model.stats.get("total").and_then(Value::as_f64).unwrap_or(0.0);
let used = self.model.stats.get("used").and_then(Value::as_f64).unwrap_or(0.0);
let decoration = self.model.get_alert_log(used, total, None);
self.model.set_view_decoration(None, "percent", decoration);
}
fn display_curse(&self) -> bool {
true
}
}
@@ -0,0 +1,85 @@
//! Swap memory plugin.
use crate::fields;
use glances_core::plugin::{Field, HistoryItem, Plugin, PluginModel, UpdateContext};
use glances_core::{Args, Config};
use serde_json::{Value, json};
pub struct MemSwapPlugin {
pub model: PluginModel,
}
impl MemSwapPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let fields = fields! {
"total" => Field::new("Total swap memory.").unit("bytes").min_symbol("K"),
"used" => Field::new("Used swap memory.").unit("bytes").min_symbol("K"),
"free" => Field::new("Free swap memory.").unit("bytes").min_symbol("K"),
"percent" => Field::new("Used swap memory in percentage.").unit("percent"),
"sin" => Field::new("The number of bytes the system has swapped in from disk (cumulative).").unit("bytes").min_symbol("K"),
"sout" => Field::new("The number of bytes the system has swapped out from disk (cumulative).").unit("bytes").min_symbol("K"),
"time_since_update" => Field::new("Number of seconds since last update.").unit("seconds"),
};
let items_history = vec![HistoryItem::new("percent", "Swap memory usage")];
let model = PluginModel::new(
"memswap",
fields,
items_history,
json!({}),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
Self { model }
}
}
impl Plugin for MemSwapPlugin {
fn name(&self) -> &str {
"memswap"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<Value> {
let meminfo = crate::collectors::read_meminfo();
let total = meminfo.get("SwapTotal").copied().unwrap_or(0);
let free = meminfo.get("SwapFree").copied().unwrap_or(0);
let used = total.saturating_sub(free);
let percent = if total > 0 { (used as f64 * 100.0 / total as f64 * 10.0).round() / 10.0 } else { 0.0 };
let vmstat = crate::collectors::read_vmstat();
// pswpin/pswpout are expressed in 4 KiB pages.
let sin = vmstat.get("pswpin").copied().unwrap_or(0) * 4 * 1024;
let sout = vmstat.get("pswpout").copied().unwrap_or(0) * 4 * 1024;
let time_since_update = glances_core::timer::get_time_since_last_update("memswap");
let mut stats = serde_json::Map::new();
stats.insert("total".to_string(), json!(total));
stats.insert("used".to_string(), json!(used));
stats.insert("free".to_string(), json!(free));
stats.insert("percent".to_string(), json!(percent));
stats.insert("sin".to_string(), json!(sin));
stats.insert("sout".to_string(), json!(sout));
stats.insert("time_since_update".to_string(), json!(time_since_update));
Ok(Value::Object(stats))
}
fn update_views(&mut self) {
let key = self.get_key();
self.model.update_views(key);
let total = self.model.stats.get("total").and_then(Value::as_f64).unwrap_or(0.0);
let used = self.model.stats.get("used").and_then(Value::as_f64).unwrap_or(0.0);
let decoration = self.model.get_alert_log(used, total, None);
self.model.set_view_decoration(None, "percent", decoration);
}
fn display_curse(&self) -> bool {
true
}
}
+46
View File
@@ -0,0 +1,46 @@
//! Plugin implementations.
//!
//! The extended plugins (containers, GPU, ports, ...) are added in a later
//! milestone.
pub mod core;
pub mod cpu;
pub mod diskio;
pub mod fs;
pub mod irq;
pub mod load;
pub mod mem;
pub mod memswap;
pub mod network;
pub mod now;
pub mod percpu;
pub mod processcount;
pub mod processlist;
pub mod programlist;
pub mod psutilversion;
pub mod quicklook;
pub mod sensors;
pub mod system;
pub mod uptime;
pub mod version;
pub use core::CorePlugin;
pub use cpu::CpuPlugin;
pub use diskio::DiskioPlugin;
pub use fs::FsPlugin;
pub use irq::IrqPlugin;
pub use load::LoadPlugin;
pub use mem::MemPlugin;
pub use memswap::MemSwapPlugin;
pub use network::NetworkPlugin;
pub use now::NowPlugin;
pub use percpu::PerCpuPlugin;
pub use processcount::ProcessCountPlugin;
pub use processlist::ProcessListPlugin;
pub use programlist::ProgramListPlugin;
pub use psutilversion::PsutilVersionPlugin;
pub use quicklook::QuicklookPlugin;
pub use sensors::SensorsPlugin;
pub use system::SystemPlugin;
pub use uptime::UptimePlugin;
pub use version::VersionPlugin;
@@ -0,0 +1,139 @@
//! Network plugin.
use crate::collectors;
use crate::fields;
use glances_core::plugin::{Field, HistoryItem, Plugin, PluginModel, UpdateContext};
use glances_core::{Args, Config};
use serde_json::{Value, json};
pub struct NetworkPlugin {
pub model: PluginModel,
hide_no_up: bool,
hide_no_ip: bool,
}
impl NetworkPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let fields = fields! {
"interface_name" => Field::new("Interface name."),
"alias" => Field::new("Interface alias name (optional)."),
"bytes_recv" => Field::new("Number of bytes received.").rate().unit("byte"),
"bytes_sent" => Field::new("Number of bytes sent.").rate().unit("byte"),
"bytes_all" => Field::new("Number of bytes received and sent.").rate().unit("byte"),
"speed" => Field::new("Maximum interface speed (in bit per second). Can return 0 on some operating-system.").unit("bitpersecond"),
"is_up" => Field::new("Is the interface up ?").unit("bool"),
};
let items_history = vec![
HistoryItem::new("bytes_recv_rate_per_sec", "Download rate per second"),
HistoryItem::new("bytes_sent_rate_per_sec", "Upload rate per second"),
];
let mut model = PluginModel::new(
"network",
fields,
items_history,
Value::Array(Vec::new()),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
model.set_item_key("interface_name");
let hide_no_up = config.has_section("network") && config.get_bool_value("network", "hide_no_up", false);
let hide_no_ip = config.has_section("network") && config.get_bool_value("network", "hide_no_ip", false);
Self { model, hide_no_up, hide_no_ip }
}
}
impl Plugin for NetworkPlugin {
fn name(&self) -> &str {
"network"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn get_key(&self) -> Option<&'static str> {
Some("interface_name")
}
fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<Value> {
let net_dev = collectors::read_net_dev();
let mut stats = Vec::new();
for (interface_name, counters) in net_dev {
if !self.model.is_display(&interface_name, "") {
continue;
}
let is_up = collectors::interface_flags(&interface_name) & libc::IFF_UP as u64 != 0;
if self.hide_no_up && !is_up {
continue;
}
if self.hide_no_ip && !collectors::interface_has_ip(&interface_name) {
continue;
}
let speed = collectors::interface_speed(&interface_name);
let alias = self.model.has_alias(&interface_name);
let mut item = serde_json::Map::new();
item.insert("bytes_sent".to_string(), json!(counters.bytes_sent));
item.insert("bytes_recv".to_string(), json!(counters.bytes_recv));
// Note: psutil's field is `isup`, which does not match the
// `is_up` fields description, so the Python implementation drops
// it from the stats (the value is only used to filter interfaces).
item.insert("speed".to_string(), json!(speed * 1048576));
item.insert("key".to_string(), json!("interface_name"));
item.insert("interface_name".to_string(), json!(interface_name));
item.insert("alias".to_string(), json!(alias));
item.insert("bytes_all".to_string(), json!(counters.bytes_sent + counters.bytes_recv));
stats.push(Value::Object(item));
}
Ok(Value::Array(stats))
}
fn update_views(&mut self) {
let key = self.get_key();
self.model.update_views(key);
let items: Vec<(String, f64, f64, f64, Option<String>)> = match &self.model.stats {
Value::Array(list) => list
.iter()
.filter_map(|item| {
let name = item.get("interface_name")?.as_str()?.to_string();
let rx = item.get("bytes_recv_rate_per_sec").and_then(Value::as_f64).unwrap_or(0.0);
let tx = item.get("bytes_sent_rate_per_sec").and_then(Value::as_f64).unwrap_or(0.0);
let speed = item.get("speed").and_then(Value::as_f64).unwrap_or(0.0);
let real_name = name.split(':').next().unwrap_or("").to_string();
Some((name, rx, tx, speed, Some(real_name)))
})
.collect(),
_ => Vec::new(),
};
for (interface_name, rx, tx, speed, action_key) in items {
let bps_rx = rx as i64 * 8;
let bps_tx = tx as i64 * 8;
let action_key = action_key.as_deref();
let mut alert_rx =
self.model.get_alert(bps_rx as f64, 0.0, 100.0, true, false, Some("rx"), action_key, false);
let mut alert_tx =
self.model.get_alert(bps_tx as f64, 0.0, 100.0, true, false, Some("tx"), action_key, false);
if alert_rx == glances_core::Decoration::Default && speed != 0.0 {
alert_rx = self.model.get_alert(bps_rx as f64, 0.0, speed, true, false, Some("rx"), None, false);
}
if alert_tx == glances_core::Decoration::Default && speed != 0.0 {
alert_tx = self.model.get_alert(bps_tx as f64, 0.0, speed, true, false, Some("tx"), None, false);
}
for field in ["bytes_recv", "bytes_recv_rate_per_sec"] {
self.model.set_view_decoration(Some(&interface_name), field, alert_rx);
}
for field in ["bytes_sent", "bytes_sent_rate_per_sec"] {
self.model.set_view_decoration(Some(&interface_name), field, alert_tx);
}
}
}
fn display_curse(&self) -> bool {
true
}
}
+70
View File
@@ -0,0 +1,70 @@
//! Current date/time plugin.
use crate::fields;
use glances_core::plugin::{Field, Plugin, PluginModel, UpdateContext};
use glances_core::{Args, Config};
use serde_json::{Value, json};
pub struct NowPlugin {
pub model: PluginModel,
}
impl NowPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let fields = fields! {
"custom" => Field::new("Current date in custom format."),
"iso" => Field::new("Current date in ISO 8601 format."),
};
let model = PluginModel::new(
"now",
fields,
Vec::new(),
json!({}),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
Self { model }
}
}
impl Plugin for NowPlugin {
fn name(&self) -> &str {
"now"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn collect(&mut self, ctx: &mut UpdateContext) -> anyhow::Result<Value> {
let now = chrono::Local::now();
let iso = now.format("%Y-%m-%dT%H:%M:%S%:z").to_string();
let strftime = if !ctx.args.strftime_format.is_empty() {
ctx.args.strftime_format.clone()
} else {
ctx.config.get_value("global", "strftime_format", Some("")).unwrap_or_default()
};
let custom = if !strftime.is_empty() {
now.format(&strftime).to_string()
} else {
// Choose the tz format depending on the timezone name length.
let tz_name = now.format("%Z").to_string();
if tz_name.len() > 6 {
now.format("%Y-%m-%d %H:%M:%S %z").to_string()
} else {
now.format("%Y-%m-%d %H:%M:%S %Z").to_string()
}
};
Ok(json!({"iso": iso, "custom": custom}))
}
fn display_curse(&self) -> bool {
true
}
}
@@ -0,0 +1,67 @@
//! Per-CPU plugin.
use crate::cpu_percent::cpu_percent;
use crate::fields;
use glances_core::plugin::{Field, Plugin, PluginModel, UpdateContext};
use glances_core::{Args, Config};
use serde_json::Value;
pub struct PerCpuPlugin {
pub model: PluginModel,
}
impl PerCpuPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let fields = fields! {
"cpu_number" => Field::new("CPU number"),
"total" => Field::new("Sum of CPU percentages (except idle) for current CPU number.").unit("percent"),
"system" => Field::new("Percent time spent in kernel space. System CPU time is the time spent running code in the Operating System kernel.").unit("percent"),
"user" => Field::new("CPU percent time spent in user space. User CPU time is the time spent on the processor running your program's code (or code in libraries).").unit("percent"),
"iowait" => Field::new("*(Linux)*: percent time spent by the CPU waiting for I/O operations to complete.").unit("percent"),
"idle" => Field::new("percent of CPU used by any program. Every program or task that runs on a computer system occupies a certain amount of processing time on the CPU. If the CPU has completed all tasks it is idle.").unit("percent"),
"irq" => Field::new("*(Linux and BSD)*: percent time spent servicing/handling hardware/software interrupts. Time servicing interrupts (hardware + software).").unit("percent"),
"nice" => Field::new("*(Unix)*: percent time occupied by user level processes with a positive nice value. The time the CPU has spent running users' processes that are *niced*.").unit("percent"),
"steal" => Field::new("*(Linux)*: percentage of time a virtual CPU waits for a real CPU while the hypervisor is servicing another virtual processor.").unit("percent"),
"guest" => Field::new("*(Linux)*: percent of time spent running a virtual CPU for guest operating systems under the control of the Linux kernel.").unit("percent"),
"guest_nice" => Field::new("*(Linux)*: percent of time spent running a niced guest (virtual CPU).").unit("percent"),
"softirq" => Field::new("*(Linux)*: percent of time spent handling software interrupts.").unit("percent"),
"dpc" => Field::new("*(Windows)*: percent of time spent handling deferred procedure calls.").unit("percent"),
"interrupt" => Field::new("*(Windows)*: percent of time spent handling software interrupts.").unit("percent"),
};
let mut model = PluginModel::new(
"percpu",
fields,
Vec::new(),
Value::Array(Vec::new()),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
model.set_item_key("cpu_number");
Self { model }
}
}
impl Plugin for PerCpuPlugin {
fn name(&self) -> &str {
"percpu"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn get_key(&self) -> Option<&'static str> {
Some("cpu_number")
}
fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<Value> {
let mut state = cpu_percent().lock().unwrap();
Ok(Value::Array(state.get_percpu().to_vec()))
}
}
@@ -0,0 +1,72 @@
//! Process count plugin. It is also in charge of updating the shared
//! process list (port of `glances.plugins.processcount`).
use crate::fields;
use glances_core::plugin::{Field, HistoryItem, Plugin, PluginModel, UpdateContext};
use glances_core::processes::processes;
use glances_core::{Args, Config};
use serde_json::{Value, json};
pub struct ProcessCountPlugin {
pub model: PluginModel,
}
impl ProcessCountPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let fields = fields! {
"total" => Field::new("Total number of processes").unit("number"),
"running" => Field::new("Total number of running processes").unit("number"),
"sleeping" => Field::new("Total number of sleeping processes").unit("number"),
"thread" => Field::new("Total number of threads").unit("number"),
"pid_max" => Field::new("Maximum PID number").unit("number"),
};
let items_history = vec![
HistoryItem::new("total", "Total number of processes"),
HistoryItem::new("running", "Total number of running processes"),
HistoryItem::new("sleeping", "Total number of sleeping processes"),
HistoryItem::new("thread", "Total number of threads"),
];
let model = PluginModel::new(
"processcount",
fields,
items_history,
json!({"total": 0, "running": 0, "sleeping": 0, "thread": 0, "pid_max": null}),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
Self { model }
}
}
impl Plugin for ProcessCountPlugin {
fn name(&self) -> &str {
"processcount"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn collect(&mut self, ctx: &mut UpdateContext) -> anyhow::Result<Value> {
let mut procs = processes().lock().unwrap();
procs.update(ctx.cursor_position, ctx.args);
let count = &procs.processcount;
Ok(json!({
"total": count.total,
"running": count.running,
"sleeping": count.sleeping,
"thread": count.thread,
"pid_max": count.pid_max,
}))
}
fn display_curse(&self) -> bool {
true
}
}
@@ -0,0 +1,115 @@
//! Process list plugin. The stats are collected by the processcount plugin.
use crate::fields;
use glances_core::plugin::{Field, Plugin, PluginModel, UpdateContext};
use glances_core::processes::processes;
use glances_core::{Args, Config};
use serde_json::Value;
pub struct ProcessListPlugin {
pub model: PluginModel,
}
impl ProcessListPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let fields = fields! {
"pid" => Field::new("Process identifier (ID)").unit("number"),
"name" => Field::new("Process name").unit("string"),
"cmdline" => Field::new("Command line with arguments").unit("list"),
"username" => Field::new("Process owner").unit("string"),
"num_threads" => Field::new("Number of threads").unit("number"),
"cpu_percent" => Field::new("Process CPU consumption (returned value can be > 100.0 in case of a process running multiple threads on different CPU cores)").unit("percent"),
"memory_percent" => Field::new("Process memory consumption").unit("percent"),
"memory_info" => Field::new("Process memory information (dict with rss, vms, shared, text, lib, data, dirty keys)").unit("byte"),
"status" => Field::new("Process status").unit("string"),
"nice" => Field::new("Process nice value").unit("number"),
"cpu_times" => Field::new("Process CPU times (dict with user, system, iowait keys)").unit("second"),
"gids" => Field::new("Process group IDs (dict with real, effective, saved keys)").unit("number"),
"io_counters" => Field::new("Process IO counters (list with read_count, write_count, read_bytes, write_bytes, io_tag keys)").unit("byte"),
"cpu_num" => Field::new("CPU core number where the process is currently executing (0-based indexing)").unit("number"),
};
let mut model = PluginModel::new(
"processlist",
fields,
Vec::new(),
Value::Array(Vec::new()),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
model.set_item_key("pid");
// Load the configuration.
if config.has_option("processlist", "sort_key") {
if let Some(sort_key) = config.get_value("processlist", "sort_key", None) {
let mut procs = processes().lock().unwrap();
procs.set_sort_key(&sort_key, false);
}
}
if config.has_option("processlist", "focus") {
if let Some(focus) = config.get_value("processlist", "focus", None) {
processes().lock().unwrap().filter_focus.set_filter(Some(&focus));
}
}
if config.has_option("processlist", "disable_stats") {
if let Some(disabled) = config.get_value("processlist", "disable_stats", None) {
let stats: Vec<String> = disabled
.split(',')
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty() && !glances_core::processes::MANDATORY_PROCESS_STATS.contains(&s.as_str()))
.collect();
processes().lock().unwrap().disable_stats = stats;
}
}
if let Some(sort_key) = args.sort_processes_key {
processes().lock().unwrap().set_sort_key(sort_key.as_str(), false);
}
Self { model }
}
}
impl Plugin for ProcessListPlugin {
fn name(&self) -> &str {
"processlist"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn get_key(&self) -> Option<&'static str> {
Some("pid")
}
fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<Value> {
// The list is updated by the processcount plugin.
let procs = processes().lock().unwrap();
Ok(Value::Array(procs.get_list(false).to_vec()))
}
fn update_views(&mut self) {
// The views are built lazily (see the Python implementation): the UI
// computes its own decorations.
self.model.reset_views();
}
fn export(&self) -> Value {
Value::Array(processes().lock().unwrap().get_export().to_vec())
}
fn display_curse(&self) -> bool {
true
}
fn align(&self) -> glances_core::Align {
glances_core::Align::Bottom
}
}
#[allow(dead_code)]
fn unused(_: Value) {}
@@ -0,0 +1,70 @@
//! Program list plugin (processes aggregated by program name).
use crate::fields;
use glances_core::plugin::{Field, Plugin, PluginModel, UpdateContext};
use glances_core::processes::processes;
use glances_core::{Args, Config};
use serde_json::Value;
pub struct ProgramListPlugin {
pub model: PluginModel,
}
impl ProgramListPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let fields = fields! {
"name" => Field::new("Process name").unit("string"),
"cmdline" => Field::new("Command line with arguments").unit("list"),
"username" => Field::new("Process owner").unit("string"),
"nprocs" => Field::new("Number of children processes").unit("number"),
"num_threads" => Field::new("Number of threads").unit("number"),
"cpu_percent" => Field::new("Process CPU consumption").unit("percent"),
"memory_percent" => Field::new("Process memory consumption").unit("percent"),
"memory_info" => Field::new("Process memory information (dict with rss, vms, shared, text, lib, data, dirty keys)").unit("byte"),
"status" => Field::new("Process status").unit("string"),
"nice" => Field::new("Process nice value").unit("number"),
"cpu_times" => Field::new("Process CPU times (dict with user, system, iowait keys)").unit("second"),
"gids" => Field::new("Process group IDs (dict with real, effective, saved keys)").unit("number"),
"io_counters" => Field::new("Process IO counters (list with read_count, write_count, read_bytes, write_bytes, io_tag keys)").unit("byte"),
};
let mut model = PluginModel::new(
"programlist",
fields,
Vec::new(),
Value::Array(Vec::new()),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
model.set_item_key("name");
Self { model }
}
}
impl Plugin for ProgramListPlugin {
fn name(&self) -> &str {
"programlist"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn get_key(&self) -> Option<&'static str> {
Some("name")
}
fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<Value> {
let mut procs = processes().lock().unwrap();
Ok(Value::Array(procs.get_list_as_programs().to_vec()))
}
fn display_curse(&self) -> bool {
true
}
}
@@ -0,0 +1,47 @@
//! Psutil version plugin.
//!
//! The Rust rewrite does not use psutil; the plugin is kept for drop-in
//! compatibility of the stats schema and reports "N/A".
use crate::fields;
use glances_core::plugin::{Plugin, PluginModel, UpdateContext};
use glances_core::{Args, Config};
use serde_json::{Value, json};
pub struct PsutilVersionPlugin {
pub model: PluginModel,
}
impl PsutilVersionPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let model = PluginModel::new(
"psutilversion",
fields! {},
Vec::new(),
Value::Null,
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
Self { model }
}
}
impl Plugin for PsutilVersionPlugin {
fn name(&self) -> &str {
"psutilversion"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<Value> {
Ok(json!("N/A"))
}
}
@@ -0,0 +1,183 @@
//! Quicklook plugin (condensed stats displayed in the top-left corner).
use crate::cpu_percent::cpu_percent;
use crate::fields;
use glances_core::plugin::{Decoration, Field, HistoryItem, Plugin, PluginModel, UpdateContext};
use glances_core::{Args, Config};
use serde_json::{Value, json};
pub struct QuicklookPlugin {
pub model: PluginModel,
pub stats_list: Vec<String>,
pub max_cpu_display: usize,
}
const AVAILABLE_STATS_LIST: [&str; 6] = ["cpu", "mem", "swap", "load", "gpu_mem", "gpu_proc"];
const DEFAULT_STATS_LIST: [&str; 3] = ["cpu", "mem", "load"];
impl QuicklookPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let fields = fields! {
"cpu" => Field::new("CPU percent usage").unit("percent"),
"percpu" => Field::new("PERCPU percent usage").unit("percent"),
"mem" => Field::new("MEM percent usage").unit("percent"),
"swap" => Field::new("SWAP percent usage").unit("percent"),
"load" => Field::new("LOAD percent usage").unit("percent").alert(),
"cpu_log_core" => Field::new("Number of logical CPU core").unit("number"),
"cpu_phys_core" => Field::new("Number of physical CPU core").unit("number"),
"cpu_name" => Field::new("CPU name"),
"cpu_hz_current" => Field::new("CPU current frequency").unit("hertz"),
"cpu_hz" => Field::new("CPU max frequency").unit("hertz"),
"gpu_mem" => Field::new("Average GPU Memory consumption").unit("percent"),
"gpu_proc" => Field::new("Average GPU Processor consumption").unit("percent"),
};
let items_history = vec![
HistoryItem::new("cpu", "CPU percent usage"),
HistoryItem::new("percpu", "PERCPU percent usage"),
HistoryItem::new("mem", "MEM percent usage"),
HistoryItem::new("swap", "SWAP percent usage"),
HistoryItem::new("load", "LOAD percent usage"),
HistoryItem::new("gpu_mem", "GPU memory percent usage"),
HistoryItem::new("gpu_proc", "GPU processor percent usage"),
];
let model = PluginModel::new(
"quicklook",
fields,
items_history,
json!({}),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
let stats_list = model.get_conf_value("list", "");
let mut stats_list: Vec<String> =
if stats_list.is_empty() { DEFAULT_STATS_LIST.iter().map(|s| s.to_string()).collect() } else { stats_list };
if stats_list.iter().any(|s| !AVAILABLE_STATS_LIST.contains(&s.as_str())) {
stats_list = DEFAULT_STATS_LIST.iter().map(|s| s.to_string()).collect();
}
let max_cpu_display = config.get_int_value("percpu", "max_cpu_display", 4).max(0) as usize;
Self { model, stats_list, max_cpu_display }
}
fn mhz_to_hz(value: Option<f64>) -> Option<f64> {
value.map(|v| v * 1_000_000.0)
}
}
impl Plugin for QuicklookPlugin {
fn name(&self) -> &str {
"quicklook"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<Value> {
let mut stats = serde_json::Map::new();
let (info, cpu_value, percpu_values) = {
let mut state = cpu_percent().lock().unwrap();
let info = state.get_info();
let cpu = state.get_cpu();
let percpu = state.get_percpu().to_vec();
(info, cpu, percpu)
};
stats.insert("cpu_name".to_string(), json!(info.cpu_name));
stats.insert("cpu_hz_current".to_string(), json!(Self::mhz_to_hz(info.cpu_hz_current)));
stats.insert("cpu_hz".to_string(), json!(Self::mhz_to_hz(info.cpu_hz)));
stats.insert("cpu".to_string(), json!(cpu_value));
stats.insert("percpu".to_string(), Value::Array(percpu_values));
let meminfo = crate::collectors::read_meminfo();
let total = meminfo.get("MemTotal").copied().unwrap_or(0) as f64;
let available = meminfo.get("MemAvailable").copied().unwrap_or(0) as f64;
stats.insert(
"mem".to_string(),
json!(if total > 0.0 { ((total - available) / total * 100.0 * 10.0).round() / 10.0 } else { 0.0 }),
);
let swap_total = meminfo.get("SwapTotal").copied().unwrap_or(0);
let swap_free = meminfo.get("SwapFree").copied().unwrap_or(0);
stats.insert(
"swap".to_string(),
json!(if swap_total > 0 {
(((swap_total - swap_free) as f64 * 100.0 / swap_total as f64) * 10.0).round() / 10.0
} else {
0.0
}),
);
let log_core = crate::collectors::logical_core_count();
stats.insert("cpu_log_core".to_string(), json!(log_core));
stats.insert("cpu_phys_core".to_string(), json!(crate::collectors::physical_core_count()));
let load = crate::collectors::read_loadavg().map(|(_, _, min15)| {
if log_core > 0 { min15 / log_core as f64 * 100.0 } else { 0.0 }
});
stats.insert("load".to_string(), json!(load));
stats.insert("gpu_mem".to_string(), Value::Null);
stats.insert("gpu_proc".to_string(), Value::Null);
Ok(Value::Object(stats))
}
fn update_views(&mut self) {
let key = self.get_key();
self.model.update_views(key);
let stats_list = self.stats_list.clone();
for stat_key in &stats_list {
if let Some(value) = self.model.stats.get(stat_key).and_then(Value::as_f64) {
let decoration =
self.model.get_alert(value, 0.0, 100.0, true, false, Some(stat_key), None, false);
self.model.set_view_decoration(None, stat_key, decoration);
}
}
// Store the list of stats to display and the per-CPU decorations.
self.model
.views
.insert("list".to_string(), json!(stats_list));
let mut percpu_decoration = serde_json::Map::new();
if let Some(percpu) = self.model.stats.get("percpu").and_then(Value::as_array) {
for cpu in percpu {
let number = cpu.get("cpu_number").map(glances_core::py_str).unwrap_or_default();
let total = cpu.get("total").and_then(Value::as_f64).unwrap_or(0.0);
percpu_decoration.insert(number, json!(decoration_for_percent(&self.model, total).name()));
}
if percpu.len() > self.max_cpu_display && self.max_cpu_display > 0 {
let mut hidden: Vec<f64> =
percpu.iter().filter_map(|cpu| cpu.get("total").and_then(Value::as_f64)).collect();
hidden.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
let hidden = &hidden[self.max_cpu_display.min(hidden.len())..];
if !hidden.is_empty() {
let mean = hidden.iter().sum::<f64>() / hidden.len() as f64;
percpu_decoration.insert("other".to_string(), json!(decoration_for_percent(&self.model, mean).name()));
}
}
}
self.model.views.insert("percpu_decoration".to_string(), Value::Object(percpu_decoration));
}
fn display_curse(&self) -> bool {
true
}
}
/// Return the style for a CPU percentage without the get_alert side effects.
fn decoration_for_percent(model: &PluginModel, percent: f64) -> Decoration {
let stat_name = model.get_stat_name(Some("cpu"), None).to_lowercase();
let critical = model.get_limit("critical", &stat_name).unwrap_or(0.0);
let warning = model.get_limit("warning", &stat_name).unwrap_or(0.0);
let careful = model.get_limit("careful", &stat_name).unwrap_or(0.0);
if critical > 0.0 && percent >= critical {
Decoration::Critical
} else if warning > 0.0 && percent >= warning {
Decoration::Warning
} else if careful > 0.0 && percent >= careful {
Decoration::Careful
} else {
Decoration::Default
}
}
@@ -0,0 +1,225 @@
//! Sensors plugin (temperatures, fans, batteries).
use crate::fields;
use glances_core::plugin::{Field, Plugin, PluginModel, UpdateContext};
use glances_core::{Args, Config, Decoration};
use serde_json::{Value, json};
pub struct SensorsPlugin {
pub model: PluginModel,
}
impl SensorsPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let fields = fields! {
"label" => Field::new("Sensor label"),
"unit" => Field::new("Sensor unit"),
"value" => Field::new("Sensor value").unit("number"),
"warning" => Field::new("System warning threshold (regardless of the user configuration in the glances.conf file)").unit("number"),
"critical" => Field::new("System critical threshold (regardless of the user configuration in the glances.conf file)").unit("number"),
"type" => Field::new("Sensor type (one of battery, temperature_core, fan_speed)"),
};
let mut model = PluginModel::new(
"sensors",
fields,
Vec::new(),
Value::Array(Vec::new()),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
model.set_item_key("label");
Self { model }
}
}
/// Read all hwmon sensors.
fn read_hwmon() -> Vec<Value> {
let mut stats = Vec::new();
let Ok(entries) = std::fs::read_dir("/sys/class/hwmon") else {
return stats;
};
for entry in entries.flatten() {
let dir = entry.path();
let Ok(files) = std::fs::read_dir(&dir) else {
continue;
};
for file in files.flatten() {
let name = file.file_name().to_string_lossy().to_string();
let path = file.path();
// Temperatures (millidegrees).
if let Some(rest) = name.strip_prefix("temp") {
if let Some(rest) = rest.strip_suffix("_input") {
let Some(raw) = read_value(&path) else {
continue;
};
let value = raw / 1000.0;
let label = read_label(&dir, &format!("temp{rest}_label"), &format!("temp{rest}"));
let warning = read_label_value(&dir, &format!("temp{rest}_max")).map(|v| v / 1000.0);
let critical = read_label_value(&dir, &format!("temp{rest}_crit")).map(|v| v / 1000.0);
stats.push(sensor_item(&label, "C", value, warning, critical, "temperature_core"));
}
}
// Fans (RPM).
if let Some(rest) = name.strip_prefix("fan") {
if let Some(rest) = rest.strip_suffix("_input") {
let Some(value) = read_value(&path) else {
continue;
};
let label = read_label(&dir, &format!("fan{rest}_label"), &format!("fan{rest}"));
stats.push(sensor_item(&label, "R", value, None, None, "fan_speed"));
}
}
}
}
stats
}
/// Read battery information from `/sys/class/power_supply`.
fn read_batteries() -> Vec<Value> {
let mut stats = Vec::new();
let Ok(entries) = std::fs::read_dir("/sys/class/power_supply") else {
return stats;
};
let mut batteries: Vec<_> = entries.flatten().collect();
batteries.sort_by_key(|e| e.file_name());
for entry in batteries {
let dir = entry.path();
let name = entry.file_name().to_string_lossy().to_string();
let is_battery = name.starts_with("BAT")
|| read_string(&dir.join("type")).map(|t| t == "Battery").unwrap_or(false);
if !is_battery {
continue;
}
let Some(value) = read_value(&dir.join("capacity")) else {
continue;
};
let mut item = sensor_item("Battery", "%", value, None, None, "battery");
if let Some(status) = read_string(&dir.join("status")) {
item.as_object_mut().unwrap().insert("status".to_string(), json!(status));
}
stats.push(item);
}
stats
}
fn sensor_item(label: &str, unit: &str, value: f64, warning: Option<f64>, critical: Option<f64>, r#type: &str) -> Value {
json!({
"label": label,
"unit": unit,
"value": value,
"warning": warning,
"critical": critical,
"type": r#type,
"key": "label",
})
}
fn read_string(path: &std::path::Path) -> Option<String> {
std::fs::read_to_string(path).ok().map(|v| v.trim().to_string())
}
fn read_value(path: &std::path::Path) -> Option<f64> {
read_string(path).and_then(|v| v.parse().ok())
}
fn read_label_value(dir: &std::path::Path, file: &str) -> Option<f64> {
read_value(&dir.join(file))
}
fn read_label(dir: &std::path::Path, file: &str, default: &str) -> String {
read_string(&dir.join(file)).unwrap_or_else(|| default.to_string())
}
impl Plugin for SensorsPlugin {
fn name(&self) -> &str {
"sensors"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn get_key(&self) -> Option<&'static str> {
Some("label")
}
fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<Value> {
let mut stats = read_hwmon();
stats.extend(read_batteries());
// Apply show/hide filters and aliases.
stats.retain(|stat| {
let label = stat.get("label").and_then(Value::as_str).unwrap_or("").to_lowercase();
self.model.is_display(&label, "")
});
for stat in stats.iter_mut() {
if let (Some(label), Some(r#type)) = (
stat.get("label").and_then(Value::as_str).map(|s| s.to_lowercase()),
stat.get("type").and_then(Value::as_str).map(|s| s.to_lowercase()),
) {
if let Some(alias) = self.model.has_alias(&format!("{}_{}", label, r#type)) {
stat.as_object_mut().unwrap().insert("label".to_string(), json!(alias));
} else if let Some(alias) = self.model.has_alias(&label) {
stat.as_object_mut().unwrap().insert("label".to_string(), json!(alias));
}
}
}
// Sort by label.
stats.sort_by_key(|stat| stat.get("label").and_then(Value::as_str).unwrap_or("").to_lowercase());
Ok(Value::Array(stats))
}
fn update_views(&mut self) {
let key = self.get_key();
self.model.update_views(key);
let items: Vec<(String, String, f64, Option<f64>, Option<f64>)> = match &self.model.stats {
Value::Array(list) => list
.iter()
.filter_map(|item| {
let label = item.get("label")?.as_str()?.to_string();
let r#type = item.get("type")?.as_str()?.to_string();
let value = item.get("value")?.as_f64()?;
let warning = item.get("warning").and_then(Value::as_f64);
let critical = item.get("critical").and_then(Value::as_f64);
Some((label, r#type, value, warning, critical))
})
.collect(),
_ => Vec::new(),
};
for (label, r#type, value, warning, critical) in items {
let decoration = if r#type == "temperature_core" {
if warning.is_some() || critical.is_some() {
system_thresholds(value, warning, critical)
} else {
self.model.get_alert(value, 0.0, 100.0, true, false, Some(&label), Some(&r#type), false)
}
} else if r#type == "battery" {
self.model.get_alert(100.0 - value, 0.0, 100.0, true, false, Some(&r#type), None, false)
} else {
self.model.get_alert(value, 0.0, 100.0, true, false, Some(&r#type), None, false)
};
self.model.set_view_decoration(Some(&label), "value", decoration);
}
}
fn display_curse(&self) -> bool {
true
}
}
fn system_thresholds(value: f64, warning: Option<f64>, critical: Option<f64>) -> Decoration {
match critical {
None => Decoration::Default,
Some(critical) if value >= critical => Decoration::Critical,
Some(_) => match warning {
None => Decoration::Default,
Some(warning) if value >= warning => Decoration::Warning,
Some(_) => Decoration::Ok,
},
}
}
@@ -0,0 +1,85 @@
//! Host/system information plugin.
use crate::fields;
use glances_core::plugin::{Field, Plugin, PluginModel, UpdateContext};
use glances_core::{Args, Config};
use serde_json::{Value, json};
pub struct SystemPlugin {
pub model: PluginModel,
}
impl SystemPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let fields = fields! {
"os_name" => Field::new("Operating system name"),
"hostname" => Field::new("Hostname"),
"platform" => Field::new("Platform (32 or 64 bits)"),
"linux_distro" => Field::new("Linux distribution"),
"os_version" => Field::new("Operating system version"),
"hr_name" => Field::new("Human readable operating system name"),
};
let model = PluginModel::new(
"system",
fields,
Vec::new(),
json!({}),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
Self { model }
}
}
impl Plugin for SystemPlugin {
fn name(&self) -> &str {
"system"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<Value> {
let os_name = "Linux";
let hostname = crate::collectors::hostname();
let platform = format!("{}bit", std::mem::size_of::<usize>() * 8);
let os_version = std::fs::read_to_string("/proc/sys/kernel/osrelease")
.map(|v| v.trim().to_string())
.unwrap_or_default();
let (distro_name, distro_version) = crate::collectors::read_os_release();
let linux_distro = if distro_version.is_empty() {
distro_name.clone()
} else {
format!("{distro_name} {distro_version}")
};
let hr_name = format!("{linux_distro} {platform} / {os_name} {os_version}");
Ok(json!({
"os_name": os_name,
"hostname": hostname,
"platform": platform,
"linux_distro": linux_distro,
"os_version": os_version,
"hr_name": hr_name,
}))
}
fn display_curse(&self) -> bool {
true
}
fn align(&self) -> glances_core::Align {
glances_core::Align::Right
}
}
/// Logical core count helper shared by the load plugin.
pub fn log_core() -> usize {
crate::collectors::logical_core_count()
}
@@ -0,0 +1,70 @@
//! Uptime plugin.
use crate::fields;
use glances_core::plugin::{Plugin, PluginModel, UpdateContext};
use glances_core::{Args, Config};
use serde_json::{Value, json};
pub struct UptimePlugin {
pub model: PluginModel,
}
impl UptimePlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let model = PluginModel::new(
"uptime",
fields! {},
Vec::new(),
Value::String(String::new()),
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
Self { model }
}
}
/// Format a duration in seconds the way Python's `str(timedelta)` does.
pub fn format_uptime(seconds: f64) -> String {
let total = seconds.max(0.0) as i64;
let days = total / 86400;
let hours = (total % 86400) / 3600;
let minutes = (total % 3600) / 60;
let secs = total % 60;
if days > 0 {
format!(
"{} day{}, {}:{:02}:{:02}",
days,
if days > 1 { "s" } else { "" },
hours,
minutes,
secs
)
} else {
format!("{hours}:{minutes:02}:{secs:02}")
}
}
impl Plugin for UptimePlugin {
fn name(&self) -> &str {
"uptime"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<Value> {
let uptime = crate::collectors::uptime_seconds().unwrap_or(0.0);
Ok(json!(format_uptime(uptime)))
}
fn display_curse(&self) -> bool {
true
}
}
@@ -0,0 +1,48 @@
//! Glances version plugin.
use crate::fields;
use glances_core::plugin::{Plugin, PluginModel, UpdateContext};
use glances_core::{Args, Config};
use serde_json::{Value, json};
/// Version reported to be compatible with the Python Glances implementation
/// this rewrite is based on.
pub const COMPAT_VERSION: &str = "4.5.7";
pub struct VersionPlugin {
pub model: PluginModel,
}
impl VersionPlugin {
pub fn new(config: &Config, args: &Args) -> Self {
let model = PluginModel::new(
"version",
fields! {},
Vec::new(),
Value::Null,
config,
args.time.unwrap_or(2.0),
args.disable_history,
args.disable_config_exec,
);
Self { model }
}
}
impl Plugin for VersionPlugin {
fn name(&self) -> &str {
"version"
}
fn model(&self) -> &PluginModel {
&self.model
}
fn model_mut(&mut self) -> &mut PluginModel {
&mut self.model
}
fn collect(&mut self, _ctx: &mut UpdateContext) -> anyhow::Result<Value> {
Ok(json!(COMPAT_VERSION))
}
}