]> git.ipfire.org Git - people/stevee/guardian.git/blobdiff - guardian
Use "Logger" module for logging purposes.
[people/stevee/guardian.git] / guardian
index fd16b3a6fd49b476cfde440dc16de30197e21eb1..ba8e99cf52400c8c92cebdbdc2a1441cf53cd95c 100644 (file)
--- a/guardian
+++ b/guardian
@@ -27,20 +27,20 @@ use Thread::Queue;
 use Linux::Inotify2;
 use Time::HiRes qw[ time sleep ];
 
+require Guardian::Base;
 require Guardian::Config;
+require Guardian::Logger;
 require Guardian::Parser;
 require Guardian::Socket;
 
 use warnings;
 
+# Disable warnings of unjoinded threads when stopping guardian.
+no warnings 'threads';
+
 # 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 = ();
 
@@ -69,6 +69,14 @@ if (defined($cmdargs{"help"})) {
 # Push the may be given config file argument.
 my %mainsettings = &Guardian::Config::UseConfig($cmdargs{"config"});
 
+# Initialize Logger.
+my $logger = Guardian::Logger->New(%mainsettings);
+$logger->Log("debug", "Logger successfully initialized...");
+
+# Shared hash between the main process and all threads. It will store all
+# monitored files and their current file position.
+my %monitored_files :shared = ();
+
 # 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";;
@@ -91,7 +99,8 @@ while(1) {
                # Grab the data of the top enqueued event.
                my $event = $queue->peek();
 
-               print "Got event: $event\n";
+               # Log processed event.
+               $logger->Log("debug", "QUEUE - Processed event: $event");
 
                # Drop processed event from queue.
                $queue->dequeue();
@@ -114,6 +123,9 @@ sub Init () {
        # Setup IPC mechanism via Socket in an own thread.
        threads->create(\&Socket);
 
+       # Generate hash of monitored files.
+       %monitored_files = &Guardian::Base::GenerateMonitoredFiles(\%mainsettings, \%monitored_files);
+
        # Start worker threads.
        &StartWorkers();
 }
@@ -144,9 +156,6 @@ sub Worker ($) {
        # Signal handler to kill worker.
        $SIG{'KILL'} = sub { threads->exit(); };
 
-       # Get the fileposition.
-       my $fileposition = &Init_fileposition("$file");
-
        # Create inotify watcher.
        my $watcher = new Linux::Inotify2 or die "Could not use inotify. $!\n";
 
@@ -156,6 +165,9 @@ sub Worker ($) {
        # Switch watcher into non-blocking mode.
        $watcher->blocking(0);
 
+       # Log successfully spawned worker.
+       $logger->Log("debug", "Spawned worker thread for: $file");
+
        # Infinite loop.
        while(1) {
                # Check for any events and perform them, if there
@@ -163,6 +175,9 @@ sub Worker ($) {
                if ($watcher->read) {
                        my @message = ();
 
+                       # Obtain fileposition from hash.
+                       my $fileposition = $monitored_files{$file};
+
                        # Open the file.
                        open (FILE, $file) or die "Could not open $file. $!\n";
 
@@ -178,8 +193,13 @@ sub Worker ($) {
                                push (@message, $line);
                        }
 
-                       # Update fileposition.
-                       $fileposition = tell(FILE);
+                       {
+                               # Lock shared hash.
+                               lock(%monitored_files);
+
+                               # Update fileposition.
+                               $monitored_files{$file} = tell(FILE);
+                       }
 
                        # Close file.
                        close(FILE);
@@ -219,6 +239,9 @@ sub Socket () {
        # Create the Server socket by calling the responsible function.
        my $server = &Guardian::Socket::Server();
 
+       # Log successfull creation of socket.
+       $logger->Log("debug", "Listening to Socket...");
+
         # Accept incomming connections from the socket.
         while (my $connection = $server->accept()) {
                # Autoflush the socket after the data
@@ -230,6 +253,9 @@ sub Socket () {
                        # Remove any newlines.
                        chomp($message);
 
+                       # Log recieved socket command.
+                       $logger->Log("debug", "Socket - Recieved message: $message");
+
                        # Send the recieved data message to the
                        # socket parser.
                        my $action = &Guardian::Socket::Message_Parser($message);
@@ -257,24 +283,23 @@ sub SignalHandler {
        $SIG{INT} = \&Shutdown;
        $SIG{TERM} = \&Shutdown;
        $SIG{QUIT} = \&Shutdown;
+       $SIG{HUP} = \&Reload;
 }
 
 #
 ## Function to start the workers (threads) for all monitored files.
 #
-## This function will loop through the array of monitored files and will
+## This function will loop through the hash of monitored files and will
 ## spawn an own thread based worker for each file. Every created worker will
 ## be added to the array of running workers.
 #
 sub StartWorkers () {
-       # Loop through the array of which files should be monitored and
-       # create a worker thread for each single one.
-       foreach my $monitored_file (@monitored_files) {
-               # Check if the file exists and is readable.
-               if (-r "$monitored_file") {
-                       # Create worker thread for the file.
-                       push @running_workers, threads->create(\&Worker,$monitored_file);
-               }
+       # Loop through the hash which contains the monitored files and start
+       # a worker thread for each single one.
+       foreach my $file (keys %monitored_files) {
+               $logger->Log("debug", "Starting worker thread for $file");
+               # Create worker thread for the file.
+               push @running_workers, threads->create(\&Worker,$file);
        }
 }
 
@@ -289,8 +314,33 @@ sub StopWorkers () {
        foreach my $worker (@running_workers) {
                # Send the worker the "KILL" signal and detach the
                # thread so perl can do an automatically clean-up.
-               $worker->kill('KILL')->detach();
+               $worker->kill('KILL');
        }
+       $logger->Log("debug", "All workers are stopped now...");
+}
+
+#
+## Reload function.
+#
+## This function will get called if the signal handler recieves a "SIGHUP" signal,
+## or the reload command will be sent via socket connection. It is responsible for
+## reloading all configure options and stopping/starting the worker threads.
+#
+sub Reload () {
+       # Log reload.
+       $logger->Log("info", "Reload configuration...");
+
+       # Stop all running workers.
+       &StopWorkers();
+
+       # Re-read configuration file.
+       %mainsettings = &Guardian::Config::UseConfig($cmdargs{"config"});
+
+       # Re-generate hash of monitored files.
+       %monitored_files = &Guardian::Base::GenerateMonitoredFiles(\%mainsettings, \%monitored_files);
+
+       # Restart the worker threads.
+       &StartWorkers();
 }
 
 #
@@ -300,12 +350,23 @@ sub StopWorkers () {
 ## by the signal handler when recieving INT (2), QUIT (3) and TERM (15) signals.
 #
 sub Shutdown () {
+       # Log shutdown.
+       $logger->Log("info", "Shutting down...");
+
        # Stop all workers.
        &StopWorkers();
 
        # Remove socket file on exit.
        &Guardian::Socket::RemoveSocketFile();
 
+       # Sleep for one second to give perl some
+       # time to proper clean up everything before
+       # exiting.
+       sleep(1);
+
+       # Log good bye message.
+       $logger->Log("debug", "Good Bye!");
+
        # Exit guardian.
        exit;
 }