Giter Club home page Giter Club logo

procshot_server's Introduction

procshot_server Build Status

A lame attempt of a rust crate (refer this for crate documentation) to record /proc stats periodically. This library just records the stats. Processing to be done separately. This is written as a part of learning rust.

Server example

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(),
    }
}

This will generate a binary with the following cli options

 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

Client example on how to read the stored data

 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);
 }

Sample output of stored data

$ 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,

procshot_server's People

Contributors

nohupped avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.