File Coverage

blib/lib/Linux/Info/LoadAVG.pm
Criterion Covered Total %
statement 24 25 96.0
branch 3 6 50.0
condition n/a
subroutine 5 5 100.0
pod 2 2 100.0
total 34 38 89.4


line stmt bran cond sub pod time code
1             package Linux::Info::LoadAVG;
2 1     1   6 use strict;
  1         2  
  1         25  
3 1     1   5 use warnings;
  1         1  
  1         28  
4 1     1   5 use Carp qw(croak);
  1         1  
  1         282  
5             our $VERSION = '1.3'; # VERSION
6              
7             =head1 NAME
8              
9             Linux::Info::LoadAVG - Collect linux load average statistics.
10              
11             =head1 SYNOPSIS
12              
13             use Linux::Info::LoadAVG;
14              
15             my $lxs = Linux::Info::LoadAVG->new;
16             my $stat = $lxs->get;
17              
18             =head1 DESCRIPTION
19              
20             Linux::Info::LoadAVG gathers the load average from the virtual F filesystem (procfs).
21              
22             For more information read the documentation of the front-end module L.
23              
24             =head1 LOAD AVERAGE STATISTICS
25              
26             Generated by F.
27              
28             avg_1 - The average processor workload of the last minute.
29             avg_5 - The average processor workload of the last five minutes.
30             avg_15 - The average processor workload of the last fifteen minutes.
31              
32             =head1 METHODS
33              
34             =head2 new()
35              
36             Call C to create a new object.
37              
38             my $lxs = Linux::Info::LoadAVG->new;
39              
40             It's possible to set the path to the proc filesystem.
41              
42             Linux::Info::LoadAVG->new(
43             files => {
44             # This is the default
45             path => '/proc',
46             loadavg => 'loadavg',
47             }
48             );
49              
50             =head2 get()
51              
52             Call C to get the statistics. C returns the statistics as a hash reference.
53              
54             my $stat = $lxs->get;
55              
56             =head1 EXPORTS
57              
58             Nothing.
59              
60             =head1 SEE ALSO
61              
62             =over
63              
64             =item *
65              
66             B
67              
68             =item *
69              
70             L
71              
72             =back
73              
74             =head1 AUTHOR
75              
76             Alceu Rodrigues de Freitas Junior, Earfreitas@cpan.orgE
77              
78             =head1 COPYRIGHT AND LICENSE
79              
80             This software is copyright (c) 2015 of Alceu Rodrigues de Freitas Junior, Earfreitas@cpan.orgE
81              
82             This file is part of Linux Info project.
83              
84             Linux-Info is free software: you can redistribute it and/or modify
85             it under the terms of the GNU General Public License as published by
86             the Free Software Foundation, either version 3 of the License, or
87             (at your option) any later version.
88              
89             Linux-Info is distributed in the hope that it will be useful,
90             but WITHOUT ANY WARRANTY; without even the implied warranty of
91             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
92             GNU General Public License for more details.
93              
94             You should have received a copy of the GNU General Public License
95             along with Linux Info. If not, see .
96              
97             =cut
98              
99             sub new {
100 1     1 1 2 my $class = shift;
101 1 50       4 my $opts = ref( $_[0] ) ? shift : {@_};
102              
103 1         3 my %self = (
104             files => {
105             path => '/proc',
106             loadavg => 'loadavg',
107             }
108             );
109              
110 1         2 foreach my $file ( keys %{ $opts->{files} } ) {
  1         3  
111 0         0 $self{files}{$file} = $opts->{files}->{$file};
112             }
113              
114 1         5 return bless \%self, $class;
115             }
116              
117             sub get {
118 1     1 1 2 my $self = shift;
119 1         2 my $class = ref $self;
120 1         3 my $file = $self->{files};
121 1         2 my %lavg = ();
122              
123             my $filename =
124 1 50       4 $file->{path} ? "$file->{path}/$file->{loadavg}" : $file->{loadavg};
125 1 50       29 open my $fh, '<', $filename
126             or croak "$class: unable to open $filename ($!)";
127              
128 1         25 ( $lavg{avg_1}, $lavg{avg_5}, $lavg{avg_15} ) =
129             ( split /\s+/, <$fh> )[ 0 .. 2 ];
130              
131 1         11 close($fh);
132 1         6 return \%lavg;
133             }
134              
135             1;