From 36e05fbc43faeeeb58ee0264f92c9a603ff2c4da Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Wed, 6 Nov 2024 09:33:09 +0100 Subject: [PATCH] improve logging reduce log level for some errors when run in crontab context --- lib/Thruk/Backend/Provider/Mysql.pm | 2 +- lib/Thruk/Utils/CLI/Logcache.pm | 2 +- lib/Thruk/Utils/Cluster.pm | 6 +----- lib/Thruk/Utils/Log.pm | 9 ++++++++- .../node-control/lib/Thruk/Utils/CLI/Nodecontrol.pm | 6 +----- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/Thruk/Backend/Provider/Mysql.pm b/lib/Thruk/Backend/Provider/Mysql.pm index f53d48a0b..709d9bab6 100644 --- a/lib/Thruk/Backend/Provider/Mysql.pm +++ b/lib/Thruk/Backend/Provider/Mysql.pm @@ -1460,7 +1460,7 @@ sub _update_logcache { _debug($error); $error = $short_err; } - _error('logcache '.$mode.' failed: '.$error) unless $error =~ m/(.*\Qplease come back later\E|\QConnection refused\E)/mx; + _cronerror('logcache '.$mode.' failed: '.$error); # don't fill the log with errors from cronjobs die($error); } diff --git a/lib/Thruk/Utils/CLI/Logcache.pm b/lib/Thruk/Utils/CLI/Logcache.pm index e02a17634..8ddf3bf51 100644 --- a/lib/Thruk/Utils/CLI/Logcache.pm +++ b/lib/Thruk/Utils/CLI/Logcache.pm @@ -273,7 +273,7 @@ sub cmd { $err = $short_err; } $err = sprintf("backend '%s' failed: %s", $backend, $err); - _error($err); + _cronerror($err); # don't fill the log with errors from cronjobs } my $elapsed = tv_interval($t1); return($log_count, $plugin_ref_count, $err, $elapsed); diff --git a/lib/Thruk/Utils/Cluster.pm b/lib/Thruk/Utils/Cluster.pm index 41f91a401..c6312882c 100644 --- a/lib/Thruk/Utils/Cluster.pm +++ b/lib/Thruk/Utils/Cluster.pm @@ -343,11 +343,7 @@ sub run_cluster { _debug($msg); } else { if(!$node->{'last_error'} && !$node->{'maintenance'}) { - if($ENV{'THRUK_CRON'}) { - _warn($msg); - } else { - _error($msg); - } + _cronerror($msg); # don't fill the log with errors from cronjobs } else { _debug($msg); } diff --git a/lib/Thruk/Utils/Log.pm b/lib/Thruk/Utils/Log.pm index 51c41ac0f..f6c410961 100644 --- a/lib/Thruk/Utils/Log.pm +++ b/lib/Thruk/Utils/Log.pm @@ -22,7 +22,7 @@ use Thruk::Base (); use Thruk::Utils::Encode (); use base 'Exporter'; -our @EXPORT_OK = qw(_fatal _error _warn _info _infos _infoc +our @EXPORT_OK = qw(_fatal _error _cronerror _warn _info _infos _infoc _debug _debug2 _debugs _debugc _trace _audit_log _debug_http_response ); @@ -68,6 +68,13 @@ sub _error { return &_log(ERROR, \@_); } +############################################## +# like _error, but only emits a warning if run from cron +sub _cronerror { + return &_log(WARNING, \@_) if $ENV{'THRUK_CRON'}; + return &_log(ERROR, \@_); +} + ############################################## sub _warn { return &_log(WARNING, \@_); diff --git a/plugins/plugins-available/node-control/lib/Thruk/Utils/CLI/Nodecontrol.pm b/plugins/plugins-available/node-control/lib/Thruk/Utils/CLI/Nodecontrol.pm index 1c7052387..d3f948a89 100644 --- a/plugins/plugins-available/node-control/lib/Thruk/Utils/CLI/Nodecontrol.pm +++ b/plugins/plugins-available/node-control/lib/Thruk/Utils/CLI/Nodecontrol.pm @@ -187,11 +187,7 @@ sub _action_facts { } if(!$facts || $facts->{'last_error'}) { my $err = sprintf("%s updating %s failed: %s\n", $peer->{'name'}, $mode, ($facts->{'last_error'}//'unknown error')); - if($ENV{'THRUK_CRON'}) { - _warn($err); # don't fill the log with errors from cronjobs - } else { - _error($err); - } + _cronerror($err); # don't fill the log with errors from cronjobs } else { _info("%s updated %s sucessfully: OK\n", $peer->{'name'}, $mode); }