Add logrotate support.
authorStefan Schantl <stefan.schantl@ipfire.org>
Wed, 17 Feb 2016 13:26:13 +0000 (14:26 +0100)
committerStefan Schantl <stefan.schantl@ipfire.org>
Wed, 17 Feb 2016 13:26:13 +0000 (14:26 +0100)
Add the ability to get informed when the monitored files
have been rotated by logrotate and therefore the stored
file positions needs to be re-newed.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
guardian
modules/Events.pm

index 691ddc6..16d4198 100644 (file)
--- a/guardian
+++ b/guardian
@@ -403,6 +403,49 @@ sub Reload () {
        &StartWorkers();
 }
 
+#
+## Logrotate function.
+#
+## This function only get called when the logrotate command will be sent via
+## the socket connection. It is responsible for validating and altering the current
+## curser positions of the monitored files and stopping/starting the worker threads.
+#
+sub Logrotate () {
+       # Stop all running workers.
+       &StopWorkers();
+
+       {
+       # Lock shared hash.
+       lock(%file_positions);
+
+       # Loop through the hash which contains the current
+       # file positions.
+       foreach my $file (keys(%file_positions)) {
+               # Obtain stored value from hash.
+               my $stored_position = $file_positions{$file};
+
+               # Call function to get the current position.
+               my $current_position = &Guardian::Base::GetFileposition($file);
+
+               # Compare if the current position still matches
+               # the stored one.
+               if ($current_position ne $stored_position) {
+                       # Update to the new position, because
+                       # they has changed during file rotation.
+                       $file_positions{$file} = $current_position;
+
+                       # Log notice about rotated file.
+                       $logger->Log("debug", "$file have been rotated - Using new file position.");
+               }
+       }
+
+       # After this bracket, the lock of the hash will be released.
+       }
+
+       # Restart all worker threads.
+       &StartWorkers();
+}
+
 #
 ## Shutdown function.
 #
index 025fe4f..2be2ac7 100644 (file)
@@ -13,6 +13,7 @@ my %commands = (
        'unblock' => \&CallUnblock,
        'flush' => \&CallFlush,
        'reload' => \&main::Reload,
+       'logrotate' => \&main::Logrotate,
 );
 
 # Hash to store addresses and their current count.