Skip to content

Commit 02dc2c9

Browse files
authored
Move syslog cleanup from daily to a scheduled job (librenms#18258)
* Move syslog cleanup from daily to a scheduled job * Apply fixes from StyleCI
1 parent 2dcfca1 commit 02dc2c9

File tree

5 files changed

+56
-31
lines changed

5 files changed

+56
-31
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Console\LnmsCommand;
6+
use App\Facades\LibrenmsConfig;
7+
use App\Models\Syslog;
8+
use Carbon\Carbon;
9+
use Illuminate\Support\Facades\Cache;
10+
11+
class MaintenanceSyslogCleanup extends LnmsCommand
12+
{
13+
protected $name = 'maintenance:syslog-cleanup';
14+
15+
/**
16+
* Execute the console command.
17+
*
18+
* @return int
19+
*/
20+
public function handle(): int
21+
{
22+
$lock = Cache::lock('syslog_purge', 86000);
23+
if (! $lock->get()) {
24+
return 1;
25+
}
26+
27+
$syslog_purge = LibrenmsConfig::get('syslog_purge');
28+
if (! is_numeric($syslog_purge)) {
29+
return 0;
30+
}
31+
32+
$deleted_total = 0;
33+
$deleted_rows = 1;
34+
while ($deleted_rows > 0) {
35+
$deleted_rows = Syslog::where('timestamp', '<=', Carbon::now()->subDays($syslog_purge)->toDateTimeString())
36+
->limit(5000)
37+
->delete();
38+
$deleted_total += $deleted_rows;
39+
}
40+
$lock->release();
41+
42+
$this->line(trans('commands.maintenance:syslog-cleanup.delete', ['days' => $syslog_purge, 'count' => $deleted_total]));
43+
44+
return 0;
45+
}
46+
}

daily.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,36 +78,6 @@
7878
}
7979
}
8080

81-
if ($options['f'] === 'syslog') {
82-
$lock = Cache::lock('syslog_purge', 86000);
83-
if ($lock->get()) {
84-
$syslog_purge = LibrenmsConfig::get('syslog_purge');
85-
86-
if (is_numeric($syslog_purge)) {
87-
$rows = (int) dbFetchCell('SELECT MIN(seq) FROM syslog');
88-
$initial_rows = $rows;
89-
while (true) {
90-
$limit = dbFetchCell('SELECT seq FROM syslog WHERE seq >= ? ORDER BY seq LIMIT 1000,1', [$rows]);
91-
if (empty($limit)) {
92-
break;
93-
}
94-
95-
// Deletes are done in blocks of 1000 to avoid a single very large operation.
96-
if (dbDelete('syslog', 'seq >= ? AND seq < ? AND timestamp < DATE_SUB(NOW(), INTERVAL ? DAY)', [$rows, $limit, $syslog_purge]) > 0) {
97-
$rows = $limit;
98-
} else {
99-
break;
100-
}
101-
}
102-
103-
dbDelete('syslog', 'seq >= ? AND timestamp < DATE_SUB(NOW(), INTERVAL ? DAY)', [$rows, $syslog_purge]);
104-
$final_rows = $rows - $initial_rows;
105-
echo "Syslog cleared for entries over $syslog_purge days (about $final_rows rows)\n";
106-
}
107-
$lock->release();
108-
}
109-
}
110-
11181
if ($options['f'] === 'ports_fdb') {
11282
$ret = lock_and_purge('ports_fdb', 'updated_at < DATE_SUB(NOW(), INTERVAL ? DAY)');
11383
exit($ret);

daily.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@ main () {
379379
options=("refresh_alert_rules"
380380
"refresh_device_groups"
381381
"recalculate_device_dependencies"
382-
"syslog"
383382
"eventlog"
384383
"authlog"
385384
"callback"

lang/en/commands.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@
202202
'error' => 'Error processing Mac OUI:',
203203
'vendor_update' => 'Adding OUI :oui for :vendor',
204204
],
205+
'maintenance:syslog-cleanup' => [
206+
'delete' => 'Cleared syslog entries older than :days days (:count rows)',
207+
],
205208
'plugin:disable' => [
206209
'description' => 'Disable all plugins with the given name',
207210
'arguments' => [

routes/console.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
use App\Console\Commands\MaintenanceCleanupNetworks;
44
use App\Console\Commands\MaintenanceFetchOuis;
5+
use App\Console\Commands\MaintenanceSyslogCleanup;
56
use App\Jobs\PingCheck;
67
use Illuminate\Support\Facades\Artisan;
8+
use Illuminate\Support\Facades\Cache;
79
use Illuminate\Support\Facades\Schedule;
810
use LibreNMS\Util\Time;
911
use Symfony\Component\Process\Process;
@@ -209,3 +211,8 @@
209211
->weeklyOn(0, Time::pseudoRandomBetween('02:00', '02:59'))
210212
->onOneServer()
211213
->appendOutputTo($maintenance_log_file);
214+
215+
Schedule::command(MaintenanceSyslogCleanup::class)
216+
->dailyAt('03:30')
217+
->onOneServer()
218+
->appendOutputTo($maintenance_log_file);

0 commit comments

Comments
 (0)