From 45bf2a03c98db3aeb415d4315933f238748ecb53 Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Mon, 23 Nov 2015 09:57:25 +0100 Subject: [PATCH] Add support for command line arguments and usage of "Config" module. guardian now can be called with some additional command line arguments, like "--config" to specify a different config file than the default one. The optoions "--help" and "--vesion" will display some help texts and version informations on the console. The "--foreground" option is not supported/implemented yet and will be used to keep the process in the foreground and not fork into background at a later time. The "Config" module now also is used to get all settings from the config file and store them into a hash called mainsettings for a later usage. Signed-off-by: Stefan Schantl --- guardian | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/guardian b/guardian index c0773ee..3435c09 100644 --- a/guardian +++ b/guardian @@ -22,17 +22,50 @@ use strict; use threads; use threads::shared; +use Getopt::Long; use Thread::Queue; use Linux::Inotify2; +require Guardian::Config; require Guardian::Parser; require Guardian::Socket; +# Define version. +my $version ="2.0"; + # Array to store the monitored logfiles. my @monitored_files = ( "/var/log/snort/alert", ); +# Get and store the given command line arguments in a hash. +my %cmdargs = (); + +&GetOptions (\%cmdargs, + 'foreground|f', + 'config|c=s', + 'help|h', + 'version|v', +); + +# Show help / version information. +if (defined($cmdargs{"help"})) { + print "Guardian $version \n"; + print "Usage: guardian \n"; + print " -c, --config\t\tspecifiy a configuration file other than the default (/etc/guardian/guardian.conf)\n"; + print " -f, --foreground\trun in the foreground (doesn't fork, any output goes to STDOUT)\n"; + print " -h, --help\t\tshows this help\n"; + print " -v, --version\t\tdisplay programm version and exit.\n"; + exit; +} elsif (defined($cmdargs{"version"})) { + print "Guardian $version \n"; + exit; +} + +# Read-in the configuration file and store the settings. +# Push the may be given config file argument. +my %mainsettings = &Guardian::Config::UseConfig($cmdargs{"config"}); + # Create the main queue. It is used to store and process all events which are # reported and enqueued by the worker threads. my $queue :shared = new Thread::Queue or die "Could not create new, empty queue. $!\n";; -- 2.39.2