Rename and move InitFileposition function.
[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 8
1e736116
SS
9#
10## Function to generate a hash of monitored files and their file positions.
11#
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
cfe5a220
SS
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.
1e736116 16#
cfe5a220
SS
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.
1e736116
SS
21#
22sub 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] };
27
28 # Private hash for storing the new monitored files.
29 my %new_monitored_files = ();
30
cfe5a220
SS
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.
1e736116
SS
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_/);
39
cfe5a220
SS
40 # Splitt monitor instruction into 2 parts, to grab the
41 # requested parser module.
42 my ($start, $parser) = split (/_/, $config_option);
43
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);
49
50 # Check if the configured parser is available and valid.
51 next unless(&Guardian::Parser::IsSupportedParser($parser));
52
1e736116
SS
53 # Get the configured file for this option.
54 my $file = $mainsettings{$config_option};
55
56 # Skip the file, if it does not exist or is not read-able.
57 next unless(-r "$file");
58
59 # Check if the file not yet has been added to the hash
60 # of monitored files.
61 unless(exists($current_monitored_files{$file})) {
62 # Add the file, init and store the fileposition.
cfe5a220
SS
63 $new_monitored_files{$file} = $parser;
64 } else {
65 # Copy file and parser information to the new hash.
1e736116
SS
66 $new_monitored_files{$file} = $current_monitored_files{$file};
67 }
68 }
69
70 # Return the new_monitored_files hash.
71 return %new_monitored_files;
72}
73
cfe5a220
SS
74#
75## The FilePositions function.
76#
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
79## monitored files.
80#
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.
83#
84sub 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] };
89
90 # Private hash for storing the new monitored files.
91 my %new_file_positions = ();
92
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.
97 #
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
102 # of file positions.
103 if (exists($current_file_positions{$file})) {
104 # Copy file position into temporary hash.
105 $new_file_positions{$file} = $current_file_positions{$file};
106 } else {
107 # Call function to obtain the file position.
7a6a2682 108 my $position = &_initFileposition($file);
cfe5a220
SS
109
110 # Add filename and position to the temporary hash.
111 $new_file_positions{$file} = $position;
112 }
113 }
114
115 # Return the new_file_positions hash.
116 return %new_file_positions;
117}
118
7a6a2682
SS
119#
120## Function for fileposition initialization.
121#
122## This function is used to get the cursor position of the end of file (EOF) of
123## a specified file.
124#
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.
128#
129sub _initFileposition ($) {
130 my $file = $_[0];
131
132 # Open the file.
133 open(FILE, $file) or die "Could not open $file. $!";
134
135 # Just seek to the end of the file (EOF).
136 seek(FILE, 0, 2);
137
138 # Get and store the position.
139 my $position = tell(FILE),
140
141 # Close the file again.
142 close(FILE);
143
144 # Return the position.
145 return $position;
146}
147
1e736116 1481;