/
Logs.pm
949 lines (796 loc) · 23.4 KB
/
Logs.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
# Copyright (C) 2008-2012 eBox Technologies S.L.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
use strict;
use warnings;
package EBox::Logs;
use base qw(EBox::Module::Service EBox::Report::DiskUsageProvider);
use EBox::Global;
use EBox::Gettext;
use EBox::Loggerd;
use EBox::Config;
use EBox::Sudo;
use EBox::Exceptions::InvalidData;
use EBox::Exceptions::External;
use EBox::Exceptions::Internal;
use EBox::DBEngineFactory;
use EBox::Service;
use EBox::Logs::SlicedBackup;
use EBox::FileSystem;
use EBox::Util::SQLTypes;
use POSIX qw(ceil);
use constant LOG_DAEMON => 'ebox.loggerd';
use constant IMAGEPATH => EBox::Config::tmp . '/varimages';
use constant PIDPATH => EBox::Config::tmp . '/pids/';
use constant ENABLED_LOG_CONF_DIR => EBox::Config::conf . '/logs';;
use constant ENABLED_LOG_CONF_FILE => ENABLED_LOG_CONF_DIR . '/enabled.conf';
use constant MYSQL_ZENTYAL_DATA_DIR => '/var/lib/mysql/zentyal';
# EBox::Module::Service interface
#
sub _create
{
my $class = shift;
my $self = $class->SUPER::_create(name => 'logs',
printableName => __('Logs'),
@_);
bless($self, $class);
return $self;
}
# Method: depends
#
# Override EBox::Module::Base::depends
#
sub depends
{
my ($self) = @_;
my $mods = $self->global()->modInstancesOfType('EBox::LogObserver');
my @names = map ($_->{name}, @$mods);
return \@names;
}
sub _daemons
{
return [
{
'name' => LOG_DAEMON,
'precondition' => \&_loggerdPrecondition,
}
];
}
# Method: _loggerdPrecondition
#
# loggerd daemon precondition, checks that a least one logger domain is
# enabled
sub _loggerdPrecondition
{
my ($self) = @_;
my $enabled = $self->allEnabledLogHelpers();
return @{ $enabled } > 0;
}
# Method: enableService
#
# Used to enable a service. Overriddien to notify all LogObserver of the
# changes.
#
# Parameters:
#
# boolean - true to enable, false to disable
#
# Overriddes:
# <EBox::Module::service::enableService >
sub enableService
{
my ($self, $status) = @_;
defined $status or
$status = 0;
return unless ($self->isEnabled() xor $status);
$self->SUPER::enableService($status);
$self->_notifyLogEnable();
}
sub _notifyLogEnable
{
my ($self, $status) = @_;
my $modules = $self->getLogsModules();
foreach my $module (@{ $modules }) {
$module->enableLog($status);
}
}
# Method: isRunning
#
# Overrides:
#
# <EBox::Module::Service::isRunning>
#
sub isRunning
{
my ($self) = @_;
return $self->isEnabled();
}
# Method: _setConf
#
# Overrides:
#
# <EBox::Module::Base::_setConf>
#
sub _setConf
{
my ($self) = @_;
$self->_saveEnabledLogsModules();
}
sub cleanup
{
my ($self) = @_;
$self->SUPER::revokeConfig();
}
# Method: allEnabledLogHelpers
#
# This function fetchs all the classes implemeting the interface
# <EBox::LogHelper> which have been enabled for the user.
#
# If the user has not configured anything yet, all are enabled
# by default.
#
# Returns:
#
# Array ref of objects <EBox::LogHelper>
#
sub allEnabledLogHelpers
{
my ($self) = @_;
my $global = $self->global();
my $enabledLogs = $self->_restoreEnabledLogsModules();
# If there's no configuration stored it means the user
# has not configured them yet. So by default, we enable all them
unless (defined($enabledLogs)) {
return $self->allLogHelpers();
}
my @enabledObjects;
my @mods = @{$global->modInstancesOfType('EBox::LogObserver')};
foreach my $mod (@mods) {
if ($global->modEnabled($mod->name) and $enabledLogs->{$mod->name}) {
my $logHelper = $mod->logHelper();
defined $logHelper or
next;
push (@enabledObjects, $logHelper);
}
}
return \@enabledObjects;
}
# Method: allLogHelpers
#
# This function fetchs all the classes implemeting the interface
# <EBox::LogHelper> and its associated fifo.
#
# Returns:
#
# An array ref containing hash references holding:
#
# fifo - path to log file
# classname - class name of the <EBox::LogHelper> implementation
#
sub allLogHelpers
{
my ($self) = @_;
my $global = $self->global();
my @objects;
my @mods = @{$global->modInstancesOfType('EBox::LogObserver')};
foreach my $mod (@mods) {
my $obj = $mod->logHelper();
next unless defined($obj);
push @objects, $obj;
}
return \@objects;
}
sub getLogsModules
{
my ($self) = @_;
my $global = $self->global();
return [grep { $_->configured() } @{$global->modInstancesOfType('EBox::LogObserver')}];
}
# Method: getAllTables
#
# Get the table information from all modules which implements
# the log observer
#
# Return:
#
# hash ref - the table information indexed by table name (index)
# as returned by <EBox::LogObserver::tableInfo> + 'helper'
# component which is the module that implements the LogObserver
# interface
#
sub getAllTables
{
my ($self, $noCache) = @_;
if (not $noCache and $self->{tables}) {
return $self->{tables};
}
my $tables = {};
foreach my $mod (@{$self->getLogsModules()}) {
my @tableInfos = @{ $self->getModTableInfos($mod) };
foreach my $comp (@tableInfos) {
$comp->{'helper'} = $mod;
next unless ($comp);
$tables->{$comp->{'tablename'}} = $comp;
}
}
$self->{tables} = $tables;
return $tables;
}
# Method: getTableInfo
#
# Accessor to the table information from a log observer
#
# Returns:
#
# hash ref - the table information returned by
# <EBox::LogObserver::tableInfo> + 'helper'
# component which is the module that implements the LogObserver
# interface
#
sub getTableInfo
{
my ($self, $index) = @_;
my $tables = $self->getAllTables();
if (exists $tables->{$index}) {
return $tables->{$index};
}
return undef;
}
sub getModTableInfos
{
my ($self, $mod) = @_;
my @tableInfos;
my $ti = $mod->tableInfo();
if (ref $ti eq 'ARRAY') {
@tableInfos = @{ $ti };
}
elsif (ref $ti eq 'HASH') {
EBox::warn('tableInfo() in ' . $mod->name .
' must return a reference to a list of hashes not the hash itself');
@tableInfos = ( $ti );
}
else {
throw EBox::Exceptions::Internal(
$mod->name .
"'s tableInfo returned invalid module"
);
}
return \@tableInfos;
}
sub clearTableInfoCache
{
my ($self) = @_;
$self->{tables} = undef;
}
sub getLogDomains
{
my ($self) = @_;
my $tables = $self->getAllTables();
my %logdomains = map { $_ => $tables->{$_}->{'name'} } keys %{$tables};
return \%logdomains;
}
sub backupDomains
{
my $name = 'logs';
my %attrs = (
printableName => __('Logs'),
description => __(q{Zentyal Server logs database}),
extraDataDump => 1,
);
return ($name, \%attrs);
}
sub dumpExtraBackupData
{
my ($self, $dir, %backupDomains) = @_;
my @domainsDumped;
if ($backupDomains{logs} ) {
my $logsDir = $dir . '/logs';
if (not -d $logsDir) {
mkdir $logsDir or
throw EBox::Exceptions::Internal("Cannot create $logsDir: $!");
}
my $dbengine = EBox::DBEngineFactory::DBEngine();
my $dumpFileBasename = "eboxlogs";
$dbengine->backupDB($logsDir, $dumpFileBasename);
push @domainsDumped, 'logs';
}
return \@domainsDumped;
}
sub dumpExtraBackupDataSize
{
my ($self, $dir, %backupDomains) = @_;
my $size = 0;
if ($backupDomains{logs} ) {
$size += EBox::FileSystem::dirDiskUsage($dir);
}
return $size;
}
sub _checkValidDate # (date)
{
my ($datestr) = @_;
my ($date, $time) = split (/ /, $datestr);
my ($year, $month, $day) = split (/-/, $date);
my ($hour, $min, $sec) = split (/:/, $time);
unless (defined($year) and defined($month) and defined($day)) {
return undef;
}
return undef unless ($year =~ /\d\d\d\d/ );
return undef unless ($month =~ /\d+/ and $month < 13 and $month > 0);
return undef unless ($day =~ /\d+/ and $day < 32 and $day > 0);
return undef unless ($hour =~ /\d+/ and $hour < 24 and $hour > -1);
return undef unless ($min =~ /\d+/ and $min < 60 and $min > -1);
return undef unless ($sec =~ /\d+/ and $sec < 60 and $sec > -1);
return 1;
}
# Method: search
#
# Search for content in stored logs (in mysql database)
#
# Parameters:
#
# from - String which represents the "from" date in "year-month-day
# hour:min:sec" format
#
# to - String which represents the "to" date in "year-month-day
# hour:min:sec" format
#
# index - String the table's name
#
# pagesize - Int the page's size to return the result
#
# page - Int the page to search for results
#
# timecol - String the timestamp column to perform date filters
#
# filters - hash ref a list of filters indexed by name which
# contains the value of the given filter (normally a
# string). Passing *undef* no filters are applied
#
# Returns:
#
# hash ref - containing the search result. The components are
# the following:
#
# totalret - Int the number of results, it could be zero
# arrayret - array ref containing the each returned row. Each
# component is an hash ref whose description is determined by
# the returned value of <EBox::Logs::getTableInfo> with
# parameter <EBox::Logs::search::index>.
#
sub search
{
my ($self, $from, $to, $index,
$pagesize, $page, $timecol, $filters) = @_;
my $dbengine = EBox::DBEngineFactory::DBEngine();
my $tables = $self->getAllTables();
my $tableinfo = $tables->{$index};
my $table = $tableinfo->{'tablename'};
unless (defined $tableinfo) {
throw EBox::Exceptions::External( __x(
'Table {table} does not exist', 'table' => $table));
}
$self->{'sqlselect'} = {};
$self->_addTableName($table);
if (_checkValidDate($from)) {
$self->_addDateFilter($timecol, $from, '>');
}
if (_checkValidDate($to)) {
$self->_addDateFilter($timecol, $to, '<');
}
if ($filters and %{$filters}) {
while (my ($field, $filterValue) = each %{$filters}) {
if (not $field) {
next;
} elsif ((not defined $filterValue) or ($filterValue =~ m/^\s*$/)) {
next;
}
if (($field eq 'event') or (not $filterValue)) {
$self->{'sqlselect'}->{'filter'}->{$field} = $filterValue;
} else {
my $type = exists $tableinfo->{types}->{$field} ?
$tableinfo->{types}->{$field} : undef;
if ($type) {
$field = EBox::Util::SQLTypes::stringifier($type, $field);
}
$self->{'sqlselect'}->{'regexp'}->{$field} = $filterValue;
}
}
}
$self->_addSelect('COUNT(*)');
my @count = @{$dbengine->query($self->_sqlStmnt())};
my $tcount = $count[0]{'COUNT(*)'};
# Do not go on if you don't have any result
if ($tcount == 0) {
return { 'totalret' => $tcount,
'arrayret' => [],
};
}
my $tpages = ceil($tcount / $pagesize) - 1;
if ($page < 0) { $page = 0; }
if ($page > $tpages) { $page = $tpages; }
my $offset = $page * $pagesize;
$self->_addPager($offset, $pagesize);
$self->_addOrder("$timecol DESC");
if ($tableinfo->{types}) {
my @keys;
foreach my $key (keys %{$tableinfo->{titles}}) {
my $type = $tableinfo->{types}->{$key};
if ($type) {
$key = EBox::Util::SQLTypes::acquirer($type, $key);
}
push (@keys, $key);
}
$self->_addSelect(join (',', @keys));
} else {
$self->_addSelect('*');
}
my @ret = @{$dbengine->query($self->_sqlStmnt())};
my $hashret = {
'totalret' => $tcount,
'arrayret' => \@ret
};
return $hashret;
}
# Method: totalRecords
#
# Get the total records stored in database for a given table
#
# Parameters:
#
# tableName - String the table name to check the number of
# records
#
# Returns:
#
# Integer - the number of records for this table
#
sub totalRecords
{
my ($self, $table) = @_;
my $dbengine = EBox::DBEngineFactory::DBEngine();
my $sql = "SELECT COUNT(*) FROM $table";
my @tarray = @{$dbengine->query($sql)};
my $tcount = $tarray[0]{'COUNT(*)'};
return $tcount;
}
# Method: consolidatedLogForDay
#
# Parameters:
# table - consolidated table. The suffix '_daily' is added automaticallu
# date - date in format yyyy-mm-dd
#
# Returns:
# array reference. Each row will be a hash reference with
# column/values as key/values. Remember that it will be always a 'date'
# field.
# If there is not data it will return a empty array
sub consolidatedLogForDay
{
my ($self, $table, $date) = @_;
$date or
throw EBox::Exceptions::MissingArgument('date');
$table or
throw EBox::Exceptions::MissingArgument('table');
# put the standard 00:00:00 hour
($date) = split '\s', $date;
$date .= ' 00:00:00';
$table = $table . '_daily';
my $dbengine = EBox::DBEngineFactory::DBEngine();
# FIXME: what happens with acquirers here?
my $sql = "SELECT * FROM $table WHERE date='$date'";
my @results = @{ $dbengine->query($sql) };
return \@results;
}
# Method: yesterdayDate
#
# Returns:
# the yesterday date in string format so it can used in SQL queries and
# i nthe consolidatedLogForDay method
sub yesterdayDate
{
my ($self) = @_;
my $yesterdayTs = time() - 86400; # 86400 seconds in a day
my ($sec,$min,$hour,$mday,$mon,$year) = localtime($yesterdayTs);
$year += 1900;
$mon +=1;
return "$year-$mon-$mday 00:00:00";
}
sub _addFilter
{
my ($self, $field, $filter) = @_;
return unless (defined($field) and defined($filter)
and length($filter) > 0);
$self->{'sqlselect'}->{'filter'}->{$field} = $filter;
}
sub _addDateFilter
{
my ($self, $field, $date, $operator) = @_;
return unless (defined($date) and defined($operator));
$self->{'sqlselect'}->{'date'}->{$operator}->{'date'} = $date;
$self->{'sqlselect'}->{'date'}->{$operator}->{'field'} = $field;
}
sub _addPager
{
my ($self, $offset, $limit) = @_;
$self->{'sqlselect'}->{'offset'} = $offset;
$self->{'sqlselect'}->{'limit'} = $limit;
}
sub _addOrder
{
my ($self, $order) = @_;
$self->{sqlselect}->{order} = $order;
}
sub _addTableName
{
my ($self, $table) = @_;
$self->{'sqlselect'}->{'table'} = $table;
}
sub _addSelect
{
my ($self, $select) = @_;
$self->{'sqlselect'}->{'select'} = $select;
}
sub _sqlStmnt
{
my ($self) = @_;
my @params;
my $sql = $self->{'sqlselect'};
my $stmt = "SELECT $sql->{'select'} FROM $sql->{'table'} ";
if ($sql->{'regexp'} or $sql->{'date'}) {
$stmt .= 'WHERE ';
}
my $and = '';
if ($sql->{'date'}) {
foreach my $op (keys %{$sql->{'date'}}) {
$stmt .= "$and $sql->{'date'}->{$op}->{'field'} $op ? ";
$and = 'AND';
push @params, $sql->{'date'}->{$op}->{'date'};
}
}
if ($sql->{'regexp'}) {
foreach my $field (keys %{$sql->{'regexp'}}) {
$stmt .= "$and CAST($field as CHAR CHARACTER SET utf8) REGEXP ? ";
$and = 'AND';
push @params, $sql->{'regexp'}->{$field};
}
}
if ($sql->{'filter'}) {
foreach my $field (keys %{$sql->{'filter'}}) {
$stmt .= "$and $field = ? ";
$and = 'AND';
push @params, $sql->{'filter'}->{$field};
}
}
if ($sql->{order}) {
$stmt .= 'ORDER BY ' . $sql->{order} . ' ';
}
if (defined ($sql->{limit})) {
$stmt .= 'LIMIT ?';
push (@params, $sql->{limit});
if (defined ($sql->{offset})) {
$stmt .= ' OFFSET ?';
push (@params, $sql->{offset});
}
}
return $stmt, @params;
}
# Method: menu
#
# Overrides EBox::Module method.
#
#
sub menu
{
my ($self, $root) = @_;
my $folder = new EBox::Menu::Folder('name' => 'Maintenance',
'text' => __('Maintenance'),
'separator' => 'Core',
'order' => 70);
$folder->add(new EBox::Menu::Item('url' => 'Maintenance/Logs',
'text' => $self->printableName(),
'order' => 20));
$root->add($folder);
}
# Helper functions
# Method: _saveEnabledLogs
#
# (Private)
#
# This function saves the enabled logs in a file.
# We have to do this because the logger daemon will request this
# configuration as root user.
#
# Another approach could be creating a separated script to
# query ebox conf.
#
sub _saveEnabledLogsModules
{
my ($self) = @_;
my $enabledLogs = $self->model('ConfigureLogs')->enabledLogs();
unless (-d ENABLED_LOG_CONF_DIR) {
mkdir (ENABLED_LOG_CONF_DIR);
}
# Create a string of domains separated by comas
my $enabledLogsString = join (',', keys %{$enabledLogs});
my $file;
unless (open($file, '>' . ENABLED_LOG_CONF_FILE)) {
throw EBox::Exceptions::Internal(
'Cannot open ' . ENABLED_LOG_CONF_FILE);
}
print $file "$enabledLogsString";
close($file);
}
# Method: _restoreEnabledLogsModules
#
# (Private)
#
# This function restores the enabled logs saved in a file by
# <EBox::Logs::_saveEnabledLogsModules>
# We have to do this because the logger daemon will request this
# configuration as root user.
#
# Anotther approach could be creating a separated script to
# query ebox conf.
#
# Returns:
#
# undef - if there's no enabled logs stored yet
# hash ref containing the enabled logs
sub _restoreEnabledLogsModules
{
my ($self) = @_;
my $file;
unless (open($file, ENABLED_LOG_CONF_FILE)) {
return undef;
}
my $string = <$file>;
close($file);
if (not $string) {
return {}
}
my %enabled;
foreach my $domain (split(/,/, $string)) {
$enabled{$domain} = 1;
}
return \%enabled;
}
# Overrides:
# EBox::Report::DiskUsageProivider::_facilitiesForDiskUsage
#
sub _facilitiesForDiskUsage
{
my ($self) = @_;
my $printableName = __('Log messages');
return {
$printableName => [ MYSQL_ZENTYAL_DATA_DIR ],
};
}
# Method: forcePurge
#
# Force to purge every table used to log data in eBox given a
# timestamp
#
# Parameters:
#
# lifetime - Int the allowed data not to purge should be
# timestamped before this limit which is set in hours
#
# Exceptions:
#
# <EBox::Exceptions::External> - thrown if the lifetime is not a
# positive number
#
sub forcePurge
{
my ($self, $lifetime) = @_;
($lifetime > 0) or
throw EBox::Exceptions::External(
__("Lifetime parameter must be a positive number of hours")
);
my $now = time();
my $thresholdDate = $self->_thresholdDate($lifetime, $now);
my @tables = map {
@{ $self->getModTableInfos($_) }
} @{ $self->getLogsModules };
foreach my $tableInfo ( @tables ) {
my $table = $tableInfo->{tablename};
my $timeCol = 'timestamp';
$self->_purgeTable($table, $timeCol, $thresholdDate);
}
}
# Method: purge
#
# Purge every table used to log data in eBox with the threshold
# lifetime defined by 'lifetime' field in
# <EBox::Logs::Model::ConfigureLogs> model
#
# This method is called by a cron job.
#
# Exceptions:
#
# <EBox::Exceptions::External> - thrown if the lifetime is not a
# positive number
#
sub purge
{
my ($self) = @_;
my $now = time();
my %thresholdByModule = ();
# get the threshold date for each domain
my $model = $self->model('ConfigureLogs');
foreach my $id (@{$model->ids()}) {
my $row_r = $model->row($id);
my $lifeTime = $row_r->valueByName('lifeTime');
# if lifeTime == 0, it should never expire
$lifeTime or
next;
my $threshold = $self->_thresholdDate($lifeTime, $now);
$thresholdByModule{$row_r->valueByName('domain')} = $threshold;
}
# purge each module
while (my ($modName, $threshold) = each %thresholdByModule) {
my $mod = $self->global()->modInstance($modName);
my @logTables = @{ $self->getModTableInfos($mod) };
foreach my $table (@logTables) {
my $dbTable = $table->{tablename};
my $timeCol = 'timestamp';
$self->_purgeTable($dbTable, $timeCol, $threshold);
}
}
}
# Transform an hour into a localtime
sub _thresholdDate
{
my ($self, $lifeTime, $now) = @_;
# lifeTime must be in hours
my $lifeTimeSeconds = $lifeTime * 3600;
my $threshold = $now - $lifeTimeSeconds;
return scalar localtime($threshold);
}
# Do perform the purge in a table
sub _purgeTable
{
my ($self, $table, $timeCol, $thresholdDate) = @_;
my $finalThreshold;
my $dbengine = EBox::DBEngineFactory::DBEngine();
if (EBox::Logs::SlicedBackup::slicedMode()) {
$finalThreshold = EBox::Logs::SlicedBackup::limitPurgeThreshold(
$dbengine,
$table,
$thresholdDate,
);
if (not defined $finalThreshold) {
EBox::info("Cannot purge logs of table $table before $thresholdDate because they are unarchived backup slices");
return;
} elsif ($thresholdDate ne $finalThreshold) {
EBox::info("Log purge of table before $thresholdDate has be restricted to records before $finalThreshold because it were not archived backup slices");
}
} else {
$finalThreshold = $thresholdDate;
}
my $sqlStatement = "DELETE FROM $table WHERE $timeCol < STR_TO_DATE('$finalThreshold','%a %b %e %T %Y')";
$dbengine->do($sqlStatement);
}
sub archiveBackupSlices
{
my ($self, $force) = @_;
if (not $force) {
my $enabled = EBox::Logs::SlicedBackup::slicedMode();
$enabled or
return;
}
my $dbengine = EBox::DBEngineFactory::DBEngine();
EBox::Logs::SlicedBackup::archive($dbengine);
}
1;