]> git.ipfire.org Git - people/stevee/guardian.git/blobdiff - modules/Base.pm
Allow to configure the used parser for a monitored file.
[people/stevee/guardian.git] / modules / Base.pm
index 0e43823628461cab951b66aba5accfd940aa3388..fbda49ac76ebdf7fc21e1df36d47f4d2b4846e9d 100644 (file)
@@ -4,7 +4,7 @@ use warnings;
 
 use Exporter qw(import);
 
-our @EXPORT_OK = qw(GenerateMonitoredFiles);
+our @EXPORT_OK = qw(GenerateMonitoredFiles FilePositions);
 
 #
 ## Function for fileposition initialization.
@@ -40,13 +40,13 @@ sub InitFileposition ($) {
 #
 ## This function is responsible for creating the hash of which files should be
 ## monitored by guardian. In order to do this, all options from the given hash of
-## main settings will be parsed and all files to monitor extracted and stored into
-## a temporary hash.
+## main settings will be parsed and all files to monitor and their configured parsers
+## get extracted, validated and stored into a temporary hash.
 #
-## Next step will be to check if the the file allready is part of the existing hash
-## of monitored files and if true to use the stored value of the current cursor position.
-## If this check fails, the responsible function to initialize the cursor position of
-## EOF (end of file) will be called and stored.
+## Next step will be to cleanup files which have been monitored in the past but have been
+## requested for beeing unmonitored for now. To do this, a check if the the file name is
+## part of the existing hash of monitored files and if true to transfer the data into the
+## new temporary hash which get returned by the function.
 #
 sub GenerateMonitoredFiles (\%\%) {
        # Dereference the given hash-refs and store
@@ -57,14 +57,28 @@ sub GenerateMonitoredFiles (\%\%) {
        # Private hash for storing the new monitored files.
        my %new_monitored_files = ();
 
-       # Loop through the temporary hash which contains the main settings
-       # and search for files which should be monitored. Compare if the file
-       # already was part of the Add them to the
-       # private new hash of monitored files which will be returned.
+       # Loop through the temporary hash which contains the main settings.
+       # Search for files which should be monitored and extract the requested
+       # parser. Compare if the file already was a part of the hash which contains
+       # the monitored files and add them to the private new hash of monitored
+       # files which will be returned.
         foreach my $config_option (keys %mainsettings) {
                 # Skip option if it does not look like "Monitor_XYZ".
                 next unless($config_option =~ m/^Monitor_/);
 
+               # Splitt monitor instruction into 2 parts, to grab the
+               # requested parser module.
+               my ($start, $parser) = split (/_/, $config_option);
+
+               # Convert parser name into lower case format.
+               # Internally the parser module name is completely handled
+               # in this way. This also prevents from any problems related
+               # how the parser name has been spelled in the config file.
+               $parser = lc($parser);
+
+               # Check if the configured parser is available and valid.
+               next unless(&Guardian::Parser::IsSupportedParser($parser));
+
                 # Get the configured file for this option.
                 my $file = $mainsettings{$config_option};
 
@@ -75,9 +89,9 @@ sub GenerateMonitoredFiles (\%\%) {
                 # of monitored files.
                 unless(exists($current_monitored_files{$file})) {
                         # Add the file, init and store the fileposition.
-                        $new_monitored_files{$file} = &InitFileposition($file);
-                } else {
-                       # Add the file and grab the fileposition from the existing hash.
+                        $new_monitored_files{$file} = $parser;
+               } else {
+                       # Copy file and parser information to the new hash.
                        $new_monitored_files{$file} = $current_monitored_files{$file};
                }
         }
@@ -86,4 +100,49 @@ sub GenerateMonitoredFiles (\%\%) {
        return %new_monitored_files;
 }
 
+#
+## The FilePositions function.
+#
+## This function is responsible for creating and/or updating the hash which
+## stores the current cursor position of the end of file (EOF) of all
+## monitored files.
+#
+## The function requires the hash of currently monitored files and the old hash
+## of the current file positions in order to work properly.
+#
+sub FilePositions (\%\%) {
+       # Dereference the given hash-refs and store
+       # them into a new temporary hashes.
+       my %monitored_files = %{ $_[0] };
+       my %current_file_positions = %{ $_[1] };
+
+       # Private hash for storing the new monitored files.
+       my %new_file_positions = ();
+
+       # Loop through the hash of monitored files.
+       # Compare if the file allready has been a part of the hash
+       # which contains the file positions and transfer the stored
+       # cursor position into the temporary hash which will be returned.
+       #
+       # Otherwise, call the responsible function to obtain the current
+       # end of file (EOF) and store it.
+       foreach my $file (keys %monitored_files) {
+               # Check if the filename is allready part of the hash
+               # of file positions.
+               if (exists($current_file_positions{$file})) {
+                       # Copy file position into temporary hash.
+                       $new_file_positions{$file} = $current_file_positions{$file};
+               } else {
+                       # Call function to obtain the file position.
+                       my $position = &InitFileposition($file);
+
+                       # Add filename and position to the temporary hash.
+                       $new_file_positions{$file} = $position;
+               }
+       }
+
+       # Return the new_file_positions hash.
+       return %new_file_positions;
+}
+
 1;