File Coverage

blib/lib/Sys/Statistics/Linux/DiskUsage.pm
Criterion Covered Total %
statement 31 32 96.8
branch 3 6 50.0
condition 2 6 33.3
subroutine 5 5 100.0
pod 2 2 100.0
total 43 51 84.3


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Sys::Statistics::Linux::DiskUsage - Collect linux disk usage.
4              
5             =head1 SYNOPSIS
6              
7             use Sys::Statistics::Linux::DiskUsage;
8              
9             my $lxs = new Sys::Statistics::Linux::DiskUsage;
10             my $stat = $lxs->get;
11              
12             =head1 DESCRIPTION
13              
14             Sys::Statistics::Linux::DiskUsage gathers the disk usage with the command C.
15              
16             For more information read the documentation of the front-end module L.
17              
18             =head1 DISK USAGE INFORMATIONS
19              
20             Generated by F.
21              
22             total - The total size of the disk.
23             usage - The used disk space in kilobytes.
24             free - The free disk space in kilobytes.
25             usageper - The used disk space in percent.
26             mountpoint - The moint point of the disk.
27              
28             =head2 GLOBAL VARS
29              
30             If you want to change the path or arguments for C you can use the following
31             variables...
32              
33             $Sys::Statistics::Linux::DiskUsage::DF_PATH = '/bin';
34             $Sys::Statistics::Linux::DiskUsage::DF_CMD = 'df -akP';
35              
36             Example:
37              
38             use Sys::Statistics::Linux;
39             use Sys::Statistics::Linux::DiskUsage;
40             $Sys::Statistics::Linux::DiskUsage::DF_CMD = 'df -akP';
41              
42             my $sys = Sys::Statistics::Linux->new(diskusage => 1);
43             my $disk = $sys->get;
44              
45             =head1 METHODS
46              
47             =head2 new()
48              
49             Call C to create a new object.
50              
51             my $lxs = Sys::Statistics::Linux::DiskUsage->new;
52              
53             It's possible to set the path to df.
54              
55             Sys::Statistics::Linux::DiskUsage->new(
56             cmd => {
57             # This is the default
58             path => '/bin',
59             df => 'df -kP 2>/dev/null',
60             }
61             );
62              
63             =head2 get()
64              
65             Call C to get the statistics. C returns the statistics as a hash reference.
66              
67             my $stat = $lxs->get;
68              
69             =head1 EXPORTS
70              
71             No exports.
72              
73             =head1 SEE ALSO
74              
75             B
76              
77             =head1 REPORTING BUGS
78              
79             Please report all bugs to .
80              
81             =head1 AUTHOR
82              
83             Jonny Schulz .
84              
85             =head1 COPYRIGHT
86              
87             Copyright (c) 2006, 2007 by Jonny Schulz. All rights reserved.
88              
89             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
90              
91             =cut
92              
93             package Sys::Statistics::Linux::DiskUsage;
94              
95 2     2   12 use strict;
  2         4  
  2         88  
96 2     2   10 use warnings;
  2         3  
  2         74  
97 2     2   10 use Carp qw(croak);
  2         4  
  2         1116  
98              
99             our $VERSION = '0.14';
100             our $DF_PATH = undef;
101             our $DF_CMD = undef;
102              
103             sub new {
104 2     2 1 6 my $class = shift;
105 2 50       9 my $opts = ref($_[0]) ? shift : {@_};
106              
107 2         9 my %self = (
108             cmd => {
109             path => '/bin',
110             df => 'df -kP 2>/dev/null',
111             }
112             );
113              
114 2         3 foreach my $p (keys %{ $opts->{cmd} }) {
  2         9  
115 0         0 $self{cmd}{$p} = $opts->{cmd}->{$p};
116             }
117              
118 2         16 return bless \%self, $class;
119             }
120              
121             sub get {
122 2     2 1 4 my $self = shift;
123 2         5 my $class = ref $self;
124 2         16 my $cmd = $self->{cmd};
125 2   33     16 my $df_cmd = $DF_CMD || $cmd->{df};
126 2         3 my (%disk_usage);
127              
128 2   33     26 local $ENV{PATH} = $DF_PATH || $cmd->{path};
129 2 50       8454 open my $fh, "$df_cmd|" or croak "$class: unable to execute '$df_cmd' ($!)";
130              
131             # filter the header
132 2         44 {my $null = <$fh>;}
  2         12180  
133              
134 2         50 while (my $line = <$fh>) {
135 16 50       167 next unless $line =~ /^(.+?)\s+(.+)$/;
136              
137 16         153 @{$disk_usage{$1}}{qw(
  16         411  
138             total
139             usage
140             free
141             usageper
142             mountpoint
143             )} = (split /\s+/, $2)[0..4];
144              
145 16         172 $disk_usage{$1}{usageper} =~ s/%//;
146             }
147              
148 2         290 close($fh);
149 2         165 return \%disk_usage;
150             }
151              
152             1;