| Line | |
|---|
| 1 |
package Core::Cpu; |
|---|
| 2 |
|
|---|
| 3 |
use strict; |
|---|
| 4 |
use warnings; |
|---|
| 5 |
|
|---|
| 6 |
use base 'Resmon::Module'; |
|---|
| 7 |
|
|---|
| 8 |
use Resmon::ExtComm qw(run_command); |
|---|
| 9 |
|
|---|
| 10 |
=pod |
|---|
| 11 |
|
|---|
| 12 |
=head1 NAME |
|---|
| 13 |
|
|---|
| 14 |
Core::Cpu - check CPU usage |
|---|
| 15 |
|
|---|
| 16 |
=head1 SYNOPSIS |
|---|
| 17 |
|
|---|
| 18 |
Core::Cpu { |
|---|
| 19 |
local : vmstat_path => /usr/bin/vmstat |
|---|
| 20 |
} |
|---|
| 21 |
|
|---|
| 22 |
=head1 DESCRIPTION |
|---|
| 23 |
|
|---|
| 24 |
This module retrieves CPU statistics. |
|---|
| 25 |
|
|---|
| 26 |
=head1 CONFIGURATION |
|---|
| 27 |
|
|---|
| 28 |
=over |
|---|
| 29 |
|
|---|
| 30 |
=item check_name |
|---|
| 31 |
|
|---|
| 32 |
Arbitrary name of the check. |
|---|
| 33 |
|
|---|
| 34 |
=item vmstat_path |
|---|
| 35 |
|
|---|
| 36 |
Optional path to the vmstat executable. |
|---|
| 37 |
|
|---|
| 38 |
=item tail_path |
|---|
| 39 |
|
|---|
| 40 |
Optional path to the tail executable. |
|---|
| 41 |
|
|---|
| 42 |
=back |
|---|
| 43 |
|
|---|
| 44 |
=head1 METRICS |
|---|
| 45 |
|
|---|
| 46 |
=over |
|---|
| 47 |
|
|---|
| 48 |
=item user (time) |
|---|
| 49 |
|
|---|
| 50 |
=item system (time) |
|---|
| 51 |
|
|---|
| 52 |
=item idle (time) |
|---|
| 53 |
|
|---|
| 54 |
=back |
|---|
| 55 |
|
|---|
| 56 |
=cut |
|---|
| 57 |
|
|---|
| 58 |
sub handler { |
|---|
| 59 |
my $self = shift; |
|---|
| 60 |
my $config = $self->{'config'}; |
|---|
| 61 |
my $vmstat_path = $config->{'vmstat_path'} || 'vmstat'; |
|---|
| 62 |
my $tail_path = $config->{'tail_path'} || 'tail'; |
|---|
| 63 |
my $output = run_command("$vmstat_path 1 2 | $tail_path -1"); |
|---|
| 64 |
my $osname = $^O; |
|---|
| 65 |
my %metrics; |
|---|
| 66 |
my @keys = qw( user system idle ); |
|---|
| 67 |
my @values; |
|---|
| 68 |
|
|---|
| 69 |
$output =~ s/^\s+//; |
|---|
| 70 |
$output =~ s/\s+/ /g; |
|---|
| 71 |
if ($osname eq 'solaris') { |
|---|
| 72 |
@values = (split(/\s+/, $output))[19..21]; |
|---|
| 73 |
} elsif ($osname eq 'linux') { |
|---|
| 74 |
@values = (split(/\s+/, $output))[12..14]; |
|---|
| 75 |
} elsif ($osname eq 'openbsd') { |
|---|
| 76 |
@values = (split(/\s+/, $output))[16..18]; |
|---|
| 77 |
} elsif ($osname eq 'freebsd') { |
|---|
| 78 |
@values = (split(/\s+/, $output))[16..18]; |
|---|
| 79 |
} else { |
|---|
| 80 |
die "Unknown platform: $osname"; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
%metrics = map { $_ => shift(@values) } @keys; |
|---|
| 84 |
|
|---|
| 85 |
return { %metrics }; |
|---|
| 86 |
}; |
|---|
| 87 |
|
|---|
| 88 |
1; |
|---|