rust crate (refer this for crate documentation) to record /proc stats periodically. This library just records the stats. Processing may be done separate.
```rust use procshotserver::{Config, checksudo, scanproc}; use std::process; use users::getcurrentuid; use procshotclient; const DATADIR: &'static str = "/var/log/procshot/data";
fn main() { match checksudo(getcurrentuid()) { Err(e) => { eprintln!("Error encountered checking privileges, {:?}", e); process::exit(1); }, _ => (), } std::fs::createdirall(DATADIR).unwrap(); let config: Config = Config::new(); match config.server{ true => scanproc(config.delay, config.hostname, DATADIR), false => procshotclient::readtest_data(), } } ```
This will generate a binary with the following cli options
```bash
procshot 1.0 nohupped@gmail.com Snapshots proc periodically. All the options except delay works when 'server' option is not used.
USAGE: procshot [FLAGS] [OPTIONS] [SUBCOMMAND]
FLAGS: -h, --help Prints help information -o Sort result by Memory or CPU. Accepted values are... -t Read stats from a specific time. Accepted format: 2015-09-05 23:56:04 -V, --version Prints version information
OPTIONS:
-d, --delay
SUBCOMMANDS: help Prints this message or the help of the given subcommand(s) server Decides whether to run as server or client ```
```rust use std::fs::File; use std::io::Read; use procshotserver::EncoDecode; pub fn readtestdata() { let mut file = File::open("./testdata.procshot").unwrap(); let mut data = Vec::new(); file.readtoend(&mut data).unwrap(); let decoded: EncoDecode = bincode::deserialize(&data[..]).unwraporelse(|x| panic!("Error reading saved data. This was either created with an older version of procshot, or the file is corrupt. Error is {}", x)); println!("Decoded test file data: {:#?}", decoded); }
```
$ sudo ./target/release/procshot
```shell
Decoded test file data: EncoDecode {
hostname: "localghost",
pidmaplist: [
{
1: PidStatus {
ppid: 0,
euid: 0,
cmdlong: [
"/sbin/init",
],
name: "systemd",
cmdshort: "systemd",
tracerpid: 0,
fdsize: 256,
state: "S (sleeping)",
vmpeak: Some(
252840,
),
vmsize: Some(
187304,
),
rsspages: 2565,
rssbytes: 10506240,
rsslimbytes: 18446744073709551615,
processorlastexecuted: Some(
11,
),
utime: 62,
stime: 377,
},
},
{
373: PidStatus {
ppid: 1,
euid: 0,
cmdlong: [
"/usr/lib/systemd/systemd-journald",
],
name: "systemd-journal",
cmdshort: "systemd-journal",
tracerpid: 0,
fdsize: 256,
state: "S (sleeping)",
vmpeak: Some(
56756,
),
vmsize: Some(
49068,
),
rsspages: 2486,
rssbytes: 10182656,
rsslimbytes: 18446744073709551615,
processorlastexecuted: Some(
2,
),
utime: 50,
stime: 39,
},
},
..... snip .....
time_epoch: 1563617611,
delay: 5,
total_cpu_time: 6331606,
```