From 4e43a2090915c432d9ad83e35f4f97ab47594bfd Mon Sep 17 00:00:00 2001
From: Ramon Novoa <rnovoa@artica.es>
Date: Wed, 3 Sep 2014 18:52:17 +0200
Subject: [PATCH] 2014-09-03  Ramon Novoa  <rnovoa@artica.es>

	* pandora_agent: Added basic support to log to syslog.
---
 pandora_agents/unix/ChangeLog     |  4 ++++
 pandora_agents/unix/pandora_agent | 22 +++++++++++++++-------
 2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/pandora_agents/unix/ChangeLog b/pandora_agents/unix/ChangeLog
index 0c5339d0b1..f9300ea981 100644
--- a/pandora_agents/unix/ChangeLog
+++ b/pandora_agents/unix/ChangeLog
@@ -1,3 +1,7 @@
+2014-09-03  Ramon Novoa  <rnovoa@artica.es>
+
+	* pandora_agent: Added basic support to log to syslog.
+
 2014-09-03  Ramon Novoa  <rnovoa@artica.es>
 
 	* pandora_agent: Added support for global macros.
diff --git a/pandora_agents/unix/pandora_agent b/pandora_agents/unix/pandora_agent
index 2c10086bd6..750dd729d4 100755
--- a/pandora_agents/unix/pandora_agent
+++ b/pandora_agents/unix/pandora_agent
@@ -29,7 +29,7 @@ use Sys::Hostname;
 use File::Basename;
 use File::Copy;
 use IO::Socket;
-
+use Sys::Syslog;
 
 # Agent XML data
 my $Xml;
@@ -286,13 +286,15 @@ sub start_log (;$) {
 	my $quiet = shift;
 
 	# Get the logfile
-	my $log_file_name = read_config ('logfile');
-	$log_file_name = '/var/log/pandora/pandora_agent.log' unless defined ($log_file_name);
+	$Conf{'logfile'} = read_config ('logfile');
+	$Conf{'logfile'} = '/var/log/pandora/pandora_agent.log' unless defined ($Conf{'logfile'});
 
 	# Open it
-	open ($LogFileFH, "> $log_file_name") or error ("Could not open log file '$log_file_name' for writing: $!.");
-	if (! defined ($quiet)) {
-		print "Logging to $log_file_name\n";
+	if ($Conf{'logfile'} eq 'syslog') {
+		openlog('pandora_agent', 'nowait', 'daemon');
+	} else {
+		open ($LogFileFH, "> $Conf{'logfile'}") or error ("Could not open log file $Conf{'logfile'} for writing: $!.");
+		print "Logging to $Conf{'logfile'}\n" if (!defined ($quiet));
 	}
 }
 
@@ -300,7 +302,11 @@ sub start_log (;$) {
 # Close the agent logfile and stop logging.
 ################################################################################
 sub stop_log () {
-	close ($LogFileFH);
+	if ($Conf{'logfile'} eq 'syslog') {
+		closelog();
+	} else {
+		close ($LogFileFH);
+	}
 }
 
 ################################################################################
@@ -311,6 +317,8 @@ sub log_message ($$;$) {
 	
 	if (defined ($dest)) {
 		print $dest strftime ('%Y/%m/%d %H:%M:%S', localtime ()) . " - [$source] - $msg\n";
+	} elsif ($Conf{'logfile'} eq 'syslog') {
+		syslog('info', $msg);
 	} else {
 		#Trying to write into log file to test its writable
 		syswrite ($LogFileFH, "");