<commit_before>package Resmon::Module::CPU;
use Resmon::ExtComm qw/cache_command/;
use Resmon::Module;
use vars qw/@ISA/;
@ISA = qw/Resmon::Module/;

sub handler {
    my $arg = shift;
    my $timeperiod = 1;
    my $limit = $arg->{'limit'} || "90";
    my $output = cache_command("vmstat $timeperiod 2", 60);
    my @lines = split(/\n/, $output);
    for (@lines) {
        if($_ =~ /us sy/) {
            # we have a header line
            @headers = split(/ +/, $_);
            break;
        }
    }

    @vals = split(/ +/, $lines[-1]);

    $cpuuser = 0;
    $cpusys  = 0;
    $cpuwait = 0;

    $fieldcount = @headers;
    for (my $i=0; $i < $fieldcount; $i++) {
        if ($headers[$i] eq 'us') {
            $cpuuser = $vals[$i];
        }
        if ($headers[$i] eq 'sy') {
            $cpusys = $vals[$i];
        }
        if ($headers[$i] eq 'wa') {
            $cpuwait = $vals[$i];
        }
    }

    #print "$cpuuser, $cpusys, $cpuwait\n";
    $total = $cpuuser + $cpusys + $cpuwait;

    if ($total > $limit) {
        return "BAD", "$total > $limit";
    } else {
        return "OK", "$total <= $limit";
    }

    return "BAD", "Unable to determine cpu info";
};
1;<commit_msg>Solaris/BSD support + show individual user/sys/wait times in the output<commit_after>package Resmon::Module::CPU;
use Resmon::ExtComm qw/cache_command/;
use Resmon::Module;
use vars qw/@ISA/;
@ISA = qw/Resmon::Module/;

sub handler {
    my $arg = shift;
    my $timeperiod = 1;
    my $limit = $arg->{'limit'} || "90";
    my $output = cache_command("vmstat $timeperiod 2", 60);
    my @lines = split(/\n/, $output);
    for (@lines) {
        if($_ =~ /us sy/) {
            # we have a header line
            @headers = split(/ +/, $_);
            break;
        }
    }

    @vals = split(/ +/, $lines[-1]);

    $cpuuser = 0;
    $cpusys  = 0;
    $cpuwait = 0;
    $havewait = 0;

    $fieldcount = @headers;
    # Look at the field header names to determine which values to use
    for (my $i=0; $i < $fieldcount; $i++) {
        # Need to compare previous/next header because solaris has two 'sy'
        # values - one for cpu and one for interrupts
        if ($headers[$i] eq 'us' && $headers[$i+1] eq 'sy') {
            $cpuuser = $vals[$i];
        }
        if ($headers[$i] eq 'sy' && $headers[$i-1] eq 'us') {
            $cpusys = $vals[$i];
        }
        # Wait appears to be a linux thing, so on other platforms it will just
        # be 0
        if ($headers[$i] eq 'wa') {
            $cpuwait = $vals[$i];
            $havewait = 1;
        }
    }

    #print "$cpuuser, $cpusys, $cpuwait\n";
    $total = $cpuuser + $cpusys + $cpuwait;

    $summary = "- us:$cpuuser sy:$cpusys";
    if ($havewait) {
        $summary = "$summary wa:$cpuwait";
    }

    if ($total > $limit) {
        return "BAD", "$total > $limit $summary";
    } else {
        return "OK", "$total <= $limit $summary";
    }

    return "BAD", "Unable to determine cpu info";
};
1;
{'fraction_non_alphanumeric': 0.18105756358768407, 'fraction_numerical': 0.009705488621151271, 'mean_word_length': 1.9979939819458374, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3709324', 'n_tokens_mistral': 1073, 'n_tokens_neox': 1028, 'n_words': 293}