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