| 1 | # | ||
| 2 | #=============================================================================== | ||
| 3 | # | ||
| 4 | # FILE: CPU.pm | ||
| 5 | # | ||
| 6 | # DESCRIPTION: get sar information for CPU | ||
| 7 | # | ||
| 8 | # FILES: --- | ||
| 9 | # BUGS: --- | ||
| 10 | # NOTES: --- | ||
| 11 | # AUTHOR: Heince Kurniawan (), heince.kurniawan@itgroupinc.asia | ||
| 12 | # ORGANIZATION: IT Group Indonesia | ||
| 13 | # VERSION: 1.0 | ||
| 14 | # CREATED: 10/14/16 18:48:42 | ||
| 15 | # REVISION: --- | ||
| 16 | #=============================================================================== | ||
| 17 | package Sysstat::Sar::CPU; | ||
| 18 | # ABSTRACT: Sysstat sar module to handle cpu information | ||
| 19 | use utf8; | ||
| 20 | use Carp; | ||
| 21 | use v5.10.1; | ||
| 22 | use Mo; | ||
| 23 | use Smart::Comments -ENV; | ||
| 24 | |||
| 25 | #------------------------------------------------------------------------------- | ||
| 26 | # detect if line is cpu | ||
| 27 | # - if second element is 'all' and >= 0 | ||
| 28 | # - total element is 11 | ||
| 29 | #------------------------------------------------------------------------------- | ||
| 30 | sub is_cpu | ||
| 31 | { | ||
| 32 | my $self = shift; | ||
| 33 | my $line = shift; | ||
| 34 | my $result = shift; | ||
| 35 | |||
| 36 | my @array = split '\s+' => $line; | ||
| 37 | if ( $#array == 10 and $array[1] =~ /^(all|\d+)/ and $array[0] =~ /^(\d+|Average)/ ) | ||
| 38 | { | ||
| 39 | ### [<now>] [<file>][<line>] line is detected as cpu information... | ||
| 40 | |||
| 41 | my $time = shift @array; # this can be time or 'Average' | ||
| 42 | my $cpu = shift @array; | ||
| 43 | |||
| 44 | if ($time =~ /^Average/) | ||
| 45 | { | ||
| 46 | ### [<now>] [<file>][<line>] array = (usr, nice, sys, iowait, steal, irq, soft, guest, idle) | ||
| 47 | ### [<now>] [<file>][<line>] Processing CPU ($cpu) Average... | ||
| 48 | ### [<now>] [<file>][<line>] Got: $line | ||
| 49 | |||
| 50 | push @{$$result{$self->hostname}{$self->date}{cpu}{'Average'}{$cpu}} , [@array]; | ||
| 51 | } | ||
| 52 | else | ||
| 53 | { | ||
| 54 | ### [<now>] [<file>][<line>] array = (time, usr, nice, sys, iowait, steal, irq, soft, guest, idle) | ||
| 55 | ### [<now>] [<file>][<line>] Processing CPU ($cpu) detail... | ||
| 56 | unshift @array, $time; | ||
| 57 | ### @array | ||
| 58 | push @{$$result{$self->hostname}{$self->date}{cpu}{$cpu}} , [@array]; | ||
| 59 | } | ||
| 60 | |||
| 61 | return $result; | ||
| 62 | } | ||
| 63 | else | ||
| 64 | { | ||
| 65 | return undef; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | 1; | ||
| 70 | |||
| 71 | __END__ |