| 1 |
package Core::DiskFree; |
|---|
| 2 |
|
|---|
| 3 |
use strict; |
|---|
| 4 |
use warnings; |
|---|
| 5 |
|
|---|
| 6 |
use base 'Resmon::Module'; |
|---|
| 7 |
|
|---|
| 8 |
use Resmon::ExtComm qw(run_command cache_command); |
|---|
| 9 |
|
|---|
| 10 |
=pod |
|---|
| 11 |
|
|---|
| 12 |
=head1 NAME |
|---|
| 13 |
|
|---|
| 14 |
Core::DiskFree - Monitor disk free space using df |
|---|
| 15 |
|
|---|
| 16 |
=head1 SYNOPSIS |
|---|
| 17 |
|
|---|
| 18 |
Core::DiskFree { |
|---|
| 19 |
/ : noop |
|---|
| 20 |
/usr : noop |
|---|
| 21 |
/var : noop |
|---|
| 22 |
} |
|---|
| 23 |
|
|---|
| 24 |
Core::DiskFree { |
|---|
| 25 |
/ : dfcmd => /bin/df -kP |
|---|
| 26 |
} |
|---|
| 27 |
|
|---|
| 28 |
=head1 DESCRIPTION |
|---|
| 29 |
|
|---|
| 30 |
This module monitors the used/free space for a filesystem using the df |
|---|
| 31 |
command. |
|---|
| 32 |
|
|---|
| 33 |
Note: For ZFS filesystems, you should use the ZpoolFree module instead of |
|---|
| 34 |
this one to measure free space. |
|---|
| 35 |
|
|---|
| 36 |
=head1 CONFIGURATION |
|---|
| 37 |
|
|---|
| 38 |
=over |
|---|
| 39 |
|
|---|
| 40 |
=item check_name |
|---|
| 41 |
|
|---|
| 42 |
The name of the check refers to the filesystem to check the free space on. It |
|---|
| 43 |
can specify either the mountpoint or the device for the filesystem. |
|---|
| 44 |
|
|---|
| 45 |
=item dfcmd |
|---|
| 46 |
|
|---|
| 47 |
This specifies the df command (including arguments) to run in the event that |
|---|
| 48 |
the default is not sufficient. It is optional and in most cases you do not |
|---|
| 49 |
need to set this. |
|---|
| 50 |
|
|---|
| 51 |
=back |
|---|
| 52 |
|
|---|
| 53 |
=head1 METRICS |
|---|
| 54 |
|
|---|
| 55 |
=over |
|---|
| 56 |
|
|---|
| 57 |
=item used_KB |
|---|
| 58 |
|
|---|
| 59 |
The used disk space in KB. |
|---|
| 60 |
|
|---|
| 61 |
=item free_KB |
|---|
| 62 |
|
|---|
| 63 |
The free disk space in KB. |
|---|
| 64 |
|
|---|
| 65 |
=item used_percent |
|---|
| 66 |
|
|---|
| 67 |
The percentage of the disk that is full. |
|---|
| 68 |
|
|---|
| 69 |
=back |
|---|
| 70 |
|
|---|
| 71 |
=cut |
|---|
| 72 |
|
|---|
| 73 |
sub new { |
|---|
| 74 |
# This is only needed if you have initialization code. Most of the time, |
|---|
| 75 |
# you can skip the new method and just implement a handler method. |
|---|
| 76 |
my ($class, $check_name, $config) = @_; |
|---|
| 77 |
my $self = $class->SUPER::new($check_name, $config); |
|---|
| 78 |
|
|---|
| 79 |
# Come up with a sensible default for the df command args |
|---|
| 80 |
$self->{default_dfcmd} = ($^O eq 'linux') ? 'df -kP' : 'df -k'; |
|---|
| 81 |
|
|---|
| 82 |
bless($self, $class); |
|---|
| 83 |
return $self; |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
sub handler { |
|---|
| 87 |
my $self = shift; |
|---|
| 88 |
my $config = $self->{config}; |
|---|
| 89 |
my $fs = $self->{check_name}; |
|---|
| 90 |
my $dfcmd = $config->{dfcmd} || $self->{default_dfcmd}; |
|---|
| 91 |
|
|---|
| 92 |
my $output = run_command("$dfcmd $fs"); |
|---|
| 93 |
my ($line) = grep(/$fs\s*/, split(/\n/, $output)); |
|---|
| 94 |
if($line =~ /(\d+)\s+(\d+)\s+(\d+)%/) { |
|---|
| 95 |
return { |
|---|
| 96 |
"used_KB" => [$1, "i"], |
|---|
| 97 |
"free_KB" => [$2, "i"], |
|---|
| 98 |
"used_percent" => [$3, "i"] |
|---|
| 99 |
}; |
|---|
| 100 |
} else { |
|---|
| 101 |
# We couldn't get the free space |
|---|
| 102 |
die "Unable to get free space\n"; |
|---|
| 103 |
} |
|---|
| 104 |
}; |
|---|
| 105 |
|
|---|
| 106 |
1; |
|---|