]>
git.ipfire.org Git - people/stevee/guardian.git/blob - modules/Base.pm
1 package Guardian
::Base
;
5 use Exporter
qw(import);
7 our @EXPORT_OK = qw(GenerateMonitoredFiles);
10 ## Function for fileposition initialization.
12 ## This function is used to get the cursor position of the end of file (EOF) of
15 ## In order to prevent from permanently read and keep files opened, or dealing
16 ## with huge logfiles, at initialization time of the worker processes, the file will
17 ## be opened once and the cursor position of the end of file (EOF) get stored.
19 sub InitFileposition
($) {
23 open(FILE
, $file) or die "Could not open $file. $!";
25 # Just seek to the end of the file (EOF).
28 # Get and store the position.
29 my $position = tell(FILE
),
31 # Close the file again.
34 # Return the position.
39 ## Function to generate a hash of monitored files and their file positions.
41 ## This function is responsible for creating the hash of which files should be
42 ## monitored by guardian. In order to do this, all options from the given hash of
43 ## main settings will be parsed and all files to monitor extracted and stored into
46 ## Next step will be to check if the the file allready is part of the existing hash
47 ## of monitored files and if true to use the stored value of the current cursor position.
48 ## If this check fails, the responsible function to initialize the cursor position of
49 ## EOF (end of file) will be called and stored.
51 sub GenerateMonitoredFiles
(\
%\
%) {
52 # Dereference the given hash-refs and store
53 # them into a new temporary hashes.
54 my %mainsettings = %{ $_[0] };
55 my %current_monitored_files = %{ $_[1] };
57 # Private hash for storing the new monitored files.
58 my %new_monitored_files = ();
60 # Loop through the temporary hash which contains the main settings
61 # and search for files which should be monitored. Compare if the file
62 # already was part of the Add them to the
63 # private new hash of monitored files which will be returned.
64 foreach my $config_option (keys %mainsettings) {
65 # Skip option if it does not look like "Monitor_XYZ".
66 next unless($config_option =~ m/^Monitor_/);
68 # Get the configured file for this option.
69 my $file = $mainsettings{$config_option};
71 # Skip the file, if it does not exist or is not read-able.
72 next unless(-r
"$file");
74 # Check if the file not yet has been added to the hash
76 unless(exists($current_monitored_files{$file})) {
77 # Add the file, init and store the fileposition.
78 $new_monitored_files{$file} = &InitFileposition
($file);
80 # Add the file and grab the fileposition from the existing hash.
81 $new_monitored_files{$file} = $current_monitored_files{$file};
85 # Return the new_monitored_files hash.
86 return %new_monitored_files;