]>
git.ipfire.org Git - people/stevee/guardian.git/blob - modules/Base.pm
2f5e73773bc825d21c6000d54d33cd2daba5f130
1 package Guardian
::Base
;
5 use Exporter
qw(import);
7 our @EXPORT_OK = qw(GenerateMonitoredFiles FilePositions);
10 ## Function to generate a hash of monitored files and their file positions.
12 ## This function is responsible for creating the hash of which files should be
13 ## monitored by guardian. In order to do this, all options from the given hash of
14 ## main settings will be parsed and all files to monitor and their configured parsers
15 ## get extracted, validated and stored into a temporary hash.
17 ## Next step will be to cleanup files which have been monitored in the past but have been
18 ## requested for beeing unmonitored for now. To do this, a check if the the file name is
19 ## part of the existing hash of monitored files and if true to transfer the data into the
20 ## new temporary hash which get returned by the function.
22 sub GenerateMonitoredFiles
(\
%\
%) {
23 # Dereference the given hash-refs and store
24 # them into a new temporary hashes.
25 my %mainsettings = %{ $_[0] };
26 my %current_monitored_files = %{ $_[1] };
28 # Private hash for storing the new monitored files.
29 my %new_monitored_files = ();
31 # Loop through the temporary hash which contains the main settings.
32 # Search for files which should be monitored and extract the requested
33 # parser. Compare if the file already was a part of the hash which contains
34 # the monitored files and add them to the private new hash of monitored
35 # files which will be returned.
36 foreach my $config_option (keys %mainsettings) {
37 # Skip option if it does not look like "Monitor_XYZ".
38 next unless($config_option =~ m/^Monitor_/);
40 # Splitt monitor instruction into 2 parts, to grab the
41 # requested parser module.
42 my ($start, $parser) = split (/_/, $config_option);
44 # Convert parser name into lower case format.
45 # Internally the parser module name is completely handled
46 # in this way. This also prevents from any problems related
47 # how the parser name has been spelled in the config file.
48 $parser = lc($parser);
50 # Check if the configured parser is available and valid.
51 next unless(&Guardian
::Parser
::IsSupportedParser
($parser));
53 # Get the configured file for this option.
54 my $file = $mainsettings{$config_option};
56 # Skip the file, if it does not exist or is not read-able.
57 next unless(-r
"$file");
59 # Check if the file not yet has been added to the hash
61 unless(exists($current_monitored_files{$file})) {
62 # Add the file, init and store the fileposition.
63 $new_monitored_files{$file} = $parser;
65 # Copy file and parser information to the new hash.
66 $new_monitored_files{$file} = $current_monitored_files{$file};
70 # Return the new_monitored_files hash.
71 return %new_monitored_files;
75 ## The FilePositions function.
77 ## This function is responsible for creating and/or updating the hash which
78 ## stores the current cursor position of the end of file (EOF) of all
81 ## The function requires the hash of currently monitored files and the old hash
82 ## of the current file positions in order to work properly.
84 sub FilePositions
(\
%\
%) {
85 # Dereference the given hash-refs and store
86 # them into a new temporary hashes.
87 my %monitored_files = %{ $_[0] };
88 my %current_file_positions = %{ $_[1] };
90 # Private hash for storing the new monitored files.
91 my %new_file_positions = ();
93 # Loop through the hash of monitored files.
94 # Compare if the file allready has been a part of the hash
95 # which contains the file positions and transfer the stored
96 # cursor position into the temporary hash which will be returned.
98 # Otherwise, call the responsible function to obtain the current
99 # end of file (EOF) and store it.
100 foreach my $file (keys %monitored_files) {
101 # Check if the filename is allready part of the hash
103 if (exists($current_file_positions{$file})) {
104 # Copy file position into temporary hash.
105 $new_file_positions{$file} = $current_file_positions{$file};
107 # Call function to obtain the file position.
108 my $position = &_initFileposition
($file);
110 # Add filename and position to the temporary hash.
111 $new_file_positions{$file} = $position;
115 # Return the new_file_positions hash.
116 return %new_file_positions;
120 ## Function for fileposition initialization.
122 ## This function is used to get the cursor position of the end of file (EOF) of
125 ## In order to prevent from permanently read and keep files opened, or dealing
126 ## with huge logfiles, at initialization time of the worker processes, the file will
127 ## be opened once and the cursor position of the end of file (EOF) get stored.
129 sub _initFileposition
($) {
133 open(FILE
, $file) or die "Could not open $file. $!";
135 # Just seek to the end of the file (EOF).
138 # Get and store the position.
139 my $position = tell(FILE
),
141 # Close the file again.
144 # Return the position.