diff --git a/lib/Core/ZpoolFree.pm b/lib/Core/ZpoolFree.pm index 3f6ea5733c24f2ac77503c8f9ca363f89251b805..7ab7feb3183152691dfb210b69671974f16ef09a 100644 --- a/lib/Core/ZpoolFree.pm +++ b/lib/Core/ZpoolFree.pm @@ -19,6 +19,10 @@ Core::ZpoolFree - monitor free space available on ZFS pools * : noop } + Core::ZpoolFree { + poolname : noop + } + Core::ZpoolFree { * : zfs_path => '/sbin/zfs' } @@ -91,6 +95,36 @@ our %units = ( 'Z' => 1180591620717411303424 ); +sub handler { + my $self = shift; + my $config = $self->{config}; # All configuration is in here + my $pool = $self->{check_name}; + my $zfs_command = $config->{zfs_command} || "/sbin/zfs"; + my $zpool_command = $config->{zpool_command} || "/sbin/zpool"; + my $status = {}; + # Sanity check + die "Invalid pool name: $pool" if $pool !~ /[a-zA-Z0-9_.-]+/; + my $zfs_output = run_command( + "$zfs_command list -H -o name,used,avail $pool"); + my ($name, $used, $uunit, $free, $funit) = $zfs_output =~ + /(\S+)\s+([0-9.]+)([BKMGTPEZ]?)\s+([0-9.]+)([BKMGTPEZ]?)/; + # Make sure we were able to match the regex + die "Unable to parse zfs command output: $zfs_output\n" + unless defined($name); + next if ($name =~ /\//); # We're only interested in the root of a pool + + # Convert human readable units to bytes + $used = $used * $units{$uunit} if $uunit; + $free = $free * $units{$funit} if $funit; + + my $percent_full = sprintf("%.2f", ($used / ($used + $free)) * 100); + $status->{"used_MB"} = [int($used/1048576), "i"]; + $status->{"free_MB"} = [int($free/1048576), "i"]; + $status->{"percent_full"} = [$percent_full, "n"]; + + return $status; +}; + sub wildcard_handler { my $self = shift; my $config = $self->{config}; # All configuration is in here