pub struct System { /* private fields */ }Expand description
Structs containing system’s information such as processes, memory and CPU.
use sysinfo::System;
if sysinfo::IS_SUPPORTED_SYSTEM {
println!("System: {:?}", System::new_all());
} else {
println!("This OS isn't supported (yet?).");
}Implementations§
Source§impl System
impl System
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new System instance with nothing loaded.
Use one of the refresh methods (like refresh_all) to update its internal information.
use sysinfo::System;
let s = System::new();Sourcepub fn new_all() -> Self
pub fn new_all() -> Self
Creates a new System instance with everything loaded.
It is an equivalent of System::new_with_specifics(RefreshKind::everything()).
use sysinfo::System;
let s = System::new_all();Examples found in repository?
460fn main() {
461 println!("Getting system information...");
462 let mut system = System::new_all();
463 let mut networks = Networks::new_with_refreshed_list();
464 let mut disks = Disks::new_with_refreshed_list();
465 let mut components = Components::new_with_refreshed_list();
466 let mut users = Users::new_with_refreshed_list();
467
468 println!("Done.");
469 let t_stin = io::stdin();
470 let mut stin = t_stin.lock();
471 let mut done = false;
472
473 println!("To get the commands' list, enter 'help'.");
474 while !done {
475 let mut input = String::new();
476 write!(&mut io::stdout(), "> ");
477 io::stdout().flush();
478
479 stin.read_line(&mut input);
480 if input.is_empty() {
481 // The string is empty, meaning there is no '\n', meaning
482 // that the user used CTRL+D so we can just quit!
483 println!("\nLeaving, bye!");
484 break;
485 }
486 if (&input as &str).ends_with('\n') {
487 input.pop();
488 }
489 done = interpret_input(
490 input.as_ref(),
491 &mut system,
492 &mut networks,
493 &mut disks,
494 &mut components,
495 &mut users,
496 );
497 }
498}Sourcepub fn new_with_specifics(refreshes: RefreshKind) -> Self
pub fn new_with_specifics(refreshes: RefreshKind) -> Self
Creates a new System instance and refresh the data corresponding to the
given RefreshKind.
use sysinfo::{ProcessRefreshKind, RefreshKind, System};
// We want to only refresh processes.
let mut system = System::new_with_specifics(
RefreshKind::nothing().with_processes(ProcessRefreshKind::everything()),
);
assert!(!system.processes().is_empty());Sourcepub fn refresh_specifics(&mut self, refreshes: RefreshKind)
pub fn refresh_specifics(&mut self, refreshes: RefreshKind)
Refreshes according to the given RefreshKind. It calls the corresponding
“refresh_” methods.
use sysinfo::{ProcessRefreshKind, RefreshKind, System};
let mut s = System::new_all();
// Let's just update processes:
s.refresh_specifics(
RefreshKind::nothing().with_processes(ProcessRefreshKind::everything()),
);Sourcepub fn refresh_all(&mut self)
pub fn refresh_all(&mut self)
Refreshes all system and processes information.
It is the same as calling system.refresh_specifics(RefreshKind::everything()).
Don’t forget to take a look at ProcessRefreshKind::everything method to see what it
will update for processes more in details.
use sysinfo::System;
let mut s = System::new();
s.refresh_all();Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn refresh_memory(&mut self)
pub fn refresh_memory(&mut self)
Refreshes RAM and SWAP usage.
It is the same as calling system.refresh_memory_specifics(MemoryRefreshKind::everything()).
If you don’t want to refresh both, take a look at System::refresh_memory_specifics.
use sysinfo::System;
let mut s = System::new();
s.refresh_memory();Sourcepub fn refresh_memory_specifics(&mut self, refresh_kind: MemoryRefreshKind)
pub fn refresh_memory_specifics(&mut self, refresh_kind: MemoryRefreshKind)
Refreshes system memory specific information.
use sysinfo::{MemoryRefreshKind, System};
let mut s = System::new();
s.refresh_memory_specifics(MemoryRefreshKind::nothing().with_ram());Sourcepub fn refresh_cpu_usage(&mut self)
pub fn refresh_cpu_usage(&mut self)
Refreshes CPUs usage.
⚠️ Please note that the result will very likely be inaccurate at the first call.
You need to call this method at least twice (with a bit of time between each call, like
200 ms, take a look at MINIMUM_CPU_UPDATE_INTERVAL for more information)
to get accurate value as it uses previous results to compute the next value.
Calling this method is the same as calling
system.refresh_cpu_specifics(CpuRefreshKind::nothing().with_cpu_usage()).
use sysinfo::System;
let mut s = System::new_all();
// Wait a bit because CPU usage is based on diff.
std::thread::sleep(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL);
// Refresh CPUs again.
s.refresh_cpu_usage();Sourcepub fn refresh_cpu_frequency(&mut self)
pub fn refresh_cpu_frequency(&mut self)
Refreshes CPUs frequency information.
Calling this method is the same as calling
system.refresh_cpu_specifics(CpuRefreshKind::nothing().with_frequency()).
use sysinfo::System;
let mut s = System::new_all();
s.refresh_cpu_frequency();Sourcepub fn refresh_cpu_list(&mut self, refresh_kind: CpuRefreshKind)
pub fn refresh_cpu_list(&mut self, refresh_kind: CpuRefreshKind)
Refreshes the list of CPU.
Normally, this should almost never be needed as it’s pretty rare for a computer to add a CPU while running, but it’s possible on some computers which shutdown CPU if the load is low enough.
The refresh_kind argument tells what information you want to be retrieved
for each CPU.
use sysinfo::{CpuRefreshKind, System};
let mut s = System::new_all();
// We already have the list of CPU filled, but we want to recompute it
// in case new CPUs were added.
s.refresh_cpu_list(CpuRefreshKind::everything());Sourcepub fn refresh_cpu_all(&mut self)
pub fn refresh_cpu_all(&mut self)
Refreshes all information related to CPUs information.
If you only want the CPU usage, use System::refresh_cpu_usage instead.
⚠️ Please note that the result will be inaccurate at the first call.
You need to call this method at least twice (with a bit of time between each call, like
200 ms, take a look at MINIMUM_CPU_UPDATE_INTERVAL for more information)
to get accurate value as it uses previous results to compute the next value.
Calling this method is the same as calling
system.refresh_cpu_specifics(CpuRefreshKind::everything()).
use sysinfo::System;
let mut s = System::new_all();
s.refresh_cpu_all();Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn refresh_cpu_specifics(&mut self, refresh_kind: CpuRefreshKind)
pub fn refresh_cpu_specifics(&mut self, refresh_kind: CpuRefreshKind)
Refreshes CPUs specific information.
use sysinfo::{System, CpuRefreshKind};
let mut s = System::new_all();
s.refresh_cpu_specifics(CpuRefreshKind::everything());Sourcepub fn refresh_processes(
&mut self,
processes_to_update: ProcessesToUpdate<'_>,
remove_dead_processes: bool,
) -> usize
pub fn refresh_processes( &mut self, processes_to_update: ProcessesToUpdate<'_>, remove_dead_processes: bool, ) -> usize
Gets all processes and updates their information.
It does the same as:
system.refresh_processes_specifics(
ProcessesToUpdate::All,
true,
ProcessRefreshKind::nothing()
.with_memory()
.with_cpu()
.with_disk_usage()
.with_exe(UpdateKind::OnlyIfNotSet),
);⚠️ remove_dead_processes works as follows: if an updated process is dead, then it is
removed. So if you refresh pids 1, 2 and 3. If 2 and 7 are dead, only 2 will be removed
since 7 is not part of the update.
⚠️ On Linux, sysinfo keeps the stat files open by default. You can change this behaviour
by using set_open_files_limit.
Example:
use sysinfo::{ProcessesToUpdate, System};
let mut s = System::new_all();
s.refresh_processes(ProcessesToUpdate::All, true);Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn refresh_processes_specifics(
&mut self,
processes_to_update: ProcessesToUpdate<'_>,
remove_dead_processes: bool,
refresh_kind: ProcessRefreshKind,
) -> usize
pub fn refresh_processes_specifics( &mut self, processes_to_update: ProcessesToUpdate<'_>, remove_dead_processes: bool, refresh_kind: ProcessRefreshKind, ) -> usize
Gets all processes and updates the specified information.
Returns the number of updated processes.
⚠️ remove_dead_processes works as follows: if an updated process is dead, then it is
removed. So if you refresh pids 1, 2 and 3. If 2 and 7 are dead, only 2 will be removed
since 7 is not part of the update.
⚠️ On Linux, sysinfo keeps the stat files open by default. You can change this behaviour
by using set_open_files_limit.
use sysinfo::{ProcessesToUpdate, ProcessRefreshKind, System};
let mut s = System::new_all();
s.refresh_processes_specifics(
ProcessesToUpdate::All,
true,
ProcessRefreshKind::everything(),
);Sourcepub fn processes(&self) -> &HashMap<Pid, Process>
pub fn processes(&self) -> &HashMap<Pid, Process>
Returns the process list.
use sysinfo::System;
let s = System::new_all();
for (pid, process) in s.processes() {
println!("{} {:?}", pid, process.name());
}Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn process(&self, pid: Pid) -> Option<&Process>
pub fn process(&self, pid: Pid) -> Option<&Process>
Returns the process corresponding to the given pid or None if no such process exists.
use sysinfo::{Pid, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{:?}", process.name());
}Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn processes_by_name<'a: 'b, 'b>(
&'a self,
name: &'b OsStr,
) -> impl Iterator<Item = &'a Process> + 'b
pub fn processes_by_name<'a: 'b, 'b>( &'a self, name: &'b OsStr, ) -> impl Iterator<Item = &'a Process> + 'b
Returns an iterator of process containing the given name.
If you want only the processes with exactly the given name, take a look at
System::processes_by_exact_name.
⚠️ Important ⚠️
On Linux, there are two things to know about processes’ name:
- It is limited to 15 characters.
- It is not always the exe name.
use sysinfo::System;
let s = System::new_all();
for process in s.processes_by_name("htop".as_ref()) {
println!("{} {:?}", process.pid(), process.name());
}Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn processes_by_exact_name<'a: 'b, 'b>(
&'a self,
name: &'b OsStr,
) -> impl Iterator<Item = &'a Process> + 'b
pub fn processes_by_exact_name<'a: 'b, 'b>( &'a self, name: &'b OsStr, ) -> impl Iterator<Item = &'a Process> + 'b
Returns an iterator of processes with exactly the given name.
If you instead want the processes containing name, take a look at
System::processes_by_name.
⚠️ Important ⚠️
On Linux, there are two things to know about processes’ name:
- It is limited to 15 characters.
- It is not always the exe name.
use sysinfo::System;
let s = System::new_all();
for process in s.processes_by_exact_name("htop".as_ref()) {
println!("{} {:?}", process.pid(), process.name());
}Sourcepub fn global_cpu_usage(&self) -> f32
pub fn global_cpu_usage(&self) -> f32
Returns “global” CPUs usage (aka the addition of all the CPUs).
To have up-to-date information, you need to call System::refresh_cpu_specifics or
System::refresh_specifics with cpu enabled.
use sysinfo::{CpuRefreshKind, RefreshKind, System};
let mut s = System::new_with_specifics(
RefreshKind::nothing().with_cpu(CpuRefreshKind::everything()),
);
// Wait a bit because CPU usage is based on diff.
std::thread::sleep(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL);
// Refresh CPUs again to get actual value.
s.refresh_cpu_usage();
println!("{}%", s.global_cpu_usage());Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn cpus(&self) -> &[Cpu]
pub fn cpus(&self) -> &[Cpu]
Returns the list of the CPUs.
By default, the list of CPUs is empty until you call System::refresh_cpu_specifics or
System::refresh_specifics with cpu enabled.
use sysinfo::{CpuRefreshKind, RefreshKind, System};
let mut s = System::new_with_specifics(
RefreshKind::nothing().with_cpu(CpuRefreshKind::everything()),
);
// Wait a bit because CPU usage is based on diff.
std::thread::sleep(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL);
// Refresh CPUs again to get actual value.
s.refresh_cpu_usage();
for cpu in s.cpus() {
println!("{}%", cpu.cpu_usage());
}Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn physical_core_count(&self) -> Option<usize>
pub fn physical_core_count(&self) -> Option<usize>
Returns the number of physical cores on the CPU or None if it couldn’t get it.
In case there are multiple CPUs, it will combine the physical core count of all the CPUs.
Important: this information is computed every time this function is called.
use sysinfo::System;
let s = System::new();
println!("{:?}", s.physical_core_count());Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn total_memory(&self) -> u64
pub fn total_memory(&self) -> u64
Returns the RAM size in bytes.
use sysinfo::System;
let s = System::new_all();
println!("{} bytes", s.total_memory());On Linux, if you want to see this information with the limit of your cgroup, take a look
at cgroup_limits.
Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn free_memory(&self) -> u64
pub fn free_memory(&self) -> u64
Returns the amount of free RAM in bytes.
Generally, “free” memory refers to unallocated memory whereas “available” memory refers to memory that is available for (re)use.
Side note: Windows doesn’t report “free” memory so this method returns the same value
as available_memory.
use sysinfo::System;
let s = System::new_all();
println!("{} bytes", s.free_memory());Sourcepub fn available_memory(&self) -> u64
pub fn available_memory(&self) -> u64
Returns the amount of available RAM in bytes.
Generally, “free” memory refers to unallocated memory whereas “available” memory refers to memory that is available for (re)use.
⚠️ Windows and FreeBSD don’t report “available” memory so System::free_memory
returns the same value as this method.
use sysinfo::System;
let s = System::new_all();
println!("{} bytes", s.available_memory());Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn used_memory(&self) -> u64
pub fn used_memory(&self) -> u64
Returns the amount of used RAM in bytes.
use sysinfo::System;
let s = System::new_all();
println!("{} bytes", s.used_memory());Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn total_swap(&self) -> u64
pub fn total_swap(&self) -> u64
Returns the SWAP size in bytes.
use sysinfo::System;
let s = System::new_all();
println!("{} bytes", s.total_swap());Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn free_swap(&self) -> u64
pub fn free_swap(&self) -> u64
Returns the amount of free SWAP in bytes.
use sysinfo::System;
let s = System::new_all();
println!("{} bytes", s.free_swap());Sourcepub fn used_swap(&self) -> u64
pub fn used_swap(&self) -> u64
Returns the amount of used SWAP in bytes.
use sysinfo::System;
let s = System::new_all();
println!("{} bytes", s.used_swap());Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn cgroup_limits(&self) -> Option<CGroupLimits>
pub fn cgroup_limits(&self) -> Option<CGroupLimits>
Retrieves the limits for the current cgroup (if any), otherwise it returns None.
This information is computed every time the method is called.
⚠️ You need to have run refresh_memory at least once before
calling this method.
⚠️ This method is only implemented for Linux. It always returns None for all other
systems.
use sysinfo::System;
let s = System::new_all();
println!("limits: {:?}", s.cgroup_limits());Sourcepub fn uptime() -> u64
pub fn uptime() -> u64
Returns system uptime (in seconds).
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("System running since {} seconds", System::uptime());Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn boot_time() -> u64
pub fn boot_time() -> u64
Returns the time (in seconds) when the system booted since UNIX epoch.
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("System booted at {} seconds", System::boot_time());Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn load_average() -> LoadAvg
pub fn load_average() -> LoadAvg
Returns the system load average value.
Important: this information is computed every time this function is called.
⚠️ This is currently not working on Windows.
use sysinfo::System;
let load_avg = System::load_average();
println!(
"one minute: {}%, five minutes: {}%, fifteen minutes: {}%",
load_avg.one,
load_avg.five,
load_avg.fifteen,
);Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn name() -> Option<String>
pub fn name() -> Option<String>
Returns the system name.
| example platform | value of System::name() |
|---|---|
| linux laptop | “Ubuntu” |
| android phone | “Pixel 9 Pro” |
| apple laptop | “Darwin” |
| windows server | “Windows” |
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("OS: {:?}", System::name());Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn kernel_version() -> Option<String>
pub fn kernel_version() -> Option<String>
Returns the system’s kernel version.
| example platform | value of System::kernel_version() |
|---|---|
| linux laptop | “6.8.0-48-generic” |
| android phone | “6.1.84-android14-11” |
| apple laptop | “24.1.0” |
| windows server | “20348” |
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("kernel version: {:?}", System::kernel_version());Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn os_version() -> Option<String>
pub fn os_version() -> Option<String>
Returns the system version (e.g. for macOS this will return 15.1 rather than the kernel version).
| example platform | value of System::os_version() |
|---|---|
| linux laptop | “24.04” |
| android phone | “15” |
| apple laptop | “15.1.1” |
| windows server | “10 (20348)” |
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("OS version: {:?}", System::os_version());Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn long_os_version() -> Option<String>
pub fn long_os_version() -> Option<String>
Returns the system long os version.
| example platform | value of System::long_os_version() |
|---|---|
| linux laptop | “Linux (Ubuntu 24.04)” |
| android phone | “Android 15 on Pixel 9 Pro” |
| apple laptop | “macOS 15.1.1 Sequoia” |
| windows server | “Windows Server 2022 Datacenter” |
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("Long OS Version: {:?}", System::long_os_version());Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Sourcepub fn distribution_id() -> String
pub fn distribution_id() -> String
Returns the distribution id as defined by os-release,
or [std::env::consts::OS].
See also
- https://www.freedesktop.org/software/systemd/man/os-release.html#ID=
- https://doc.rust-lang.org/std/env/consts/constant.OS.html
| example platform | value of System::distribution_id() |
|---|---|
| linux laptop | “ubuntu” |
| android phone | “android” |
| apple laptop | “macos” |
| windows server | “windows” |
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("Distribution ID: {:?}", System::distribution_id());Sourcepub fn host_name() -> Option<String>
pub fn host_name() -> Option<String>
Returns the system hostname based off DNS.
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("Hostname: {:?}", System::host_name());Examples found in repository?
149fn interpret_input(
150 input: &str,
151 sys: &mut System,
152 networks: &mut Networks,
153 disks: &mut Disks,
154 components: &mut Components,
155 users: &mut Users,
156) -> bool {
157 match input.trim() {
158 "help" => print_help(),
159 "refresh_disks" => {
160 writeln!(&mut io::stdout(), "Refreshing disk list...");
161 disks.refresh(true);
162 writeln!(&mut io::stdout(), "Done.");
163 }
164 "refresh_users" => {
165 writeln!(&mut io::stdout(), "Refreshing user list...");
166 users.refresh();
167 writeln!(&mut io::stdout(), "Done.");
168 }
169 "refresh_networks" => {
170 writeln!(&mut io::stdout(), "Refreshing network list...");
171 networks.refresh(true);
172 writeln!(&mut io::stdout(), "Done.");
173 }
174 "refresh_components" => {
175 writeln!(&mut io::stdout(), "Refreshing component list...");
176 components.refresh(true);
177 writeln!(&mut io::stdout(), "Done.");
178 }
179 "refresh_cpu" => {
180 writeln!(&mut io::stdout(), "Refreshing CPUs...");
181 sys.refresh_cpu_all();
182 writeln!(&mut io::stdout(), "Done.");
183 }
184 "signals" => {
185 let mut nb = 1i32;
186
187 for sig in signals {
188 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
189 nb += 1;
190 }
191 }
192 "cpus" => {
193 // Note: you should refresh a few times before using this, so that usage statistics
194 // can be ascertained
195 writeln!(
196 &mut io::stdout(),
197 "number of physical cores: {}",
198 sys.physical_core_count()
199 .map(|c| c.to_string())
200 .unwrap_or_else(|| "Unknown".to_owned()),
201 );
202 writeln!(
203 &mut io::stdout(),
204 "total CPU usage: {}%",
205 sys.global_cpu_usage(),
206 );
207 for cpu in sys.cpus() {
208 writeln!(&mut io::stdout(), "{cpu:?}");
209 }
210 }
211 "memory" => {
212 writeln!(
213 &mut io::stdout(),
214 "total memory: {: >10} KB",
215 sys.total_memory() / 1_000
216 );
217 writeln!(
218 &mut io::stdout(),
219 "available memory: {: >10} KB",
220 sys.available_memory() / 1_000
221 );
222 writeln!(
223 &mut io::stdout(),
224 "used memory: {: >10} KB",
225 sys.used_memory() / 1_000
226 );
227 writeln!(
228 &mut io::stdout(),
229 "total swap: {: >10} KB",
230 sys.total_swap() / 1_000
231 );
232 writeln!(
233 &mut io::stdout(),
234 "used swap: {: >10} KB",
235 sys.used_swap() / 1_000
236 );
237 }
238 "quit" | "exit" => return true,
239 "all" => {
240 for (pid, proc_) in sys.processes() {
241 writeln!(
242 &mut io::stdout(),
243 "{}:{} status={:?}",
244 pid,
245 proc_.name().to_string_lossy(),
246 proc_.status()
247 );
248 }
249 }
250 "frequency" => {
251 for cpu in sys.cpus() {
252 writeln!(
253 &mut io::stdout(),
254 "[{}] {} MHz",
255 cpu.name(),
256 cpu.frequency(),
257 );
258 }
259 }
260 "vendor_id" => {
261 writeln!(
262 &mut io::stdout(),
263 "vendor ID: {}",
264 sys.cpus()[0].vendor_id()
265 );
266 }
267 "brand" => {
268 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
269 }
270 "load_avg" => {
271 let load_avg = System::load_average();
272 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
273 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
274 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
275 }
276 e if e.starts_with("show ") => {
277 let tmp: Vec<&str> = e.split(' ').collect();
278
279 if tmp.len() != 2 {
280 writeln!(
281 &mut io::stdout(),
282 "show command takes a pid or a name in parameter!"
283 );
284 writeln!(&mut io::stdout(), "example: show 1254");
285 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
286 match sys.process(pid) {
287 Some(p) => writeln!(&mut io::stdout(), "{:?}", *p),
288 None => writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found"),
289 };
290 } else {
291 let proc_name = tmp[1];
292 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
293 writeln!(
294 &mut io::stdout(),
295 "==== {} ====",
296 proc_.name().to_string_lossy()
297 );
298 writeln!(&mut io::stdout(), "{proc_:?}");
299 }
300 }
301 }
302 "temperature" => {
303 for component in components.iter() {
304 writeln!(&mut io::stdout(), "{component:?}");
305 }
306 }
307 "network" => {
308 for (interface_name, data) in networks.iter() {
309 writeln!(
310 &mut io::stdout(),
311 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
312 interface_name,
313 data.mac_address(),
314 data.received(),
315 data.total_received(),
316 data.transmitted(),
317 data.total_transmitted(),
318 );
319 }
320 }
321 "show" => {
322 writeln!(
323 &mut io::stdout(),
324 "'show' command expects a pid number or a process name"
325 );
326 }
327 e if e.starts_with("kill ") => {
328 let tmp: Vec<&str> = e.split(' ').collect();
329
330 if tmp.len() != 3 {
331 writeln!(
332 &mut io::stdout(),
333 "kill command takes the pid and a signal number in parameter!"
334 );
335 writeln!(&mut io::stdout(), "example: kill 1254 9");
336 } else {
337 let pid = Pid::from_str(tmp[1]).unwrap();
338 let signal = i32::from_str(tmp[2]).unwrap();
339
340 if signal < 1 || signal > 31 {
341 writeln!(
342 &mut io::stdout(),
343 "Signal must be between 0 and 32 ! See the signals list with the \
344 signals command"
345 );
346 } else {
347 match sys.process(pid) {
348 Some(p) => {
349 if let Some(res) =
350 p.kill_with(*signals.get(signal as usize - 1).unwrap())
351 {
352 writeln!(&mut io::stdout(), "kill: {res}");
353 } else {
354 writeln!(
355 &mut io::stdout(),
356 "kill: signal not supported on this platform"
357 );
358 }
359 }
360 None => {
361 writeln!(&mut io::stdout(), "pid not found");
362 }
363 };
364 }
365 }
366 }
367 "disks" => {
368 for disk in disks {
369 writeln!(&mut io::stdout(), "{disk:?}");
370 }
371 }
372 "users" => {
373 for user in users {
374 writeln!(
375 &mut io::stdout(),
376 "{:?} => {:?}",
377 user.name(),
378 user.groups()
379 );
380 }
381 }
382 "boot_time" => {
383 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
384 }
385 "uptime" => {
386 let up = System::uptime();
387 let mut uptime = up;
388 let days = uptime / 86400;
389 uptime -= days * 86400;
390 let hours = uptime / 3600;
391 uptime -= hours * 3600;
392 let minutes = uptime / 60;
393 writeln!(
394 &mut io::stdout(),
395 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
396 );
397 }
398 x if x.starts_with("refresh") => {
399 if x == "refresh" {
400 writeln!(&mut io::stdout(), "Getting processes' information...");
401 sys.refresh_all();
402 writeln!(&mut io::stdout(), "Done.");
403 } else if x.starts_with("refresh ") {
404 writeln!(&mut io::stdout(), "Getting process' information...");
405 if let Some(pid) = x
406 .split(' ')
407 .filter_map(|pid| pid.parse().ok())
408 .take(1)
409 .next()
410 {
411 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
412 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
413 } else {
414 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
415 }
416 } else {
417 writeln!(&mut io::stdout(), "Invalid [pid] received...");
418 }
419 } else {
420 writeln!(
421 &mut io::stdout(),
422 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
423 list.",
424 );
425 }
426 }
427 "pid" => {
428 writeln!(
429 &mut io::stdout(),
430 "PID: {}",
431 sysinfo::get_current_pid().expect("failed to get PID")
432 );
433 }
434 "system" => {
435 writeln!(
436 &mut io::stdout(),
437 "System name: {}\n\
438 System kernel version: {}\n\
439 System OS version: {}\n\
440 System OS (long) version: {}\n\
441 System host name: {}",
442 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
443 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
444 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
445 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
446 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
447 );
448 }
449 e => {
450 writeln!(
451 &mut io::stdout(),
452 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
453 list.",
454 );
455 }
456 }
457 false
458}Trait Implementations§
Auto Trait Implementations§
impl Freeze for System
impl RefUnwindSafe for System
impl Send for System
impl Sync for System
impl Unpin for System
impl UnwindSafe for System
Blanket Implementations§
§impl<T> Any for Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere
T: 'static + ?Sized,
§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere
T: ?Sized,
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T, U> Into<U> for Twhere
U: From<T>,
impl<T, U> Into<U> for Twhere
U: From<T>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>where
F: FnOnce(&Self) -> bool,
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>where
F: FnOnce(&Self) -> bool,
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more