<commit_before>//! Check RAM usage of the currently-running container

extern crate rustc_serialize;
extern crate docopt;

extern crate turbine_plugins;

use std::cmp::max;

use docopt::Docopt;

use turbine_plugins::Status;
use turbine_plugins::sys::fs::cgroup::memory::{Stat, limit_in_bytes};
use turbine_plugins::linux::{pages_to_human_size, bytes_to_human_size};
use turbine_plugins::procfs::{RunningProcs, MemInfo};

static USAGE: &'static str = "
Usage:
    check-container-ram [--show-hogs=<count>] [--invalid-limit=<status>] [options]
    check-container-ram (-h | --help)

Check the RAM usage of the currently-running container. This must be run from
inside the container to be checked.

This checks as a ratio of the limit specified in the cgroup memory limit, and
if there is no limit set (or the limit is greater than the total memory
available on the system) this checks against the total system memory.

Options:
    -h, --help                 Show this message and exit

    -w, --warn=<percent>       Warn at this percent used           [default: 85]
    -c, --crit=<percent>       Critical at this percent used       [default: 95]

    --invalid-limit=<status>   Status to consider this check if the CGroup limit
                               is greater than the system ram      [default: ok]

    --show-hogs=<count>        Show the most ram-hungry procs      [default: 0]
";

#[derive(RustcDecodable, Debug)]
struct Args {
    flag_warn: f64,
    flag_crit: f64,

    flag_invalid_limit: Status,
    flag_show_hogs: u32
}

fn main() {
    let args: Args = Docopt::new(USAGE)
        .and_then(|d| d.decode())
        .unwrap_or_else(|e| e.exit());
    let mut status = Status::Ok;

    let mut limit = limit_in_bytes().unwrap();
    let mem = MemInfo::load();
    let system_bytes = mem.total.unwrap() * 1024;
    if limit > system_bytes {
        if args.flag_invalid_limit != Status::Ok {
            println!("{}: CGroup memory limit is greater than system memory ({} > {})",
                     args.flag_invalid_limit,
                     bytes_to_human_size(limit as u64),
                     bytes_to_human_size(system_bytes as u64));
            status = args.flag_invalid_limit;
        }
        limit = system_bytes;
    }

    let cgroup_stat = Stat::load().unwrap();
    let ratio = cgroup_stat.rss as f64 / limit as f64;
    let percent = ratio * 100.0;

    if percent > args.flag_crit {
        println!("CRITICAL: cgroup is using {:.1}% of {} limit (greater than {}%)",
                 percent, bytes_to_human_size(limit as u64), args.flag_crit);
        status = Status::Critical;
    } else if percent > args.flag_warn {
        println!("WARNING: cgroup is using {:.1}% of {} limit (greater than {}%)",
                 percent, bytes_to_human_size(limit as u64), args.flag_warn);
        status = max(status, Status::Warning);
    } else {
        println!("OK: cgroup is using {:.1}% of {} limit (less than {}%)",
                 percent, bytes_to_human_size(limit as u64), args.flag_warn);
    }<commit_msg>check-c-ram: Show the limit type in output<commit_after>//! Check RAM usage of the currently-running container

extern crate rustc_serialize;
extern crate docopt;

extern crate turbine_plugins;

use std::fmt;
use std::cmp::max;

use docopt::Docopt;

use turbine_plugins::Status;
use turbine_plugins::sys::fs::cgroup::memory::{Stat, limit_in_bytes};
use turbine_plugins::linux::{pages_to_human_size, bytes_to_human_size};
use turbine_plugins::procfs::{RunningProcs, MemInfo};

static USAGE: &'static str = "
Usage:
    check-container-ram [--show-hogs=<count>] [--invalid-limit=<status>] [options]
    check-container-ram (-h | --help)

Check the RAM usage of the currently-running container. This must be run from
inside the container to be checked.

This checks as a ratio of the limit specified in the cgroup memory limit, and
if there is no limit set (or the limit is greater than the total memory
available on the system) this checks against the total system memory.

Options:
    -h, --help                 Show this message and exit

    -w, --warn=<percent>       Warn at this percent used           [default: 85]
    -c, --crit=<percent>       Critical at this percent used       [default: 95]

    --invalid-limit=<status>   Status to consider this check if the CGroup limit
                               is greater than the system ram      [default: ok]

    --show-hogs=<count>        Show the most ram-hungry procs      [default: 0]
";

#[derive(RustcDecodable, Debug)]
struct Args {
    flag_warn: f64,
    flag_crit: f64,

    flag_invalid_limit: Status,
    flag_show_hogs: u32
}

enum Limit {
    CGroup,
    System
}

impl fmt::Display for Limit {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Limit::CGroup => write!(f, "cgroup limit"),
            Limit::System => write!(f, "system ram")
        }
    }
}

fn main() {
    let args: Args = Docopt::new(USAGE)
        .and_then(|d| d.decode())
        .unwrap_or_else(|e| e.exit());
    let mut status = Status::Ok;

    let mut limit = limit_in_bytes().unwrap();
    let mut limit_type = Limit::CGroup;
    let mem = MemInfo::load();
    let system_bytes = mem.total.unwrap() * 1024;
    if limit > system_bytes {
        if args.flag_invalid_limit != Status::Ok {
            println!("{}: CGroup memory limit is greater than system memory ({} > {})",
                     args.flag_invalid_limit,
                     bytes_to_human_size(limit as u64),
                     bytes_to_human_size(system_bytes as u64));
            status = args.flag_invalid_limit;
        }
        limit = system_bytes;
        limit_type = Limit::System;
    }

    let cgroup_stat = Stat::load().unwrap();
    let ratio = cgroup_stat.rss as f64 / limit as f64;
    let percent = ratio * 100.0;

    if percent > args.flag_crit {
        println!("CRITICAL: cgroup is using {:.1}% of {} {} (greater than {}%)",
                 percent, bytes_to_human_size(limit as u64), limit_type, args.flag_crit);
        status = Status::Critical;
    } else if percent > args.flag_warn {
        println!("WARNING: cgroup is using {:.1}% of {} {} (greater than {}%)",
                 percent, bytes_to_human_size(limit as u64), limit_type, args.flag_warn);
        status = max(status, Status::Warning);
    } else {
        println!("OK: cgroup is using {:.1}% of {} {} (less than {}%)",
                 percent, bytes_to_human_size(limit as u64), limit_type, args.flag_warn);
    }
{'fraction_non_alphanumeric': 0.1191059399877526, 'fraction_numerical': 0.011022657685241886, 'mean_word_length': 2.6909604519774013, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '970172', 'n_tokens_mistral': 2065, 'n_tokens_neox': 1971, 'n_words': 616}