From 18fd0c89cf7f288ad5ff4527484d9e989ed3e5f7 Mon Sep 17 00:00:00 2001 From: Kyle Hall Date: Fri, 17 Jun 2022 06:59:11 -0400 Subject: [PATCH] Bug 30984: Action logs should log the cronjob script name that generated the given log When something is changed by a cronjob, and that entity is logged via action logs, we can know what changed, and that it was via a cronjob, but we cannot necessarily know which cronjob made that change. The closest we can come is to find the action logs for the cronjob module which ran before the change which is by no means reliable assuming the CronLog is even enabled. We should add a new column to action logs to store the name of the script ran for any action logs where the interface is "cron". Test plan: 1) Apply this patch 2) Run updatedatabase.pl 3) Enable all the Log/Logging sysprefs 4) Run some cronjobs that will generate action logs 5) Note the new action_logs column "script" contains the filename of the cronjob that caused the change. Signed-off-by: Martin Renvoize Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- C4/Log.pm | 10 ++++++++-- installer/data/mysql/atomicupdate/bug_30984.pl | 17 +++++++++++++++++ installer/data/mysql/kohastructure.sql | 1 + 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100755 installer/data/mysql/atomicupdate/bug_30984.pl diff --git a/C4/Log.pm b/C4/Log.pm index 19e4a57ec1..b38fb9c163 100644 --- a/C4/Log.pm +++ b/C4/Log.pm @@ -27,6 +27,7 @@ use warnings; use Data::Dumper qw( Dumper ); use JSON qw( to_json ); use Scalar::Util qw( blessed ); +use File::Basename qw( basename ); use C4::Context; use Koha::Logger; @@ -89,9 +90,14 @@ sub logaction { } } + my $script = + $interface eq 'cron' ? basename($0) + : $interface eq 'commandline' ? basename($0) + : undef; + my $dbh = C4::Context->dbh; - my $sth=$dbh->prepare("Insert into action_logs (timestamp,user,module,action,object,info,interface) values (now(),?,?,?,?,?,?)"); - $sth->execute($usernumber,$modulename,$actionname,$objectnumber,$infos,$interface); + my $sth=$dbh->prepare("Insert into action_logs (timestamp,user,module,action,object,info,interface,script) values (now(),?,?,?,?,?,?,?)"); + $sth->execute($usernumber,$modulename,$actionname,$objectnumber,$infos,$interface,$script); $sth->finish; my $logger = Koha::Logger->get( diff --git a/installer/data/mysql/atomicupdate/bug_30984.pl b/installer/data/mysql/atomicupdate/bug_30984.pl new file mode 100755 index 0000000000..6947bcf734 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_30984.pl @@ -0,0 +1,17 @@ +use Modern::Perl; + +return { + bug_number => "30984", + description => "Log the cron script that generated an action log if there is one", + up => sub { + my ($args) = @_; + my ($dbh, $out) = @$args{qw(dbh out)}; + if( !column_exists( 'action_logs', 'script' ) ) { + $dbh->do(q{ + ALTER TABLE action_logs + ADD COLUMN script mediumtext COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'the name of the cron script that caused this change' + AFTER interface + }); + } + }, +}; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 79b1b6ea4f..3eb8c332f9 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -175,6 +175,7 @@ CREATE TABLE `action_logs` ( `object` int(11) DEFAULT NULL COMMENT 'the object that the action was taken against (could be a borrowernumber, itemnumber, etc)', `info` mediumtext COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'information about the action (usually includes SQL statement)', `interface` varchar(30) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'the context this action was taken in', + `script` mediumtext COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'the name of the cron script that caused this change', PRIMARY KEY (`action_id`), KEY `timestamp_idx` (`timestamp`), KEY `user_idx` (`user`), -- 2.39.5