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