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?
473fn main() {
474 println!("Getting system information...");
475 let mut system = System::new_all();
476 let mut networks = Networks::new_with_refreshed_list();
477 let mut disks = Disks::new_with_refreshed_list();
478 let mut components = Components::new_with_refreshed_list();
479 let mut users = Users::new_with_refreshed_list();
480
481 println!("Done.");
482 let t_stin = io::stdin();
483 let mut stin = t_stin.lock();
484 let mut done = false;
485
486 println!("To get the commands' list, enter 'help'.");
487 while !done {
488 let mut input = String::new();
489 write!(&mut io::stdout(), "> ");
490 io::stdout().flush();
491
492 stin.read_line(&mut input);
493 if input.is_empty() {
494 // The string is empty, meaning there is no '\n', meaning
495 // that the user used CTRL+D so we can just quit!
496 println!("\nLeaving, bye!");
497 break;
498 }
499 if (&input as &str).ends_with('\n') {
500 input.pop();
501 }
502 done = interpret_input(
503 input.as_ref(),
504 &mut system,
505 &mut networks,
506 &mut disks,
507 &mut components,
508 &mut users,
509 );
510 }
511}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.
It will remove dead processes if RefreshKind::processes returns Some.
If you want to keep dead processes, use System::refresh_processes_specifics
directly.
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.
It will remove dead processes. If you want to keep dead processes, use
System::refresh_processes_specifics directly.
use sysinfo::System;
let mut s = System::new();
s.refresh_all();Examples found in repository?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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, along with all the tasks each process has.
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.
⚠️ On Linux, if you dont need the tasks of each process, you can use
refresh_processes_specifics with ProcessRefreshKind::everything().without_tasks().
Refreshesing all processes and their tasks can be quite expensive. For more information
see ProcessRefreshKind.
Example:
use sysinfo::{ProcessesToUpdate, System};
let mut s = System::new_all();
s.refresh_processes(ProcessesToUpdate::All, true);Examples found in repository?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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 distribution_id_like() -> Vec<String>
pub fn distribution_id_like() -> Vec<String>
Returns the distribution ids of operating systems that are closely related to the local operating system in regards to packaging and programming interfaces, for example listing one or more OS identifiers the local OS is a derivative from.
See also
| example platform | value of System::distribution_id_like() |
|---|---|
| android phone | [] |
| archlinux laptop | [] |
| centos server | [“rhel”, “fedora”] |
| ubuntu laptop | [“debian”] |
| windows laptop | [] |
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("Distribution ID_LIKE: {:?}", System::distribution_id_like());Sourcepub fn kernel_long_version() -> String
pub fn kernel_long_version() -> String
Provides kernel version following this string format:
| Platform | Result |
|---|---|
| Windows | Windows OS Build 20348.2227 |
| Linux | Linux 6.12.13-200.fc41.x86_64 |
| Android | Android 612.13-200 |
| MacOS | Darwin 21.6.0 |
| FreeBSD | FreeBSD 199506 |
If any of the information is not available, it will be replaced with “unknown”.
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("Kernel long version: {}", System::kernel_long_version());Examples found in repository?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}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?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}Sourcepub fn cpu_arch() -> String
pub fn cpu_arch() -> String
Returns the CPU architecture (eg. x86, amd64, aarch64, …).
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("CPU Architecture: {:?}", System::cpu_arch());Sourcepub fn physical_core_count() -> Option<usize>
pub fn physical_core_count() -> 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!("{:?}", System::physical_core_count());Examples found in repository?
150fn interpret_input(
151 input: &str,
152 sys: &mut System,
153 networks: &mut Networks,
154 disks: &mut Disks,
155 components: &mut Components,
156 users: &mut Users,
157) -> bool {
158 match input.trim() {
159 "help" => print_help(),
160 "refresh_disks" => {
161 writeln!(&mut io::stdout(), "Refreshing disk list...");
162 disks.refresh(true);
163 writeln!(&mut io::stdout(), "Done.");
164 }
165 "refresh_users" => {
166 writeln!(&mut io::stdout(), "Refreshing user list...");
167 users.refresh();
168 writeln!(&mut io::stdout(), "Done.");
169 }
170 "refresh_networks" => {
171 writeln!(&mut io::stdout(), "Refreshing network list...");
172 networks.refresh(true);
173 writeln!(&mut io::stdout(), "Done.");
174 }
175 "refresh_components" => {
176 writeln!(&mut io::stdout(), "Refreshing component list...");
177 components.refresh(true);
178 writeln!(&mut io::stdout(), "Done.");
179 }
180 "refresh_cpu" => {
181 writeln!(&mut io::stdout(), "Refreshing CPUs...");
182 sys.refresh_cpu_all();
183 writeln!(&mut io::stdout(), "Done.");
184 }
185 "signals" => {
186 let mut nb = 1i32;
187
188 for sig in signals {
189 writeln!(&mut io::stdout(), "{nb:2}:{sig:?}");
190 nb += 1;
191 }
192 }
193 "cpus" => {
194 // Note: you should refresh a few times before using this, so that usage statistics
195 // can be ascertained
196 writeln!(
197 &mut io::stdout(),
198 "number of physical cores: {}",
199 System::physical_core_count()
200 .map(|c| c.to_string())
201 .unwrap_or_else(|| "Unknown".to_owned()),
202 );
203 writeln!(
204 &mut io::stdout(),
205 "total CPU usage: {}%",
206 sys.global_cpu_usage(),
207 );
208 for cpu in sys.cpus() {
209 writeln!(&mut io::stdout(), "{cpu:?}");
210 }
211 }
212 "memory" => {
213 writeln!(
214 &mut io::stdout(),
215 "total memory: {: >10} KB",
216 sys.total_memory() / 1_000
217 );
218 writeln!(
219 &mut io::stdout(),
220 "available memory: {: >10} KB",
221 sys.available_memory() / 1_000
222 );
223 writeln!(
224 &mut io::stdout(),
225 "used memory: {: >10} KB",
226 sys.used_memory() / 1_000
227 );
228 writeln!(
229 &mut io::stdout(),
230 "total swap: {: >10} KB",
231 sys.total_swap() / 1_000
232 );
233 writeln!(
234 &mut io::stdout(),
235 "used swap: {: >10} KB",
236 sys.used_swap() / 1_000
237 );
238 }
239 "quit" | "exit" => return true,
240 "all" => {
241 for (pid, proc_) in sys.processes() {
242 writeln!(
243 &mut io::stdout(),
244 "{}:{} status={:?}",
245 pid,
246 proc_.name().to_string_lossy(),
247 proc_.status()
248 );
249 }
250 }
251 "frequency" => {
252 for cpu in sys.cpus() {
253 writeln!(
254 &mut io::stdout(),
255 "[{}] {} MHz",
256 cpu.name(),
257 cpu.frequency(),
258 );
259 }
260 }
261 "vendor_id" => {
262 writeln!(
263 &mut io::stdout(),
264 "vendor ID: {}",
265 sys.cpus()[0].vendor_id()
266 );
267 }
268 "brand" => {
269 writeln!(&mut io::stdout(), "brand: {}", sys.cpus()[0].brand());
270 }
271 "load_avg" => {
272 let load_avg = System::load_average();
273 writeln!(&mut io::stdout(), "one minute : {}%", load_avg.one);
274 writeln!(&mut io::stdout(), "five minutes : {}%", load_avg.five);
275 writeln!(&mut io::stdout(), "fifteen minutes: {}%", load_avg.fifteen);
276 }
277 e if e.starts_with("show ") => {
278 let tmp: Vec<&str> = e.split(' ').collect();
279
280 if tmp.len() != 2 {
281 writeln!(
282 &mut io::stdout(),
283 "show command takes a pid or a name in parameter!"
284 );
285 writeln!(&mut io::stdout(), "example: show 1254");
286 } else if let Ok(pid) = Pid::from_str(tmp[1]) {
287 match sys.process(pid) {
288 Some(p) => {
289 writeln!(&mut io::stdout(), "{:?}", *p);
290 writeln!(
291 &mut io::stdout(),
292 "Files open/limit: {:?}/{:?}",
293 p.open_files(),
294 p.open_files_limit(),
295 );
296 }
297 None => {
298 writeln!(&mut io::stdout(), "pid \"{pid:?}\" not found");
299 }
300 }
301 } else {
302 let proc_name = tmp[1];
303 for proc_ in sys.processes_by_name(proc_name.as_ref()) {
304 writeln!(
305 &mut io::stdout(),
306 "==== {} ====",
307 proc_.name().to_string_lossy()
308 );
309 writeln!(&mut io::stdout(), "{proc_:?}");
310 }
311 }
312 }
313 "temperature" => {
314 for component in components.iter() {
315 writeln!(&mut io::stdout(), "{component:?}");
316 }
317 }
318 "network" => {
319 for (interface_name, data) in networks.iter() {
320 writeln!(
321 &mut io::stdout(),
322 "{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
323 interface_name,
324 data.mac_address(),
325 data.received(),
326 data.total_received(),
327 data.transmitted(),
328 data.total_transmitted(),
329 );
330 }
331 }
332 "show" => {
333 writeln!(
334 &mut io::stdout(),
335 "'show' command expects a pid number or a process name"
336 );
337 }
338 e if e.starts_with("kill ") => {
339 let tmp: Vec<&str> = e.split(' ').collect();
340
341 if tmp.len() != 3 {
342 writeln!(
343 &mut io::stdout(),
344 "kill command takes the pid and a signal number in parameter!"
345 );
346 writeln!(&mut io::stdout(), "example: kill 1254 9");
347 } else {
348 let pid = Pid::from_str(tmp[1]).unwrap();
349 let signal = i32::from_str(tmp[2]).unwrap();
350
351 if signal < 1 || signal > 31 {
352 writeln!(
353 &mut io::stdout(),
354 "Signal must be between 0 and 32 ! See the signals list with the \
355 signals command"
356 );
357 } else {
358 match sys.process(pid) {
359 Some(p) => {
360 if let Some(res) =
361 p.kill_with(*signals.get(signal as usize - 1).unwrap())
362 {
363 writeln!(&mut io::stdout(), "kill: {res}");
364 } else {
365 writeln!(
366 &mut io::stdout(),
367 "kill: signal not supported on this platform"
368 );
369 }
370 }
371 None => {
372 writeln!(&mut io::stdout(), "pid not found");
373 }
374 };
375 }
376 }
377 }
378 "disks" => {
379 for disk in disks {
380 writeln!(&mut io::stdout(), "{disk:?}");
381 }
382 }
383 "users" => {
384 for user in users {
385 writeln!(
386 &mut io::stdout(),
387 "{:?} => {:?}",
388 user.name(),
389 user.groups()
390 );
391 }
392 }
393 "boot_time" => {
394 writeln!(&mut io::stdout(), "{} seconds", System::boot_time());
395 }
396 "uptime" => {
397 let up = System::uptime();
398 let mut uptime = up;
399 let days = uptime / 86400;
400 uptime -= days * 86400;
401 let hours = uptime / 3600;
402 uptime -= hours * 3600;
403 let minutes = uptime / 60;
404 writeln!(
405 &mut io::stdout(),
406 "{days} days {hours} hours {minutes} minutes ({up} seconds in total)",
407 );
408 }
409 x if x.starts_with("refresh") => {
410 if x == "refresh" {
411 writeln!(&mut io::stdout(), "Getting processes' information...");
412 sys.refresh_all();
413 writeln!(&mut io::stdout(), "Done.");
414 } else if x.starts_with("refresh ") {
415 writeln!(&mut io::stdout(), "Getting process' information...");
416 if let Some(pid) = x
417 .split(' ')
418 .filter_map(|pid| pid.parse().ok())
419 .take(1)
420 .next()
421 {
422 if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
423 writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
424 } else {
425 writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
426 }
427 } else {
428 writeln!(&mut io::stdout(), "Invalid [pid] received...");
429 }
430 } else {
431 writeln!(
432 &mut io::stdout(),
433 "\"{x}\": Unknown command. Enter 'help' if you want to get the commands' \
434 list.",
435 );
436 }
437 }
438 "pid" => {
439 writeln!(
440 &mut io::stdout(),
441 "PID: {}",
442 sysinfo::get_current_pid().expect("failed to get PID")
443 );
444 }
445 "system" => {
446 writeln!(
447 &mut io::stdout(),
448 "System name: {}\n\
449 System kernel version: {}\n\
450 System OS version: {}\n\
451 System OS (long) version: {}\n\
452 System host name: {}\n\
453 System kernel: {}",
454 System::name().unwrap_or_else(|| "<unknown>".to_owned()),
455 System::kernel_version().unwrap_or_else(|| "<unknown>".to_owned()),
456 System::os_version().unwrap_or_else(|| "<unknown>".to_owned()),
457 System::long_os_version().unwrap_or_else(|| "<unknown>".to_owned()),
458 System::host_name().unwrap_or_else(|| "<unknown>".to_owned()),
459 System::kernel_long_version(),
460 );
461 }
462 e => {
463 writeln!(
464 &mut io::stdout(),
465 "\"{e}\": Unknown command. Enter 'help' if you want to get the commands' \
466 list.",
467 );
468 }
469 }
470 false
471}