]>
git.ipfire.org Git - people/stevee/guardian.git/blob - modules/Base.pm
fbda49ac76ebdf7fc21e1df36d47f4d2b4846e9d
1 package Guardian
::Base
;
5 use Exporter
qw(import);
7 our @EXPORT_OK = qw(GenerateMonitoredFiles FilePositions);
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 and their configured parsers
44 ## get extracted, validated and stored into a temporary hash.
46 ## Next step will be to cleanup files which have been monitored in the past but have been
47 ## requested for beeing unmonitored for now. To do this, a check if the the file name is
48 ## part of the existing hash of monitored files and if true to transfer the data into the
49 ## new temporary hash which get returned by the function.
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 # Search for files which should be monitored and extract the requested
62 # parser. Compare if the file already was a part of the hash which contains
63 # the monitored files and add them to the private new hash of monitored
64 # files which will be returned.
65 foreach my $config_option (keys %mainsettings) {
66 # Skip option if it does not look like "Monitor_XYZ".
67 next unless($config_option =~ m/^Monitor_/);
69 # Splitt monitor instruction into 2 parts, to grab the
70 # requested parser module.
71 my ($start, $parser) = split (/_/, $config_option);
73 # Convert parser name into lower case format.
74 # Internally the parser module name is completely handled
75 # in this way. This also prevents from any problems related
76 # how the parser name has been spelled in the config file.
77 $parser = lc($parser);
79 # Check if the configured parser is available and valid.
80 next unless(&Guardian
::Parser
::IsSupportedParser
($parser));
82 # Get the configured file for this option.
83 my $file = $mainsettings{$config_option};
85 # Skip the file, if it does not exist or is not read-able.
86 next unless(-r
"$file");
88 # Check if the file not yet has been added to the hash
90 unless(exists($current_monitored_files{$file})) {
91 # Add the file, init and store the fileposition.
92 $new_monitored_files{$file} = $parser;
94 # Copy file and parser information to the new hash.
95 $new_monitored_files{$file} = $current_monitored_files{$file};
99 # Return the new_monitored_files hash.
100 return %new_monitored_files;
104 ## The FilePositions function.
106 ## This function is responsible for creating and/or updating the hash which
107 ## stores the current cursor position of the end of file (EOF) of all
110 ## The function requires the hash of currently monitored files and the old hash
111 ## of the current file positions in order to work properly.
113 sub FilePositions
(\
%\
%) {
114 # Dereference the given hash-refs and store
115 # them into a new temporary hashes.
116 my %monitored_files = %{ $_[0] };
117 my %current_file_positions = %{ $_[1] };
119 # Private hash for storing the new monitored files.
120 my %new_file_positions = ();
122 # Loop through the hash of monitored files.
123 # Compare if the file allready has been a part of the hash
124 # which contains the file positions and transfer the stored
125 # cursor position into the temporary hash which will be returned.
127 # Otherwise, call the responsible function to obtain the current
128 # end of file (EOF) and store it.
129 foreach my $file (keys %monitored_files) {
130 # Check if the filename is allready part of the hash
132 if (exists($current_file_positions{$file})) {
133 # Copy file position into temporary hash.
134 $new_file_positions{$file} = $current_file_positions{$file};
136 # Call function to obtain the file position.
137 my $position = &InitFileposition
($file);
139 # Add filename and position to the temporary hash.
140 $new_file_positions{$file} = $position;
144 # Return the new_file_positions hash.
145 return %new_file_positions;