Blame SOURCES/logwatch-journal.patch

98ab6d
commit 23e714ad43285d59c5b5852ef2c6013593d64671
98ab6d
Author: bjorn <bjorn1@users.sourceforge.net>
98ab6d
Date:   Sun May 15 13:49:08 2016 -0700
98ab6d
98ab6d
    [journalctl] Added shared script contributed by Mark Grimes.
98ab6d
98ab6d
diff --git a/scripts/shared/journalctl b/scripts/shared/journalctl
98ab6d
new file mode 100755
98ab6d
index 0000000..1627fd4
98ab6d
--- /dev/null
98ab6d
+++ b/scripts/shared/journalctl
98ab6d
@@ -0,0 +1,83 @@
98ab6d
+#!/usr/bin/perl
98ab6d
+#
98ab6d
+# The purpose of this script is to pass the output of the journalctl
98ab6d
+# command to the logwatch parsers.  The corresponding conf/logfile 
98ab6d
+# can be simple.  The following example shows a logfile with two lines:
98ab6d
+# LogFile = /dev/null
98ab6d
+# *JournalCtl = "--output=cat --unit=service_name.service"
98ab6d
+#
98ab6d
+# In the example above, the arguments to the JournalCtl command are
98ab6d
+# passed to the journalctl system command.  It is advised to delimit
98ab6d
+# the arguments in double quotes to preserve mixed case, if
98ab6d
+# applicable.
98ab6d
+
98ab6d
+use strict;
98ab6d
+use warnings;
98ab6d
+
98ab6d
+eval "use Date::Manip";
98ab6d
+my $hasDM = $@ ? 0 : 1;
98ab6d
+
98ab6d
+# logwatch passes arguments as one string delimited by single quotes
98ab6d
+my @args  = split(" ", $ARGV[0]);
98ab6d
+my @range = get_range( $ENV{LOGWATCH_DATE_RANGE} );
98ab6d
+
98ab6d
+my $Debug = $ENV{'LOGWATCH_DEBUG'} || 0;
98ab6d
+
98ab6d
+if ($Debug > 5) {
98ab6d
+   warn join " ", 'journalctl', @args, @range, "\n";
98ab6d
+}
98ab6d
+
98ab6d
+system( 'journalctl', @args, @range );
98ab6d
+
98ab6d
+sub get_range {
98ab6d
+    my $range = lc( shift || 'all' );
98ab6d
+    my @range;
98ab6d
+
98ab6d
+    if ( !$range || $range eq 'all' ) {
98ab6d
+        @range = ();
98ab6d
+    } elsif ( $range eq 'yesterday' ) {
98ab6d
+        push @range, '--since', 'yesterday', '--until', 'today';
98ab6d
+    } elsif ( $range eq 'today' ) {
98ab6d
+        push @range, '--since', 'today', '--until', 'tomorrow';
98ab6d
+    } elsif ($hasDM) {
98ab6d
+
98ab6d
+        # Strip off any period
98ab6d
+        $range =~
98ab6d
+          s/for\s+(?:those|that|this)\s+((year|month|day|hour|minute|second)s?)\s*$//;
98ab6d
+
98ab6d
+        # Look for between x and y
98ab6d
+        my ( $range1, $range2 ) =
98ab6d
+          ( $range =~ /^between\s+(.*)\s+and\s+(.*)\s*$/ );
98ab6d
+
98ab6d
+        # Look for since x
98ab6d
+        if ( $range =~ /^\s*since\s+/ ) {
98ab6d
+            ($range1) = ( $range =~ /\s*since\s+(.*)/ );
98ab6d
+            $range2 = "now";
98ab6d
+        }
98ab6d
+
98ab6d
+        # Now convert to journalctl friendly dates
98ab6d
+        if ( $range1 && $range2 ) {
98ab6d
+
98ab6d
+            # Parse dates
98ab6d
+            my $date1 = ParseDate($range1);
98ab6d
+            my $date2 = ParseDate($range2);
98ab6d
+
98ab6d
+            # Switch if date2 is before date1
98ab6d
+            if ( $date1 && $date2 and Date_Cmp( $date1, $date2 ) > 0 ) {
98ab6d
+                my $switch_date = $date1;
98ab6d
+                $date1 = $date2;
98ab6d
+                $date2 = $switch_date;
98ab6d
+            }
98ab6d
+
98ab6d
+            # If we ask for 1/1 to 1/2, we mean 1/2 inclusive. DM returns
98ab6d
+            # 1/2 00:00:00. So we add 1 day to the end time.
98ab6d
+            $date2 = DateCalc( $date2, '1 day' );
98ab6d
+
98ab6d
+            my $fmt = "%Y-%m-%d %H:%M:%S";
98ab6d
+            push @range, '--since', UnixDate( $date1, $fmt ), '--until',
98ab6d
+              UnixDate( $date2, $fmt );
98ab6d
+        }
98ab6d
+    }
98ab6d
+
98ab6d
+    return @range;
98ab6d
+}