#stats #proc #procfs #record #scan #continuously #server

procshot_server

此crate可用于持续扫描/proc文件系统并将数据写入datadir。这是一个对procfs crate的封装,因此此crate的兼容性取决于procfs crate的兼容性。

6个版本

0.1.5 2019年10月27日
0.1.4 2019年7月27日

#1224 in 文件系统

MIT/Apache

26KB
325

procshot_server 构建状态

这是一个rust crate (参考此处的crate文档)的拙劣尝试,以定期记录/proc统计信息。此库仅记录统计信息。处理将在单独的部分进行。这是作为学习rust的一部分编写的。

服务器示例

use procshot_server::{Config, check_sudo, scan_proc};
use std::process;
use users::get_current_uid;
use procshot_client;
const DATADIR: &'static str = "/var/log/procshot/data";

fn main() {
    match check_sudo(get_current_uid()) {
        Err(e) => {
            eprintln!("Error encountered checking privileges, {:?}", e);
            process::exit(1);
        },
        _ => (),
    }
    std::fs::create_dir_all(DATADIR).unwrap();
    let config: Config = Config::new();
    match config.server{
        true => scan_proc(config.delay, config.hostname, DATADIR),
        false => procshot_client::read_test_data(),
    }
}

这将生成一个具有以下命令行选项的二进制文件


 procshot 1.0
 [email protected]
 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 <delay>      Sets delay in seconds before it scans /proc every time. Defaults to 60 seconds. [default: 60]


 SUBCOMMANDS:
     help      Prints this message or the help of the given subcommand(s)
     server    Decides whether to run as server or client

如何读取存储数据的客户端示例

 use std::fs::File;
 use std::io::Read;
 use procshot_server::EncoDecode;
 pub fn read_test_data() {
         let mut file = File::open("./test_data.procshot").unwrap();
         let mut data = Vec::new();
         file.read_to_end(&mut data).unwrap();
         let decoded: EncoDecode = bincode::deserialize(&data[..]).unwrap_or_else(|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

Decoded test file data: EncoDecode {
    hostname: "localghost",
    pid_map_list: {
        169883: PidStatus {
            ppid: 3284,
            euid: 1000,
            cmd_long: [
                "/opt/google/chrome/chrome --type=renderer --field-trial-handle=11321657219978072658,9761930160602287450,131072 --lang=en-US --disable-client-side-phishing-detection --enable-auto-reload --num-raster-threads=4 --enable-main-frame-before-activation --service-request-channel-token=1438087544763584805 --renderer-client-id=214 --no-v8-untrusted-code-mitigations --shared-files=v8_context_snapshot_data:100,v8_natives_data:101",
            ],
            name: "chrome",
            cmd_short: "chrome",
            tracerpid: 0,
            fdsize: 64,
            state: "S (sleeping)",
            vmpeak: Some(
                549888,
            ),
            vmsize: Some(
                525300,
            ),
            rss_pages: 13139,
            rss_bytes: 53817344,
            rsslim_bytes: 18446744073709551615,
            processor_last_executed: Some(
                4,
            ),
            utime: 5,
            stime: 2,
            user_cpu_usage: 0.0,
            sys_cpu_usage: 0.0,
        },
        2078: PidStatus {
            ppid: 1783,
            euid: 1000,
            cmd_long: [
                "/usr/lib/at-spi2-registryd",
                "--use-gnome-session",
            ],
            name: "at-spi2-registr",
            cmd_short: "at-spi2-registr",
            tracerpid: 0,
            fdsize: 64,
            state: "S (sleeping)",
            vmpeak: Some(
                229136,
            ),
            vmsize: Some(
                163600,
            ),
            rss_pages: 1472,
            rss_bytes: 6029312,
            rsslim_bytes: 18446744073709551615,
            processor_last_executed: Some(
                2,
            ),
            utime: 1,
            stime: 0,
            user_cpu_usage: 0.0,
            sys_cpu_usage: 0.0,
        },
        2130: PidStatus {
            ppid: 1783,
            euid: 1000,
            cmd_long: [
                "/usr/lib/gsd-screensaver-proxy",
            ],
            name: "gsd-screensaver",
            cmd_short: "gsd-screensaver",
            tracerpid: 0,
            fdsize: 64,
            state: "S (sleeping)",
            vmpeak: Some(
                234084,
            ),
            vmsize: Some(
                234084,
            ),
            rss_pages: 1094,
            rss_bytes: 4481024,
            rsslim_bytes: 18446744073709551615,
            processor_last_executed: Some(
                6,
            ),
            utime: 1,
            stime: 0,
            user_cpu_usage: 0.0,
            sys_cpu_usage: 0.0,
        },
        2112: PidStatus {
            ppid: 1783,
            euid: 1000,
            cmd_long: [
                "/usr/lib/gsd-housekeeping",
            ],
            name: "gsd-housekeepin",
            cmd_short: "gsd-housekeepin",
            tracerpid: 0,
            fdsize: 64,
            state: "S (sleeping)",
            vmpeak: Some(
                375152,
            ),
            vmsize: Some(
                310204,
            ),
            rss_pages: 1764,
            rss_bytes: 7225344,
            rsslim_bytes: 18446744073709551615,
            processor_last_executed: Some(
                10,
            ),
            utime: 7,
            stime: 7,
            user_cpu_usage: 0.0,
            sys_cpu_usage: 0.0,
        },
..... snip .....

    time_epoch: 1563617611,
    delay: 5,
    total_cpu_time: 6331606,


依赖项

~2.8–4MB
~65K SLoC