Vendor dependencies
Let's see how I like this workflow.
This commit is contained in:
parent
34d1830413
commit
9c435dc440
7500 changed files with 1665121 additions and 99 deletions
53
vendor/procfs/examples/netstat.rs
vendored
Normal file
53
vendor/procfs/examples/netstat.rs
vendored
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#![allow(clippy::print_literal)]
|
||||
|
||||
extern crate procfs;
|
||||
|
||||
use procfs::process::{FDTarget, Stat};
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn main() {
|
||||
// get all processes
|
||||
let all_procs = procfs::process::all_processes().unwrap();
|
||||
|
||||
// build up a map between socket inodes and processes:
|
||||
let mut map: HashMap<u64, Stat> = HashMap::new();
|
||||
for p in all_procs {
|
||||
let process = p.unwrap();
|
||||
if let (Ok(stat), Ok(fds)) = (process.stat(), process.fd()) {
|
||||
for fd in fds {
|
||||
if let FDTarget::Socket(inode) = fd.unwrap().target {
|
||||
map.insert(inode, stat.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// get the tcp table
|
||||
let tcp = procfs::net::tcp().unwrap();
|
||||
let tcp6 = procfs::net::tcp6().unwrap();
|
||||
|
||||
println!(
|
||||
"{:<26} {:<26} {:<15} {:<8} {}",
|
||||
"Local address", "Remote address", "State", "Inode", "PID/Program name"
|
||||
);
|
||||
|
||||
for entry in tcp.into_iter().chain(tcp6) {
|
||||
// find the process (if any) that has an open FD to this entry's inode
|
||||
let local_address = format!("{}", entry.local_address);
|
||||
let remote_addr = format!("{}", entry.remote_address);
|
||||
let state = format!("{:?}", entry.state);
|
||||
if let Some(stat) = map.get(&entry.inode) {
|
||||
println!(
|
||||
"{:<26} {:<26} {:<15} {:<12} {}/{}",
|
||||
local_address, remote_addr, state, entry.inode, stat.pid, stat.comm
|
||||
);
|
||||
} else {
|
||||
// We might not always be able to find the process assocated with this socket
|
||||
println!(
|
||||
"{:<26} {:<26} {:<15} {:<12} -",
|
||||
local_address, remote_addr, state, entry.inode
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue