root/lib/Core/Cpu.pm

Revision 93a3227a1d25c9f5fcfe6d8840dd8158c37cd40b, 1.5 kB (checked in by Jason Dixon <jdixon@omniti.com>, 4 years ago)

new CPU module

git-svn-id: https://labs.omniti.com/resmon/branches/resmon2@303 8c0face9-b7db-6ec6-c4b3-d5f7145c7d55

  • Property mode set to 100644
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 : path_to_vmstat => /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 path_to_vmstat
35
36 Optional path to the vmstat executable.
37
38 =item path_to_tail
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 =item error_msg
55
56 =back
57
58 =cut
59
60 sub handler {
61     my $self = shift;
62     my $config = $self->{'config'};
63     my $path_to_vmstat = $config->{'path_to_vmstat'} || 'vmstat';
64     my $path_to_tail = $config->{'path_to_tail'} || 'tail';
65     my $output = run_command("$path_to_vmstat 1 2 | $path_to_tail -1");
66     my $osname = $^O;
67     my %metrics;
68     my @keys = qw( user system idle );
69     my @values;
70
71     $output =~ s/^\s+//;
72     $output =~ s/\s+/ /g;
73     if ($osname eq 'solaris') {
74         @values = (split($output))[19..21];
75     } elsif ($osname eq 'linux') {
76         @values = (split($output))[12..14];
77     } elsif ($osname eq 'openbsd') {
78         @values = (split(/\s+/, $output))[16..18];
79     } elsif ($osname eq 'freebsd') {
80         @values = (split($output))[16..18];
81     } else {
82         return { 'error_msg' => 'unknown operating system' };
83     }
84
85     %metrics = map { $_ => shift(@values) } @keys;
86
87     return { %metrics };
88 };
89
90 1;
Note: See TracBrowser for help on using the browser.