]>
git.ipfire.org Git - people/stevee/guardian.git/blob - modules/Base.pm
1 package Guardian
::Base
;
5 use Exporter
qw(import);
7 our @EXPORT_OK = qw(GenerateMonitoredFiles FilePositions);
12 ## Function to generate a hash of monitored files and their file positions.
14 ## This function is responsible for creating the hash of which files should be
15 ## monitored by guardian. In order to do this, all options from the given hash of
16 ## main settings will be parsed and all files to monitor and their configured parsers
17 ## get extracted, validated and stored into a temporary hash.
19 ## Next step will be to cleanup files which have been monitored in the past but have been
20 ## requested for beeing unmonitored for now. To do this, a check if the the file name is
21 ## part of the existing hash of monitored files and if true to transfer the data into the
22 ## new temporary hash which get returned by the function.
24 sub GenerateMonitoredFiles
(\
%\
%) {
25 # Dereference the given hash-refs and store
26 # them into a new temporary hashes.
27 my %mainsettings = %{ $_[0] };
28 my %current_monitored_files = %{ $_[1] };
30 # Private hash for storing the new monitored files.
31 my %new_monitored_files = ();
33 # Loop through the temporary hash which contains the main settings.
34 # Search for files which should be monitored and extract the requested
35 # parser. Compare if the file already was a part of the hash which contains
36 # the monitored files and add them to the private new hash of monitored
37 # files which will be returned.
38 foreach my $config_option (keys %mainsettings) {
39 # Skip option if it does not look like "Monitor_XYZ".
40 next unless($config_option =~ m/^Monitor_/);
42 # Splitt monitor instruction into 2 parts, to grab the
43 # requested parser module.
44 my ($start, $parser) = split (/_/, $config_option);
46 # Convert parser name into lower case format.
47 # Internally the parser module name is completely handled
48 # in this way. This also prevents from any problems related
49 # how the parser name has been spelled in the config file.
50 $parser = lc($parser);
52 # Check if the configured parser is available and valid.
53 next unless(&Guardian
::Parser
::IsSupportedParser
($parser));
55 # Get the configured file for this option.
56 my $file = $mainsettings{$config_option};
58 # Skip the file, if it does not exist or is not read-able.
59 next unless(-r
"$file");
61 # Check if the file not yet has been added to the hash
63 unless(exists($current_monitored_files{$file})) {
64 # Add the file, init and store the fileposition.
65 $new_monitored_files{$file} = $parser;
67 # Copy file and parser information to the new hash.
68 $new_monitored_files{$file} = $current_monitored_files{$file};
72 # Return the new_monitored_files hash.
73 return %new_monitored_files;
77 ## The FilePositions function.
79 ## This function is responsible for creating and/or updating the hash which
80 ## stores the current cursor position of the end of file (EOF) of all
83 ## The function requires the hash of currently monitored files and the old hash
84 ## of the current file positions in order to work properly.
86 sub FilePositions
(\
%\
%) {
87 # Dereference the given hash-refs and store
88 # them into a new temporary hashes.
89 my %monitored_files = %{ $_[0] };
90 my %current_file_positions = %{ $_[1] };
92 # Private hash for storing the new monitored files.
93 my %new_file_positions = ();
95 # Loop through the hash of monitored files.
96 # Compare if the file allready has been a part of the hash
97 # which contains the file positions and transfer the stored
98 # cursor position into the temporary hash which will be returned.
100 # Otherwise, call the responsible function to obtain the current
101 # end of file (EOF) and store it.
102 foreach my $file (keys %monitored_files) {
103 # Check if the filename is allready part of the hash
105 if (exists($current_file_positions{$file})) {
106 # Copy file position into temporary hash.
107 $new_file_positions{$file} = $current_file_positions{$file};
109 # Call function to obtain the file position.
110 my $position = &_initFileposition
($file);
112 # Add filename and position to the temporary hash.
113 $new_file_positions{$file} = $position;
117 # Return the new_file_positions hash.
118 return %new_file_positions;
122 ## Wrapper function for IP address and network validation.
124 ## This wrapper function uses the external Net::IP perl module to
125 ## check if a given input is a valid IPv4/IPv6 address or network.
127 sub IsValidAddressOrNetwork
($) {
130 # Check if the address is a valid IPv4/IPv6 address or network.
131 # Return "undef" False if the address is not valid.
132 my $ip = new Net
::IP
($address) || return undef;
134 # If we got here, the address is valid. Return True.
139 ## Function for fileposition initialization.
141 ## This function is used to get the cursor position of the end of file (EOF) of
144 ## In order to prevent from permanently read and keep files opened, or dealing
145 ## with huge logfiles, at initialization time of the worker processes, the file will
146 ## be opened once and the cursor position of the end of file (EOF) get stored.
148 sub _initFileposition
($) {
152 open(FILE
, $file) or die "Could not open $file. $!";
154 # Just seek to the end of the file (EOF).
157 # Get and store the position.
158 my $position = tell(FILE
),
160 # Close the file again.
163 # Return the position.