File Coverage

blib/lib/Message/Passing/Input/Syslog.pm
Criterion Covered Total %
statement 27 27 100.0
branch 4 6 66.6
condition 2 3 66.6
subroutine 8 8 100.0
pod 0 1 0.0
total 41 45 91.1


line stmt bran cond sub pod time code
1             package Message::Passing::Input::Syslog;
2 3     3   131128 use Moo;
  3         22476  
  3         17  
3 3     3   4616 use MRO::Compat;
  3         5459  
  3         97  
4 3     3   1731 use Time::ParseDate;
  3         33262  
  3         231  
5 3     3   593 use Socket qw( getnameinfo NI_NUMERICHOST NI_NUMERICSERV );
  3         3667  
  3         876  
6 3     3   1711 use Parse::Syslog::Line qw( parse_syslog_line );
  3         78276  
  3         274  
7              
8             # for speed, we don't need the created DateTime object
9             $Parse::Syslog::Line::DateTimeCreate = 0;
10              
11 3     3   977 use namespace::clean -except => 'meta';
  3         22525  
  3         26  
12              
13             extends 'Message::Passing::Input::Socket::UDP';
14              
15             has '+port' => (
16             default => sub { 5140 },
17             required => 0,
18             );
19              
20             has protocol => (
21             is => 'ro',
22             default => sub { 'udp' },
23             );
24              
25             sub BUILD {
26 2     2 0 1506 my $self = shift;
27 2 50       22 die sprintf("Protocol '%s' is not supported, only 'udp' currently", $self->protocol)
28             if $self->protocol ne 'udp';
29             }
30              
31             sub _send_data {
32 2     2   2675 my ( $self, $message, $from ) = @_;
33              
34 2         13 my $msg = parse_syslog_line( $message );
35 2 100       701 my $time = defined $msg->{datetime_raw} ? parsedate($msg->{datetime_raw}) : undef;
36 2   66     1143 $msg->{epochtime} = $time || time();
37              
38 2         25 my ( $err, $ipaddr, $port ) = getnameinfo( $from, NI_NUMERICHOST, NI_NUMERICSERV );
39 2 50       12 $msg->{received_from} = $ipaddr
40             unless $err;
41              
42 2         29 $self->output_to->consume( $msg );
43             }
44              
45             1;
46              
47             =head1 NAME
48              
49             Message::Passing::Input::Syslog - input messages from Syslog.
50              
51             =head1 SYNOPSIS
52              
53             message-pass --input Syslog --input_options '{"port":"5140"}' --output STDOUT
54              
55             =head1 DESCRIPTION
56              
57             Provides a syslog server for UDP syslog.
58              
59             Can be used to ship syslog logs into a L<Message::Passing> system.
60              
61             The message format is a hashref containing all keys returned from
62             L<Parse::Syslog::Line/parse_syslog_line> plus received_from.
63              
64             =head1 ATTRIBUTES
65              
66             =head2 hostname
67              
68             The IP to bind the daemon to. By default, binds to 127.0.0.1, which
69             means that the server can only be accessed from localhost. Use C<0.0.0.0>
70             to bind to all interfaces.
71              
72             =head2 port
73              
74             The port to bind to, defaults to 5140, as the default syslog port (514)
75             is likely already taken by your regular syslogd, and needs root permissio
76             to bind to it.
77              
78             =head2 protocol
79              
80             The protocol to listen on, currently only UDP is supported.
81              
82             =head1 SEE ALSO
83              
84             =over
85              
86             =item L<Message::Passing::Syslog>
87              
88             =item L<Message::Passing>
89              
90             =back
91              
92             =head1 AUTHOR, COPYRIGHT AND LICENSE
93              
94             See L<Message::Passing::Syslog>.
95              
96             =cut