From 6fd3320faf6363631d81d05ed4c02d63a9f2dcbd Mon Sep 17 00:00:00 2001 From: Jordi Sanfeliu Date: Fri, 28 Jul 2017 13:02:10 +0200 Subject: [PATCH] added the ability to include an alert for each defined sensor in 'hptemp.pm' --- Changes | 1 + lib/hptemp.pm | 40 ++++++++++++++++++++++++++++++++++++++++ monitorix.conf | 2 ++ 3 files changed, 43 insertions(+) diff --git a/Changes b/Changes index 3497a53..7a22dd9 100644 --- a/Changes +++ b/Changes @@ -34,6 +34,7 @@ - Added the new option 'stats_rate' in 'mail.pm' to be able to choose between 'real' (new default) and 'per_second'. - Added the ability to include an alert for each defined sensor in 'gensens.pm'. +- Added the ability to include an alert for each defined sensor in 'hptemp.pm'. - Fixed an undeclared global symbol "$imgfmt_lc" in 'traffacct.pm'. - Fixed the MIME type of graphs in 'emailreports.pm' and in 'traffacct.pm' to honor the 'image_format' option. [#174] diff --git a/lib/hptemp.pm b/lib/hptemp.pm index 04bb642..4476014 100644 --- a/lib/hptemp.pm +++ b/lib/hptemp.pm @@ -132,10 +132,44 @@ sub hptemp_init { } } + $config->{hptemp_hist_alerts} = (); push(@{$config->{func_update}}, $package); logger("$myself: Ok") if $debug; } +sub hptemp_alerts { + my $myself = (caller(0))[3]; + my $config = (shift); + my $sensor = (shift); + my $val = (shift); + + my $hptemp = $config->{hptemp}; + my @al = split(',', $hptemp->{alerts}->{$sensor} || ""); + + if(scalar(@al)) { + my $timeintvl = trim($al[0]); + my $threshold = trim($al[1]); + my $script = trim($al[2]); + + if(!$threshold || $val < $threshold) { + $config->{hptemp_hist_alerts}->{$sensor} = 0; + } else { + if(!$config->{hptemp_hist_alerts}->{$sensor}) { + $config->{hptemp_hist_alerts}->{$sensor} = time; + } + if($config->{hptemp_hist_alerts}->{$sensor} > 0 && (time - $config->{hptemp_hist_alerts}->{$sensor}) >= $timeintvl) { + if(-x $script) { + logger("$myself: alert on HP Temp ($sensor): executing script '$script'."); + system($script . " " . $timeintvl . " " . $threshold . " " . $val); + } else { + logger("$myself: ERROR: script '$script' doesn't exist or don't has execution permissions."); + } + $config->{hptemp_hist_alerts}->{$sensor} = time; + } + } + } +} + sub hptemp_update { my $myself = (caller(0))[3]; my ($package, $config, $debug) = @_; @@ -165,6 +199,8 @@ sub hptemp_update { chomp($temp); $temp =~ s/C//; push(@hptemp1, map {$_ eq "---" ? 0 : $_} ($temp)); + # check alerts for each sensor defined + hptemp_alerts($config, $str, $temp); } } foreach my $t (split(',', ($hptemp->{graph_1} || ""))) { @@ -174,6 +210,8 @@ sub hptemp_update { chomp($temp); $temp =~ s/C//; push(@hptemp2, map {$_ eq "---" ? 0 : $_} ($temp)); + # check alerts for each sensor defined + hptemp_alerts($config, $str, $temp); } } foreach my $t (split(',', ($hptemp->{graph_2} || ""))) { @@ -183,6 +221,8 @@ sub hptemp_update { chomp($temp); $temp =~ s/C//; push(@hptemp3, map {$_ eq "---" ? 0 : $_} ($temp)); + # check alerts for each sensor defined + hptemp_alerts($config, $str, $temp); } } } diff --git a/monitorix.conf b/monitorix.conf index d21e921..fc6eb79 100644 --- a/monitorix.conf +++ b/monitorix.conf @@ -172,6 +172,8 @@ secure_log_date_format = %b %e graph_0 = 2, 3 graph_1 = 1, 6 graph_2 = 16, 18, 19, 20, 21, 22 + +