Make Syslog optional

This commit is contained in:
Félix Suárez 2023-06-04 11:59:09 -06:00
parent e2550c22e3
commit f248ea1fe6
1 changed files with 18 additions and 8 deletions

View File

@ -1008,9 +1008,22 @@ use Sys::Hostname;
use File::Basename;
use File::Copy;
use IO::Socket;
use Sys::Syslog;
use Time::Local;
eval {
require Sys::Syslog;
Sys::Syslog->import();
};
if ($@) {
print ("[INFO] Could not import Sys::Syslog module\n\n");
}
print "---------------------------------------------------------------------\n";
my $result = eval { Sys::Syslog->can('openlog') };
print $result . "\n\n";
BEGIN { push @INC, '/usr/lib/perl5'; }
# Agent XML data
@ -1413,7 +1426,7 @@ sub start_log (;$) {
$Conf{'logfile'} = '/var/log/pandora/pandora_agent.log' unless defined ($Conf{'logfile'});
# Open it
if ($Conf{'logfile'} eq 'syslog') {
if ($Conf{'logfile'} eq 'syslog' && eval { Sys::Syslog->can('openlog') }) {
openlog('pandora_agent', 'nowait', 'daemon');
} else {
open ($LogFileFH, "> $Conf{'logfile'}") or error ("Could not open log file $Conf{'logfile'} for writing: $!.");
@ -1425,16 +1438,13 @@ sub start_log (;$) {
# Rotates the agent logfile.
################################################################################
sub rotate_log () {
if ($Conf{'logfile'} eq 'syslog') {
if ($Conf{'logfile'} eq 'syslog' && eval { Sys::Syslog->can('syslog') }) {
# No action needed
return;
} else {
if ($Conf{'logrotate'} < 0){
$Conf{'logrotate'} = DEFAULT_LOG_ROTATE;
}
if ($Conf{'logfile'} eq 'syslog') {
return;
}
# Rotate file
$LogFileIdx = ($LogFileIdx+1) % $Conf{'logrotate'};
@ -1451,7 +1461,7 @@ sub rotate_log () {
# Close the agent logfile and stop logging.
################################################################################
sub stop_log () {
if ($Conf{'logfile'} eq 'syslog') {
if ($Conf{'logfile'} eq 'syslog' && eval { Sys::Syslog->can('closelog') }) {
closelog();
} else {
close ($LogFileFH);
@ -1466,7 +1476,7 @@ sub log_message ($$;$) {
if (defined ($dest)) {
print $dest strftime ('%Y/%m/%d %H:%M:%S', localtime ()) . " - [$source] - $msg\n";
} elsif ($Conf{'logfile'} eq 'syslog') {
} elsif ($Conf{'logfile'} eq 'syslog' && eval { Sys::Syslog->can('syslog') }) {
syslog('info', $msg);
} else {
#Trying to write into log file to test its writable