]> git.ipfire.org Git - people/stevee/guardian.git/blame - modules/Base.pm
Adjust error messages in case of failure.
[people/stevee/guardian.git] / modules / Base.pm
CommitLineData
1e736116
SS
1package Guardian::Base;
2use strict;
3use warnings;
4
5use Exporter qw(import);
6
7our @EXPORT_OK = qw(GenerateMonitoredFiles);
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
43## main settings will be parsed and all files to monitor extracted and stored into
44## a temporary hash.
45#
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.
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
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_/);
67
68 # Get the configured file for this option.
69 my $file = $mainsettings{$config_option};
70
71 # Skip the file, if it does not exist or is not read-able.
72 next unless(-r "$file");
73
74 # Check if the file not yet has been added to the hash
75 # of monitored files.
76 unless(exists($current_monitored_files{$file})) {
77 # Add the file, init and store the fileposition.
78 $new_monitored_files{$file} = &InitFileposition($file);
79 } else {
80 # Add the file and grab the fileposition from the existing hash.
81 $new_monitored_files{$file} = $current_monitored_files{$file};
82 }
83 }
84
85 # Return the new_monitored_files hash.
86 return %new_monitored_files;
87}
88
891;