Improved the alerting system in 'gensens.pm'

From now on, the alerts accepts a forth parameter that specifies
when the alert will be triggered. This parameter accepts two values:
'above' or 'below', being the 'above' the default value if it's not
specified, in order to keep backwards compatibility.

This way, if some value in the graph is 'above' or 'below' from the
threshold during more time than specified in the timeout value, the
alert will be triggered.

#221
This commit is contained in:
Jordi Sanfeliu 2019-02-08 09:45:12 +01:00
parent 487a260067
commit acc3f1bb62
2 changed files with 32 additions and 15 deletions

View File

@ -5,7 +5,7 @@
MAILTO="root@localhost"
if [ $# != 3 ] ; then
if [ $# != 3 ] && [ $# != 4 ] ; then
echo "$0: Wrong number of arguments."
exit 1
fi
@ -13,12 +13,13 @@ fi
ALERT_TIMEINTVL=$1
ALERT_THRESHOLD=$2
current_value=$3
ALERT_WHEN=$4
(
cat << EOF
Message from hostname '$HOSTNAME'.
This system is reaching/exceeding the defined threshold value ($ALERT_THRESHOLD) during the last '$ALERT_TIMEINTVL' seconds.
This system is reaching/exceeding ($ALERT_WHEN) the defined threshold value ($ALERT_THRESHOLD) during the last '$ALERT_TIMEINTVL' seconds.
The current value is: $current_value

View File

@ -164,22 +164,38 @@ sub gensens_update {
my $timeintvl = trim($al[0]);
my $threshold = trim($al[1]);
my $script = trim($al[2]);
if(!$threshold || $val < $threshold) {
$config->{gensens_hist_alerts}->{$str} = 0;
} else {
if(!$config->{gensens_hist_alerts}->{$str}) {
$config->{gensens_hist_alerts}->{$str} = time;
}
if($config->{gensens_hist_alerts}->{$str} > 0 && (time - $config->{gensens_hist_alerts}->{$str}) >= $timeintvl) {
if(-x $script) {
logger("$myself: alert on Generic Sensor ($str): executing script '$script'.");
system($script . " " . $timeintvl . " " . $threshold . " " . $val);
my $when = lc(trim($al[3] || ""));
my @range = split('-', $threshold);
$when = "above" if !$when; # 'above' is the default
$threshold = 0 if !$threshold;
if(scalar(@range) == 1) {
if($when eq "above" && $val < $threshold) {
$config->{gensens_hist_alerts}->{$str} = 0;
} elsif($when eq "below" && $val > $threshold) {
$config->{gensens_hist_alerts}->{$str} = 0;
} else {
if($when eq "above" || $when eq "below") {
if(!$config->{gensens_hist_alerts}->{$str}) {
$config->{gensens_hist_alerts}->{$str} = time;
}
if($config->{gensens_hist_alerts}->{$str} > 0 && (time - $config->{gensens_hist_alerts}->{$str}) >= $timeintvl) {
if(-x $script) {
logger("$myself: alert on Generic Sensor ($str): executing script '$script'.");
system($script . " " . $timeintvl . " " . $threshold . " " . $val . " " . $when);
} else {
logger("$myself: ERROR: script '$script' doesn't exist or don't has execution permissions.");
}
$config->{gensens_hist_alerts}->{$str} = time;
}
} else {
logger("$myself: ERROR: script '$script' doesn't exist or don't has execution permissions.");
logger("$myself: ERROR: invalid when value '$when'");
}
$config->{gensens_hist_alerts}->{$str} = time;
}
} elsif(scalar(@range) == 2) {
print "range = $range[0] - $range[1]\n";
print "not supported yet\n";
} else {
logger("$myself: ERROR: invalid threshold value '$threshold'");
}
}