File Coverage

blib/lib/Log/Saftpresse/Input/Command.pm
Criterion Covered Total %
statement 15 38 39.4
branch 0 4 0.0
condition n/a
subroutine 5 10 50.0
pod 0 5 0.0
total 20 57 35.0


line stmt bran cond sub pod time code
1             package Log::Saftpresse::Input::Command;
2              
3 1     1   1617 use Moose;
  1         1  
  1         7  
4              
5             # ABSTRACT: log input for slurping the output of a command
6             our $VERSION = '1.6'; # VERSION
7              
8              
9 1     1   5594 use Log::Saftpresse::Input::Command::Child;
  1         3  
  1         45  
10 1     1   9 use Log::Saftpresse::Log4perl;
  1         1  
  1         149  
11              
12 1     1   9 use Time::Piece;
  1         2  
  1         15  
13 1     1   102 use Sys::Hostname;
  1         1  
  1         513  
14              
15             extends 'Log::Saftpresse::Input';
16              
17             has 'command' => ( is => 'ro', isa => 'Str', required => 1);
18             has 'max_chunk_lines' => ( is => 'rw', isa => 'Int', default => 1024 );
19              
20             has '_child' => (
21             is => 'rw', isa => 'Log::Saftpresse::Input::Command::Child', lazy => 1,
22             default => sub {
23             my $self = shift;
24             my $c = Log::Saftpresse::Input::Command::Child->new(
25             command => $self->command,
26             blocking => 0,
27             );
28             $c->start;
29             return $c;
30             },
31             clearer => '_reset_child',
32             );
33              
34             has 'io_select' => (
35             is => 'ro', isa => 'IO::Select', lazy => 1,
36             default => sub {
37             my $self = shift;
38             my $s = IO::Select->new();
39             $s->add( $self->_child->stdout );
40             return $s;
41             },
42             );
43              
44             sub io_handles {
45 0     0 0   my $self = shift;
46 0           return $self->_child->stdout;
47             }
48              
49             sub read_events {
50 0     0 0   my ( $self ) = @_;
51 0           my @events;
52 0           my $cnt = 0;
53 0           while( defined( my $line = $self->_child->stdout->getline ) ) {
54 0           my $event = {
55             'host' => hostname,
56             'time' => Time::Piece->new,
57             $self->process_line( $line ),
58             };
59 0           push( @events, $event );
60 0           $cnt++;
61 0 0         if( $cnt > $self->max_chunk_lines ) {
62 0           last;
63             }
64             }
65 0 0         if( ! $cnt ) {
66 0           $log->warn('input command "'.$self->command.'" at EOF...restarting it');
67 0           $self->_reset_child;
68             }
69 0           return @events;
70             }
71              
72             sub process_line {
73 0     0 0   my ( $self, $line ) = @_;
74 0           chomp( $line );
75 0           return message => $line;
76             }
77              
78             sub eof {
79 0     0 0   my $self = shift;
80 0           return 0;
81             }
82              
83             sub can_read {
84 0     0 0   my ( $self ) = @_;
85 0           my @can_read = $self->io_select->can_read(0);
86 0           return( scalar @can_read );
87             }
88              
89             1;
90              
91             __END__
92              
93             =pod
94              
95             =encoding UTF-8
96              
97             =head1 NAME
98              
99             Log::Saftpresse::Input::Command - log input for slurping the output of a command
100              
101             =head1 VERSION
102              
103             version 1.6
104              
105             =head1 Description
106              
107             This input watches executes a command and will follow its output.
108              
109             =head1 Synopsis
110              
111             <Input alive>
112             module = "Command"
113             command = "journalctl -f"
114             </Input>
115              
116             =head1 Format
117              
118             Foreach line appended to the file a event with the following fields is generated:
119              
120             =over
121              
122             =item message
123              
124             Content of the line.
125              
126             =item host
127              
128             The hostname of the system.
129              
130             =item time
131              
132             The current time.
133              
134             =back
135              
136             =head1 AUTHOR
137              
138             Markus Benning <ich@markusbenning.de>
139              
140             =head1 COPYRIGHT AND LICENSE
141              
142             This software is Copyright (c) 1998 by James S. Seymour, 2015 by Markus Benning.
143              
144             This is free software, licensed under:
145              
146             The GNU General Public License, Version 2, June 1991
147              
148             =cut