| 1 | # | ||
| 2 | #=============================================================================== | ||
| 3 | # | ||
| 4 | # FILE: Sar.pm | ||
| 5 | # | ||
| 6 | # DESCRIPTION: generic info from sar | ||
| 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:51:12 | ||
| 15 | # REVISION: --- | ||
| 16 | #=============================================================================== | ||
| 17 | package Sysstat::Sar; | ||
| 18 | # ABSTRACT: Sysstat sar file parser | ||
| 19 | use utf8; | ||
| 20 | use Carp; | ||
| 21 | use v5.10.1; | ||
| 22 | use Mo; | ||
| 23 | use Smart::Comments -ENV; | ||
| 24 | extends 'Sysstat::Sar::CPU'; | ||
| 25 | |||
| 26 | has 'sarfilepath' => ( is => 'ro' ); | ||
| 27 | has 'hostname' => (is => 'rw'); | ||
| 28 | has 'date' => (is => 'rw'); | ||
| 29 | |||
| 30 | sub BUILD | ||
| 31 | { | ||
| 32 | my $self = shift; | ||
| 33 | |||
| 34 | #------------------------------------------------------------------------------- | ||
| 35 | # die if sar file is not found | ||
| 36 | #------------------------------------------------------------------------------- | ||
| 37 | ### [<now>] [<file>][<line>] check if sar file exists... | ||
| 38 | carp "sar file not valid" unless -f $self->sarfilepath; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub parse | ||
| 42 | { | ||
| 43 | my $self = shift; | ||
| 44 | my %result; | ||
| 45 | |||
| 46 | ### [<now>] [<file>][<line>] opening sar file... | ||
| 47 | open ( my $fh, "<", $self->sarfilepath ) or carp "$!"; | ||
| 48 | |||
| 49 | ### [<now>] [<file>][<line>] loop lines... | ||
| 50 | while ( <$fh> ) | ||
| 51 | { | ||
| 52 | $self->check_header( $_, \%result ); | ||
| 53 | $self->is_cpu( $_, \%result ); | ||
| 54 | } | ||
| 55 | |||
| 56 | close $fh; | ||
| 57 | |||
| 58 | return \%result; | ||
| 59 | } | ||
| 60 | |||
| 61 | #------------------------------------------------------------------------------- | ||
| 62 | # return $result hash ref hostname->date->{os,kernel,arch,totalcpu} | ||
| 63 | #------------------------------------------------------------------------------- | ||
| 64 | sub check_header | ||
| 65 | { | ||
| 66 | my $self = shift; | ||
| 67 | my $line = shift; | ||
| 68 | my $result = shift; | ||
| 69 | |||
| 70 | if ( $line =~ /.*CPU\)/ ) | ||
| 71 | { | ||
| 72 | # return array of (OS, kernel, hostname, date, arch, no_of_cpu) | ||
| 73 | my @header = split '\s+' => $line; | ||
| 74 | my $hostname = $header[ 2 ]; | ||
| 75 | my $date = $header[ 3 ]; | ||
| 76 | |||
| 77 | #------------------------------------------------------------------------------- | ||
| 78 | # remove '(' and ')' character on hostname | ||
| 79 | #------------------------------------------------------------------------------- | ||
| 80 | $hostname =~ s/(\(|\))//g; | ||
| 81 | |||
| 82 | $$result{ $hostname }{ $date }{ os } = $header[ 0 ]; | ||
| 83 | $$result{ $hostname }{ $date }{ kernel } = $header[ 1 ]; | ||
| 84 | $$result{ $hostname }{ $date }{ arch } = $header[ 4 ]; | ||
| 85 | $$result{ $hostname }{ $date }{ totalcpu } = $header[ 5 ] . $header[ 6 ]; | ||
| 86 | |||
| 87 | $self->hostname($hostname); | ||
| 88 | $self->date($date); | ||
| 89 | |||
| 90 | return $result; | ||
| 91 | } | ||
| 92 | else | ||
| 93 | { | ||
| 94 | return undef; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | 1; | ||
| 99 | |||
| 100 | __END__ |