From 95d7a8a553debd0ada244ae7472040052890d07b Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 3 Sep 2013 11:24:03 +0200 Subject: [PATCH 1/7] Fix triggered_by field in ScheduleDowntimeForm is not a select box refs #4496 --- .../forms/Command/ScheduleDowntimeForm.php | 73 +++- .../Command/ScheduleDowntimeFormTest.php | 409 ++++++++++++++++++ 2 files changed, 465 insertions(+), 17 deletions(-) create mode 100644 modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php diff --git a/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php b/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php index 2944aef21..4848c2ba3 100644 --- a/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php +++ b/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php @@ -35,6 +35,7 @@ use \Icinga\Web\Form\Element\DateTimePicker; use \Icinga\Protocol\Commandpipe\Downtime; use \Icinga\Protocol\Commandpipe\Comment; use \Icinga\Util\DateTimeFactory; +use \Monitoring\Backend; /** * Form for scheduling downtimes @@ -51,6 +52,8 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm */ const TYPE_FLEXIBLE = 'flexible'; + private $downtimes; + /** * Initialize form */ @@ -72,6 +75,55 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm ); } + /** + * Fetch all available downtimes from the database + * + * @return array + */ + private function getCurrentDowntimes() + { + if (isset($this->downtimes)) { + return $this->downtimes; + } + + $cfg = $this->getConfiguration(); + $preferences = $this->getUserPreferences(); + $downtimes = Backend::getInstance($this->getRequest()->getParam('backend'))->select() + ->from('downtime', array( + 'host_name', + 'service_description', + 'downtime_scheduled_start_time', + 'downtime_internal_downtime_id', + ))->fetchAll(); + + $options = array(); + foreach ($downtimes as $downtime) + { + $dt = DateTimeFactory::create($downtime->downtime_scheduled_start_time); + $date_format = $preferences->get('app.dateFormat', $cfg->get('app.dateFormat', 'd/m/Y')); + $time_format = $preferences->get('app.timeFormat', $cfg->get('app.timeFormat', 'g:i A')); + $label = sprintf( + 'ID %s: %s%s starting @ %s', + $downtime->downtime_internal_downtime_id, + $downtime->host_name, + !empty($downtime->service_description) ? ' (' . $downtime->service_description . ')' : '', + $dt->format($date_format . ' ' . $time_format) + ); + $options[$downtime->downtime_internal_downtime_id] = $label; + } + return $options; + } + + /** + * Set the downtimes displayed by this form (used for testing) + * + * @param array $downtimes list of strings + */ + public function setCurrentDowntimes($downtimes) + { + $this->downtimes = $downtimes; + } + /** * Create the form's elements */ @@ -108,25 +160,12 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm * */ $this->addElement( - 'text', + 'select', 'triggered', array( - 'label' => t('Triggered by'), - 'value' => 0, - 'required' => true, - 'validators' => array( - array( - 'Digits', - true - ), - array( - 'GreaterThan', - true, - array( - 'min' => -1 - ) - ) - ) + 'label' => t('Triggered by'), + 'required' => true, + 'multiOptions' => $this->getCurrentDowntimes() ) ); diff --git a/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php b/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php new file mode 100644 index 000000000..bcb8e67fb --- /dev/null +++ b/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php @@ -0,0 +1,409 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Test\Monitoring\Forms\Command; + +// @codingStandardsIgnoreStart +require_once realpath(__DIR__ . '/../../../../../../../library/Icinga/Test/BaseTestCase.php'); +// @codingStandardsIgnoreEnd + +use Icinga\Test\BaseTestCase; +// @codingStandardsIgnoreStart +require_once 'Zend/Validate/Digits.php'; +require_once 'Zend/Validate/GreaterThan.php'; +require_once BaseTestCase::$libDir . '/Web/Form.php'; +require_once BaseTestCase::$libDir . '/Util/DateTimeFactory.php'; +require_once BaseTestCase::$libDir . '/Util/ConfigAwareFactory.php'; +require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/ScheduleDowntimeForm.php'; +// @codingStandardsIgnoreEnd + +use \DateTimeZone; +use \Icinga\Util\DateTimeFactory; +use \Monitoring\Form\Command\ScheduleDowntimeForm; // Used by constant FORM_CLASS + +class ScheduleDowntimeFormTest extends BaseTestCase +{ + const FORM_CLASS = 'Monitoring\Form\Command\ScheduleDowntimeForm'; + + /** + * Set up the default time zone + * + * Utilizes singleton DateTimeFactory + * + * @backupStaticAttributes enabled + */ + public function setUp() + { + date_default_timezone_set('UTC'); + DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC'))); + } + + public function testCorrectFormElementCreation() + { + $formFixed = $this->createForm(self::FORM_CLASS); + $formFixed->setCurrentDowntimes(array('foo')); + $formFixed->buildForm(); + $formFlexible = $this->createForm( + self::FORM_CLASS, + array( + 'type' => 'flexible' + ) + ); + $formFlexible->setCurrentDowntimes(array('foo')); + $formFlexible->buildForm(); + + $form = $this->createForm(self::FORM_CLASS); + $form->setCurrentDowntimes(array('foo')); + $form->setWithChildren(true); + $form->buildForm(); + } + + public function testCorrectValidationWithChildrend() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + 'btn_submit' => 'foo', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertTrue( + $form->isSubmittedAndValid(), + 'Asserting a correct fixed downtime form to be considered valid' + ); + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, + 'hours' => '10', + 'minutes' => '10', + 'btn_submit' => 'foo' + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertTrue( + $form->isSubmittedAndValid(), + 'Asserting a correct flexible downtime form to be considered valid' + ); + } + + public function testMissingFlexibleDurationRecognition() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, + 'hours' => '', + 'minutes' => '', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert missing hours and minutes in downtime form to cause failing validation' + ); + } + + public function testMissingAuthorRecognition() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => '', + 'comment' => 'DING DING', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert missing author to cause validation errors in fixed downtime' + ); + } + + public function testMissingCommentRecognition() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'OK', + 'comment' => '', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert missing comment to cause validation errors in fixed downtime' + ); + } + + public function testInvalidTriggeredFieldValueRecognition() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'OK', + 'comment' => 'OK', + 'triggered' => 'HAHA', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert invalid trigger field to cause validation to fail' + ); + } + + public function testInvalidStartTimeRecognition() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'OK', + 'comment' => 'OK', + 'triggered' => '0', + 'starttime' => '17/07/2013', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert incorrect start time to cause validation errors in fixed downtime' + ); + } + + public function testInvalidEndTimeRecognition() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'OK', + 'comment' => 'OK', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => 'DING', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert invalid endtime to cause validation errors in fixed downtime' + ); + } + + + public function testInvalidHoursValueRecognitionInFlexibleDowntime() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'OK', + 'comment' => 'OK', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, + 'hours' => '-1', + 'minutes' => '12', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert negative hours to cause validation errors in flexible downtime' + ); + } + + public function testInvalidMinutesValueRecognitionInFlexibleDowntime() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'OK', + 'comment' => 'OK', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FLEXIBLE, + 'hours' => '12', + 'minutes' => 'DING', + ) + ); + $form->setWithChildren(true); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert non numeric valud to cause validation errors in flexible downtime ' + ); + + } + + public function testCorrectScheduleDowntimeWithoutChildrenForm() + { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + 'btn_submit' => 'foo', + 'childobjects' => '0', + ) + ); + $form->setWithChildren(false); + $form->setCurrentDowntimes(array('foo')); + + $this->assertTrue( + $form->isSubmittedAndValid(), + 'Assert a correct schedule downtime without children form to be considered valid' + ); + } + + public function testIncorrectChildObjectsRecognition() { + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + 'childobjects' => 'AHA', + ) + ); + $form->setWithChildren(false); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert and incorrect (non-numeric) childobjects value to cause validation errors' + ); + + $form = $this->createForm( + self::FORM_CLASS, + array( + 'author' => 'TEST_AUTHOR', + 'comment' => 'DING DING', + 'triggered' => '0', + 'starttime' => '17/07/2013 10:30 AM', + 'endtime' => '18/07/2013 10:30 AM', + 'type' => ScheduleDowntimeForm::TYPE_FIXED, + 'hours' => '', + 'minutes' => '', + 'childobjects' => '4', + ) + ); + $form->setWithChildren(false); + $form->setCurrentDowntimes(array('foo')); + + $this->assertFalse( + $form->isSubmittedAndValid(), + 'Assert and incorrect (numeric) childobjects value to cause validation errors' + ); + } + + public function testTimeRange() + { + $form = $this->createForm(self::FORM_CLASS); + $form->setCurrentDowntimes(array('foo')); + $form->buildForm(); + + $time1 = $form->getElement('starttime')->getValue(); + $time2 = $form->getElement('endtime')->getValue(); + + $this->assertEquals(3600, ($time2 - $time1)); + } +} From 79eb6588c582a23732db89097c8ecabf2d4c0455 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 3 Sep 2013 14:49:36 +0200 Subject: [PATCH 2/7] Restructure dispatch process of commands refs #4580 --- .../Icinga/Protocol/Commandpipe/Command.php | 58 +++++++++++++ .../Protocol/Commandpipe/CommandPipe.php | 19 +++- .../Monitoring/Command/MonitoringCommand.php | 86 +++++++++++++++++++ 3 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 library/Icinga/Protocol/Commandpipe/Command.php create mode 100644 modules/monitoring/library/Monitoring/Command/MonitoringCommand.php diff --git a/library/Icinga/Protocol/Commandpipe/Command.php b/library/Icinga/Protocol/Commandpipe/Command.php new file mode 100644 index 000000000..a5651b617 --- /dev/null +++ b/library/Icinga/Protocol/Commandpipe/Command.php @@ -0,0 +1,58 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Icinga\Protocol\Commandpipe; + +/** + * Interface definition for command objects processed by the CommandPipe + */ +interface Command +{ + /** + * Set the hostname for this command + * + * @param string $hostname + * @return self + */ + public function setHost($hostname); + + /** + * Set the service description for this command + * + * @param string $service_description + * @return self + */ + public function setService($service_description); + + /** + * Return a string representation of this command + * + * @return string + */ + public function __toString(); +} \ No newline at end of file diff --git a/library/Icinga/Protocol/Commandpipe/CommandPipe.php b/library/Icinga/Protocol/Commandpipe/CommandPipe.php index c20c18a46..89b06b540 100644 --- a/library/Icinga/Protocol/Commandpipe/CommandPipe.php +++ b/library/Icinga/Protocol/Commandpipe/CommandPipe.php @@ -28,8 +28,6 @@ namespace Icinga\Protocol\Commandpipe; -use Icinga\Application\Logger as IcingaLogger; - use Icinga\Protocol\Commandpipe\Transport\Transport; use Icinga\Protocol\Commandpipe\Transport\LocalPipe; use Icinga\Protocol\Commandpipe\Transport\SecureShell; @@ -138,6 +136,23 @@ class CommandPipe $this->transport->send($command); } + /** + * Send a command to the icinga pipe + * + * @param \Icinga\Protocol\Commandpipe\Command $command + * @param array $objects + */ + public function sendCommand(Command $command, array $objects) + { + foreach ($objects as $object) { + if (isset($object->service_description)) { + $command->setService($object->service_description); + } + $command->setHost($object->host_name); + $this->transport->send((string) $command); + } + } + /** * Acknowledge a set of monitoring objects * diff --git a/modules/monitoring/library/Monitoring/Command/MonitoringCommand.php b/modules/monitoring/library/Monitoring/Command/MonitoringCommand.php new file mode 100644 index 000000000..d2cd90d16 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/MonitoringCommand.php @@ -0,0 +1,86 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Monitoring\Command; + +use \Icinga\Protocol\Commandpipe\Command; +use \Icinga\Protocol\Commandpipe\Comment; + +abstract class MonitoringCommand implements Command +{ + /** + * The hostname for this command + * + * @var string + */ + protected $hostname; + + /** + * The service description for this command + * + * @var string + */ + protected $service_description; + + /** + * The comment associated to this command + * + * @var Comment + */ + protected $comment; + + /** + * @see Command::setHost() + */ + public function setHost($hostname) + { + $this->hostname = $hostname; + return $this; + } + + /** + * @see Command::setService() + */ + public function setService($service_description) + { + $this->service_description = $service_description; + return $this; + } + + /** + * Set the comment for this command + * + * @param Comment $comment + * @return self + */ + public function setComment(Comment $comment) + { + $this->comment = $comment; + return $this; + } +} \ No newline at end of file From 1593406f315bc3551e06381bafc27c545318c2ad Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 3 Sep 2013 14:50:50 +0200 Subject: [PATCH 3/7] Refactor Acknowledgement command handling #refs 4580 --- .../controllers/CommandController.php | 6 +- .../forms/Command/AcknowledgeForm.php | 28 ++-- .../Monitoring/Command/AcknowledgeCommand.php | 122 ++++++++++++++++++ 3 files changed, 140 insertions(+), 16 deletions(-) create mode 100644 modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php diff --git a/modules/monitoring/application/controllers/CommandController.php b/modules/monitoring/application/controllers/CommandController.php index ce1dad859..277e9107e 100644 --- a/modules/monitoring/application/controllers/CommandController.php +++ b/modules/monitoring/application/controllers/CommandController.php @@ -682,11 +682,11 @@ class Monitoring_CommandController extends ActionController $form->setRequest($this->getRequest()); $form->setConfiguration(Config::app()); - $this->setForm($form); - if ($form->IsSubmittedAndValid() === true) { - $this->target->acknowledge($this->view->objects, $form->getAcknowledgement()); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } + + $this->setForm($form); } /** diff --git a/modules/monitoring/application/forms/Command/AcknowledgeForm.php b/modules/monitoring/application/forms/Command/AcknowledgeForm.php index 00b35343b..cd7201ecd 100644 --- a/modules/monitoring/application/forms/Command/AcknowledgeForm.php +++ b/modules/monitoring/application/forms/Command/AcknowledgeForm.php @@ -29,9 +29,9 @@ namespace Icinga\Module\Monitoring\Form\Command; use \Icinga\Web\Form\Element\DateTimePicker; -use \Icinga\Protocol\Commandpipe\Acknowledgement; use \Icinga\Protocol\Commandpipe\Comment; use \Icinga\Util\DateTimeFactory; +use \Monitoring\Command\AcknowledgeCommand; /** * Form for problem acknowledgements @@ -156,25 +156,27 @@ class AcknowledgeForm extends CommandForm } /** - * Create acknowledgement from request data + * Create the acknowledgement command object * - * @return \Icinga\Protocol\Commandpipe\Acknowledgement + * @return AcknowledgeCommand */ - public function getAcknowledgement() + public function createCommand() { - $expireTime = -1; - if ($this->getValue('expire')) { - $expireTime = $this->getValue('expiretime'); - } - return new Acknowledgement( + $command = new AcknowledgeCommand(); + $command->setComment( new Comment( $this->getAuthorName(), $this->getValue('comment'), $this->getValue('persistent') - ), - $this->getValue('notify'), - $expireTime, - $this->getValue('sticky') + ) ); + $command->setNotify($this->getValue('notify')); + $command->setSticky($this->getValue('sticky')); + + if ($this->getValue('expire')) { + $command->setExpireTime($expireTime); + } + + return $command; } } diff --git a/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php b/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php new file mode 100644 index 000000000..f1af9c6a3 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php @@ -0,0 +1,122 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Monitoring\Command; + +use \Icinga\Protocol\Commandpipe\Command; +use \Icinga\Protocol\Commandpipe\CommandPipe; +use \Icinga\Protocol\Commandpipe\Acknowledgement; + +class AcknowledgeCommand extends MonitoringCommand implements Command +{ + /** + * When this acknowledgement should expire + * + * @var int + */ + private $expireTime = -1; + + /** + * Whether notifications are going to be sent out + * + * @var bool + */ + private $notify; + + /** + * Whether this acknowledgement is going to be stickied + * + * @var bool + */ + private $sticky; + + /** + * Set the time when this acknowledgement should expire + * + * @param int $expireTime + * @return self + */ + public function setExpireTime($expireTime) + { + $this->expireTime = $expireTime; + return $this; + } + + /** + * Set whether notifications should be sent out + * + * @param bool $state + * @return self + */ + public function setNotify($state) + { + $this->notify = $state; + return $this; + } + + /** + * Set whether this acknowledgement should be stickied + * + * @param bool $state + * @return self + */ + public function setSticky($state) + { + $this->sticky = $state; + return $this; + } + + /** + * Create the acknowledgement object + * + * @return \Icinga\Protocol\Commandpipe\Acknowledgement + */ + public function createAcknowledgement() + { + return new Acknowledgement( + $this->comment, + $this->notify, + $this->expireTime, + $this->sticky + ); + } + + /** + * @see Command::__toString() + */ + public function __toString() + { + if (isset($this->service_description)) { + $template = $this->createAcknowledgement()->getFormatString(CommandPipe::TYPE_SERVICE); + return sprintf($template, $this->hostname, $this->service_description); + } else { + $template = $this->createAcknowledgement()->getFormatString(CommandPipe::TYPE_HOST); + return sprintf($template, $this->hostname); + } + } +} \ No newline at end of file From 9a476f16f4b5894b2e6a892a7251eeee5a801a21 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 4 Sep 2013 10:50:00 +0200 Subject: [PATCH 4/7] Re-refactor dispatch process of commands and acknowledgement handling refs #4580 --- .../Protocol/Commandpipe/Acknowledgement.php | 136 ------------------ .../Protocol/Commandpipe/CommandPipe.php | 35 +---- .../{Command.php => CommandType.php} | 24 +--- .../forms/Command/AcknowledgeForm.php | 16 +-- .../Monitoring/Command/AcknowledgeCommand.php | 118 ++++++++++----- .../Monitoring/Command/BaseCommand.php | 96 +++++++++++++ .../Monitoring/Command/MonitoringCommand.php | 86 ----------- .../Commandpipe/AcknowledgementTest.php | 47 ++---- .../Commandpipe/CommandPipeLoader.php | 4 +- .../Protocol/Commandpipe/CommandPipeTest.php | 67 +++++---- 10 files changed, 243 insertions(+), 386 deletions(-) delete mode 100644 library/Icinga/Protocol/Commandpipe/Acknowledgement.php rename library/Icinga/Protocol/Commandpipe/{Command.php => CommandType.php} (71%) create mode 100644 modules/monitoring/library/Monitoring/Command/BaseCommand.php delete mode 100644 modules/monitoring/library/Monitoring/Command/MonitoringCommand.php diff --git a/library/Icinga/Protocol/Commandpipe/Acknowledgement.php b/library/Icinga/Protocol/Commandpipe/Acknowledgement.php deleted file mode 100644 index 9001997fe..000000000 --- a/library/Icinga/Protocol/Commandpipe/Acknowledgement.php +++ /dev/null @@ -1,136 +0,0 @@ - - * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 - * @author Icinga Development Team - */ -// {{{ICINGA_LICENSE_HEADER}}} - -namespace Icinga\Protocol\Commandpipe; - -use \Icinga\Protocol\Commandpipe\Exception\InvalidCommandException; -use \Icinga\Protocol\Commandpipe\Comment; - -/** - * Container for a host/service Acknowledgement - */ -class Acknowledgement implements IComment -{ - /** - * The expire time of this acknowledgement or -1 if no expire time is used - * - * @var int - */ - private $expireTime = -1; - - /** - * Whether to set the notify flag of the acknowledgment - * - * @var bool - */ - private $notify = false; - - /** - * The comment text of this acknowledgment - * - * @var Comment - */ - private $comment; - - /** - * true if this is a sticky acknowledgment - * - * @var bool - */ - public $sticky; - - /** - * Set the expire time of this acknowledgment to $time - * - * @param int $time The new expire time as a UNIX timestamp - */ - public function setExpireTime($time) - { - $this->expireTime = intval($time); - } - - /** - * Set the notify flag of this object - * - * @param boolean $bool True if notify should be set, otherwise false - */ - public function setNotify($bool) - { - $this->notify = (bool)$bool; - } - - /** - * Create a new acknowledgment container - * - * @param Comment $comment The comment to use for the acknowledgement - * @param bool $notify Whether to set the notify flag - * @param int $expire The expire time or -1 of not expiring - * @param bool $sticky Whether to set the sticky flag - */ - public function __construct(Comment $comment, $notify = false, $expire = -1, $sticky = false) - { - $this->comment = $comment; - $this->setNotify($notify); - $this->setExpireTime($expire); - $this->sticky = $sticky; - } - - /** - * Return the ACKNOWLEDGE_?_PROBLEM string to be used for submitting an external icinga command - * - * @param string $type Either CommandPipe::TYPE_HOST or CommandPipe::TYPE_SERVICE - * @return string The command string to be submitted to the command pipe - * @throws InvalidCommandException - */ - public function getFormatString($type) - { - $params = ';' - . ($this->sticky ? '2' : '0') - . ';' . ($this->notify ? '1 ' : '0') - . ';' . ($this->comment->persistent ? '1' : '0'); - - $params .= ($this->expireTime > -1 ? ';'. $this->expireTime . ';' : ';') - . $this->comment->author . ';' . $this->comment->comment; - - switch ($type) { - case CommandPipe::TYPE_HOST: - $typeVar = "HOST"; - $params = ";%s" . $params; - break; - case CommandPipe::TYPE_SERVICE: - $typeVar = "SVC"; - $params = ";%s;%s" . $params; - break; - default: - throw new InvalidCommandException("Acknowledgements can only apply on hosts and services "); - } - - $base = "ACKNOWLEDGE_{$typeVar}_PROBLEM" . ($this->expireTime > -1 ? '_EXPIRE' : ''); - return $base . $params; - } -} diff --git a/library/Icinga/Protocol/Commandpipe/CommandPipe.php b/library/Icinga/Protocol/Commandpipe/CommandPipe.php index 89b06b540..bdd73ad8c 100644 --- a/library/Icinga/Protocol/Commandpipe/CommandPipe.php +++ b/library/Icinga/Protocol/Commandpipe/CommandPipe.php @@ -139,41 +139,16 @@ class CommandPipe /** * Send a command to the icinga pipe * - * @param \Icinga\Protocol\Commandpipe\Command $command - * @param array $objects + * @param \Icinga\Protocol\Commandpipe\CommandType $command + * @param array $objects */ - public function sendCommand(Command $command, array $objects) + public function sendCommand(CommandType $command, array $objects) { foreach ($objects as $object) { if (isset($object->service_description)) { - $command->setService($object->service_description); - } - $command->setHost($object->host_name); - $this->transport->send((string) $command); - } - } - - /** - * Acknowledge a set of monitoring objects - * - * $objects can be a mixed array of host and service objects - * - * @param array $objects An array of host and service objects - * @param IComment $acknowledgementOrComment An acknowledgement or comment object to use as the comment - */ - public function acknowledge($objects, IComment $acknowledgementOrComment) - { - if (is_a($acknowledgementOrComment, 'Icinga\Protocol\Commandpipe\Comment')) { - $acknowledgementOrComment = new Acknowledgement($acknowledgementOrComment); - } - - foreach ($objects as $object) { - if (isset($object->service_description)) { - $format = $acknowledgementOrComment->getFormatString(self::TYPE_SERVICE); - $this->send(sprintf($format, $object->host_name, $object->service_description)); + $this->transport->send($command->getServiceCommand($object->host_name, $object->service_description)); } else { - $format = $acknowledgementOrComment->getFormatString(self::TYPE_HOST); - $this->send(sprintf($format, $object->host_name)); + $this->transport->send($command->getHostCommand($object->host_name)); } } } diff --git a/library/Icinga/Protocol/Commandpipe/Command.php b/library/Icinga/Protocol/Commandpipe/CommandType.php similarity index 71% rename from library/Icinga/Protocol/Commandpipe/Command.php rename to library/Icinga/Protocol/Commandpipe/CommandType.php index a5651b617..5608b3a66 100644 --- a/library/Icinga/Protocol/Commandpipe/Command.php +++ b/library/Icinga/Protocol/Commandpipe/CommandType.php @@ -31,28 +31,6 @@ namespace Icinga\Protocol\Commandpipe; /** * Interface definition for command objects processed by the CommandPipe */ -interface Command +interface CommandType { - /** - * Set the hostname for this command - * - * @param string $hostname - * @return self - */ - public function setHost($hostname); - - /** - * Set the service description for this command - * - * @param string $service_description - * @return self - */ - public function setService($service_description); - - /** - * Return a string representation of this command - * - * @return string - */ - public function __toString(); } \ No newline at end of file diff --git a/modules/monitoring/application/forms/Command/AcknowledgeForm.php b/modules/monitoring/application/forms/Command/AcknowledgeForm.php index cd7201ecd..d4b8b372d 100644 --- a/modules/monitoring/application/forms/Command/AcknowledgeForm.php +++ b/modules/monitoring/application/forms/Command/AcknowledgeForm.php @@ -162,21 +162,15 @@ class AcknowledgeForm extends CommandForm */ public function createCommand() { - $command = new AcknowledgeCommand(); - $command->setComment( + return new AcknowledgeCommand( new Comment( $this->getAuthorName(), $this->getValue('comment'), $this->getValue('persistent') - ) + ), + $this->getValue('expire') ? $this->getValue('expire') : -1, + $this->getValue('notify'), + $this->getValue('sticky') ); - $command->setNotify($this->getValue('notify')); - $command->setSticky($this->getValue('sticky')); - - if ($this->getValue('expire')) { - $command->setExpireTime($expireTime); - } - - return $command; } } diff --git a/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php b/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php index f1af9c6a3..8c3bc089a 100644 --- a/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php +++ b/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php @@ -28,28 +28,33 @@ namespace Monitoring\Command; -use \Icinga\Protocol\Commandpipe\Command; -use \Icinga\Protocol\Commandpipe\CommandPipe; -use \Icinga\Protocol\Commandpipe\Acknowledgement; +use \Icinga\Protocol\Commandpipe\Comment; -class AcknowledgeCommand extends MonitoringCommand implements Command +class AcknowledgeCommand extends BaseCommand { /** * When this acknowledgement should expire * - * @var int + * @var int The time as UNIX timestamp or -1 if it shouldn't expire */ private $expireTime = -1; /** - * Whether notifications are going to be sent out + * The comment associated to this acknowledgment + * + * @var Comment + */ + protected $comment; + + /** + * Whether to set the notify flag of this acknowledgment * * @var bool */ private $notify; /** - * Whether this acknowledgement is going to be stickied + * Whether this acknowledgement is of type sticky * * @var bool */ @@ -58,65 +63,106 @@ class AcknowledgeCommand extends MonitoringCommand implements Command /** * Set the time when this acknowledgement should expire * - * @param int $expireTime - * @return self + * @param int $expireTime The time as UNIX timestamp or -1 if it shouldn't expire + * @return self */ - public function setExpireTime($expireTime) + public function setExpire($expireTime) { - $this->expireTime = $expireTime; + $this->expireTime = intval($expireTime); return $this; } /** - * Set whether notifications should be sent out + * Set the comment for this acknowledgement * - * @param bool $state - * @return self + * @param Comment $comment + * @return self + */ + public function setComment(Comment $comment) + { + $this->comment = $comment; + return $this; + } + + /** + * Set whether the notify flag of this acknowledgment should be set + * + * @param bool $state + * @return self */ public function setNotify($state) { - $this->notify = $state; + $this->notify = (bool) $state; return $this; } /** - * Set whether this acknowledgement should be stickied + * Set whether this acknowledgement is of type sticky * - * @param bool $state - * @return self + * @param bool $state + * @return self */ public function setSticky($state) { - $this->sticky = $state; + $this->sticky = (bool) $state; return $this; } /** - * Create the acknowledgement object + * Initialise a new acknowledgement command object * - * @return \Icinga\Protocol\Commandpipe\Acknowledgement + * @param Comment $comment The comment to use for this acknowledgement + * @param int $expire The expire time or -1 of not expiring + * @param bool $notify Whether to set the notify flag + * @param bool $sticky Whether to set the sticky flag */ - public function createAcknowledgement() + public function __construct(Comment $comment, $expire = -1, $notify = false, $sticky = false) { - return new Acknowledgement( - $this->comment, - $this->notify, - $this->expireTime, - $this->sticky - ); + $this->expireTime = $expire; + $this->comment = $comment; + $this->notify = $notify; + $this->sticky = $sticky; } /** - * @see Command::__toString() + * Return the parameters in the right order + * + * @return array */ - public function __toString() + public function getParameters() { - if (isset($this->service_description)) { - $template = $this->createAcknowledgement()->getFormatString(CommandPipe::TYPE_SERVICE); - return sprintf($template, $this->hostname, $this->service_description); - } else { - $template = $this->createAcknowledgement()->getFormatString(CommandPipe::TYPE_HOST); - return sprintf($template, $this->hostname); + $parameters = array( + $this->sticky ? '2' : '0', + $this->notify ? '1' : '0', + $this->comment->persistent ? '1' : '0', + $this->comment->author, + $this->comment->comment + ); + + if ($this->expireTime > -1) { + array_splice($parameters, 3, 0, array($this->expireTime)); } + + return $parameters; + } + + /** + * @see BaseCommand::getHostCommand() + */ + public function getHostCommand($hostname) + { + $parameters = $this->getParameters(); + return sprintf('ACKNOWLEDGE_HOST_PROBLEM%s;', $this->expireTime > -1 ? '_EXPIRE' : '') + . implode(';', array_merge(array($hostname), $parameters)); + } + + /** + * @see BaseCommand::getServiceCommand() + */ + public function getServiceCommand($hostname, $servicename) + { + $parameters = $this->getParameters(); + return sprintf('ACKNOWLEDGE_SVC_PROBLEM%s;', $this->expireTime > -1 ? '_EXPIRE' : '') + . implode(';', array_merge(array($hostname, $servicename), $parameters)); } } \ No newline at end of file diff --git a/modules/monitoring/library/Monitoring/Command/BaseCommand.php b/modules/monitoring/library/Monitoring/Command/BaseCommand.php new file mode 100644 index 000000000..9a968d617 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/BaseCommand.php @@ -0,0 +1,96 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Monitoring\Command; + +use Icinga\Exception\NotImplementedError; +use Icinga\Protocol\Commandpipe\CommandType; + +/** + * Base class for any concrete command implementation + * + * Provides some example methods and often used routines. When implementing + * a new command one is encouraged to override one of those examples. + */ +class BaseCommand implements CommandType +{ + /** + * Return the parameters in the right order for this command + * + * @return array + */ + public function getParameters() + { + throw new NotImplementedError(); + } + + /** + * Return the command as a string with the given host being inserted + * + * @param string $hostname The name of the host to insert + * @return string The string representation of the command + */ + public function getHostCommand($hostname) + { + throw new NotImplementedError(); + } + + /** + * Return the command as a string with the given host and service being inserted + * + * @param string $hostname The name of the host to insert + * @param string $servicename The name of the service to insert + * @return string The string representation of the command + */ + public function getServiceCommand($hostname, $servicename) + { + throw new NotImplementedError(); + } + + /** + * Return the command as a string with the given hostgroup being inserted + * + * @param string $hostgroup The name of the hostgroup to insert + * @return string The string representation of the command + */ + public function getHostgroupCommand($hostgroup) + { + throw new NotImplementedError(); + } + + /** + * Return the command as a string with the given servicegroup being inserted + * + * @param string $servicegroup The name of the servicegroup to insert + * @return string The string representation of the command + */ + public function getServicegroupCommand($servicegroup) + { + throw new NotImplementedError(); + } +} \ No newline at end of file diff --git a/modules/monitoring/library/Monitoring/Command/MonitoringCommand.php b/modules/monitoring/library/Monitoring/Command/MonitoringCommand.php deleted file mode 100644 index d2cd90d16..000000000 --- a/modules/monitoring/library/Monitoring/Command/MonitoringCommand.php +++ /dev/null @@ -1,86 +0,0 @@ - - * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 - * @author Icinga Development Team - */ -// {{{ICINGA_LICENSE_HEADER}}} - -namespace Monitoring\Command; - -use \Icinga\Protocol\Commandpipe\Command; -use \Icinga\Protocol\Commandpipe\Comment; - -abstract class MonitoringCommand implements Command -{ - /** - * The hostname for this command - * - * @var string - */ - protected $hostname; - - /** - * The service description for this command - * - * @var string - */ - protected $service_description; - - /** - * The comment associated to this command - * - * @var Comment - */ - protected $comment; - - /** - * @see Command::setHost() - */ - public function setHost($hostname) - { - $this->hostname = $hostname; - return $this; - } - - /** - * @see Command::setService() - */ - public function setService($service_description) - { - $this->service_description = $service_description; - return $this; - } - - /** - * Set the comment for this command - * - * @param Comment $comment - * @return self - */ - public function setComment(Comment $comment) - { - $this->comment = $comment; - return $this; - } -} \ No newline at end of file diff --git a/test/php/library/Icinga/Protocol/Commandpipe/AcknowledgementTest.php b/test/php/library/Icinga/Protocol/Commandpipe/AcknowledgementTest.php index 9575af1a7..a4e9bc190 100644 --- a/test/php/library/Icinga/Protocol/Commandpipe/AcknowledgementTest.php +++ b/test/php/library/Icinga/Protocol/Commandpipe/AcknowledgementTest.php @@ -2,54 +2,33 @@ namespace Tests\Icinga\Protocol\Commandpipe; -use Icinga\Protocol\Commandpipe\Comment as Comment; -use Icinga\Protocol\Commandpipe\Commandpipe as Commandpipe; +use Icinga\Protocol\Commandpipe\Comment; +use Monitoring\Command\AcknowledgeCommand; require_once("../../library/Icinga/Protocol/Commandpipe/IComment.php"); require_once("../../library/Icinga/Protocol/Commandpipe/Comment.php"); +require_once("../../library/Icinga/Protocol/Commandpipe/CommandType.php"); require_once("../../library/Icinga/Protocol/Commandpipe/CommandPipe.php"); -require_once("../../library/Icinga/Protocol/Commandpipe/Acknowledgement.php"); -require_once("../../library/Icinga/Protocol/Commandpipe/Exception/InvalidCommandException.php"); +require_once('../../modules/monitoring/library/Monitoring/Command/BaseCommand.php'); +require_once('../../modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php'); class AcknowledgementTest extends \PHPUnit_Framework_TestCase { - - public function testAcknowledgeHostMessage() { - $ack = new \Icinga\Protocol\Commandpipe\Acknowledgement(new Comment("author","commentdata"),false); - $this->assertEquals("ACKNOWLEDGE_HOST_PROBLEM;%s;0;0;0;author;commentdata",$ack->getFormatString(CommandPipe::TYPE_HOST)); + $ack = new AcknowledgeCommand(new Comment("author", "commentdata")); + $this->assertEquals("ACKNOWLEDGE_HOST_PROBLEM;foo;0;0;0;author;commentdata", $ack->getHostCommand('foo')); - $ack->setExpireTime(1000); - $this->assertEquals("ACKNOWLEDGE_HOST_PROBLEM_EXPIRE;%s;0;0;0;1000;author;commentdata",$ack->getFormatString(CommandPipe::TYPE_HOST)); + $ack->setExpire(1000); + $this->assertEquals("ACKNOWLEDGE_HOST_PROBLEM_EXPIRE;bar;0;0;0;1000;author;commentdata", $ack->getHostCommand('bar')); } public function testAcknowledgeServiceMessage() { - $ack = new \Icinga\Protocol\Commandpipe\Acknowledgement(new Comment("author","commentdata"),false); - $this->assertEquals("ACKNOWLEDGE_SVC_PROBLEM;%s;%s;0;0;0;author;commentdata",$ack->getFormatString(CommandPipe::TYPE_SERVICE)); - - $ack->setExpireTime(1000); - $this->assertEquals("ACKNOWLEDGE_SVC_PROBLEM_EXPIRE;%s;%s;0;0;0;1000;author;commentdata",$ack->getFormatString(CommandPipe::TYPE_SERVICE)); - } - - /** - * @expectedException \Icinga\Protocol\Commandpipe\Exception\InvalidCommandException - */ - public function testInvalidServicegroupAcknowledgement() - { - $ack = new \Icinga\Protocol\Commandpipe\Acknowledgement(new Comment("author","commentdata"),false); - $ack->getFormatString(CommandPipe::TYPE_SERVICEGROUP); - - } - - /** - * @expectedException \Icinga\Protocol\Commandpipe\Exception\InvalidCommandException - */ - public function testInvalidHostgroupAcknowledgement() - { - $ack = new \Icinga\Protocol\Commandpipe\Acknowledgement(new Comment("author","commentdata"),false); - $ack->getFormatString(CommandPipe::TYPE_HOSTGROUP); + $ack = new AcknowledgeCommand(new Comment("author","commentdata")); + $this->assertEquals("ACKNOWLEDGE_SVC_PROBLEM;foo;bar;0;0;0;author;commentdata", $ack->getServiceCommand('foo', 'bar')); + $ack->setExpire(1000); + $this->assertEquals("ACKNOWLEDGE_SVC_PROBLEM_EXPIRE;bar;foo;0;0;0;1000;author;commentdata", $ack->getServiceCommand('bar', 'foo')); } } diff --git a/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php b/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php index 4204a661f..4aa06b745 100644 --- a/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php +++ b/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php @@ -18,8 +18,8 @@ class CommandPipeLoader extends LibraryLoader { require_once("../../library/Icinga/Protocol/Commandpipe/IComment.php"); require_once("../../library/Icinga/Protocol/Commandpipe/Comment.php"); + require_once("../../library/Icinga/Protocol/Commandpipe/CommandType.php"); require_once("../../library/Icinga/Protocol/Commandpipe/CommandPipe.php"); - require_once("../../library/Icinga/Protocol/Commandpipe/Acknowledgement.php"); require_once("../../library/Icinga/Protocol/Commandpipe/Downtime.php"); require_once("../../library/Icinga/Protocol/Commandpipe/PropertyModifier.php"); require_once("../../library/Icinga/Protocol/Commandpipe/Exception/InvalidCommandException.php"); @@ -27,6 +27,8 @@ class CommandPipeLoader extends LibraryLoader { require_once("../../library/Icinga/Protocol/Commandpipe/Transport/SecureShell.php"); require_once("../../library/Icinga/Protocol/Commandpipe/Transport/LocalPipe.php"); require_once('../../library/Icinga/Protocol/Commandpipe/CustomNotification.php'); + require_once('../../modules/monitoring/library/Monitoring/Command/BaseCommand.php'); + require_once('../../modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php'); } } // @codingStandardsIgnoreEnd diff --git a/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeTest.php b/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeTest.php index 675cfb237..81e9809bd 100644 --- a/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeTest.php +++ b/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeTest.php @@ -9,12 +9,12 @@ require_once(__DIR__.'/CommandPipeLoader.php'); CommandPipeLoader::requireLibrary(); use Icinga\Protocol\Commandpipe\Comment as Comment; -use Icinga\Protocol\Commandpipe\Acknowledgement as Acknowledgement; use Icinga\Protocol\Commandpipe\CustomNotification; use Icinga\Protocol\Commandpipe\Downtime as Downtime; use Icinga\Protocol\Commandpipe\Commandpipe as Commandpipe; -use \Icinga\Protocol\Commandpipe\PropertyModifier as MONFLAG; +use Icinga\Protocol\Commandpipe\PropertyModifier as MONFLAG; use Icinga\Protocol\Ldap\Exception; +use Monitoring\Command\AcknowledgeCommand; if(!defined("EXTCMD_TEST_BIN")) define("EXTCMD_TEST_BIN", "./bin/extcmd_test"); @@ -131,12 +131,15 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase { $pipe = $this->getLocalTestPipe(); try { - $ack = new Acknowledgement(new Comment("I can","sends teh ack")); - $pipe->acknowledge(array( - (object) array( - "host_name" => "hostA" + $ack = new AcknowledgeCommand(new Comment("I can","sends teh ack")); + $pipe->sendCommand( + $ack, + array( + (object) array( + "host_name" => "hostA" + ) ) - ),$ack); + ); $this->assertCommandSucceeded("ACKNOWLEDGE_HOST_PROBLEM;hostA;0;0;0;I can;sends teh ack"); } catch(Exception $e) { $this->cleanup(); @@ -154,28 +157,31 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase { $pipe = $this->getLocalTestPipe(); try { - $ack = new Comment("I can","sends teh ack"); + $ack = new AcknowledgeCommand(new Comment("I can","sends teh ack")); $pipe->getTransport()->setOpenMode("a"); - $pipe->acknowledge(array( - (object) array( - "host_name" => "hostA" - ),(object) array( - "host_name" => "hostB" - ),(object) array( - "host_name" => "hostC" - ),(object) array( - "host_name" => "hostC", - "service_description" => "svc" + $pipe->sendCommand( + $ack, + array( + (object) array( + "host_name" => "hostA" + ),(object) array( + "host_name" => "hostB" + ),(object) array( + "host_name" => "hostC" + ),(object) array( + "host_name" => "hostC", + "service_description" => "svc" + ) ) - ),$ack); + ); - $result = explode("\n",file_get_contents($this->getPipeName())); + $result = explode("\n", file_get_contents($this->getPipeName())); $this->assertCount(5, $result, "Asserting the correct number of commands being written to the command pipe"); - $this->assertCommandSucceeded("ACKNOWLEDGE_HOST_PROBLEM;hostA;0;0;0;I can;sends teh ack",$result[0]); - $this->assertCommandSucceeded("ACKNOWLEDGE_HOST_PROBLEM;hostB;0;0;0;I can;sends teh ack",$result[1]); - $this->assertCommandSucceeded("ACKNOWLEDGE_HOST_PROBLEM;hostC;0;0;0;I can;sends teh ack",$result[2]); - $this->assertCommandSucceeded("ACKNOWLEDGE_SVC_PROBLEM;hostC;svc;0;0;0;I can;sends teh ack",$result[3]); + $this->assertCommandSucceeded("ACKNOWLEDGE_HOST_PROBLEM;hostA;0;0;0;I can;sends teh ack", $result[0]); + $this->assertCommandSucceeded("ACKNOWLEDGE_HOST_PROBLEM;hostB;0;0;0;I can;sends teh ack", $result[1]); + $this->assertCommandSucceeded("ACKNOWLEDGE_HOST_PROBLEM;hostC;0;0;0;I can;sends teh ack", $result[2]); + $this->assertCommandSucceeded("ACKNOWLEDGE_SVC_PROBLEM;hostC;svc;0;0;0;I can;sends teh ack", $result[3]); } catch(Exception $e) { $this->cleanup(); @@ -484,12 +490,15 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase } $pipe = $this->getSSHTestPipe(); try { - $ack = new Acknowledgement(new Comment("I can","sends teh ack")); - $pipe->acknowledge(array( - (object) array( - "host_name" => "hostA" + $ack = new AcknowledgeCommand(new Comment("I can","sends teh ack")); + $pipe->sendCommand( + $ack, + array( + (object) array( + "host_name" => "hostA" + ) ) - ),$ack); + ); $this->assertCommandSucceeded("ACKNOWLEDGE_HOST_PROBLEM;hostA;0;0;0;I can;sends teh ack"); } catch(Exception $e) { $this->cleanup(); From e0620aa04ef46070fb6e289e5fbcd6763c0f8377 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 4 Sep 2013 11:24:08 +0200 Subject: [PATCH 5/7] Fix DateTimePicker not respecting user preferences refs #4675 --- application/views/helpers/DateFormat.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/application/views/helpers/DateFormat.php b/application/views/helpers/DateFormat.php index 3816c291a..f59ca1062 100644 --- a/application/views/helpers/DateFormat.php +++ b/application/views/helpers/DateFormat.php @@ -4,17 +4,15 @@ // {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}} -use \DateTime; use \Icinga\Application\Icinga; -use \Icinga\Application\Config as IcingaConfig; +use \Icinga\Application\Config; use \Icinga\Util\DateTimeFactory; -use \Zend_Controller_Request_Http; use \Icinga\Web\Form\Validator\DateTimeValidator; /** * Helper to format date and time. Utilizes DateTimeFactory to ensure time zone awareness * - * @see \Icinga\Util\DateTimeFactory::create() + * @see DateTimeFactory::create() */ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract { @@ -110,7 +108,7 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract public function getDateFormat() { return $this->request->getUser()->getPreferences()->get( - 'dateFormat', IcingaConfig::app()->global->get('dateFormat', 'Y-m-d') + 'app.dateFormat', Config::app()->global->get('dateFormat', 'd/m/Y') ); } @@ -122,7 +120,7 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract public function getTimeFormat() { return $this->request->getUser()->getPreferences()->get( - 'timeFormat', IcingaConfig::app()->global->get('timeFormat', 'H:i:s') + 'app.timeFormat', Config::app()->global->get('timeFormat', 'g:i A') ); } From b74e264f01000f8e018d732a8240d0595755a18b Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 4 Sep 2013 16:21:44 +0200 Subject: [PATCH 6/7] Refactor comment command handling - Refactored Comment class - Dropped IComment interface - Added AddCommentCommand class - Updated CommentForm refs #4580 --- .../Protocol/Commandpipe/CommandPipe.php | 20 ----- .../Icinga/Protocol/Commandpipe/Comment.php | 58 +++++--------- .../Icinga/Protocol/Commandpipe/Downtime.php | 2 +- .../Icinga/Protocol/Commandpipe/IComment.php | 37 --------- .../controllers/CommandController.php | 2 +- .../forms/Command/AcknowledgeForm.php | 2 +- .../application/forms/Command/CommentForm.php | 19 +++-- .../forms/Command/ScheduleDowntimeForm.php | 2 +- .../Monitoring/Command/AcknowledgeCommand.php | 52 ++++++------ .../Monitoring/Command/AddCommentCommand.php | 80 +++++++++++++++++++ .../Monitoring/Command/BaseCommand.php | 2 +- .../Command/ScheduleDowntimeFormTest.php | 8 +- .../Commandpipe/AcknowledgementTest.php | 5 +- .../Commandpipe/CommandPipeLoader.php | 2 +- .../Protocol/Commandpipe/CommandPipeTest.php | 66 ++++++++------- 15 files changed, 193 insertions(+), 164 deletions(-) delete mode 100644 library/Icinga/Protocol/Commandpipe/IComment.php create mode 100644 modules/monitoring/library/Monitoring/Command/AddCommentCommand.php diff --git a/library/Icinga/Protocol/Commandpipe/CommandPipe.php b/library/Icinga/Protocol/Commandpipe/CommandPipe.php index bdd73ad8c..54f59fc10 100644 --- a/library/Icinga/Protocol/Commandpipe/CommandPipe.php +++ b/library/Icinga/Protocol/Commandpipe/CommandPipe.php @@ -237,26 +237,6 @@ class CommandPipe } } - /** - * Add a comment to all submitted objects - * - * @param array $objects An array of hosts and services to add a comment for - * @param Comment $comment The comment object to add - */ - public function addComment(array $objects, Comment $comment) - { - foreach ($objects as $object) { - if (isset($object->service_description)) { - $format = $comment->getFormatString(self::TYPE_SERVICE); - $this->send(sprintf($format, $object->host_name, $object->service_description)); - } else { - $format = $comment->getFormatString(self::TYPE_HOST); - $this->send(sprintf($format, $object->host_name)); - } - } - - } - /** * Removes the submitted comments * diff --git a/library/Icinga/Protocol/Commandpipe/Comment.php b/library/Icinga/Protocol/Commandpipe/Comment.php index 4b4103d74..ac55bfd15 100644 --- a/library/Icinga/Protocol/Commandpipe/Comment.php +++ b/library/Icinga/Protocol/Commandpipe/Comment.php @@ -30,70 +30,56 @@ namespace Icinga\Protocol\Commandpipe; /** * Container for comment information that can be send to icinga's external command pipe - * */ -class Comment implements IComment +class Comment { /** - * Whether the persistent flag should be submitted with this command + * Whether this comment is persistent or not * * @var bool */ - public $persistent = false; + public $persistent; /** - * The author of this comment + * The author of this comment * - * @var string + * @var string */ - public $author = ""; + public $author; /** - * The comment text to use + * The text of this comment * - * @var string + * @var string */ - public $comment = ""; + public $content; /** * Create a new comment object * - * @param string $author The author name to use for this object - * @param string $comment The comment text to use - * @param bool $persistent Whether this comment should persist icinga restarts + * @param string $author The name of the comment's author + * @param string $content The text for this comment + * @param bool $persistent Whether this comment should be persistent or not */ - public function __construct($author, $comment, $persistent = false) + public function __construct($author, $content, $persistent = false) { $this->author = $author; - $this->comment = $comment; + $this->content = $content; $this->persistent = $persistent; } /** - * Return this comment as an ADD_?_COMMENT external command string that can directly be send to the command pipe + * Return this comment's properties as list of command parameters * - * @param string $type either CommandPipe::TYPE_HOST or CommandPipe::TYPE_SERVICE - * - * @return string The ADD_HOST_COMMENT or ADD_SVC_COMMENT command, without the timestamp - * - * @throws InvalidCommandException When $type is unknown + * @param bool $ignorePersistentFlag Whether the persistent flag should be included or not + * @return array */ - public function getFormatString($type) + public function getParameters($ignorePersistentFlag = false) { - $params = ';' . ($this->persistent ? '1' : '0') . ';' . $this->author . ';' . $this->comment; - - switch ($type) { - case CommandPipe::TYPE_HOST: - $typeVar = "HOST"; - $params = ";%s" . $params; - break; - case CommandPipe::TYPE_SERVICE: - $typeVar = "SVC"; - $params = ";%s;%s" . $params; - break; - default: - throw new InvalidCommandException("Acknowledgements can only apply on hosts and services "); + if ($ignorePersistentFlag) { + return array($this->author, $this->content); + } else { + return array($this->persistent ? '1' : '0', $this->author, $this->content); } - return "ADD_{$typeVar}_COMMENT$params"; } } diff --git a/library/Icinga/Protocol/Commandpipe/Downtime.php b/library/Icinga/Protocol/Commandpipe/Downtime.php index 3e5f7a3d7..fe0f246e2 100644 --- a/library/Icinga/Protocol/Commandpipe/Downtime.php +++ b/library/Icinga/Protocol/Commandpipe/Downtime.php @@ -148,7 +148,7 @@ class Downtime . $this->trigger_id . ';' . $this->duration . ';' . $this->comment->author . ';' - . $this->comment->comment; + . $this->comment->content; } /** diff --git a/library/Icinga/Protocol/Commandpipe/IComment.php b/library/Icinga/Protocol/Commandpipe/IComment.php deleted file mode 100644 index a374a4ff2..000000000 --- a/library/Icinga/Protocol/Commandpipe/IComment.php +++ /dev/null @@ -1,37 +0,0 @@ - - * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 - * @author Icinga Development Team - */ -// {{{ICINGA_LICENSE_HEADER}}} - -namespace Icinga\Protocol\Commandpipe; - -/** - * Interface flagging a class as being a comment - * - */ -interface IComment -{ -} diff --git a/modules/monitoring/application/controllers/CommandController.php b/modules/monitoring/application/controllers/CommandController.php index 277e9107e..b43e05ed3 100644 --- a/modules/monitoring/application/controllers/CommandController.php +++ b/modules/monitoring/application/controllers/CommandController.php @@ -651,7 +651,7 @@ class Monitoring_CommandController extends ActionController $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $this->target->addComment($this->view->objects, $form->getComment()); + $this->target->sendCommand($form->createCommand(), $this->view->objects); } } diff --git a/modules/monitoring/application/forms/Command/AcknowledgeForm.php b/modules/monitoring/application/forms/Command/AcknowledgeForm.php index d4b8b372d..9b9dfdbdd 100644 --- a/modules/monitoring/application/forms/Command/AcknowledgeForm.php +++ b/modules/monitoring/application/forms/Command/AcknowledgeForm.php @@ -31,7 +31,7 @@ namespace Icinga\Module\Monitoring\Form\Command; use \Icinga\Web\Form\Element\DateTimePicker; use \Icinga\Protocol\Commandpipe\Comment; use \Icinga\Util\DateTimeFactory; -use \Monitoring\Command\AcknowledgeCommand; +use \Icinga\Module\Monitoring\Command\AcknowledgeCommand; /** * Form for problem acknowledgements diff --git a/modules/monitoring/application/forms/Command/CommentForm.php b/modules/monitoring/application/forms/Command/CommentForm.php index a9175d1cb..e8b60e74a 100644 --- a/modules/monitoring/application/forms/Command/CommentForm.php +++ b/modules/monitoring/application/forms/Command/CommentForm.php @@ -28,7 +28,8 @@ namespace Icinga\Module\Monitoring\Form\Command; -use \Icinga\Protocol\Commandpipe\Comment; +use Icinga\Protocol\Commandpipe\Comment; +use Icinga\Module\Monitoring\Command\AddCommentCommand; /** * Form for adding comment commands @@ -40,6 +41,8 @@ class CommentForm extends CommandForm */ protected function create() { + $this->setName('form_CommentForm'); + $this->addNote(t('This command is used to add a comment to hosts or services.')); $this->addElement($this->createAuthorField()); @@ -78,12 +81,18 @@ class CommentForm extends CommandForm } /** - * Create comment from request data + * Create the command object to add comments * - * @return \Icinga\Protocol\Commandpipe\Comment + * @return AddCommentCommand */ - public function getComment() + public function createCommand() { - return new Comment($this->getAuthorName(), $this->getValue('comment'), $this->getValue('persistent')); + return new AddCommentCommand( + new Comment( + $this->getAuthorName(), + $this->getValue('comment'), + $this->getValue('persistent') + ) + ); } } diff --git a/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php b/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php index 4848c2ba3..55075b4ed 100644 --- a/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php +++ b/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php @@ -35,7 +35,7 @@ use \Icinga\Web\Form\Element\DateTimePicker; use \Icinga\Protocol\Commandpipe\Downtime; use \Icinga\Protocol\Commandpipe\Comment; use \Icinga\Util\DateTimeFactory; -use \Monitoring\Backend; +use \Icinga\Module\Monitoring\Backend; /** * Form for scheduling downtimes diff --git a/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php b/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php index 8c3bc089a..2bff674d1 100644 --- a/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php +++ b/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php @@ -26,9 +26,9 @@ */ // {{{ICINGA_LICENSE_HEADER}}} -namespace Monitoring\Command; +namespace Icinga\Module\Monitoring\Command; -use \Icinga\Protocol\Commandpipe\Comment; +use Icinga\Protocol\Commandpipe\Comment; class AcknowledgeCommand extends BaseCommand { @@ -44,7 +44,7 @@ class AcknowledgeCommand extends BaseCommand * * @var Comment */ - protected $comment; + private $comment; /** * Whether to set the notify flag of this acknowledgment @@ -60,6 +60,22 @@ class AcknowledgeCommand extends BaseCommand */ private $sticky; + /** + * Initialise a new acknowledgement command object + * + * @param Comment $comment The comment to use for this acknowledgement + * @param int $expire The expire time or -1 of not expiring + * @param bool $notify Whether to set the notify flag + * @param bool $sticky Whether to set the sticky flag + */ + public function __construct(Comment $comment, $expire = -1, $notify = false, $sticky = false) + { + $this->expireTime = $expire; + $this->comment = $comment; + $this->notify = $notify; + $this->sticky = $sticky; + } + /** * Set the time when this acknowledgement should expire * @@ -109,34 +125,18 @@ class AcknowledgeCommand extends BaseCommand } /** - * Initialise a new acknowledgement command object - * - * @param Comment $comment The comment to use for this acknowledgement - * @param int $expire The expire time or -1 of not expiring - * @param bool $notify Whether to set the notify flag - * @param bool $sticky Whether to set the sticky flag - */ - public function __construct(Comment $comment, $expire = -1, $notify = false, $sticky = false) - { - $this->expireTime = $expire; - $this->comment = $comment; - $this->notify = $notify; - $this->sticky = $sticky; - } - - /** - * Return the parameters in the right order + * Return this command's parameters properly arranged in an array * * @return array */ public function getParameters() { - $parameters = array( - $this->sticky ? '2' : '0', - $this->notify ? '1' : '0', - $this->comment->persistent ? '1' : '0', - $this->comment->author, - $this->comment->comment + $parameters = array_merge( + array( + $this->sticky ? '2' : '0', + $this->notify ? '1' : '0' + ), + $this->comment->getParameters() ); if ($this->expireTime > -1) { diff --git a/modules/monitoring/library/Monitoring/Command/AddCommentCommand.php b/modules/monitoring/library/Monitoring/Command/AddCommentCommand.php new file mode 100644 index 000000000..2c546e85e --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/AddCommentCommand.php @@ -0,0 +1,80 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Icinga\Module\Monitoring\Command; + +use Icinga\Protocol\Commandpipe\Comment; + +class AddCommentCommand extends BaseCommand +{ + /** + * The comment associated to this command + * + * @var Comment + */ + private $comment; + + /** + * Initialise a new command object to add comments + * + * @param Comment $comment The comment to use for this acknowledgement + */ + public function __construct(Comment $comment) + { + $this->comment = $comment; + } + + /** + * Set the comment for this command + * + * @param Comment $comment + * @return self + */ + public function setComment(Comment $comment) + { + $this->comment = $comment; + return $this; + } + + /** + * @see BaseCommand::getHostCommand() + */ + public function getHostCommand($hostname) + { + return sprintf('ADD_HOST_COMMENT;%s;', $hostname) . implode(';', $this->comment->getParameters()); + } + + /** + * @see BaseCommand::getServiceCommand() + */ + public function getServiceCommand($hostname, $servicename) + { + return sprintf('ADD_SVC_COMMENT;%s;%s;', $hostname, $servicename) + . implode(';', $this->comment->getParameters()); + } +} diff --git a/modules/monitoring/library/Monitoring/Command/BaseCommand.php b/modules/monitoring/library/Monitoring/Command/BaseCommand.php index 9a968d617..770c9878a 100644 --- a/modules/monitoring/library/Monitoring/Command/BaseCommand.php +++ b/modules/monitoring/library/Monitoring/Command/BaseCommand.php @@ -26,7 +26,7 @@ */ // {{{ICINGA_LICENSE_HEADER}}} -namespace Monitoring\Command; +namespace Icinga\Module\Monitoring\Command; use Icinga\Exception\NotImplementedError; use Icinga\Protocol\Commandpipe\CommandType; diff --git a/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php b/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php index bcb8e67fb..125f01ee0 100644 --- a/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php @@ -42,13 +42,13 @@ require_once BaseTestCase::$libDir . '/Util/ConfigAwareFactory.php'; require_once BaseTestCase::$moduleDir . '/monitoring/application/forms/Command/ScheduleDowntimeForm.php'; // @codingStandardsIgnoreEnd -use \DateTimeZone; -use \Icinga\Util\DateTimeFactory; -use \Monitoring\Form\Command\ScheduleDowntimeForm; // Used by constant FORM_CLASS +use DateTimeZone; +use Icinga\Util\DateTimeFactory; +use Icinga\Module\Monitoring\Form\Command\ScheduleDowntimeForm; // Used by constant FORM_CLASS class ScheduleDowntimeFormTest extends BaseTestCase { - const FORM_CLASS = 'Monitoring\Form\Command\ScheduleDowntimeForm'; + const FORM_CLASS = 'Icinga\Module\Monitoring\Form\Command\ScheduleDowntimeForm'; /** * Set up the default time zone diff --git a/test/php/library/Icinga/Protocol/Commandpipe/AcknowledgementTest.php b/test/php/library/Icinga/Protocol/Commandpipe/AcknowledgementTest.php index a4e9bc190..3c676fd54 100644 --- a/test/php/library/Icinga/Protocol/Commandpipe/AcknowledgementTest.php +++ b/test/php/library/Icinga/Protocol/Commandpipe/AcknowledgementTest.php @@ -3,9 +3,8 @@ namespace Tests\Icinga\Protocol\Commandpipe; use Icinga\Protocol\Commandpipe\Comment; -use Monitoring\Command\AcknowledgeCommand; +use Icinga\Module\Monitoring\Command\AcknowledgeCommand; -require_once("../../library/Icinga/Protocol/Commandpipe/IComment.php"); require_once("../../library/Icinga/Protocol/Commandpipe/Comment.php"); require_once("../../library/Icinga/Protocol/Commandpipe/CommandType.php"); require_once("../../library/Icinga/Protocol/Commandpipe/CommandPipe.php"); @@ -25,7 +24,7 @@ class AcknowledgementTest extends \PHPUnit_Framework_TestCase public function testAcknowledgeServiceMessage() { - $ack = new AcknowledgeCommand(new Comment("author","commentdata")); + $ack = new AcknowledgeCommand(new Comment("author", "commentdata")); $this->assertEquals("ACKNOWLEDGE_SVC_PROBLEM;foo;bar;0;0;0;author;commentdata", $ack->getServiceCommand('foo', 'bar')); $ack->setExpire(1000); diff --git a/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php b/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php index 4aa06b745..0f446788b 100644 --- a/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php +++ b/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeLoader.php @@ -16,7 +16,6 @@ class CommandPipeLoader extends LibraryLoader { require_once("Zend/Log.php"); require_once("../../library/Icinga/Application/Logger.php"); - require_once("../../library/Icinga/Protocol/Commandpipe/IComment.php"); require_once("../../library/Icinga/Protocol/Commandpipe/Comment.php"); require_once("../../library/Icinga/Protocol/Commandpipe/CommandType.php"); require_once("../../library/Icinga/Protocol/Commandpipe/CommandPipe.php"); @@ -29,6 +28,7 @@ class CommandPipeLoader extends LibraryLoader { require_once('../../library/Icinga/Protocol/Commandpipe/CustomNotification.php'); require_once('../../modules/monitoring/library/Monitoring/Command/BaseCommand.php'); require_once('../../modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php'); + require_once('../../modules/monitoring/library/Monitoring/Command/AddCommentCommand.php'); } } // @codingStandardsIgnoreEnd diff --git a/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeTest.php b/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeTest.php index 81e9809bd..c35c848f2 100644 --- a/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeTest.php +++ b/test/php/library/Icinga/Protocol/Commandpipe/CommandPipeTest.php @@ -8,13 +8,15 @@ namespace Tests\Icinga\Protocol\Commandpipe; require_once(__DIR__.'/CommandPipeLoader.php'); CommandPipeLoader::requireLibrary(); -use Icinga\Protocol\Commandpipe\Comment as Comment; +use Zend_Config; +use Icinga\Protocol\Commandpipe\Comment; use Icinga\Protocol\Commandpipe\CustomNotification; use Icinga\Protocol\Commandpipe\Downtime as Downtime; use Icinga\Protocol\Commandpipe\Commandpipe as Commandpipe; use Icinga\Protocol\Commandpipe\PropertyModifier as MONFLAG; use Icinga\Protocol\Ldap\Exception; -use Monitoring\Command\AcknowledgeCommand; +use Icinga\Module\Monitoring\Command\AcknowledgeCommand; +use Icinga\Module\Monitoring\Command\AddCommentCommand; if(!defined("EXTCMD_TEST_BIN")) define("EXTCMD_TEST_BIN", "./bin/extcmd_test"); @@ -50,14 +52,14 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase $this->cleanup(); touch($tmpPipe); - $cfg = new \Zend_Config(array( - "path" => $tmpPipe, - "name" => "test" - )); - $comment = new Comment("Autor","Comment"); - $pipe = new Commandpipe($cfg); + $cfg = new Zend_Config( + array( + "path" => $tmpPipe, + "name" => "test" + ) + ); - return $pipe; + return new Commandpipe($cfg); } /** @@ -71,18 +73,18 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase $this->cleanup(); touch($tmpPipe); - $cfg = new \Zend_Config(array( - "path" => $tmpPipe, - "user" => "vagrant", - "password" => "vagrant", - "host" => 'localhost', - "port" => 22, - "name" => "test" - )); - $comment = new Comment("Autor","Comment"); - $pipe = new Commandpipe($cfg); + $cfg = new Zend_Config( + array( + "path" => $tmpPipe, + "user" => "vagrant", + "password" => "vagrant", + "host" => 'localhost', + "port" => 22, + "name" => "test" + ) + ); - return $pipe; + return new Commandpipe($cfg); } /** @@ -131,7 +133,7 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase { $pipe = $this->getLocalTestPipe(); try { - $ack = new AcknowledgeCommand(new Comment("I can","sends teh ack")); + $ack = new AcknowledgeCommand(new Comment("I can", "sends teh ack")); $pipe->sendCommand( $ack, array( @@ -157,7 +159,7 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase { $pipe = $this->getLocalTestPipe(); try { - $ack = new AcknowledgeCommand(new Comment("I can","sends teh ack")); + $ack = new AcknowledgeCommand(new Comment("I can", "sends teh ack")); $pipe->getTransport()->setOpenMode("a"); $pipe->sendCommand( $ack, @@ -193,14 +195,24 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase /** * Test whether a single host comment is correctly serialized and send to the command pipe * - * @throws \Exception|Exception + * @throws Exception */ public function testAddHostComment() { $pipe = $this->getLocalTestPipe(); try { - $pipe->addComment(array((object) array("host_name" => "hostA")), - new Comment("Autor","Comment") + $pipe->sendCommand( + new AddCommentCommand( + new Comment( + "Autor", + "Comment" + ) + ), + array( + (object) array( + "host_name" => "hostA" + ) + ) ); $this->assertCommandSucceeded("ADD_HOST_COMMENT;hostA;0;Autor;Comment"); } catch(Exception $e) { @@ -364,7 +376,7 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase { $pipe = $this->getLocalTestPipe(); try { - $downtime = new Downtime(25,26,new Comment("me","test")); + $downtime = new Downtime(25, 26, new Comment("me", "test")); $pipe->scheduleDowntime(array( (object) array( "host_name" => "Testhost" @@ -490,7 +502,7 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase } $pipe = $this->getSSHTestPipe(); try { - $ack = new AcknowledgeCommand(new Comment("I can","sends teh ack")); + $ack = new AcknowledgeCommand(new Comment("I can", "sends teh ack")); $pipe->sendCommand( $ack, array( From 7f4e1936f71e20ac698fec07cf0b72b695dd65f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20Mo=C3=9Fhammer?= Date: Wed, 4 Sep 2013 18:07:24 +0200 Subject: [PATCH 7/7] Fix trigger downtime default value issing, docstring fixes refs #4580 --- .../Protocol/Commandpipe/CommandType.php | 2 +- .../forms/Command/ScheduleDowntimeForm.php | 26 +++++++++++-------- .../Monitoring/Command/AcknowledgeCommand.php | 12 ++++++++- .../Monitoring/Command/AddCommentCommand.php | 12 +++++++++ .../Monitoring/Command/BaseCommand.php | 2 +- 5 files changed, 40 insertions(+), 14 deletions(-) diff --git a/library/Icinga/Protocol/Commandpipe/CommandType.php b/library/Icinga/Protocol/Commandpipe/CommandType.php index 5608b3a66..e10a54d64 100644 --- a/library/Icinga/Protocol/Commandpipe/CommandType.php +++ b/library/Icinga/Protocol/Commandpipe/CommandType.php @@ -33,4 +33,4 @@ namespace Icinga\Protocol\Commandpipe; */ interface CommandType { -} \ No newline at end of file +} diff --git a/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php b/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php index 55075b4ed..3c3a94c67 100644 --- a/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php +++ b/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php @@ -89,21 +89,25 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm $cfg = $this->getConfiguration(); $preferences = $this->getUserPreferences(); $downtimes = Backend::getInstance($this->getRequest()->getParam('backend'))->select() - ->from('downtime', array( - 'host_name', - 'service_description', - 'downtime_scheduled_start_time', - 'downtime_internal_downtime_id', - ))->fetchAll(); + ->from( + 'downtime', + array( + 'host_name', + 'service_description', + 'downtime_scheduled_start_time', + 'downtime_internal_downtime_id' + ) + )->fetchAll(); - $options = array(); - foreach ($downtimes as $downtime) - { + $options = array( + '0' => 'No Triggered Downtime ' + ); + foreach ($downtimes as $downtime) { $dt = DateTimeFactory::create($downtime->downtime_scheduled_start_time); $date_format = $preferences->get('app.dateFormat', $cfg->get('app.dateFormat', 'd/m/Y')); $time_format = $preferences->get('app.timeFormat', $cfg->get('app.timeFormat', 'g:i A')); $label = sprintf( - 'ID %s: %s%s starting @ %s', + 'ID %s: %s%s Starting @ %s', $downtime->downtime_internal_downtime_id, $downtime->host_name, !empty($downtime->service_description) ? ' (' . $downtime->service_description . ')' : '', @@ -327,7 +331,7 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm /** * Create Downtime from request Data * - * @return \Icinga\Protocol\Commandpipe\Downtime + * @return Downtime */ public function getDowntime() { diff --git a/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php b/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php index 2bff674d1..7003e6ec6 100644 --- a/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php +++ b/modules/monitoring/library/Monitoring/Command/AcknowledgeCommand.php @@ -30,6 +30,9 @@ namespace Icinga\Module\Monitoring\Command; use Icinga\Protocol\Commandpipe\Comment; +/** + * Command for acknowledging an object + */ class AcknowledgeCommand extends BaseCommand { /** @@ -147,6 +150,9 @@ class AcknowledgeCommand extends BaseCommand } /** + * @param String $hostname The name of the host to create the command for for + * + * @return String The command string to return for the host * @see BaseCommand::getHostCommand() */ public function getHostCommand($hostname) @@ -157,6 +163,10 @@ class AcknowledgeCommand extends BaseCommand } /** + * @param String $hostname The name of the host to create the command for + * @param String $servicename The name of the service to create the command for + * + * @return String The command string to return for the service * @see BaseCommand::getServiceCommand() */ public function getServiceCommand($hostname, $servicename) @@ -165,4 +175,4 @@ class AcknowledgeCommand extends BaseCommand return sprintf('ACKNOWLEDGE_SVC_PROBLEM%s;', $this->expireTime > -1 ? '_EXPIRE' : '') . implode(';', array_merge(array($hostname, $servicename), $parameters)); } -} \ No newline at end of file +} diff --git a/modules/monitoring/library/Monitoring/Command/AddCommentCommand.php b/modules/monitoring/library/Monitoring/Command/AddCommentCommand.php index 2c546e85e..f9194a234 100644 --- a/modules/monitoring/library/Monitoring/Command/AddCommentCommand.php +++ b/modules/monitoring/library/Monitoring/Command/AddCommentCommand.php @@ -30,6 +30,11 @@ namespace Icinga\Module\Monitoring\Command; use Icinga\Protocol\Commandpipe\Comment; +/** + * Icinga Command for adding comments + * + * @see BaseCommand + */ class AddCommentCommand extends BaseCommand { /** @@ -62,6 +67,9 @@ class AddCommentCommand extends BaseCommand } /** + * @param String $hostname The name of the host to create the command for for + * + * @return String The command string to return for the host * @see BaseCommand::getHostCommand() */ public function getHostCommand($hostname) @@ -70,6 +78,10 @@ class AddCommentCommand extends BaseCommand } /** + * @param String $hostname The name of the host to create the command for + * @param String $servicename The name of the service to create the command for + * + * @return String The command string to return for the service * @see BaseCommand::getServiceCommand() */ public function getServiceCommand($hostname, $servicename) diff --git a/modules/monitoring/library/Monitoring/Command/BaseCommand.php b/modules/monitoring/library/Monitoring/Command/BaseCommand.php index 770c9878a..6682fc66d 100644 --- a/modules/monitoring/library/Monitoring/Command/BaseCommand.php +++ b/modules/monitoring/library/Monitoring/Command/BaseCommand.php @@ -93,4 +93,4 @@ class BaseCommand implements CommandType { throw new NotImplementedError(); } -} \ No newline at end of file +}