File: journalctl

package info (click to toggle)
logwatch 7.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,572 kB
  • sloc: perl: 8,290; sh: 354; makefile: 38
file content (118 lines) | stat: -rw-r--r-- 4,325 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

########################################################
# Covered under the included MIT/X-Consortium License:
#    http://www.opensource.org/licenses/mit-license.php
# All modifications and contributions by other persons to
# this script are assumed to have been donated to the
# Logwatch project and thus assume the above copyright
# and licensing terms.  If you want to make contributions
# under your own copyright or a different license this
# must be explicitly stated in the contribution an the
# Logwatch project reserves the right to not accept such
# contributions.  If you have made significant
# contributions to this script and want to claim
# copyright please contact logwatch-devel@lists.sourceforge.net.
########################################################
#
# The purpose of this script is to pass the output of the journalctl
# command to the logwatch parsers.  The corresponding service file
# in conf/services/ can be simple.  The following example shows a
# service configuration file with two lines:
#   LogFile = none
#   *JournalCtl = "--unit=service_name.service"
# or when combining journals from multiple sources:
#   *JournalCtl = "--merge --no-pager --unit=service_name.service"
#
# If the *JournalCtl command is called from a logfile configuration
# file (in directory conf/logfiles) rather than the service
# configuration file (in directory conf/services), then the
# following is needed in the logfile configuration file:
#   LogFile =
#   LogFile = /dev/null
#   *JournalCtl = "--no-pager --unit=service_name.service"
#
# In addition to the examples above that use --unit, additional
# field values may be needed.  Both "man journalctl" and
# "man systemd.journal-fields" describe additional options and
# fields that may be required.  For example, --facility, --priority,
# --identifier, and _TRANSPORT may need to be specified.
#
# To strip the output of journalctl of additional output, including
# timestamps, the option "--output cat" can be used.  Otherwise,
# additional commands in logwatch (such as "*ApplyStdDate" or
# "*RemoveHeaders", for example) may be needed.
#
# In the examples above, the arguments to the JournalCtl command are
# passed to the journalctl system command.  It is advised to delimit
# the arguments in double quotes to preserve mixed case.

use strict;
use warnings;

eval "use Date::Manip";
my $hasDM = $@ ? 0 : 1;

# logwatch passes arguments as one string delimited by single quotes
my @args  = split(" ", $ARGV[0]);
my @range = get_range( $ENV{LOGWATCH_DATE_RANGE} );

my $Debug = $ENV{'LOGWATCH_DEBUG'} || 0;

if ($Debug > 5) {
   warn join " ", 'journalctl', @args, @range, "\n";
}

system( 'journalctl', @args, @range );

sub get_range {
    my $range = lc( shift || 'all' );
    my @range;

    if ( !$range || $range eq 'all' ) {
        @range = ();
    } elsif ( $range eq 'yesterday' ) {
        push @range, '--since', 'yesterday', '--until', 'today';
    } elsif ( $range eq 'today' ) {
        push @range, '--since', 'today', '--until', 'tomorrow';
    } elsif ($hasDM) {

        # Strip off any period
        $range =~
          s/for\s+(?:those|that|this)\s+((year|month|day|hour|minute|second)s?)\s*$//;

        # Look for between x and y
        my ( $range1, $range2 ) =
          ( $range =~ /^between\s+(.*)\s+and\s+(.*)\s*$/ );

        # Look for since x
        if ( $range =~ /^\s*since\s+/ ) {
            ($range1) = ( $range =~ /\s*since\s+(.*)/ );
            $range2 = "now";
        }

        # Now convert to journalctl friendly dates
        if ( $range1 && $range2 ) {

            # Parse dates
            my $date1 = ParseDate($range1);
            my $date2 = ParseDate($range2);

            # Switch if date2 is before date1
            if ( $date1 && $date2 and Date_Cmp( $date1, $date2 ) > 0 ) {
                my $switch_date = $date1;
                $date1 = $date2;
                $date2 = $switch_date;
            }

            # If we ask for 1/1 to 1/2, we mean 1/2 inclusive. DM returns
            # 1/2 00:00:00. So we add 1 day to the end time.
            $date2 = DateCalc( $date2, '1 day' );

            my $fmt = "%Y-%m-%d %H:%M:%S";
            push @range, '--since', UnixDate( $date1, $fmt ), '--until',
              UnixDate( $date2, $fmt );
        }
    }

    return @range;
}