Merge pull request #306 from phreaker0/monitor-time-period

support monitor time period suffixes
This commit is contained in:
Jim Salter 2018-12-17 13:08:48 -05:00 committed by GitHub
commit f1f31412af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 11 deletions

48
sanoid
View File

@ -143,8 +143,8 @@ sub monitor_snapshots {
my $typewarn = $type . '_warn';
my $typecrit = $type . '_crit';
my $warn = $config{$section}{$typewarn} * $smallerperiod;
my $crit = $config{$section}{$typecrit} * $smallerperiod;
my $warn = convertTimePeriod($config{$section}{$typewarn}, $smallerperiod);
my $crit = convertTimePeriod($config{$section}{$typecrit}, $smallerperiod);
my $elapsed = -1;
if (defined $snapsbytype{$path}{$type}{'newest'}) {
$elapsed = $snapsbytype{$path}{$type}{'newest'};
@ -153,7 +153,7 @@ sub monitor_snapshots {
my $dispwarn = displaytime($warn);
my $dispcrit = displaytime($crit);
if ( $elapsed > $crit || $elapsed == -1) {
if ($config{$section}{$typecrit} > 0) {
if ($crit > 0) {
if (! $config{$section}{'monitor_dont_crit'}) { $errorlevel = 2; }
if ($elapsed == -1) {
push @msgs, "CRIT: $path has no $type snapshots at all!";
@ -162,7 +162,7 @@ sub monitor_snapshots {
}
}
} elsif ($elapsed > $warn) {
if ($config{$section}{$typewarn} > 0) {
if ($warn > 0) {
if (! $config{$section}{'monitor_dont_warn'} && ($errorlevel < 2) ) { $errorlevel = 1; }
push @msgs, "WARN: $path\'s newest $type snapshot is $dispelapsed old (should be < $dispwarn)";
}
@ -1511,6 +1511,46 @@ sub runscript {
return $ret;
}
#######################################################################################################################3
#######################################################################################################################3
#######################################################################################################################3
sub convertTimePeriod {
my $value=shift;
my $period=shift;
if ($value =~ /^\d+Y$/) {
$period = 60*60*24*31*365;
chop $value;
} elsif ($value =~ /^\d+M$/) {
$period = 60*60*24*31;
chop $value;
} elsif ($value =~ /^\d+W$/) {
$period = 60*60*24*7;
chop $value;
} elsif ($value =~ /^\d+D$/) {
$period = 60*60*24;
chop $value;
} elsif ($value =~ /^\d+h$/) {
$period = 60*60;
chop $value;
} elsif ($value =~ /^\d+m$/) {
$period = 60;
chop $value;
} elsif ($value =~ /^\d+s$/) {
$period = 1;
chop $value;
} elsif ($value =~ /^\d+$/) {
# no unit, provided fallback period is used
} else {
# invalid value, return smallest valid value as fallback
# (will trigger a warning message for monitoring for sure)
return 1;
}
return $value * $period;
}
__END__
=head1 NAME

View File

@ -83,7 +83,8 @@ yearly_min = 0
# monitoring plugin - define warn / crit levels for each snapshot type by age, in units of one period down
# example hourly_warn = 90 means issue WARNING if most recent hourly snapshot is not less than 90 minutes old,
# daily_crit = 36 means issue CRITICAL if most recent daily snapshot is not less than 36 hours old,
# monthly_warn = 36 means issue WARNING if most recent monthly snapshot is not less than 36 days old... etc.
# monthly_warn = 5 means issue WARNING if most recent monthly snapshot is not less than 5 weeks old... etc.
# the following time suffixes can also be used: Y = years, M = months, W = weeks, D = days, h = hours, m = minutes, s = seconds
#
# monitor_dont_warn = yes will cause the monitoring service to report warnings as text, but with status OK.
# monitor_dont_crit = yes will cause the monitoring service to report criticals as text, but with status OK.
@ -94,14 +95,14 @@ monitor_dont_warn = no
monitor_dont_crit = no
frequently_warn = 0
frequently_crit = 0
hourly_warn = 90
hourly_crit = 360
daily_warn = 28
daily_crit = 32
hourly_warn = 90m
hourly_crit = 360m
daily_warn = 28h
daily_crit = 32h
weekly_warn = 0
weekly_crit = 0
monthly_warn = 32
monthly_crit = 40
monthly_warn = 32D
monthly_crit = 40D
yearly_warn = 0
yearly_crit = 0