nushell/crates/nu-command/src/system/sys.rs

361 lines
8.8 KiB
Rust
Raw Normal View History

2021-10-01 08:53:47 +02:00
use nu_protocol::{
ast::Call,
2021-10-25 08:31:39 +02:00
engine::{Command, EngineState, EvaluationContext, Stack},
2021-10-25 06:01:02 +02:00
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Value,
2021-10-01 08:53:47 +02:00
};
use sysinfo::{ComponentExt, DiskExt, NetworkExt, ProcessorExt, System, SystemExt, UserExt};
2021-10-25 06:01:02 +02:00
#[derive(Clone)]
2021-10-01 08:53:47 +02:00
pub struct Sys;
impl Command for Sys {
fn name(&self) -> &str {
"sys"
}
fn signature(&self) -> Signature {
Signature::build("sys")
.desc("View information about the current system.")
.filter()
}
fn usage(&self) -> &str {
"View information about the system."
}
fn run(
&self,
2021-10-25 08:31:39 +02:00
_engine_state: &EngineState,
_stack: &mut Stack,
2021-10-01 08:53:47 +02:00
call: &Call,
2021-10-25 06:01:02 +02:00
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
2021-10-01 08:53:47 +02:00
run_sys(call)
}
2021-10-01 23:53:13 +02:00
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Show info about the system",
example: "sys",
result: None,
}]
}
2021-10-01 08:53:47 +02:00
}
2021-10-25 06:01:02 +02:00
fn run_sys(call: &Call) -> Result<PipelineData, ShellError> {
2021-10-01 08:53:47 +02:00
let span = call.head;
let mut sys = System::new();
let mut headers = vec![];
let mut values = vec![];
if let Some(value) = host(&mut sys, span) {
headers.push("host".into());
values.push(value);
}
if let Some(value) = cpu(&mut sys, span) {
headers.push("cpu".into());
values.push(value);
}
if let Some(value) = disks(&mut sys, span) {
headers.push("disks".into());
values.push(value);
}
if let Some(value) = mem(&mut sys, span) {
headers.push("mem".into());
values.push(value);
}
if let Some(value) = temp(&mut sys, span) {
headers.push("temp".into());
values.push(value);
}
if let Some(value) = net(&mut sys, span) {
headers.push("net".into());
values.push(value);
}
Ok(Value::Record {
cols: headers,
vals: values,
span,
2021-10-25 06:01:02 +02:00
}
.into_pipeline_data())
2021-10-01 08:53:47 +02:00
}
pub fn trim_cstyle_null(s: String) -> String {
s.trim_matches(char::from(0)).to_string()
}
pub fn disks(sys: &mut System, span: Span) -> Option<Value> {
sys.refresh_disks();
sys.refresh_disks_list();
let mut output = vec![];
for disk in sys.disks() {
let mut cols = vec![];
let mut vals = vec![];
cols.push("device".into());
vals.push(Value::String {
val: trim_cstyle_null(disk.name().to_string_lossy().to_string()),
span,
});
cols.push("type".into());
vals.push(Value::String {
val: trim_cstyle_null(String::from_utf8_lossy(disk.file_system()).to_string()),
span,
});
cols.push("mount".into());
vals.push(Value::String {
val: disk.mount_point().to_string_lossy().to_string(),
span,
});
cols.push("total".into());
vals.push(Value::Filesize {
2021-10-05 04:27:39 +02:00
val: disk.total_space() as i64,
2021-10-01 08:53:47 +02:00
span,
});
cols.push("free".into());
vals.push(Value::Filesize {
2021-10-05 04:27:39 +02:00
val: disk.available_space() as i64,
2021-10-01 08:53:47 +02:00
span,
});
output.push(Value::Record { cols, vals, span });
}
if !output.is_empty() {
Some(Value::List { vals: output, span })
} else {
None
}
}
pub fn net(sys: &mut System, span: Span) -> Option<Value> {
sys.refresh_networks();
sys.refresh_networks_list();
let mut output = vec![];
for (iface, data) in sys.networks() {
let mut cols = vec![];
let mut vals = vec![];
cols.push("name".into());
vals.push(Value::String {
val: trim_cstyle_null(iface.to_string()),
span,
});
cols.push("sent".into());
vals.push(Value::Filesize {
2021-10-05 04:27:39 +02:00
val: data.total_transmitted() as i64,
2021-10-01 08:53:47 +02:00
span,
});
cols.push("recv".into());
vals.push(Value::Filesize {
2021-10-05 04:27:39 +02:00
val: data.total_received() as i64,
2021-10-01 08:53:47 +02:00
span,
});
output.push(Value::Record { cols, vals, span });
}
if !output.is_empty() {
Some(Value::List { vals: output, span })
} else {
None
}
}
pub fn cpu(sys: &mut System, span: Span) -> Option<Value> {
sys.refresh_cpu();
let mut output = vec![];
for cpu in sys.processors() {
let mut cols = vec![];
let mut vals = vec![];
cols.push("name".into());
vals.push(Value::String {
val: trim_cstyle_null(cpu.name().to_string()),
span,
});
cols.push("brand".into());
vals.push(Value::String {
val: trim_cstyle_null(cpu.brand().to_string()),
span,
});
cols.push("freq".into());
vals.push(Value::Int {
val: cpu.frequency() as i64,
span,
});
output.push(Value::Record { cols, vals, span });
}
if !output.is_empty() {
Some(Value::List { vals: output, span })
} else {
None
}
}
pub fn mem(sys: &mut System, span: Span) -> Option<Value> {
sys.refresh_memory();
let mut cols = vec![];
let mut vals = vec![];
let total_mem = sys.total_memory();
let free_mem = sys.free_memory();
let total_swap = sys.total_swap();
let free_swap = sys.free_swap();
cols.push("total".into());
vals.push(Value::Filesize {
2021-10-05 04:27:39 +02:00
val: total_mem as i64 * 1000,
2021-10-01 08:53:47 +02:00
span,
});
cols.push("free".into());
vals.push(Value::Filesize {
2021-10-05 04:27:39 +02:00
val: free_mem as i64 * 1000,
2021-10-01 08:53:47 +02:00
span,
});
cols.push("swap total".into());
vals.push(Value::Filesize {
2021-10-05 04:27:39 +02:00
val: total_swap as i64 * 1000,
2021-10-01 08:53:47 +02:00
span,
});
cols.push("swap free".into());
vals.push(Value::Filesize {
2021-10-05 04:27:39 +02:00
val: free_swap as i64 * 1000,
2021-10-01 08:53:47 +02:00
span,
});
Some(Value::Record { cols, vals, span })
}
pub fn host(sys: &mut System, span: Span) -> Option<Value> {
sys.refresh_users_list();
let mut cols = vec![];
let mut vals = vec![];
if let Some(name) = sys.name() {
cols.push("name".into());
vals.push(Value::String {
val: trim_cstyle_null(name),
span,
});
}
if let Some(version) = sys.os_version() {
cols.push("os version".into());
vals.push(Value::String {
val: trim_cstyle_null(version),
span,
});
}
if let Some(version) = sys.kernel_version() {
cols.push("kernel version".into());
vals.push(Value::String {
val: trim_cstyle_null(version),
span,
});
}
if let Some(hostname) = sys.host_name() {
cols.push("hostname".into());
vals.push(Value::String {
val: trim_cstyle_null(hostname),
span,
});
}
2021-10-02 04:59:11 +02:00
cols.push("uptime".into());
vals.push(Value::Duration {
2021-10-05 04:27:39 +02:00
val: 1000000000 * sys.uptime() as i64,
2021-10-02 04:59:11 +02:00
span,
});
2021-10-01 08:53:47 +02:00
let mut users = vec![];
for user in sys.users() {
let mut cols = vec![];
let mut vals = vec![];
cols.push("name".into());
vals.push(Value::String {
val: trim_cstyle_null(user.name().to_string()),
span,
});
let mut groups = vec![];
for group in user.groups() {
groups.push(Value::String {
val: trim_cstyle_null(group.to_string()),
span,
});
}
cols.push("groups".into());
vals.push(Value::List { vals: groups, span });
users.push(Value::Record { cols, vals, span });
}
if !users.is_empty() {
cols.push("sessions".into());
vals.push(Value::List { vals: users, span });
}
Some(Value::Record { cols, vals, span })
}
pub fn temp(sys: &mut System, span: Span) -> Option<Value> {
sys.refresh_components();
sys.refresh_components_list();
let mut output = vec![];
for component in sys.components() {
let mut cols = vec![];
let mut vals = vec![];
cols.push("unit".into());
vals.push(Value::String {
val: component.label().to_string(),
span,
});
cols.push("temp".into());
vals.push(Value::Float {
val: component.temperature() as f64,
span,
});
cols.push("high".into());
vals.push(Value::Float {
val: component.max() as f64,
span,
});
if let Some(critical) = component.critical() {
cols.push("critical".into());
vals.push(Value::Float {
val: critical as f64,
span,
});
}
output.push(Value::Record { cols, vals, span });
}
if !output.is_empty() {
Some(Value::List { vals: output, span })
} else {
None
}
}