root/lib/Resmon/Module/CPU.pm

Revision 9388834a964cf8c12dc67ed5efca3141c123dc1b, 1.1 kB (checked in by Mark Harrison <mark@omniti.com>, 5 years ago)

First draft of CPU check

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

  • Property mode set to 100644
Line 
1 package Resmon::Module::CPU;
2 use Resmon::ExtComm qw/cache_command/;
3 use Resmon::Module;
4 use vars qw/@ISA/;
5 @ISA = qw/Resmon::Module/;
6
7 sub handler {
8     my $arg = shift;
9     my $timeperiod = 1;
10     my $limit = $arg->{'limit'} || "90";
11     my $output = cache_command("vmstat $timeperiod 2", 60);
12     my @lines = split(/\n/, $output);
13     for (@lines) {
14         if($_ =~ /us sy/) {
15             # we have a header line
16             @headers = split(/ +/, $_);
17             break;
18         }
19     }
20
21     @vals = split(/ +/, $lines[-1]);
22
23     $cpuuser = 0;
24     $cpusys  = 0;
25     $cpuwait = 0;
26
27     $fieldcount = @headers;
28     for (my $i=0; $i < $fieldcount; $i++) {
29         if ($headers[$i] eq 'us') {
30             $cpuuser = $vals[$i];
31         }
32         if ($headers[$i] eq 'sy') {
33             $cpusys = $vals[$i];
34         }
35         if ($headers[$i] eq 'wa') {
36             $cpuwait = $vals[$i];
37         }
38     }
39
40     #print "$cpuuser, $cpusys, $cpuwait\n";
41     $total = $cpuuser + $cpusys + $cpuwait;
42
43     if ($total > $limit) {
44         return "BAD", "$total > $limit";
45     } else {
46         return "OK", "$total <= $limit";
47     }
48
49     return "BAD", "Unable to determine cpu info";
50 };
51 1;
Note: See TracBrowser for help on using the browser.