]> git.ipfire.org Git - people/stevee/guardian.git/blob - modules/Parser.pm
Except and log if an event could not be parsed correctly.
[people/stevee/guardian.git] / modules / Parser.pm
1 package Guardian::Parser;
2 use strict;
3 use warnings;
4
5 use Exporter qw(import);
6
7 our @EXPORT_OK = qw(IsSupportedParser Parser);
8
9 # This hash contains all supported parsers and which function
10 # has to be called to parse messages in the right way.
11 my %logfile_parsers = (
12 "snort" => \&message_parser_snort,
13 );
14
15 #
16 ## The main parsing function.
17 #
18 ## It is used to determine which sub-parser has to be used to
19 ## parse the given message in the right way and to return if
20 ## any action should be performed.
21 #
22 sub Parser ($$) {
23 my ($parser, @message) = @_;
24
25 # If no responsible message parser could be found, just return nothing.
26 unless (exists($logfile_parsers{$parser})) {
27 return;
28 }
29
30 # Call responsible message parser.
31 my $action = $logfile_parsers{$parser}->(@message);
32
33 # If the parser successfully parsed the message, an action
34 # has been returned.
35 if ($action) {
36 # Return which action should be performed.
37 return "count $action";
38 }
39
40 # If the parser was not able to parse the the given message
41 # in the right way, return Nothing.
42 return;
43 }
44
45 #
46 ## IsSupportedParser function.
47 #
48 ## This very tiny function checks if a given parser name is available and
49 ## therefore a supported parser.
50 #
51 ## To perform these check, the function is going to lookup if a key in the
52 ## hash of supported parsers is available
53 #
54 sub IsSupportedParser ($) {
55 my $parser = $_[0];
56
57 # Check if a key for the given parser exists in the hash of logfile_parsers.
58 if(exists($logfile_parsers{$parser})) {
59 # Found a valid parser, so return nothing.
60 return 1;
61 }
62
63 # Return "False" if we got here, and therefore no parser
64 # is available.
65 return;
66 }
67
68 #
69 ## The Snort message parser.
70 #
71 ## This subfunction is responsible for parsing sort alerts and determine if
72 ## an action should be performed.
73 #
74 sub message_parser_snort($) {
75 my @message = @_;
76
77 # XXX
78 # Currently this parser just returns a simple message.
79 return "$message[0] SNORT A simple Snort Message";
80 }
81
82 1;