46 lines
1.7 KiB
PHP
46 lines
1.7 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use Illuminate\Console\Command;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use App\Models\Setting;
|
||
|
||
class SpamAvCollectCommand extends Command
|
||
{
|
||
protected $signature = 'spamav:collect';
|
||
protected $description = 'Collect Rspamd/ClamAV metrics and persist to Settings (DB→Redis)';
|
||
|
||
public function handle(): int
|
||
{
|
||
$this->info('Collecting Spam/AV metrics…');
|
||
|
||
$collect = function (): array {
|
||
$out = trim(@shell_exec('rspamc counters 2>/dev/null') ?? '');
|
||
$ham = preg_match('/\bham\s*:\s*(\d+)/i', $out, $m1) ? (int)$m1[1] : 0;
|
||
$spam = preg_match('/\bspam\s*:\s*(\d+)/i', $out, $m2) ? (int)$m2[1] : 0;
|
||
$reject = preg_match('/\breject\s*:\s*(\d+)/i', $out, $m3) ? (int)$m3[1] : 0;
|
||
|
||
$rspamdVer = trim(@shell_exec('rspamadm version 2>/dev/null') ?? '') ?: '–';
|
||
$clamVer = trim(@shell_exec('clamd --version 2>/dev/null || clamscan --version 2>/dev/null') ?? '') ?: '–';
|
||
|
||
$sigUpdated = null;
|
||
$log = @shell_exec('grep -i "Database updated" /var/log/clamav/freshclam.log | tail -n1 2>/dev/null');
|
||
if ($log) { $sigUpdated = trim($log); }
|
||
|
||
return compact('ham','spam','reject','rspamdVer','clamVer','sigUpdated') + ['ts' => time()];
|
||
};
|
||
|
||
$data = $collect();
|
||
Setting::set('spamav.metrics', $data);
|
||
Cache::put('dash.spamav', $data, 60);
|
||
|
||
$this->info(sprintf(
|
||
'ham=%d spam=%d reject=%d | rspamd=%s | clam=%s',
|
||
$data['ham'], $data['spam'], $data['reject'], $data['rspamdVer'], $data['clamVer']
|
||
));
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
}
|