root/lib/Core/ZpoolFree.pm

Revision e94b190fea86764ca5f320ba34d11a4ca03dedba, 2.8 kB (checked in by Mark Harrison <mark@omniti.com>, 4 years ago)

Zpool free space module

git-svn-id: https://labs.omniti.com/resmon/branches/resmon2@298 8c0face9-b7db-6ec6-c4b3-d5f7145c7d55

  • Property mode set to 100644
Line 
1 package Core::ZpoolFree;
2
3 use strict;
4 use warnings;
5
6 use base 'Resmon::Module';
7
8 use Resmon::ExtComm qw(run_command);
9
10 =pod
11
12 =head1 NAME
13
14 Core::ZpoolFree - monitor free space available on ZFS pools
15
16 =head1 SYNOPSIS
17
18  Core::ZpoolFree {
19      zpools: noop
20  }
21
22  Core::ZpoolFree {
23      zpools: zfs_path = '/sbin/zfs'
24  }
25
26 =head1 DESCRIPTION
27
28 This module monitors the free space on ZFS pools. Free space is reported for
29 all pools on the system. Multiple checks are not required for individual
30 pools.
31
32 Implementation Note: The 'zfs list' command is used rather than 'zpool list'
33 in order to get a more accurate view of the available space in certain cases
34 where zpool list does not report the true usable free space (e.g. raidz
35 pools). See http://www.cuddletech.com/blog/pivot/entry.php?id=1013 for another
36 case where zpool list does not report the correct values for monitoring.
37
38 =head1 CONFIGURATION
39
40 =over
41
42 =item check_name
43
44 The check name is descriptive only in this check. It is not used for anything.
45
46 =item zfs_path
47
48 Specify an alternative location for the zfs command. Default: /sbin/zfs.
49
50 =back
51
52 =head1 METRICS
53
54 A set of metrics is returned for each pool on the system, with the name of the
55 pool being used as a prefix. For example, if you have rpool and data pools,
56 then you will end up with both rpool_free_KB and data_free_KB (as well as the
57 rest of the metrics for each pool).
58
59 =over
60
61 =item poolname_free_MB
62
63 The amount of free space in the pool, measured in megabytes.
64
65 =item poolname_used_MB
66
67 The amount of used space in the pool, measured in megabytes.
68
69 =item poolname_percent_full
70
71 The amount of used space in the pool, expressed as a percentage of the total
72 space.
73
74 =back
75
76 =cut
77
78 our %units = (
79     'B' => 1,
80     'K' => 1024,
81     'M' => 1048576,
82     'G' => 1073741824,
83     'T' => 1099511627776,
84     'P' => 1125899906842624,
85     'E' => 1152921504606846976,
86     'Z' => 1180591620717411303424
87 );
88
89 sub handler {
90     my $self = shift;
91     my $config = $self->{config}; # All configuration is in here
92     my $zfs_command = $config->{zfs_command} || "/sbin/zfs";
93     my $status = {};
94     my $output = run_command("$zfs_command list -H -o name,used,avail");
95     foreach my $line (split /\n/, $output) {
96         my ($name, $used, $uunit, $free, $funit) = $line =~
97             /(\S+)\s+([0-9.]+)([BKMGTPEZ]?)\s+([0-9.]+)([BKMGTPEZ]?)/;
98         next if ($name =~ /\//); # We're only interested in the root of a pool
99
100         # Convert human readable units to bytes
101         $used = $used * $units{$uunit} if $uunit;
102         $free = $free * $units{$funit} if $funit;
103
104         my $percent_full = sprintf("%.2f", ($used / ($used + $free)) * 100);
105         $status->{"${name}_used_MB"} = [int($used/1048576), "i"];
106         $status->{"${name}_free_MB"} = [int($free/1048576), "i"];
107         $status->{"${name}_percent_full"} = [$percent_full, "n"];
108     }
109
110     return $status;
111 };
112
113 1;
Note: See TracBrowser for help on using the browser.