lots more work on this

This commit is contained in:
Zane C. B-H 2021-11-21 13:13:34 -06:00
parent 72862a3c93
commit 9b041af15e
2 changed files with 150 additions and 12 deletions

View File

@ -0,0 +1,133 @@
#!/usr/bin/env perl
#
#This software is Copyright (c) 2021 by Zane C. Bowers-Hadley.
#
#This is free software, licensed under:
#
# The Artistic License 2.0 (GPL Compatible)
use strict;
use warnings;
use TOML qw(from_toml);
use File::Syslogger;
use File::Slurp qw(read_file);
use Getopt::Long qw(:config pass_through);
my $version = '0.0.1';
my $help;
my $toml_file = '/usr/local/etc/filesyslogger.toml';
my $pri;
my $fac;
my $socket;
my $program;
GetOptions(
'p=s' => \$pri,
'P=s' => \$program,
'f=s' => \$fac,
't=s' => \$toml_file,
's=s' => \$socket,
'h' => \$help,
'help' => \$help,
);
# make sure the file exists
if ( !-f $toml_file ) {
die( '"' . $toml_file . '" does not exist' );
}
# read the in or die
my $toml_raw = read_file($toml_file) or die 'Failed to read "' . $toml_file . '"';
# read the specified config
my ( $toml, $err ) = from_toml($toml_raw);
unless ($toml) {
die "Error parsing toml,'" . $toml_file . "'" . $err;
}
# read in the defaults, letting the switches over ride
if (
defined( $toml->{program} )
&& !defined($program) )
{
$program = $toml->{program};
}
if (
defined( $toml->{'facility'} )
&& !defined($fac) )
{
$fac = $toml->{facility};
}
if (
defined( $toml->{priority} )
&& !defined($pri) )
{
$pri = $toml->{priority};
}
if (
defined( $toml->{socket} )
&& !defined($socket) )
{
$socket = $toml->{socket};
}
# process the config
my %files;
my @toml_keys = keys( %{$toml} );
my $int = 0;
while ( defined( $toml_keys[$int] ) ) {
my $item = $toml_keys[$int];
if ( ref( $toml->{$item} ) eq "HASH" ) {
# add the file in question
$files{$item} = $toml->{$item};
}
$int++;
}
File::Syslogger->run(
facility => $fac,
pri => $pri,
socket => $socket,
files => \%files,
);
=head1 NAME
filesyslogger - Tails the configured files and sends it to syslog.
=head1 SYNOPSIS
filesyslogger [B<-P> <program>] [B<-p> <priority>] [B<-f> <facility>] [B<-t> <config>] [B<-s> <socket>]
=head1 FLAGS
=head2 -P <program>
The program name to use. If not specified, 'fileSyslogger' is used.
=head2 -p <priority>
The priority to use. If not specified, 'notice' is used.
=head2 -f <facility>
The facility to use. If not specified, 'daemon' is used.
=head2 -s <socket>
The socket to use. If not specified, '/var/run/log' is used.
=head2 -t <config file>
This is the config file to use. If not specified, '/usr/local/etc/filesyslogger.toml' is used.
=head1 CONFIG FILE
The file format used is TOML.
=cut

View File

@ -9,22 +9,18 @@ use Sys::Hostname;
=head1 NAME
File::Syslogger -
File::Syslogger - Use POE to tail a file and read new lines into syslog.
=head1 VERSION
Version 0.0,1
Version 0.0.1
=cut
our $VERSION = '0.01';
our $VERSION = '0.0.1';
=head1 SYNOPSIS
Quick summary of what the module does.
Perhaps a little code snippet.
use File::Syslogger;
File::Syslogger->run(
@ -48,13 +44,16 @@ This will die if there are any config issues.
The following options are optionaal.
pri - The priority of the logged item.
Default is 'info'.
Default is 'notice'.
facility - The facility for logging.
Default is 'daemon'.
program - Name of the program logging.
Default is 'fileSyslogger'.
socket - The syslogd socket.
Default is "/var/run/log"
The option files is a hash of hashes. It has one mandatory
key, 'file', which is the file to follow. All the above
@ -113,6 +112,10 @@ sub run {
$opts{program} = 'fileSyslogger';
}
if (!defined( $opts{socket} )) {
$opts{socket}="/var/run/log";
}
#mapping for severity for constant handling
my %sev_mapping = (
'emerg' => LOG_EMERG,
@ -129,7 +132,7 @@ sub run {
# default to info if none is specified
if ( !defined( $opts{pri} ) ) {
$opts{pri} = "info";
$opts{pri} = "notice";
}
else {
# one was specified, convert to lower case and make sure it valid
@ -165,7 +168,7 @@ sub run {
# default to system if none is specified
if ( !defined( $opts{facility} ) ) {
$opts{facility} = 'system';
$opts{facility} = 'daemon';
}
else {
# one was specified, convert to lower case and make sure it valid
@ -198,6 +201,7 @@ sub run {
# none specified, so using default
$item_fac = $opts{facility};
}
$item_fac=$fac_mapping{$item_fac};
# figure out what facility to use for this item
my $item_pri;
@ -213,6 +217,7 @@ sub run {
# none specified, so using default
$item_pri = $opts{pri};
}
$item_pri=$sev_mapping{$item_pri};
# figure out what program name to use
my $item_program;
@ -225,7 +230,7 @@ sub run {
}
# create the logger that will be used by the POE session
my $logger = Log::Syslog::Fast->new( LOG_UNIX, undef, undef, $item_fac, $item_pri, hostname, $item_program );
my $logger = Log::Syslog::Fast->new( LOG_UNIX, $opts{socket}, 1, $item_fac, $item_pri, hostname, $item_program );
# create the POE session
POE::Session->create(
@ -237,7 +242,7 @@ sub run {
);
},
got_log_line => sub {
print "Log: $_[ARG0]\n";
$_[HEAP]{logger}->send( $_[ARG0] );
},
},
heap => { file => $opts{files}{$item}{file}, logger => $logger },