Navigation Menu

Skip to content

Commit

Permalink
let monitor-capacity parse the limits from the configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
phreaker0 committed Mar 1, 2018
1 parent f961a9f commit b405b58
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 44 deletions.
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -64,8 +64,7 @@ Which would be enough to tell sanoid to take and keep 36 hourly snapshots, 30 da

+ --monitor-capacity

This option is designed to be run by a Nagios monitoring system. It reports on the capacity of the zpool your filesystems are on. It only monitors pools that are configured in the sanoid.conf file. The default limits are 80% for the warning and 95% for the critical state. Those can be overridden by providing them
along like '=80,95".
This option is designed to be run by a Nagios monitoring system. It reports on the capacity of the zpool your filesystems are on. It only monitors pools that are configured in the sanoid.conf file.

+ --force-update

Expand Down
92 changes: 50 additions & 42 deletions sanoid
Expand Up @@ -19,7 +19,7 @@ my %args = ("configdir" => "/etc/sanoid");
GetOptions(\%args, "verbose", "debug", "cron", "readonly", "quiet",
"monitor-health", "force-update", "configdir=s",
"monitor-snapshots", "take-snapshots", "prune-snapshots",
"monitor-capacity:s"
"monitor-capacity"
) or pod2usage(2);

# If only config directory (or nothing) has been specified, default to --cron --verbose
Expand Down Expand Up @@ -53,7 +53,7 @@ my @params = ( \%config, \%snaps, \%snapsbytype, \%snapsbypath );
if ($args{'debug'}) { $args{'verbose'}=1; blabber (@params); }
if ($args{'monitor-snapshots'}) { monitor_snapshots(@params); }
if ($args{'monitor-health'}) { monitor_health(@params); }
if (defined($args{'monitor-capacity'})) { monitor_capacity(@params); }
if ($args{'monitor-capacity'}) { monitor_capacity(@params); }
if ($args{'force-update'}) { my $snaps = getsnaps( \%config, $cacheTTL, 1 ); }

if ($args{'cron'}) {
Expand Down Expand Up @@ -187,35 +187,39 @@ sub monitor_capacity {
my @messages;
my $errlevel=0;

my %capacitylimits = (
"warn" => 80,
"crit" => 95
);
# build pool list with corresponding capacity limits
foreach my $section (keys %config) {
my @pool = split ('/',$section);

# if provided, parse capacity limits
if ($args{'monitor-capacity'} ne "") {
my @values = split(',', $args{'monitor-capacity'});
if (scalar @pool == 1 || !defined($pools{$pool[0]}) ) {
my %capacitylimits;

if (!check_capacity_limit($values[0])) {
die "ERROR: invalid zpool capacity warning limit!\n";
}
$capacitylimits{"warn"} = $values[0];
if (!check_capacity_limit($config{$section}{'capacity_warn'})) {
die "ERROR: invalid zpool capacity warning limit!\n";
}

if ($config{$section}{'capacity_warn'} != 0) {
$capacitylimits{'warn'} = $config{$section}{'capacity_warn'};
}

if (scalar @values > 1) {
if (!check_capacity_limit($values[1])) {
if (!check_capacity_limit($config{$section}{'capacity_crit'})) {
die "ERROR: invalid zpool capacity critical limit!\n";
}
$capacitylimits{"crit"} = $values[1];
}
}

foreach my $path (keys %{ $snapsbypath}) {
my @pool = split ('/',$path);
$pools{$pool[0]}=1;
if ($config{$section}{'capacity_crit'} != 0) {
$capacitylimits{'crit'} = $config{$section}{'capacity_crit'};
}

if (%capacitylimits) {
$pools{$pool[0]} = \%capacitylimits;
}
}
}

foreach my $pool (keys %pools) {
my ($exitcode, $msg) = check_zpool_capacity($pool,\%capacitylimits);
my $capacitylimitsref = $pools{$pool};

my ($exitcode, $msg) = check_zpool_capacity($pool,\%$capacitylimitsref);
if ($exitcode > $errlevel) { $errlevel = $exitcode; }
chomp $msg;
push (@messages, $msg);
Expand Down Expand Up @@ -956,11 +960,11 @@ sub check_zpool() {
sub check_capacity_limit() {
my $value = shift;
if ($value !~ /^\d+\z/) {
if (!defined($value) || $value !~ /^\d+\z/) {
return undef;
}
if ($value < 1 || $value > 100) {
if ($value < 0 || $value > 100) {
return undef;
}
Expand Down Expand Up @@ -1004,12 +1008,16 @@ sub check_zpool_capacity() {
my $capn = $cap;
$capn =~ s/\D//g;
if ($capn >= $capacitylimits{"warn"}) {
$state = "WARNING";
if (defined($capacitylimits{"warn"})) {
if ($capn >= $capacitylimits{"warn"}) {
$state = "WARNING";
}
}
if ($capn >= $capacitylimits{"crit"}) {
$state = "CRITICAL";
if (defined($capacitylimits{"crit"})) {
if ($capn >= $capacitylimits{"crit"}) {
$state = "CRITICAL";
}
}
$msg = sprintf "ZPOOL %s : %s\n", $pool, $cap;
Expand Down Expand Up @@ -1187,20 +1195,20 @@ Assumes --cron --verbose if no other arguments (other than configdir) are specif
Options:
--configdir=DIR Specify a directory to find config file sanoid.conf
--configdir=DIR Specify a directory to find config file sanoid.conf
--cron Creates snapshots and purges expired snapshots
--verbose Prints out additional information during a sanoid run
--readonly Simulates creation/deletion of snapshots
--quiet Suppresses non-error output
--force-update Clears out sanoid's zfs snapshot cache
--cron Creates snapshots and purges expired snapshots
--verbose Prints out additional information during a sanoid run
--readonly Simulates creation/deletion of snapshots
--quiet Suppresses non-error output
--force-update Clears out sanoid's zfs snapshot cache
--monitor-health Reports on zpool "health", in a Nagios compatible format
--monitor-capacity[=wlimit[,climit]] Reports on zpool capacity, in a Nagios compatible format
--monitor-snapshots Reports on snapshot "health", in a Nagios compatible format
--take-snapshots Creates snapshots as specified in sanoid.conf
--prune-snapshots Purges expired snapshots as specified in sanoid.conf
--monitor-health Reports on zpool "health", in a Nagios compatible format
--monitor-capacity Reports on zpool capacity, in a Nagios compatible format
--monitor-snapshots Reports on snapshot "health", in a Nagios compatible format
--take-snapshots Creates snapshots as specified in sanoid.conf
--prune-snapshots Purges expired snapshots as specified in sanoid.conf
--help Prints this helptext
--version Prints the version number
--debug Prints out a lot of additional information during a sanoid run
--help Prints this helptext
--version Prints the version number
--debug Prints out a lot of additional information during a sanoid run
4 changes: 4 additions & 0 deletions sanoid.defaults.conf
Expand Up @@ -70,3 +70,7 @@ monthly_warn = 32
monthly_crit = 35
yearly_warn = 0
yearly_crit = 0

# default limits for capacity checks (if set to 0, limit will not be checked)
capacity_warn = 80
capacity_crit = 95

0 comments on commit b405b58

Please sign in to comment.