Koozali.org: home of the SME Server

[feature request] Programmable clamscan history limit

Offline sages

  • *
  • 211
  • +0/-0
    • http://www.sages.com.au
[feature request] Programmable clamscan history limit
« on: November 02, 2026, 05:51:00 AM »
Long story short. My kz11 system has ~2TB of files and with a J3160 cpu it's taking the best part of 2 1/2 days to do a complete file system scan. My use case has very low incidence of file addition or modification. I'm prepared to take the potential risk of limiting the virus scan to recently modified files.

I started here and faffed my way with some perl and tweaked the existing smeserver-clamscan script to include a configurable cut off date for unmodified files in the clamscan.

in /sbin/e-smith/smeserver-clamscan

Was (only showing area around modifications)

Code: [Select]
$clamscan_opts .= " --official-db-only=yes"
        if ($db->get_prop("clamav", "FilesystemScanUnofficialSigs") || 'no') ne "yes";

open CLAMSCAN, "-|", "nice /usr/bin/clamscan $clamscan_opts $filesystems 2> /var/log/clamd/smeserver-clamscan.log";

my @report = <CLAMSCAN>;
close CLAMSCAN;

changes
Code: [Select]
$clamscan_opts .= " --official-db-only=yes"
        if ($db->get_prop("clamav", "FilesystemScanUnofficialSigs") || 'no') ne "yes";

my $history2scan = $db->get_prop("clamscan", "HistoryToScan") || '365';

open CLAMSCAN, "-|", "nice find $filesystems -ctime -$history2scan -type f -print0 | xargs -0 /usr/bin/clamscan $clamscan_opts 2> /var/log/clamd/smeserver-clamscan.log";

my @report = <CLAMSCAN>;
close CLAMSCAN;

There is a new item added to the configuration database -> clamscan -> HistoryToScan
This defaults to 365 days

Utopia would be to add it to the server panel for clamscan configuration.

Disclaimer, I'm certain that my perl programming is anything like fluent or without issues.
 
...

Offline Jean-Philippe Pialasse

  • *
  • 3,049
  • +11/-0
  • aka Unnilennium
    • http://smeserver.pialasse.com
Re: [feature request] Programmable clamscan history limit
« Reply #1 on: November 07, 2026, 09:19:11 PM »
why default to 365 when you scan every weeks or so?
could be only the time since last scan

current proposition is intended to reduce load on the server but will open a new clam session for every file and hence loading the db in memory on every file (could take up to 6 min)

approach would be to put the list in a temp file then use it to call clam.

NB: the approach assume that the modification time has not been tampered by the virus...
« Last Edit: November 07, 2026, 09:20:59 PM by Jean-Philippe Pialasse »

Offline sages

  • *
  • 211
  • +0/-0
    • http://www.sages.com.au
Re: [feature request] Programmable clamscan history limit
« Reply #2 on: November 08, 2026, 12:01:10 AM »
The mod was based upon an article from here https://rsmitty.github.io/clamscan-files-by-date/

The suggestion of 365 days is just that, a suggestion. (In my case it indicated that I have very few new files on my server) As it's configurable, the argument on it's value is just that, an argument that just depends on the use case.

My server cpu load graphs and reduction in scan duration support the intention of reducing the scan time for my system. ie my goals were met.
Now if that is just due to the reduction in the number of files scanned even with the loading of the scan db then so be it.

As far as a virus altering the modification time, noted but then it's a known risk that the user can either accept or not.

Using a file list as opposed to xargs is a programming choice. Both work. My understanding of the overhead for the clam db is it occurs per call of clamscan not per file. ( https://docs.clamav.net/manual/Usage/Scanning.html 
Code: [Select]
clamscan is a command line tool which uses libclamav to scan files and/or directories for viruses. Unlike clamdscan, clamscan does not require a running clamd instance to function. Instead, clamscan will create a new engine and load in the virus database each time it is run. It will then scan the files and/or directories specified at the command line, create a scan report, and exit.)  The overhead introduced by using xargs in my use case was 8 calls to clamscan, yes, this will change depending upon how xrgs breaks up the file list but that'ss not much in the scheme of things.
Use a file or use xargs, pick one.

There's always a better mouse trap  :grin:
...

Offline Jean-Philippe Pialasse

  • *
  • 3,049
  • +11/-0
  • aka Unnilennium
    • http://smeserver.pialasse.com
Re: [feature request] Programmable clamscan history limit
« Reply #3 on: November 08, 2026, 06:12:27 AM »
8 files does only load 8 times the 2.5-4GB clamav  DB in memory. (depends if you have or not the unofficial sigantures)

try to change 50-100 files in the last interval...

and if you only scan 8 files, you have again way more optimization possible by only loading it once.

Offline sages

  • *
  • 211
  • +0/-0
    • http://www.sages.com.au
Re: [feature request] Programmable clamscan history limit
« Reply #4 on: November 08, 2026, 06:26:02 AM »
8 calls, not 8 files
the point of the exercise with xargs (in this instance) is that it fills the command line with multiple files from the find command
ie
find
file 1
file 2
file 3
etc
then (pseudo syntax)
xargs clamscan file 1 file 2 file 3 ......
in my example the resultant call  to clamscan was 8 times
o/p from report
Code: [Select]
----------- SCAN SUMMARY -----------
Known viruses: 3628253
Engine version: 1.4.6
Scanned directories: 0
Scanned files: 1310
Infected files: 0
Data scanned: 2530.60 MB
Data read: 394598.66 MB (ratio 0.01:1)
Time: 728.904 sec (12 m 8 s)
Start Date: 2026:11:07 00:58:12
End Date:   2026:11:07 01:10:21

----------- SCAN SUMMARY -----------
Known viruses: 3628253
Engine version: 1.4.6
Scanned directories: 0
Scanned files: 1122
Infected files: 0
Data scanned: 238.22 MB
Data read: 6335.10 MB (ratio 0.04:1)
Time: 188.560 sec (3 m 8 s)
Start Date: 2026:11:07 01:10:21
End Date:   2026:11:07 01:13:29

----------- SCAN SUMMARY -----------
Known viruses: 3628253
Engine version: 1.4.6
Scanned directories: 0
Scanned files: 1027
Infected files: 0
Data scanned: 406.29 MB
Data read: 5389.16 MB (ratio 0.08:1)
Time: 232.085 sec (3 m 52 s)
Start Date: 2026:11:07 01:13:29
End Date:   2026:11:07 01:17:21

----------- SCAN SUMMARY -----------
Known viruses: 3628253
Engine version: 1.4.6
Scanned directories: 0
Scanned files: 1177
Infected files: 0
Data scanned: 333.31 MB
Data read: 5650.14 MB (ratio 0.06:1)
Time: 254.765 sec (4 m 14 s)
Start Date: 2026:11:07 01:17:21
End Date:   2026:11:07 01:21:36

----------- SCAN SUMMARY -----------
Known viruses: 3628253
Engine version: 1.4.6
Scanned directories: 0
Scanned files: 1309
Infected files: 0
Data scanned: 5673.89 MB
Data read: 15517.37 MB (ratio 0.37:1)
Time: 2689.552 sec (44 m 49 s)
Start Date: 2026:11:07 01:21:36
End Date:   2026:11:07 02:06:26

----------- SCAN SUMMARY -----------
Known viruses: 3628253
Engine version: 1.4.6
Scanned directories: 0
Scanned files: 469
Infected files: 0
Data scanned: 481.74 MB
Data read: 7940.71 MB (ratio 0.06:1)
Time: 332.067 sec (5 m 32 s)
Start Date: 2026:11:07 02:06:26
End Date:   2026:11:07 02:11:58

----------- SCAN SUMMARY -----------
Known viruses: 3628253
Engine version: 1.4.6
Scanned directories: 0
Scanned files: 755
Infected files: 0
Data scanned: 7129.08 MB
Data read: 101621.90 MB (ratio 0.07:1)
Time: 5356.679 sec (89 m 16 s)
Start Date: 2026:11:07 02:11:58
End Date:   2026:11:07 03:41:14

----------- SCAN SUMMARY -----------
Known viruses: 3628253
Engine version: 1.4.6
Scanned directories: 0
Scanned files: 147
Infected files: 0
Data scanned: 2234.37 MB
Data read: 13821.60 MB (ratio 0.16:1)
Time: 1629.931 sec (27 m 9 s)
Start Date: 2026:11:07 03:41:14
End Date:   2026:11:07 04:08:24
...

Offline Jean-Philippe Pialasse

  • *
  • 3,049
  • +11/-0
  • aka Unnilennium
    • http://smeserver.pialasse.com
Re: [feature request] Programmable clamscan history limit
« Reply #5 on: November 08, 2026, 06:48:52 AM »

give a try to this one
Code: [Select]
#!/usr/bin/perl -w
use File::Temp qw(tempfile);
use strict;
use esmith::ConfigDB;

my $db = esmith::ConfigDB->open_ro or die "Couldn't open ConfigDB";

my $filesystems = $db->get_prop("clamav", "FilesystemScanFilesystems") || '/';

my $MailReport = $db->get_prop("clamav", "FilesystemScanReportTo") || 'admin';

# could be based on FilesystemScan property value (daily,weekly)  and /or  last scan date.
my $history2scan          = $db->get_prop("clamscan", "HistoryToScan")            || '365';


my @clamscan_opts;
push @clamscan_opts, " --recursive --infected --stdout";

push @clamscan_opts, " --log /var/log/clamd/clamscan.log";

my $quarantine_dir = $db->get_prop("clamav", "QuarantineDirectory") ||
                "/var/spool/clamav/quarantine";

my @exclude = split /,/, ($db->get_prop("clamav", "FilesystemScanExclude") ||
                          "/proc,/sys,/usr/share/doc");

push @exclude, $quarantine_dir;

push @clamscan_opts, " --exclude=$_" for (@exclude);

push @clamscan_opts, " --no-html"
      if ($db->get_prop("clamav", "ScanHTML") || "yes") eq "no";

push @clamscan_opts, " --no-mail"
        if ($db->get_prop("clamav", "ScanMail") || "yes") eq "no";

push @clamscan_opts, " --move=$quarantine_dir"
        if ($db->get_prop("clamav", "Quarantine") || "disabled") eq "enabled";

push @clamscan_opts, "--official-db-only=yes"
      if ($db->get_prop("clamav", "FilesystemScanUnofficialSigs") || 'no') ne "yes";


my ($fh, $temp_list_file) = tempfile(CLEANUP => 1);

# Safely execute 'find' to list the files changed within the history window
my $find_cmd = "nice find $filesystems -ctime -$history2scan -type f";
open(my $find_fh, "-|", $find_cmd) or die "Failed to execute find: $!";
while (<$find_fh>) {
    print $fh $_; # Write each file path into the temporary file
}
close($find_fh);
close($fh);

my $clamav_binary = "/usr/bin/clamscan";
my $log_file      = "/var/log/clamd/smeserver-clamscan.log";

my @cmd = (
    "nice", $clamav_binary,
    @clamscan_opts,
    "--file-list=$temp_list_file"
);

push @cmd, "2>", $log_file;

open(my $clam_fh, "-|", join(" ", @cmd)) or die "Failed to launch ClamAV scan: $!";
my @report = <$clam_fh>;
close($clam_fh);


my $hostname = $db->get_value("SystemName") . "." .
        $db->get_value("DomainName");

my $date = localtime;

open MAIL, "| /bin/mail " .
        "-s \"[$hostname] Clam Antivirus Scan Results - $date\" $MailReport";

print MAIL @report;
close MAIL;





Offline sages

  • *
  • 211
  • +0/-0
    • http://www.sages.com.au
Re: [feature request] Programmable clamscan history limit
« Reply #6 on: November 09, 2026, 12:04:56 AM »
Your script
Code: [Select]
----------- SCAN SUMMARY -----------
Known viruses: 3628258
Engine version: 1.4.6
Scanned directories: 0
Scanned files: 2698
Infected files: 0
Data scanned: 11739.91 MB
Data read: 170461.78 MB (ratio 0.07:1)
Time: 8438.818 sec (140 m 38 s)
Start Date: 2026:11:08 17:29:47
End Date:   2026:11:08 19:50:26

And reran mine from op with same parameters
Code: [Select]
----------- SCAN SUMMARY -----------
Known viruses: 3628258
Engine version: 1.4.6
Scanned directories: 0
Scanned files: 1016
Infected files: 0
Data scanned: 502.49 MB
Data read: 6914.36 MB (ratio 0.07:1)
Time: 389.127 sec (6 m 29 s)
Start Date: 2026:11:08 19:56:25
End Date:   2026:11:08 20:02:54

----------- SCAN SUMMARY -----------
Known viruses: 3628258
Engine version: 1.4.6
Scanned directories: 0
Scanned files: 601
Infected files: 0
Data scanned: 2073.57 MB
Data read: 48090.80 MB (ratio 0.04:1)
Time: 1376.843 sec (22 m 56 s)
Start Date: 2026:11:08 20:02:55
End Date:   2026:11:08 20:25:51

----------- SCAN SUMMARY -----------
Known viruses: 3628258
Engine version: 1.4.6
Scanned directories: 0
Scanned files: 575
Infected files: 0
Data scanned: 5063.35 MB
Data read: 42926.29 MB (ratio 0.12:1)
Time: 3658.712 sec (60 m 58 s)
Start Date: 2026:11:08 20:25:51
End Date:   2026:11:08 21:26:50

----------- SCAN SUMMARY -----------
Known viruses: 3628258
Engine version: 1.4.6
Scanned directories: 0
Scanned files: 506
Infected files: 0
Data scanned: 4319.22 MB
Data read: 72530.34 MB (ratio 0.06:1)
Time: 3328.990 sec (55 m 28 s)
Start Date: 2026:11:08 21:26:50
End Date:   2026:11:08 22:22:1

Near enough to the same runtime (1 email file was deleted during the scan) just executed in 4 chunks.
Now we have at least two ways of removing the outer coating from the feline. :smile:
...

Offline Jean-Philippe Pialasse

  • *
  • 3,049
  • +11/-0
  • aka Unnilennium
    • http://smeserver.pialasse.com
Re: [feature request] Programmable clamscan history limit
« Reply #7 on: November 09, 2026, 07:18:05 PM »
Sages,
thanks for taking the time to test with real use case!

3 questions:
- how much ram do you have? (probably more impact if not a lot of ram)
- what do you think of using the frequency of scan as the window to scan with a switch to either scan all or scan since last scan?
- would you mind to bug it as NFR?

Offline sages

  • *
  • 211
  • +0/-0
    • http://www.sages.com.au
Re: [feature request] Programmable clamscan history limit
« Reply #8 on: November 09, 2026, 11:15:18 PM »
1/ Intel J3160 cpu, 8G ram, OS drive ssd with data (/home/e-smith/files) mounted on raid 1 3tb wd red hdd. Attached images show cpu load, memory free and used for both runs, 1st run your script, 2nd initial script using xargs. Time matches quoted scan reports in previous reply)

2/ Configurable to do as per current (all files) or files since last scan sounds good. Wondering if having an overlap on files since last scan + 1 day would be overkill or cover any timing issues with any file changes during scan. ie for a weekly scan you actually scan for 8 days.

3/ ok, https://bugs.koozali.org/show_bug.cgi?id=13822
Original full clamscan was taking ~ 2.5 days to complete for ~ 2TB of files
With history2scan set at 14 days, both scripts took just under 2 1/5 hours to run. see previous posts
« Last Edit: November 09, 2026, 11:17:30 PM by sages »
...

Offline sages

  • *
  • 211
  • +0/-0
    • http://www.sages.com.au
Re: [feature request] Programmable clamscan history limit
« Reply #9 on: November 09, 2026, 11:17:06 PM »
missing attachment from previous post
...

Offline Jean-Philippe Pialasse

  • *
  • 3,049
  • +11/-0
  • aka Unnilennium
    • http://smeserver.pialasse.com
Re: [feature request] Programmable clamscan history limit
« Reply #10 on: November 10, 2026, 12:40:49 AM »
you read my mind for the overlap !