From 52e6293b9602ccc1f45cf090cd4d9144b43416ac Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Fri, 21 Oct 2016 17:09:22 +0200 Subject: [PATCH 1/3] Allow to configure the Syslog facility refs #11214 --- .../Config/General/LoggingConfigForm.php | 61 ++++++++++++++----- .../Logger/Writer/SyslogWriter.php | 32 +++++++++- 2 files changed, 75 insertions(+), 18 deletions(-) diff --git a/application/forms/Config/General/LoggingConfigForm.php b/application/forms/Config/General/LoggingConfigForm.php index 35cd37a89..cf2228f7c 100644 --- a/application/forms/Config/General/LoggingConfigForm.php +++ b/application/forms/Config/General/LoggingConfigForm.php @@ -4,6 +4,7 @@ namespace Icinga\Forms\Config\General; use Icinga\Application\Logger; +use Icinga\Application\Platform; use Icinga\Web\Form; /** @@ -90,22 +91,50 @@ class LoggingConfigForm extends Form ) ) ); - /* - * Note(el): Since we provide only one possible value for the syslog facility, I opt against exposing - * this configuration. - */ -// $this->addElement( -// 'select', -// 'logging_facility', -// array( -// 'required' => true, -// 'label' => $this->translate('Facility'), -// 'description' => $this->translate('The syslog facility to utilize.'), -// 'multiOptions' => array( -// 'user' => 'LOG_USER' -// ) -// ) -// ); + + if (Platform::isWindows()) { + /* @see https://secure.php.net/manual/en/function.openlog.php */ + $this->addElement( + 'hidden', + 'logging_facility', + array( + 'value' => 'user', + 'disabled' => true + ) + ); + } else { + $this->addElement( + 'select', + 'logging_facility', + array( + 'required' => true, + 'label' => $this->translate('Facility'), + 'description' => $this->translate('The syslog facility to utilize.'), + 'value' => 'user', + 'multiOptions' => array( + 'auth' => 'LOG_AUTH', + 'authpriv' => 'LOG_AUTHPRIV', + 'cron' => 'LOG_CRON', + 'daemon' => 'LOG_DAEMON', + 'kern' => 'LOG_KERN', + 'local0' => 'LOG_LOCAL0', + 'local1' => 'LOG_LOCAL1', + 'local2' => 'LOG_LOCAL2', + 'local3' => 'LOG_LOCAL3', + 'local4' => 'LOG_LOCAL4', + 'local5' => 'LOG_LOCAL5', + 'local6' => 'LOG_LOCAL6', + 'local7' => 'LOG_LOCAL7', + 'lpr' => 'LOG_LPR', + 'mail' => 'LOG_MAIL', + 'news' => 'LOG_NEWS', + 'syslog' => 'LOG_SYSLOG', + 'user' => 'LOG_USER', + 'uucp' => 'LOG_UUCP' + ) + ) + ); + } } elseif (isset($formData['logging_log']) && $formData['logging_log'] === 'file') { $this->addElement( 'text', diff --git a/library/Icinga/Application/Logger/Writer/SyslogWriter.php b/library/Icinga/Application/Logger/Writer/SyslogWriter.php index b1387078c..f26a07412 100644 --- a/library/Icinga/Application/Logger/Writer/SyslogWriter.php +++ b/library/Icinga/Application/Logger/Writer/SyslogWriter.php @@ -6,6 +6,7 @@ namespace Icinga\Application\Logger\Writer; use Icinga\Data\ConfigObject; use Icinga\Application\Logger; use Icinga\Application\Logger\LogWriter; +use Icinga\Exception\ConfigurationError; /** * Log to the syslog service @@ -32,7 +33,25 @@ class SyslogWriter extends LogWriter * @var array */ public static $facilities = array( - 'user' => LOG_USER + 'auth' => LOG_AUTH, + 'authpriv' => LOG_AUTHPRIV, + 'cron' => LOG_CRON, + 'daemon' => LOG_DAEMON, + 'kern' => LOG_KERN, + 'local0' => LOG_LOCAL0, + 'local1' => LOG_LOCAL1, + 'local2' => LOG_LOCAL2, + 'local3' => LOG_LOCAL3, + 'local4' => LOG_LOCAL4, + 'local5' => LOG_LOCAL5, + 'local6' => LOG_LOCAL6, + 'local7' => LOG_LOCAL7, + 'lpr' => LOG_LPR, + 'mail' => LOG_MAIL, + 'news' => LOG_NEWS, + 'syslog' => LOG_SYSLOG, + 'user' => LOG_USER, + 'uucp' => LOG_UUCP ); /** @@ -55,7 +74,16 @@ class SyslogWriter extends LogWriter public function __construct(ConfigObject $config) { $this->ident = $config->get('application', 'icingaweb2'); - $this->facility = static::$facilities['user']; + + $configuredFacility = $config->get('facility', 'user'); + if (! isset(static::$facilities[$configuredFacility])) { + throw new ConfigurationError( + 'Invalid logging facility: "%s" (expected one of: %s)', + $configuredFacility, + implode(', ', array_keys(static::$facilities)) + ); + } + $this->facility = static::$facilities[$configuredFacility]; } /** From 00af3d61b7728a5bb4f8e1525bd76341bfd94ffa Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Mon, 24 Oct 2016 10:43:27 +0200 Subject: [PATCH 2/3] LoggingConfigForm: provide less Syslog facilities refs #11214 --- .../forms/Config/General/LoggingConfigForm.php | 14 ++------------ .../Application/Logger/Writer/SyslogWriter.php | 14 ++------------ 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/application/forms/Config/General/LoggingConfigForm.php b/application/forms/Config/General/LoggingConfigForm.php index cf2228f7c..428e5a75e 100644 --- a/application/forms/Config/General/LoggingConfigForm.php +++ b/application/forms/Config/General/LoggingConfigForm.php @@ -112,11 +112,7 @@ class LoggingConfigForm extends Form 'description' => $this->translate('The syslog facility to utilize.'), 'value' => 'user', 'multiOptions' => array( - 'auth' => 'LOG_AUTH', - 'authpriv' => 'LOG_AUTHPRIV', - 'cron' => 'LOG_CRON', - 'daemon' => 'LOG_DAEMON', - 'kern' => 'LOG_KERN', + 'user' => 'LOG_USER', 'local0' => 'LOG_LOCAL0', 'local1' => 'LOG_LOCAL1', 'local2' => 'LOG_LOCAL2', @@ -124,13 +120,7 @@ class LoggingConfigForm extends Form 'local4' => 'LOG_LOCAL4', 'local5' => 'LOG_LOCAL5', 'local6' => 'LOG_LOCAL6', - 'local7' => 'LOG_LOCAL7', - 'lpr' => 'LOG_LPR', - 'mail' => 'LOG_MAIL', - 'news' => 'LOG_NEWS', - 'syslog' => 'LOG_SYSLOG', - 'user' => 'LOG_USER', - 'uucp' => 'LOG_UUCP' + 'local7' => 'LOG_LOCAL7' ) ) ); diff --git a/library/Icinga/Application/Logger/Writer/SyslogWriter.php b/library/Icinga/Application/Logger/Writer/SyslogWriter.php index f26a07412..93efc2a19 100644 --- a/library/Icinga/Application/Logger/Writer/SyslogWriter.php +++ b/library/Icinga/Application/Logger/Writer/SyslogWriter.php @@ -33,11 +33,7 @@ class SyslogWriter extends LogWriter * @var array */ public static $facilities = array( - 'auth' => LOG_AUTH, - 'authpriv' => LOG_AUTHPRIV, - 'cron' => LOG_CRON, - 'daemon' => LOG_DAEMON, - 'kern' => LOG_KERN, + 'user' => LOG_USER, 'local0' => LOG_LOCAL0, 'local1' => LOG_LOCAL1, 'local2' => LOG_LOCAL2, @@ -45,13 +41,7 @@ class SyslogWriter extends LogWriter 'local4' => LOG_LOCAL4, 'local5' => LOG_LOCAL5, 'local6' => LOG_LOCAL6, - 'local7' => LOG_LOCAL7, - 'lpr' => LOG_LPR, - 'mail' => LOG_MAIL, - 'news' => LOG_NEWS, - 'syslog' => LOG_SYSLOG, - 'user' => LOG_USER, - 'uucp' => LOG_UUCP + 'local7' => LOG_LOCAL7 ); /** From 893daf3a7bf98203c6b7c3f0a96e10c4d2601a0e Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Mon, 24 Oct 2016 10:55:15 +0200 Subject: [PATCH 3/3] Store available Syslog facilities non-redundandly refs #11214 --- .../forms/Config/General/LoggingConfigForm.php | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/application/forms/Config/General/LoggingConfigForm.php b/application/forms/Config/General/LoggingConfigForm.php index 428e5a75e..5f1b27a4d 100644 --- a/application/forms/Config/General/LoggingConfigForm.php +++ b/application/forms/Config/General/LoggingConfigForm.php @@ -4,6 +4,7 @@ namespace Icinga\Forms\Config\General; use Icinga\Application\Logger; +use Icinga\Application\Logger\Writer\SyslogWriter; use Icinga\Application\Platform; use Icinga\Web\Form; @@ -103,6 +104,7 @@ class LoggingConfigForm extends Form ) ); } else { + $facilities = array_keys(SyslogWriter::$facilities); $this->addElement( 'select', 'logging_facility', @@ -111,17 +113,7 @@ class LoggingConfigForm extends Form 'label' => $this->translate('Facility'), 'description' => $this->translate('The syslog facility to utilize.'), 'value' => 'user', - 'multiOptions' => array( - 'user' => 'LOG_USER', - 'local0' => 'LOG_LOCAL0', - 'local1' => 'LOG_LOCAL1', - 'local2' => 'LOG_LOCAL2', - 'local3' => 'LOG_LOCAL3', - 'local4' => 'LOG_LOCAL4', - 'local5' => 'LOG_LOCAL5', - 'local6' => 'LOG_LOCAL6', - 'local7' => 'LOG_LOCAL7' - ) + 'multiOptions' => array_combine($facilities, $facilities) ) ); }