]> git.ipfire.org Git - people/stevee/guardian.git/blame - modules/Base.pm
Allow to configure the used parser for a monitored file.
[people/stevee/guardian.git] / modules / Base.pm
CommitLineData
1e736116
SS
1package Guardian::Base;
2use strict;
3use warnings;
4
5use Exporter qw(import);
6
cfe5a220 7our @EXPORT_OK = qw(GenerateMonitoredFiles FilePositions);
1e736116
SS
8
9#
10## Function for fileposition initialization.
11#
12## This function is used to get the cursor position of the end of file (EOF) of
13## a specified file.
14#
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.
18#
19sub InitFileposition ($) {
20 my $file = $_[0];
21
22 # Open the file.
c0a59a63 23 open(FILE, $file) or die "Could not open $file. $!";
1e736116
SS
24
25 # Just seek to the end of the file (EOF).
26 seek(FILE, 0, 2);
27
28 # Get and store the position.
29 my $position = tell(FILE),
30
31 # Close the file again.
32 close(FILE);
33
34 # Return the position.
35 return $position;
36}
37
38#
39## Function to generate a hash of monitored files and their file positions.
40#
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
cfe5a220
SS
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.
1e736116 45#
cfe5a220
SS
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.
1e736116
SS
50#
51sub 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] };
56
57 # Private hash for storing the new monitored files.
58 my %new_monitored_files = ();
59
cfe5a220
SS
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.
1e736116
SS
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_/);
68
cfe5a220
SS
69 # Splitt monitor instruction into 2 parts, to grab the
70 # requested parser module.
71 my ($start, $parser) = split (/_/, $config_option);
72
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);
78
79 # Check if the configured parser is available and valid.
80 next unless(&Guardian::Parser::IsSupportedParser($parser));
81
1e736116
SS
82 # Get the configured file for this option.
83 my $file = $mainsettings{$config_option};
84
85 # Skip the file, if it does not exist or is not read-able.
86 next unless(-r "$file");
87
88 # Check if the file not yet has been added to the hash
89 # of monitored files.
90 unless(exists($current_monitored_files{$file})) {
91 # Add the file, init and store the fileposition.
cfe5a220
SS
92 $new_monitored_files{$file} = $parser;
93 } else {
94 # Copy file and parser information to the new hash.
1e736116
SS
95 $new_monitored_files{$file} = $current_monitored_files{$file};
96 }
97 }
98
99 # Return the new_monitored_files hash.
100 return %new_monitored_files;
101}
102
cfe5a220
SS
103#
104## The FilePositions function.
105#
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
108## monitored files.
109#
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.
112#
113sub 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] };
118
119 # Private hash for storing the new monitored files.
120 my %new_file_positions = ();
121
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.
126 #
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
131 # of file positions.
132 if (exists($current_file_positions{$file})) {
133 # Copy file position into temporary hash.
134 $new_file_positions{$file} = $current_file_positions{$file};
135 } else {
136 # Call function to obtain the file position.
137 my $position = &InitFileposition($file);
138
139 # Add filename and position to the temporary hash.
140 $new_file_positions{$file} = $position;
141 }
142 }
143
144 # Return the new_file_positions hash.
145 return %new_file_positions;
146}
147
1e736116 1481;