2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-20 07:16:03 +00:00

fhem.pl & 92_Filelog.pm: archiveCompress attribute (Forum #41245)

git-svn-id: https://svn.fhem.de/fhem/trunk@9581 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2015-10-21 19:06:58 +00:00
parent 77832f5858
commit 8fde1df22a
2 changed files with 52 additions and 19 deletions

View File

@ -39,7 +39,8 @@ FileLog_Initialize($)
$hash->{AttrFn} = "FileLog_Attr"; $hash->{AttrFn} = "FileLog_Attr";
# logtype is used by the frontend # logtype is used by the frontend
$hash->{AttrList} = "disable:0,1 disabledForIntervals logtype reformatFn ". $hash->{AttrList} = "disable:0,1 disabledForIntervals logtype reformatFn ".
"nrarchive archivedir archivecmd addStateEvent:0,1"; "nrarchive archivedir archivecmd addStateEvent:0,1 ".
"archiveCompress";
$hash->{FW_summaryFn} = "FileLog_fhemwebFn"; $hash->{FW_summaryFn} = "FileLog_fhemwebFn";
$hash->{FW_detailFn} = "FileLog_fhemwebFn"; $hash->{FW_detailFn} = "FileLog_fhemwebFn";
@ -1202,14 +1203,20 @@ FileLog_regexpFn($$)
shell command (no enclosing " is needed), and each % in the command shell command (no enclosing " is needed), and each % in the command
will be replaced with the name of the old logfile.<br> will be replaced with the name of the old logfile.<br>
If this attribute is not set, but nrarchive and/or archivecmd are set, If this attribute is not set, but nrarchive is set, then nrarchive old
then nrarchive old logfiles are kept along the current one while older logfiles are kept along the current one while older ones are moved to
ones are moved to archivedir (or deleted if archivedir is not set). archivedir (or deleted if archivedir is not set). <br>
<br>
Note: setting these attributes for the global instance will effect the Note: setting these attributes for the global instance will effect the
<a href="#logfile">FHEM logfile</a> only. <a href="#logfile">FHEM logfile</a> only.
</li><br> </li><br>
<a name="archiveCompress"></a>
<li>archiveCompress<br>
If nrarchive, archivedir and archiveCompress is set, then the files
in the archivedir will be compressed.
</li><br>
<li><a href="#disable">disable</a></li> <li><a href="#disable">disable</a></li>
<li><a href="#disabledForIntervals">disabledForIntervals</a></li> <li><a href="#disabledForIntervals">disabledForIntervals</a></li>
@ -1486,18 +1493,22 @@ FileLog_regexpFn($$)
shell-Kommando ( eine Einbettung in " ist nicht notwendig), und jedes % shell-Kommando ( eine Einbettung in " ist nicht notwendig), und jedes %
in diesem Befehl wird durch den Namen des alten Logfiles ersetzt.<br> in diesem Befehl wird durch den Namen des alten Logfiles ersetzt.<br>
Wenn dieses Attribut nicht gesetzt wird, aber daf&uuml;r nrarchive Wenn dieses Attribut nicht gesetzt wird, aber daf&uuml;r nrarchive,
und/oder archivecmd, werden nrarchive viele Logfiles im aktuellen werden nrarchive viele Logfiles im aktuellen Verzeichnis gelassen, und
Verzeichnis gelassen, und &auml;ltere Dateien in das Archivverzeichnis &auml;ltere Dateien in das Archivverzeichnis (archivedir) verschoben
(archivedir) verschoben (oder gel&ouml;scht, falls kein archivedir (oder gel&ouml;scht, falls kein archivedir gesetzt wurde).<br>
gesetzt wurde).<br>
Hinweis: Werden diese Attribute als global instance gesetzt, hat das Hinweis: Werden diese Attribute als global instance gesetzt, hat das
auschlie&szlig;lich auf das <a href="#logfile">FHEM logfile</a> auschlie&szlig;lich auf das <a href="#logfile">FHEM logfile</a>
Auswirkungen. Auswirkungen. </li><br>
<a name="archiveCompress"></a>
<li>archiveCompress<br>
Falls nrarchive, archivedir und archiveCompress gesetzt ist, dann
werden die Dateien im archivedir komprimiert abgelegt.
</li><br> </li><br>
<li><a href="#disable">disable</a></li> <li><a href="#disable">disable</a></li>
<li><a href="#addStateEvent">addStateEvent</a></li> <li><a href="#addStateEvent">addStateEvent</a></li>

View File

@ -114,7 +114,7 @@ sub getAllGets($);
sub getAllSets($); sub getAllSets($);
sub getUniqueId(); sub getUniqueId();
sub latin1ToUtf8($); sub latin1ToUtf8($);
sub myrename($$); sub myrename($$$);
sub notifyRegexpChanged($$); sub notifyRegexpChanged($$);
sub readingsBeginUpdate($); sub readingsBeginUpdate($);
sub readingsBulkUpdate($$$@); sub readingsBulkUpdate($$$@);
@ -255,6 +255,7 @@ my @globalAttrList = qw(
apiversion apiversion
archivecmd archivecmd
archivedir archivedir
archiveCompress
autoload_undefined_devices:1,0 autoload_undefined_devices:1,0
autosave:1,0 autosave:1,0
backup_before_update backup_before_update
@ -3121,9 +3122,19 @@ doGlobalDef($)
##################################### #####################################
# rename does not work over Filesystems: lets copy it # rename does not work over Filesystems: lets copy it
sub sub
myrename($$) myrename($$$)
{ {
my ($from, $to) = @_; my ($name, $from, $to) = @_;
my $ca = AttrVal($name, "archiveCompress", 0);
if($ca) {
eval { require Compress::Zlib; };
if($@) {
$ca = 0;
Log 1, $@;
}
}
$to .= ".gz" if($ca);
if(!open(F, $from)) { if(!open(F, $from)) {
Log(1, "Rename: Cannot open $from: $!"); Log(1, "Rename: Cannot open $from: $!");
@ -3133,8 +3144,18 @@ myrename($$)
Log(1, "Rename: Cannot open $to: $!"); Log(1, "Rename: Cannot open $to: $!");
return; return;
} }
while(my $l = <F>) {
print T $l; if($ca) {
my $d = Compress::Zlib::deflateInit(-WindowBits=>31);
my $buf;
while(sysread(F,$buf,32768) > 0) {
syswrite(T, $d->deflate($buf));
}
syswrite(T, $d->flush());
} else {
while(my $l = <F>) {
print T $l;
}
} }
close(F); close(F);
close(T); close(T);
@ -3146,13 +3167,14 @@ myrename($$)
sub sub
HandleArchiving($;$) HandleArchiving($;$)
{ {
my ($log,$diff) = @_; my ($log,$flogInitial) = @_;
my $ln = $log->{NAME}; my $ln = $log->{NAME};
return if(!$attr{$ln}); return if(!$attr{$ln});
# If there is a command, call that # If there is a command, call that
my $cmd = $attr{$ln}{archivecmd}; my $cmd = $attr{$ln}{archivecmd};
if($cmd) { if($cmd) {
return if($flogInitial); # Forum #41245
$cmd =~ s/%/$log->{currentlogfile}/g; $cmd =~ s/%/$log->{currentlogfile}/g;
Log 2, "Archive: calling $cmd"; Log 2, "Archive: calling $cmd";
system($cmd); system($cmd);
@ -3179,11 +3201,11 @@ HandleArchiving($;$)
closedir(DH); closedir(DH);
my $max = int(@files)-$nra; my $max = int(@files)-$nra;
$max -= $diff if($diff); $max-- if($flogInitial);
for(my $i = 0; $i < $max; $i++) { for(my $i = 0; $i < $max; $i++) {
if($ard) { if($ard) {
Log 2, "Moving $files[$i] to $ard"; Log 2, "Moving $files[$i] to $ard";
myrename("$dir/$files[$i]", "$ard/$files[$i]"); myrename($ln, "$dir/$files[$i]", "$ard/$files[$i]");
} else { } else {
Log 2, "Deleting $files[$i]"; Log 2, "Deleting $files[$i]";
unlink("$dir/$files[$i]"); unlink("$dir/$files[$i]");