File Coverage

blib/lib/Sys/Statistics/Linux/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             =head1 NAME
2              
3             Sys::Statistics::Linux::LoadAVG - Collect linux load average statistics.
4              
5             =head1 SYNOPSIS
6              
7             use Sys::Statistics::Linux::LoadAVG;
8              
9             my $lxs = Sys::Statistics::Linux::LoadAVG->new;
10             my $stat = $lxs->get;
11              
12             =head1 DESCRIPTION
13              
14             Sys::Statistics::Linux::LoadAVG gathers the load average from the virtual F filesystem (procfs).
15              
16             For more information read the documentation of the front-end module L.
17              
18             =head1 LOAD AVERAGE STATISTICS
19              
20             Generated by F.
21              
22             avg_1 - The average processor workload of the last minute.
23             avg_5 - The average processor workload of the last five minutes.
24             avg_15 - The average processor workload of the last fifteen minutes.
25              
26             =head1 METHODS
27              
28             =head2 new()
29              
30             Call C to create a new object.
31              
32             my $lxs = Sys::Statistics::Linux::LoadAVG->new;
33              
34             It's possible to set the path to the proc filesystem.
35              
36             Sys::Statistics::Linux::LoadAVG->new(
37             files => {
38             # This is the default
39             path => '/proc',
40             loadavg => 'loadavg',
41             }
42             );
43              
44             =head2 get()
45              
46             Call C to get the statistics. C returns the statistics as a hash reference.
47              
48             my $stat = $lxs->get;
49              
50             =head1 EXPORTS
51              
52             No exports.
53              
54             =head1 SEE ALSO
55              
56             B
57              
58             =head1 REPORTING BUGS
59              
60             Please report all bugs to .
61              
62             =head1 AUTHOR
63              
64             Jonny Schulz .
65              
66             =head1 COPYRIGHT
67              
68             Copyright (c) 2006, 2007 by Jonny Schulz. All rights reserved.
69              
70             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
71              
72             =cut
73              
74             package Sys::Statistics::Linux::LoadAVG;
75              
76 1     1   7 use strict;
  1         2  
  1         40  
77 1     1   5 use warnings;
  1         1  
  1         35  
78 1     1   5 use Carp qw(croak);
  1         2  
  1         402  
79              
80             our $VERSION = '0.08';
81              
82             sub new {
83 1     1 1 2 my $class = shift;
84 1 50       3 my $opts = ref($_[0]) ? shift : {@_};
85              
86 1         5 my %self = (
87             files => {
88             path => '/proc',
89             loadavg => 'loadavg',
90             }
91             );
92              
93 1         1 foreach my $file (keys %{ $opts->{files} }) {
  1         4  
94 0         0 $self{files}{$file} = $opts->{files}->{$file};
95             }
96              
97 1         7 return bless \%self, $class;
98             }
99              
100             sub get {
101 1     1 1 2 my $self = shift;
102 1         1 my $class = ref $self;
103 1         5 my $file = $self->{files};
104 1         3 my %lavg = ();
105              
106 1 50       5 my $filename = $file->{path} ? "$file->{path}/$file->{loadavg}" : $file->{loadavg};
107 1 50       43 open my $fh, '<', $filename or croak "$class: unable to open $filename ($!)";
108              
109 1         30 ( $lavg{avg_1}
110             , $lavg{avg_5}
111             , $lavg{avg_15}
112             ) = (split /\s+/, <$fh>)[0..2];
113              
114 1         11 close($fh);
115 1         6 return \%lavg;
116             }
117              
118             1;