From 0e307c6482f85f0fdbd277579620d114815959a4 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 31 Aug 2016 12:57:50 +0200 Subject: [PATCH 01/22] IDO: Provide comment name column The Icinga 2 API requires the comment's name when removing the comment. refs #11398 --- .../library/Monitoring/Backend/Ido/Query/CommentQuery.php | 4 ++++ .../library/Monitoring/Backend/Ido/Query/HostcommentQuery.php | 4 ++++ .../Monitoring/Backend/Ido/Query/ServicecommentQuery.php | 4 ++++ modules/monitoring/library/Monitoring/DataView/Comment.php | 1 + .../monitoring/library/Monitoring/DataView/Hostcomment.php | 1 + .../monitoring/library/Monitoring/DataView/Servicecomment.php | 1 + 6 files changed, 15 insertions(+) diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommentQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommentQuery.php index 2fd3fe15f..1d723a0c1 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommentQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/CommentQuery.php @@ -23,6 +23,7 @@ class CommentQuery extends IdoQuery 'comment_expiration' => 'c.comment_expiration', 'comment_internal_id' => 'c.comment_internal_id', 'comment_is_persistent' => 'c.comment_is_persistent', + 'comment_name' => 'c.comment_name', 'comment_timestamp' => 'c.comment_timestamp', 'comment_type' => 'c.comment_type', 'instance_name' => 'c.instance_name', @@ -85,6 +86,9 @@ class CommentQuery extends IdoQuery */ protected function joinBaseTables() { + if (version_compare($this->getIdoVersion(), '1.14.0', '<')) { + $this->columnMap['comments']['comment_name'] = '(NULL)'; + } $this->commentQuery = $this->db->select(); $this->select->from( array('c' => $this->commentQuery), diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostcommentQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostcommentQuery.php index 056eda400..30cd414ed 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostcommentQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostcommentQuery.php @@ -34,6 +34,7 @@ class HostcommentQuery extends IdoQuery 'comment_expiration' => 'CASE c.expires WHEN 1 THEN UNIX_TIMESTAMP(c.expiration_time) ELSE NULL END', 'comment_internal_id' => 'c.internal_comment_id', 'comment_is_persistent' => 'c.is_persistent', + 'comment_name' => 'c.name', 'comment_timestamp' => 'UNIX_TIMESTAMP(c.comment_time)', 'comment_type' => "CASE c.entry_type WHEN 1 THEN 'comment' WHEN 2 THEN 'downtime' WHEN 3 THEN 'flapping' WHEN 4 THEN 'ack' END", 'host' => 'ho.name1 COLLATE latin1_general_ci', @@ -72,6 +73,9 @@ class HostcommentQuery extends IdoQuery */ protected function joinBaseTables() { + if (version_compare($this->getIdoVersion(), '1.14.0', '<')) { + $this->columnMap['comments']['comment_name'] = '(NULL)'; + } $this->select->from( array('c' => $this->prefix . 'comments'), array() diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicecommentQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicecommentQuery.php index bebc6b7fd..2225058fd 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicecommentQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicecommentQuery.php @@ -34,6 +34,7 @@ class ServicecommentQuery extends IdoQuery 'comment_expiration' => 'CASE c.expires WHEN 1 THEN UNIX_TIMESTAMP(c.expiration_time) ELSE NULL END', 'comment_internal_id' => 'c.internal_comment_id', 'comment_is_persistent' => 'c.is_persistent', + 'comment_name' => 'c.name', 'comment_timestamp' => 'UNIX_TIMESTAMP(c.comment_time)', 'comment_type' => "CASE c.entry_type WHEN 1 THEN 'comment' WHEN 2 THEN 'downtime' WHEN 3 THEN 'flapping' WHEN 4 THEN 'ack' END", 'host' => 'so.name1 COLLATE latin1_general_ci', @@ -77,6 +78,9 @@ class ServicecommentQuery extends IdoQuery */ protected function joinBaseTables() { + if (version_compare($this->getIdoVersion(), '1.14.0', '<')) { + $this->columnMap['comments']['comment_name'] = '(NULL)'; + } $this->select->from( array('c' => $this->prefix . 'comments'), array() diff --git a/modules/monitoring/library/Monitoring/DataView/Comment.php b/modules/monitoring/library/Monitoring/DataView/Comment.php index 56facbec6..3a035bc79 100644 --- a/modules/monitoring/library/Monitoring/DataView/Comment.php +++ b/modules/monitoring/library/Monitoring/DataView/Comment.php @@ -19,6 +19,7 @@ class Comment extends DataView 'comment_expiration', 'comment_internal_id', 'comment_is_persistent', + 'comment_name', 'comment_timestamp', 'comment_type', 'host_display_name', diff --git a/modules/monitoring/library/Monitoring/DataView/Hostcomment.php b/modules/monitoring/library/Monitoring/DataView/Hostcomment.php index 92020abe5..74fc2efef 100644 --- a/modules/monitoring/library/Monitoring/DataView/Hostcomment.php +++ b/modules/monitoring/library/Monitoring/DataView/Hostcomment.php @@ -20,6 +20,7 @@ class Hostcomment extends DataView 'comment_expiration', 'comment_internal_id', 'comment_is_persistent', + 'comment_name', 'comment_timestamp', 'comment_type', 'host_display_name', diff --git a/modules/monitoring/library/Monitoring/DataView/Servicecomment.php b/modules/monitoring/library/Monitoring/DataView/Servicecomment.php index b404955d7..78c133386 100644 --- a/modules/monitoring/library/Monitoring/DataView/Servicecomment.php +++ b/modules/monitoring/library/Monitoring/DataView/Servicecomment.php @@ -20,6 +20,7 @@ class Servicecomment extends DataView 'comment_expiration', 'comment_internal_id', 'comment_is_persistent', + 'comment_name', 'comment_timestamp', 'comment_type', 'host_display_name', From c55c5a9e64e56c8b6075663620eac3d75b53f867 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 31 Aug 2016 12:59:39 +0200 Subject: [PATCH 02/22] IDO: Provide downtime name column The Icinga 2 API requires the downtimes's name when removing the downtime. refs #11398 --- .../library/Monitoring/Backend/Ido/Query/DowntimeQuery.php | 4 ++++ .../Monitoring/Backend/Ido/Query/HostdowntimeQuery.php | 4 ++++ .../Monitoring/Backend/Ido/Query/ServicedowntimeQuery.php | 4 ++++ modules/monitoring/library/Monitoring/DataView/Downtime.php | 1 + .../monitoring/library/Monitoring/DataView/Hostdowntime.php | 1 + .../library/Monitoring/DataView/Servicedowntime.php | 1 + 6 files changed, 15 insertions(+) diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/DowntimeQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/DowntimeQuery.php index ae0c08d9c..2c85c1547 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/DowntimeQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/DowntimeQuery.php @@ -27,6 +27,7 @@ class DowntimeQuery extends IdoQuery 'downtime_is_fixed' => 'd.downtime_is_fixed', 'downtime_is_flexible' => 'd.downtime_is_flexible', 'downtime_is_in_effect' => 'd.downtime_is_in_effect', + 'downtime_name' => 'd.downtime_name', 'downtime_scheduled_end' => 'd.downtime_scheduled_end', 'downtime_scheduled_start' => 'd.downtime_scheduled_start', 'downtime_start' => 'd.downtime_start', @@ -90,6 +91,9 @@ class DowntimeQuery extends IdoQuery */ protected function joinBaseTables() { + if (version_compare($this->getIdoVersion(), '1.14.0', '<')) { + $this->columnMap['downtimes']['downtime_name'] = '(NULL)'; + } $this->downtimeQuery = $this->db->select(); $this->select->from( array('d' => $this->downtimeQuery), diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostdowntimeQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostdowntimeQuery.php index b3be3420b..3bea210de 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostdowntimeQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/HostdowntimeQuery.php @@ -38,6 +38,7 @@ class HostdowntimeQuery extends IdoQuery 'downtime_is_fixed' => 'sd.is_fixed', 'downtime_is_flexible' => 'CASE WHEN sd.is_fixed = 0 THEN 1 ELSE 0 END', 'downtime_is_in_effect' => 'sd.is_in_effect', + 'downtime_name' => 'sd.name', 'downtime_scheduled_end' => 'UNIX_TIMESTAMP(sd.scheduled_end_time)', 'downtime_scheduled_start' => 'UNIX_TIMESTAMP(sd.scheduled_start_time)', 'downtime_start' => 'UNIX_TIMESTAMP(CASE WHEN UNIX_TIMESTAMP(sd.trigger_time) > 0 then sd.trigger_time ELSE sd.scheduled_start_time END)', @@ -78,6 +79,9 @@ class HostdowntimeQuery extends IdoQuery */ protected function joinBaseTables() { + if (version_compare($this->getIdoVersion(), '1.14.0', '<')) { + $this->columnMap['downtimes']['downtime_name'] = '(NULL)'; + } $this->select->from( array('sd' => $this->prefix . 'scheduleddowntime'), array() diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicedowntimeQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicedowntimeQuery.php index c0b0fca2a..2c370bea9 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicedowntimeQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicedowntimeQuery.php @@ -38,6 +38,7 @@ class ServicedowntimeQuery extends IdoQuery 'downtime_is_fixed' => 'sd.is_fixed', 'downtime_is_flexible' => 'CASE WHEN sd.is_fixed = 0 THEN 1 ELSE 0 END', 'downtime_is_in_effect' => 'sd.is_in_effect', + 'downtime_name' => 'sd.name', 'downtime_scheduled_end' => 'UNIX_TIMESTAMP(sd.scheduled_end_time)', 'downtime_scheduled_start' => 'UNIX_TIMESTAMP(sd.scheduled_start_time)', 'downtime_start' => 'UNIX_TIMESTAMP(CASE WHEN UNIX_TIMESTAMP(sd.trigger_time) > 0 then sd.trigger_time ELSE sd.scheduled_start_time END)', @@ -83,6 +84,9 @@ class ServicedowntimeQuery extends IdoQuery */ protected function joinBaseTables() { + if (version_compare($this->getIdoVersion(), '1.14.0', '<')) { + $this->columnMap['downtimes']['downtime_name'] = '(NULL)'; + } $this->select->from( array('sd' => $this->prefix . 'scheduleddowntime'), array() diff --git a/modules/monitoring/library/Monitoring/DataView/Downtime.php b/modules/monitoring/library/Monitoring/DataView/Downtime.php index 8e14a13ee..ca42e2da5 100644 --- a/modules/monitoring/library/Monitoring/DataView/Downtime.php +++ b/modules/monitoring/library/Monitoring/DataView/Downtime.php @@ -23,6 +23,7 @@ class Downtime extends DataView 'downtime_is_fixed', 'downtime_is_flexible', 'downtime_is_in_effect', + 'downtime_name', 'downtime_scheduled_end', 'downtime_scheduled_start', 'downtime_start', diff --git a/modules/monitoring/library/Monitoring/DataView/Hostdowntime.php b/modules/monitoring/library/Monitoring/DataView/Hostdowntime.php index 8782b33fe..f5e4e8005 100644 --- a/modules/monitoring/library/Monitoring/DataView/Hostdowntime.php +++ b/modules/monitoring/library/Monitoring/DataView/Hostdowntime.php @@ -24,6 +24,7 @@ class Hostdowntime extends DataView 'downtime_is_fixed', 'downtime_is_flexible', 'downtime_is_in_effect', + 'downtime_name', 'downtime_scheduled_end', 'downtime_scheduled_start', 'downtime_start', diff --git a/modules/monitoring/library/Monitoring/DataView/Servicedowntime.php b/modules/monitoring/library/Monitoring/DataView/Servicedowntime.php index baed8c195..43d895ede 100644 --- a/modules/monitoring/library/Monitoring/DataView/Servicedowntime.php +++ b/modules/monitoring/library/Monitoring/DataView/Servicedowntime.php @@ -21,6 +21,7 @@ class Servicedowntime extends DataView 'downtime_is_fixed', 'downtime_is_flexible', 'downtime_is_in_effect', + 'downtime_name', 'downtime_scheduled_end', 'downtime_scheduled_start', 'downtime_start', From b8df909ad5555390fae9050b9447e6eca0697ac4 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 31 Aug 2016 13:00:39 +0200 Subject: [PATCH 03/22] Add name property to the delete comment command refs #11398 --- .../Command/Object/DeleteCommentCommand.php | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/modules/monitoring/library/Monitoring/Command/Object/DeleteCommentCommand.php b/modules/monitoring/library/Monitoring/Command/Object/DeleteCommentCommand.php index 9ba9e0d85..4d791064f 100644 --- a/modules/monitoring/library/Monitoring/Command/Object/DeleteCommentCommand.php +++ b/modules/monitoring/library/Monitoring/Command/Object/DeleteCommentCommand.php @@ -17,6 +17,15 @@ class DeleteCommentCommand extends IcingaCommand */ protected $commentId; + /** + * Name of the comment (Icinga 2.4+) + * + * Required for removing the comment via Icinga 2's API. + * + * @var string + */ + protected $commentName; + /** * Whether the command affects a service comment * @@ -47,6 +56,33 @@ class DeleteCommentCommand extends IcingaCommand return $this; } + /** + * Get the name of the comment (Icinga 2.4+) + * + * Required for removing the comment via Icinga 2's API. + * + * @return string + */ + public function getCommentName() + { + return $this->commentName; + } + + /** + * Set the name of the comment (Icinga 2.4+) + * + * Required for removing the comment via Icinga 2's API. + * + * @param string $commentName + * + * @return $this + */ + public function setCommentName($commentName) + { + $this->commentName = $commentName; + return $this; + } + /** * Get whether the command affects a service comment * From 62f2f92ae8517b60bcfbcdbe58a94a9b72b9cc1e Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 31 Aug 2016 13:01:19 +0200 Subject: [PATCH 04/22] Add name property to the delete downtime command refs #11398 --- .../Command/Object/DeleteDowntimeCommand.php | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/modules/monitoring/library/Monitoring/Command/Object/DeleteDowntimeCommand.php b/modules/monitoring/library/Monitoring/Command/Object/DeleteDowntimeCommand.php index ded5cc5e2..63f4bbce3 100644 --- a/modules/monitoring/library/Monitoring/Command/Object/DeleteDowntimeCommand.php +++ b/modules/monitoring/library/Monitoring/Command/Object/DeleteDowntimeCommand.php @@ -17,6 +17,15 @@ class DeleteDowntimeCommand extends IcingaCommand */ protected $downtimeId; + /** + * Name of the downtime (Icinga 2.4+) + * + * Required for removing the downtime via Icinga 2's API. + * + * @var string + */ + protected $downtimeName; + /** * Whether the command affects a service downtime * @@ -47,6 +56,33 @@ class DeleteDowntimeCommand extends IcingaCommand return $this; } + /** + * Get the name of the downtime (Icinga 2.4+) + * + * Required for removing the downtime via Icinga 2's API. + * + * @return string + */ + public function getDowntimeName() + { + return $this->downtimeName; + } + + /** + * Set the name of the downtime (Icinga 2.4+) + * + * Required for removing the downtime via Icinga 2's API. + * + * @param string $downtimeName + * + * @return $this + */ + public function setDowntimeName($downtimeName) + { + $this->downtimeName = $downtimeName; + return $this; + } + /** * Get whether the command affects a service * From 59dceb472905cd587f690f9aebdac172810adbff Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 31 Aug 2016 13:01:57 +0200 Subject: [PATCH 05/22] Add name to the delete comment command form refs #11398 --- .../Object/DeleteCommentCommandForm.php | 85 ++++++++++--------- 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/modules/monitoring/application/forms/Command/Object/DeleteCommentCommandForm.php b/modules/monitoring/application/forms/Command/Object/DeleteCommentCommandForm.php index e526d60e7..3b1cd5927 100644 --- a/modules/monitoring/application/forms/Command/Object/DeleteCommentCommandForm.php +++ b/modules/monitoring/application/forms/Command/Object/DeleteCommentCommandForm.php @@ -20,42 +20,6 @@ class DeleteCommentCommandForm extends CommandForm $this->setAttrib('class', 'inline'); } - /** - * {@inheritdoc} - */ - public function createElements(array $formData = array()) - { - $this->addElements( - array( - array( - 'hidden', - 'comment_id', - array( - 'required' => true, - 'validators' => array('NotEmpty'), - 'decorators' => array('ViewHelper') - ) - ), - array( - 'hidden', - 'comment_is_service', - array( - 'filters' => array('Boolean'), - 'decorators' => array('ViewHelper') - ) - ), - array( - 'hidden', - 'redirect', - array( - 'decorators' => array('ViewHelper') - ) - ) - ) - ); - return $this; - } - /** * {@inheritdoc} */ @@ -80,14 +44,59 @@ class DeleteCommentCommandForm extends CommandForm return $this; } + /** + * {@inheritdoc} + */ + public function createElements(array $formData = array()) + { + $this->addElements( + array( + array( + 'hidden', + 'comment_id', + array( + 'required' => true, + 'validators' => array('NotEmpty'), + 'decorators' => array('ViewHelper') + ) + ), + array( + 'hidden', + 'comment_is_service', + array( + 'filters' => array('Boolean'), + 'decorators' => array('ViewHelper') + ) + ), + array( + 'hidden', + 'comment_name', + array( + 'decorators' => array('ViewHelper') + ) + ), + array( + 'hidden', + 'redirect', + array( + 'decorators' => array('ViewHelper') + ) + ) + ) + ); + return $this; + } + /** * {@inheritdoc} */ public function onSuccess() { $cmd = new DeleteCommentCommand(); - $cmd->setIsService($this->getElement('comment_is_service')->getValue()) - ->setCommentId($this->getElement('comment_id')->getValue()); + $cmd + ->setCommentId($this->getElement('comment_id')->getValue()) + ->setCommentName($this->getElement('comment_name')->getValue()) + ->setIsService($this->getElement('comment_is_service')->getValue()); $this->getTransport($this->request)->send($cmd); $redirect = $this->getElement('redirect')->getValue(); if (! empty($redirect)) { From 69db59a38eb613a622ef0dae065f63bf64c2cc9e Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 31 Aug 2016 13:02:16 +0200 Subject: [PATCH 06/22] Add name to the delete downtime command form refs #11398 --- .../Command/Object/DeleteDowntimeCommandForm.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/modules/monitoring/application/forms/Command/Object/DeleteDowntimeCommandForm.php b/modules/monitoring/application/forms/Command/Object/DeleteDowntimeCommandForm.php index 15c0e8ebd..be2109eec 100644 --- a/modules/monitoring/application/forms/Command/Object/DeleteDowntimeCommandForm.php +++ b/modules/monitoring/application/forms/Command/Object/DeleteDowntimeCommandForm.php @@ -68,6 +68,13 @@ class DeleteDowntimeCommandForm extends CommandForm 'filters' => array('Boolean') ) ), + array( + 'hidden', + 'downtime_name', + array( + 'decorators' => array('ViewHelper') + ) + ), array( 'hidden', 'redirect', @@ -86,8 +93,10 @@ class DeleteDowntimeCommandForm extends CommandForm public function onSuccess() { $cmd = new DeleteDowntimeCommand(); - $cmd->setDowntimeId($this->getElement('downtime_id')->getValue()); - $cmd->setIsService($this->getElement('downtime_is_service')->getValue()); + $cmd + ->setDowntimeId($this->getElement('downtime_id')->getValue()) + ->setDowntimeName($this->getElement('downtime_name')->getValue()) + ->setIsService($this->getElement('downtime_is_service')->getValue()); $this->getTransport($this->request)->send($cmd); $redirect = $this->getElement('redirect')->getValue(); From 39f225a6276bdb763f3581fe8fc7b2433a584551 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 31 Aug 2016 13:04:11 +0200 Subject: [PATCH 07/22] Select comment name in the comment list view refs #11398 --- modules/monitoring/application/controllers/ListController.php | 1 + .../views/scripts/partials/comment/comment-detail.phtml | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/monitoring/application/controllers/ListController.php b/modules/monitoring/application/controllers/ListController.php index 8e493ed44..22630caab 100644 --- a/modules/monitoring/application/controllers/ListController.php +++ b/modules/monitoring/application/controllers/ListController.php @@ -422,6 +422,7 @@ class ListController extends Controller 'type' => 'comment_type', 'persistent' => 'comment_is_persistent', 'expiration' => 'comment_expiration', + 'name' => 'comment_name', 'host_name', 'service_description', 'host_display_name', diff --git a/modules/monitoring/application/views/scripts/partials/comment/comment-detail.phtml b/modules/monitoring/application/views/scripts/partials/comment/comment-detail.phtml index 079fd5880..2ed932916 100644 --- a/modules/monitoring/application/views/scripts/partials/comment/comment-detail.phtml +++ b/modules/monitoring/application/views/scripts/partials/comment/comment-detail.phtml @@ -53,7 +53,8 @@ $deleteButton->populate( array( 'comment_id' => $comment->id, - 'comment_is_service' => isset($comment->service_description) + 'comment_is_service' => isset($comment->service_description), + 'comment_name' => $comment->name ) ); $deleteButton->getElement('btn_submit') From 25a17b0bc94a49b3666b6fe55d025bb127def88a Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 31 Aug 2016 13:46:09 +0200 Subject: [PATCH 08/22] Select comment name in the comment detail view refs #11398 --- .../monitoring/application/controllers/CommentController.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/monitoring/application/controllers/CommentController.php b/modules/monitoring/application/controllers/CommentController.php index 122927531..b5d5e6720 100644 --- a/modules/monitoring/application/controllers/CommentController.php +++ b/modules/monitoring/application/controllers/CommentController.php @@ -37,6 +37,7 @@ class CommentController extends Controller 'type' => 'comment_type', 'persistent' => 'comment_is_persistent', 'expiration' => 'comment_expiration', + 'name' => 'comment_name', 'host_name', 'service_description', 'host_display_name', @@ -73,6 +74,7 @@ class CommentController extends Controller ->populate(array( 'comment_id' => $this->comment->id, 'comment_is_service' => isset($this->comment->service_description), + 'comment_name' => $this->comment->name, 'redirect' => $listUrl )) ->handleRequest(); From b0be1e56ddeb4e87c6ff36a89e5e16179874433d Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 31 Aug 2016 13:47:31 +0200 Subject: [PATCH 09/22] Select comment name in the multi select detail view refs #11398 --- .../monitoring/application/controllers/CommentsController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/monitoring/application/controllers/CommentsController.php b/modules/monitoring/application/controllers/CommentsController.php index ac118c477..9375636dc 100644 --- a/modules/monitoring/application/controllers/CommentsController.php +++ b/modules/monitoring/application/controllers/CommentsController.php @@ -46,6 +46,7 @@ class CommentsController extends Controller 'type' => 'comment_type', 'persistent' => 'comment_is_persistent', 'expiration' => 'comment_expiration', + 'name' => 'comment_name', 'host_name', 'service_description', 'host_display_name', From 573752168314592517d944a1d61d8e46bb64652e Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 31 Aug 2016 13:47:56 +0200 Subject: [PATCH 10/22] Add comment name to the delete comments command form refs #11398 --- .../forms/Command/Object/DeleteCommentsCommandForm.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/monitoring/application/forms/Command/Object/DeleteCommentsCommandForm.php b/modules/monitoring/application/forms/Command/Object/DeleteCommentsCommandForm.php index 2b5b9a79e..71886639e 100644 --- a/modules/monitoring/application/forms/Command/Object/DeleteCommentsCommandForm.php +++ b/modules/monitoring/application/forms/Command/Object/DeleteCommentsCommandForm.php @@ -72,6 +72,7 @@ class DeleteCommentsCommandForm extends CommandForm $cmd = new DeleteCommentCommand(); $cmd ->setCommentId($comment->id) + ->setCommentName($comment->name) ->setIsService(isset($comment->service_description)); $this->getTransport($this->request)->send($cmd); } From 57406245cae51860e1a3ed20273d609c1e4cb456 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 31 Aug 2016 14:05:36 +0200 Subject: [PATCH 11/22] Select downtime name in the downtime list view refs #11398 --- .../monitoring/application/controllers/ListController.php | 1 + .../views/scripts/partials/downtime/downtime-header.phtml | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/monitoring/application/controllers/ListController.php b/modules/monitoring/application/controllers/ListController.php index 22630caab..5499cfdf9 100644 --- a/modules/monitoring/application/controllers/ListController.php +++ b/modules/monitoring/application/controllers/ListController.php @@ -229,6 +229,7 @@ class ListController extends Controller 'is_fixed' => 'downtime_is_fixed', 'is_in_effect' => 'downtime_is_in_effect', 'entry_time' => 'downtime_entry_time', + 'name' => 'downtime_name', 'host_state', 'service_state', 'host_name', diff --git a/modules/monitoring/application/views/scripts/partials/downtime/downtime-header.phtml b/modules/monitoring/application/views/scripts/partials/downtime/downtime-header.phtml index eb9f26ac3..bfe88a761 100644 --- a/modules/monitoring/application/views/scripts/partials/downtime/downtime-header.phtml +++ b/modules/monitoring/application/views/scripts/partials/downtime/downtime-header.phtml @@ -61,8 +61,9 @@ $deleteButton->setAttrib('class', $deleteButton->getAttrib('class') . ' remove-action'); $deleteButton->populate( array( - 'downtime_id' => $downtime->id, - 'downtime_is_service' => isset($downtime->service_description) + 'downtime_id' => $downtime->id, + 'downtime_is_service' => isset($downtime->service_description), + 'downtime_name' => $downtime->name ) ); $deleteButton->getElement('btn_submit') From c982c96e164f23308c6cd7957de210349a7ad712 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 31 Aug 2016 14:07:15 +0200 Subject: [PATCH 12/22] Select downtime name in the downtime detail view refs #11398 --- .../monitoring/application/controllers/DowntimeController.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/monitoring/application/controllers/DowntimeController.php b/modules/monitoring/application/controllers/DowntimeController.php index 6e8a19809..e19ce38c0 100644 --- a/modules/monitoring/application/controllers/DowntimeController.php +++ b/modules/monitoring/application/controllers/DowntimeController.php @@ -44,6 +44,7 @@ class DowntimeController extends Controller 'is_fixed' => 'downtime_is_fixed', 'is_in_effect' => 'downtime_is_in_effect', 'entry_time' => 'downtime_entry_time', + 'name' => 'downtime_name', 'host_state', 'service_state', 'host_name', @@ -91,6 +92,7 @@ class DowntimeController extends Controller ->populate(array( 'downtime_id' => $this->downtime->id, 'downtime_is_service' => $isService, + 'downtime_name' => $this->downtime->name, 'redirect' => Url::fromPath('monitoring/list/downtimes'), )) ->handleRequest(); From 9c766c02413cb95918e6128a2875cad965f9464a Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 31 Aug 2016 14:08:05 +0200 Subject: [PATCH 13/22] Select downtime name in the multi select detail view refs #11398 --- .../monitoring/application/controllers/DowntimesController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/monitoring/application/controllers/DowntimesController.php b/modules/monitoring/application/controllers/DowntimesController.php index fb15c98de..0b33aa339 100644 --- a/modules/monitoring/application/controllers/DowntimesController.php +++ b/modules/monitoring/application/controllers/DowntimesController.php @@ -51,6 +51,7 @@ class DowntimesController extends Controller 'is_fixed' => 'downtime_is_fixed', 'is_in_effect' => 'downtime_is_in_effect', 'entry_time' => 'downtime_entry_time', + 'name' => 'downtime_name', 'host_state', 'service_state', 'host_name', From 4fda29c1f0a297666fd846bf4f82bd2faab850a1 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 31 Aug 2016 14:08:29 +0200 Subject: [PATCH 14/22] Add downtime name to the delete downtimes command form refs #11398 --- .../forms/Command/Object/DeleteDowntimesCommandForm.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/monitoring/application/forms/Command/Object/DeleteDowntimesCommandForm.php b/modules/monitoring/application/forms/Command/Object/DeleteDowntimesCommandForm.php index 52590d145..1e0399c88 100644 --- a/modules/monitoring/application/forms/Command/Object/DeleteDowntimesCommandForm.php +++ b/modules/monitoring/application/forms/Command/Object/DeleteDowntimesCommandForm.php @@ -72,6 +72,7 @@ class DeleteDowntimesCommandForm extends CommandForm $delDowntime = new DeleteDowntimeCommand(); $delDowntime ->setDowntimeId($downtime->id) + ->setDowntimeName($downtime->name) ->setIsService(isset($downtime->service_description)); $this->getTransport($this->request)->send($delDowntime); } From 0f4fa2d492fa3e48de1f649f0e74677fcba16b58 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 1 Sep 2016 10:44:59 +0200 Subject: [PATCH 15/22] Add IcingaApiCommand class refs #11398 --- .../Monitoring/Command/IcingaApiCommand.php | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 modules/monitoring/library/Monitoring/Command/IcingaApiCommand.php diff --git a/modules/monitoring/library/Monitoring/Command/IcingaApiCommand.php b/modules/monitoring/library/Monitoring/Command/IcingaApiCommand.php new file mode 100644 index 000000000..b14f96bc4 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/IcingaApiCommand.php @@ -0,0 +1,86 @@ +setEndpoint($endpoint) + ->setData($data); + return $command; + } + + /** + * Get the command data + * + * @return array + */ + public function getData() + { + return $this->data; + } + + /** + * Set the command data + * + * @param array $data + * + * @return $this + */ + public function setData($data) + { + $this->data = $data; + + return $this; + } + + /** + * Get the name of the endpoint + * + * @return string + */ + public function getEndpoint() + { + return $this->endpoint; + } + + /** + * Set the name of the endpoint + * + * @param string $endpoint + * + * @return $this + */ + public function setEndpoint($endpoint) + { + $this->endpoint = $endpoint; + + return $this; + } +} From ad658b049c506e49fba879f2c19cb55fc96a6012 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 1 Sep 2016 10:45:28 +0200 Subject: [PATCH 16/22] Add IcingaApiCommandRenderer class refs #11398 --- .../Renderer/IcingaApiCommandRenderer.php | 276 ++++++++++++++++++ 1 file changed, 276 insertions(+) create mode 100644 modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php diff --git a/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php new file mode 100644 index 000000000..cb346bca5 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php @@ -0,0 +1,276 @@ +app; + } + + /** + * Set the name of the Icinga application object + * + * @param string $app + * + * @return $this + */ + public function setApp($app) + { + $this->app = $app; + + return $this; + } + + /** + * Apply filter to query data + * + * @param array $data + * @param MonitoredObject $object + * + * @return array + */ + protected function applyFilter(array &$data, MonitoredObject $object) + { + if ($object->getType() === $object::TYPE_HOST) { + /** @var \Icinga\Module\Monitoring\Object\Host $object */ + $data['host'] = $object->getName(); + } else { + /** @var \Icinga\Module\Monitoring\Object\Service $object */ + $data['service'] = sprintf('%s!%s', $object->getHost()->getName(), $object->getName()); + } + } + + /** + * Render a command + * + * @param IcingaCommand $command + * + * @return IcingaApiCommand + */ + public function render(IcingaCommand $command) + { + $renderMethod = 'render' . $command->getName(); + if (! method_exists($this, $renderMethod)) { + die($renderMethod); + } + return $this->$renderMethod($command); + } + + public function renderAddComment(AddCommentCommand $command) + { + $endpoint = 'actions/add-comment'; + $data = array( + 'author' => $command->getAuthor(), + 'comment' => $command->getComment() + ); + $this->applyFilter($data, $command->getObject()); + return IcingaApiCommand::create($endpoint, $data); + } + + public function renderSendCustomNotification(SendCustomNotificationCommand $command) + { + $endpoint = 'actions/send-custom-notification'; + $data = array( + 'author' => $command->getAuthor(), + 'comment' => $command->getComment(), + 'force' => $command->getForced() + ); + $this->applyFilter($data, $command->getObject()); + return IcingaApiCommand::create($endpoint, $data); + } + + public function renderProcessCheckResult(ProcessCheckResultCommand $command) + { + $endpoint = 'actions/process-check-result'; + $data = array( + 'exit_status' => $command->getStatus(), + 'plugin_output' => $command->getOutput(), + 'performance_data' => $command->getPerformanceData() + ); + $this->applyFilter($data, $command->getObject()); + return IcingaApiCommand::create($endpoint, $data); + } + + public function renderScheduleCheck(ScheduleServiceCheckCommand $command) + { + $endpoint = 'actions/reschedule-check'; + $data = array( + 'next_check' => $command->getCheckTime(), + 'force_check' => $command->getForced() + ); + $this->applyFilter($data, $command->getObject()); + return IcingaApiCommand::create($endpoint, $data); + } + + public function renderScheduleDowntime(ScheduleServiceDowntimeCommand $command) + { + $endpoint = 'actions/schedule-downtime'; + $data = array( + 'author' => $command->getAuthor(), + 'comment' => $command->getComment(), + 'start_time' => $command->getStart(), + 'end_time' => $command->getEnd(), + 'duration' => $command->getDuration(), + 'fixed' => $command->getFixed(), + 'trigger_name' => $command->getTriggerId() + ); + $this->applyFilter($data, $command->getObject()); + return IcingaApiCommand::create($endpoint, $data); + } + + public function renderAcknowledgeProblem(AcknowledgeProblemCommand $command) + { + $endpoint = 'actions/acknowledge-problem'; + $data = array( + 'author' => $command->getAuthor(), + 'comment' => $command->getComment(), + 'expiry' => $command->getExpireTime(), + 'sticky' => $command->getSticky(), + 'notify' => $command->getNotify() + ); + $this->applyFilter($data, $command->getObject()); + return IcingaApiCommand::create($endpoint, $data); + } + + public function renderToggleObjectFeature(ToggleObjectFeatureCommand $command) + { + if ($command->getEnabled() === true) { + $enabled = true; + } else { + $enabled = false; + } + switch ($command->getFeature()) { + case ToggleObjectFeatureCommand::FEATURE_ACTIVE_CHECKS: + $attr = 'enable_active_checks'; + break; + case ToggleObjectFeatureCommand::FEATURE_PASSIVE_CHECKS: + $attr = 'enable_passive_checks'; + break; + case ToggleObjectFeatureCommand::FEATURE_NOTIFICATIONS: + $attr = 'enable_notifications'; + break; + case ToggleObjectFeatureCommand::FEATURE_EVENT_HANDLER: + $attr = 'enable_event_handler'; + break; + case ToggleObjectFeatureCommand::FEATURE_FLAP_DETECTION: + $attr = 'enable_flapping'; + break; + default: + throw new InvalidArgumentException($command->getFeature()); + } + $endpoint = 'objects/'; + $object = $command->getObject(); + if ($object->getType() === ToggleObjectFeatureCommand::TYPE_HOST) { + /** @var \Icinga\Module\Monitoring\Object\Host $object */ + $endpoint .= 'hosts'; + } else { + /** @var \Icinga\Module\Monitoring\Object\Service $object */ + $endpoint .= 'services'; + } + $data = array( + 'attrs' => array( + $attr => $enabled + ) + ); + $this->applyFilter($data, $object); + return IcingaApiCommand::create($endpoint, $data); + } + + public function renderDeleteComment(DeleteCommentCommand $command) + { + $endpoint = 'actions/remove-comment'; + $data = array( + 'comment' => $command->getCommentName() + ); + return IcingaApiCommand::create($endpoint, $data); + } + + public function renderDeleteDowntime(DeleteDowntimeCommand $command) + { + $endpoint = 'actions/remove-downtime'; + $data = array( + 'downtime' => $command->getDowntimeName() + ); + return IcingaApiCommand::create($endpoint, $data); + } + + public function renderRemoveAcknowledgement(RemoveAcknowledgementCommand $command) + { + $endpoint = 'actions/remove-acknowledgement'; + $data = array(); + $this->applyFilter($data, $command->getObject()); + return IcingaApiCommand::create($endpoint, $data); + } + + public function renderToggleInstanceFeature(ToggleInstanceFeatureCommand $command) + { + $endpoint = 'objects/icingaapplications/' . $this->getApp(); + if ($command->getEnabled() === true) { + $enabled = true; + } else { + $enabled = false; + } + switch ($command->getFeature()) { + case ToggleInstanceFeatureCommand::FEATURE_ACTIVE_HOST_CHECKS: + $attr = 'enable_host_checks'; + break; + case ToggleInstanceFeatureCommand::FEATURE_ACTIVE_SERVICE_CHECKS: + $attr = 'enable_service_checks'; + break; + case ToggleInstanceFeatureCommand::FEATURE_EVENT_HANDLERS: + $attr = 'enable_event_handlers'; + break; + case ToggleInstanceFeatureCommand::FEATURE_FLAP_DETECTION: + $attr = 'enable_flapping'; + break; + case ToggleInstanceFeatureCommand::FEATURE_NOTIFICATIONS: + $attr = 'enable_notifications'; + break; + case ToggleInstanceFeatureCommand::FEATURE_PERFORMANCE_DATA: + $attr = 'enable_perfdata'; + break; + default: + throw new InvalidArgumentException($command->getFeature()); + } + $data = array( + 'attrs' => array( + $attr => $enabled + ) + ); + return IcingaApiCommand::create($endpoint, $data); + } +} From cfc0f60042d60869a986e3e1e4cb0ce031e227a0 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 1 Sep 2016 10:47:06 +0200 Subject: [PATCH 17/22] Add RestRequest class refs #11398 --- .../Monitoring/Web/Rest/RestRequest.php | 269 ++++++++++++++++++ 1 file changed, 269 insertions(+) create mode 100644 modules/monitoring/library/Monitoring/Web/Rest/RestRequest.php diff --git a/modules/monitoring/library/Monitoring/Web/Rest/RestRequest.php b/modules/monitoring/library/Monitoring/Web/Rest/RestRequest.php new file mode 100644 index 000000000..7506f6418 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Web/Rest/RestRequest.php @@ -0,0 +1,269 @@ +uri = $uri; + $request->method = 'POST'; + return $request; + } + + /** + * Send content type JSON + * + * @return $this + */ + public function sendJson() + { + $this->contentType = 'application/json'; + + return $this; + } + + /** + * Set basic auth credentials + * + * @param string $username + * @param string $password + * + * @return $this + */ + public function authenticateWith($username, $password) + { + $this->hasBasicAuth = true; + $this->username = $username; + $this->password = $password; + + return $this; + } + + /** + * Set request payload + * + * @param mixed $payload + * + * @return $this + */ + public function setPayload($payload) + { + $this->payload = $payload; + + return $this; + } + + /** + * Disable strict SSL + * + * @return $this + */ + public function noStrictSsl() + { + $this->strictSsl = false; + + return $this; + } + + /** + * Serialize payload according to content type + * + * @param mixed $payload + * @param string $contentType + * + * @return string + */ + public function serializePayload($payload, $contentType) + { + switch ($contentType) { + case 'application/json': + $payload = json_encode($payload); + break; + } + + return $payload; + } + + /** + * Send the request + * + * @return mixed + * + * @throws Exception + */ + public function send() + { + $defaults = array( + 'host' => 'localhost', + 'path' => '/' + ); + + $url = array_merge($defaults, parse_url($this->uri)); + + if (isset($url['port'])) { + $url['host'] .= sprintf(':%u', $url['port']); + } + + if (isset($url['query'])) { + $url['path'] .= sprintf('?%s', $url['query']); + } + + $headers = array( + "{$this->method} {$url['path']} HTTP/1.1", + "Host: {$url['host']}", + "Content-Type: {$this->contentType}", + 'Accept: application/json', + // Bypass "Expect: 100-continue" timeouts + 'Expect:' + ); + + $ch = curl_init(); + + $options = array( + CURLOPT_URL => $this->uri, + CURLOPT_TIMEOUT => $this->timeout, + // Ignore proxy settings + CURLOPT_PROXY => '', + CURLOPT_CUSTOMREQUEST => $this->method, + CURLOPT_RETURNTRANSFER => true + ); + + if ($this->strictSsl) { + $options[CURLOPT_SSL_VERIFYHOST] = 2; + $options[CURLOPT_SSL_VERIFYPEER] = true; + } else { + $options[CURLOPT_SSL_VERIFYHOST] = false; + $options[CURLOPT_SSL_VERIFYPEER] = false; + } + + if ($this->hasBasicAuth) { + $options[CURLOPT_USERPWD] = sprintf('%s:%s', $this->username, $this->password); + } + + if (! empty($this->payload)) { + $payload = $this->serializePayload($this->payload, $this->contentType); + $options[CURLOPT_POSTFIELDS] = $payload; + } + + $options[CURLOPT_HTTPHEADER] = $headers; + + curl_setopt_array($ch, $options); + + $result = curl_exec($ch); + + if ($result === false) { + throw new Exception(curl_error($ch)); + } + + curl_close($ch); + + $response = @json_decode($result, true); + + if ($response === null) { + if (version_compare(PHP_VERSION, '5.5.0', '>=')) { + throw new Exception(json_last_error_msg()); + } else { + switch (json_last_error()) { + case JSON_ERROR_DEPTH: + $msg = 'The maximum stack depth has been exceeded'; + break; + case JSON_ERROR_CTRL_CHAR: + $msg = 'Control character error, possibly incorrectly encoded'; + break; + case JSON_ERROR_STATE_MISMATCH: + $msg = 'Invalid or malformed JSON'; + break; + case JSON_ERROR_SYNTAX: + $msg = 'Syntax error'; + break; + case JSON_ERROR_UTF8: + $msg = 'Malformed UTF-8 characters, possibly incorrectly encoded'; + break; + default: + $msg = 'An error occured when parsing a JSON string'; + } + throw new Exception($msg); + } + } + + return $response; + } +} From db41024c894f297f5ef96a5f32ac8560c371ac8e Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 1 Sep 2016 10:47:27 +0200 Subject: [PATCH 18/22] Add ApiCommandTransport class refs #11398 --- .../Command/Transport/ApiCommandTransport.php | 227 ++++++++++++++++++ 1 file changed, 227 insertions(+) create mode 100644 modules/monitoring/library/Monitoring/Command/Transport/ApiCommandTransport.php diff --git a/modules/monitoring/library/Monitoring/Command/Transport/ApiCommandTransport.php b/modules/monitoring/library/Monitoring/Command/Transport/ApiCommandTransport.php new file mode 100644 index 000000000..1058e9178 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Transport/ApiCommandTransport.php @@ -0,0 +1,227 @@ +renderer = new IcingaApiCommandRenderer(); + } + + /** + * Set the name of the Icinga application object + * + * @param string $app + * + * @return $this + */ + public function setApp($app) + { + $this->renderer->setApp($app); + + return $this; + } + + /** + * Get the API host + * + * @return string + */ + public function getHost() + { + return $this->host; + } + + /** + * Set the API host + * + * @param string $host + * + * @return $this + */ + public function setHost($host) + { + $this->host = $host; + + return $this; + } + + /** + * Get the API password + * + * @return string + */ + public function getPassword() + { + return $this->password; + } + + /** + * Set the API password + * + * @param string $password + * + * @return $this + */ + public function setPassword($password) + { + $this->password = $password; + + return $this; + } + + /** + * Get the API port + * + * @return int + */ + public function getPort() + { + return $this->port; + } + + /** + * Set the API port + * + * @param int $port + * + * @return $this + */ + public function setPort($port) + { + $this->port = (int) $port; + + return $this; + } + + /** + * Get the API username + * + * @return string + */ + public function getUsername() + { + return $this->username; + } + + /** + * Set the API username + * + * @param string $username + * + * @return $this + */ + public function setUsername($username) + { + $this->username = $username; + + return $this; + } + + /** + * Get URI for endpoint + * + * @param string $endpoint + * + * @return string + */ + protected function getUriFor($endpoint) + { + return sprintf('https://%s:%u/v1/%s', $this->getHost(), $this->getPort(), $endpoint); + } + + /** + * Send the Icinga command over the Icinga 2 API + * + * @param IcingaCommand $command + * @param int|null $now + * + * @throws CommandTransportException + */ + public function send(IcingaCommand $command, $now = null) + { + $command = $this->renderer->render($command); + Logger::debug( + 'Sending Icinga command "%s" to the API "%s:%u"', + $command->getEndpoint(), + $this->getHost(), + $this->getPort() + ); + $response = RestRequest::post($this->getUriFor($command->getEndpoint())) + ->authenticateWith($this->getUsername(), $this->getPassword()) + ->sendJson() + ->noStrictSsl() + ->setPayload($command->getData()) + ->send(); + if (isset($response['error'])) { + throw new CommandTransportException( + 'Can\'t send external Icinga command: %u %s', + $response['error'], + $response['status'] + ); + } + $result = array_pop($response['results']); + + if ($result['code'] < 200 || $result['code'] >= 300) { + throw new CommandTransportException( + 'Can\'t send external Icinga command: %u %s', + $result['code'], + $result['status'] + ); + } + } +} From 9b310ca1ca5eedd115f4411c9c487ee3d7b460bd Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 1 Sep 2016 10:47:52 +0200 Subject: [PATCH 19/22] Add ApiTransportForm class refs #11398 --- .../Config/Transport/ApiTransportForm.php | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 modules/monitoring/application/forms/Config/Transport/ApiTransportForm.php diff --git a/modules/monitoring/application/forms/Config/Transport/ApiTransportForm.php b/modules/monitoring/application/forms/Config/Transport/ApiTransportForm.php new file mode 100644 index 000000000..3a54ef2e9 --- /dev/null +++ b/modules/monitoring/application/forms/Config/Transport/ApiTransportForm.php @@ -0,0 +1,73 @@ +setName('form_config_command_transport_api'); + } + + /** + * {@inheritdoc} + */ + public function createElements(array $formData = array()) + { + $this->addElements(array( + array( + 'text', + 'host', + array( + 'required' => true, + 'label' => $this->translate('Host'), + 'description' => $this->translate( + 'Hostname or address of the remote Icinga instance' + ) + ) + ), + array( + 'number', + 'port', + array( + 'required' => true, + 'label' => $this->translate('Port'), + 'description' => $this->translate('SSH port to connect to on the remote Icinga instance'), + 'value' => 5665 + ) + ), + array( + 'text', + 'username', + array( + 'required' => true, + 'label' => $this->translate('API Username'), + 'description' => $this->translate( + 'User to log in as on the remote Icinga instance. Please note that key-based SSH login must be' + . ' possible for this user' + ) + ) + ), + array( + 'password', + 'password', + array( + 'required' => true, + 'label' => $this->translate('API Password'), + 'description' => $this->translate( + 'User to log in as on the remote Icinga instance. Please note that key-based SSH login must be' + . ' possible for this user' + ) + ) + ) + )); + + return $this; + } +} From 3ccbf371615fa0adf87ca3c8258e57ec6f53b18f Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 1 Sep 2016 10:48:36 +0200 Subject: [PATCH 20/22] Provide API transport in the transport config form refs #11398 --- .../application/forms/Config/TransportConfigForm.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/modules/monitoring/application/forms/Config/TransportConfigForm.php b/modules/monitoring/application/forms/Config/TransportConfigForm.php index 4f14bfd5e..0e5ad4f49 100644 --- a/modules/monitoring/application/forms/Config/TransportConfigForm.php +++ b/modules/monitoring/application/forms/Config/TransportConfigForm.php @@ -7,8 +7,10 @@ use InvalidArgumentException; use Icinga\Exception\IcingaException; use Icinga\Exception\NotFoundError; use Icinga\Forms\ConfigForm; +use Icinga\Module\Monitoring\Command\Transport\ApiCommandTransport; use Icinga\Module\Monitoring\Command\Transport\LocalCommandFile; use Icinga\Module\Monitoring\Command\Transport\RemoteCommandFile; +use Icinga\Module\Monitoring\Forms\Config\Transport\ApiTransportForm; use Icinga\Module\Monitoring\Forms\Config\Transport\LocalTransportForm; use Icinga\Module\Monitoring\Forms\Config\Transport\RemoteTransportForm; @@ -68,7 +70,7 @@ class TransportConfigForm extends ConfigForm * * @param string $type The transport type for which to return a form * - * @return Form + * @return \Icinga\Web\Form * * @throws InvalidArgumentException In case the given transport type is invalid */ @@ -79,6 +81,8 @@ class TransportConfigForm extends ConfigForm return new LocalTransportForm(); case RemoteCommandFile::TRANSPORT; return new RemoteTransportForm(); + case ApiCommandTransport::TRANSPORT: + return new ApiTransportForm(); default: throw new InvalidArgumentException( sprintf($this->translate('Invalid command transport type "%s" given'), $type) @@ -223,7 +227,8 @@ class TransportConfigForm extends ConfigForm $transportTypes = array( LocalCommandFile::TRANSPORT => $this->translate('Local Command File'), - RemoteCommandFile::TRANSPORT => $this->translate('Remote Command File') + RemoteCommandFile::TRANSPORT => $this->translate('Remote Command File'), + ApiCommandTransport::TRANSPORT => $this->translate('Icinga 2 API') ); $transportType = isset($formData['transport']) ? $formData['transport'] : null; From 440a3f5fdd8872d0ee00fce9336de2b01425c5a9 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 1 Sep 2016 10:49:08 +0200 Subject: [PATCH 21/22] Respect API transport in the command transport factory refs #11398 --- .../Monitoring/Command/Transport/CommandTransport.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/modules/monitoring/library/Monitoring/Command/Transport/CommandTransport.php b/modules/monitoring/library/Monitoring/Command/Transport/CommandTransport.php index 81fb54939..512de2f28 100644 --- a/modules/monitoring/library/Monitoring/Command/Transport/CommandTransport.php +++ b/modules/monitoring/library/Monitoring/Command/Transport/CommandTransport.php @@ -65,6 +65,9 @@ class CommandTransport implements CommandTransportInterface case RemoteCommandFile::TRANSPORT: $transport = new RemoteCommandFile(); break; + case ApiCommandTransport::TRANSPORT: + $transport = new ApiCommandTransport(); + break; case LocalCommandFile::TRANSPORT: case '': // Casting null to string is the empty string $transport = new LocalCommandFile(); @@ -74,12 +77,13 @@ class CommandTransport implements CommandTransportInterface mt( 'monitoring', 'Cannot create command transport "%s". Invalid transport' - . ' defined in "%s". Use one of "%s" or "%s".' + . ' defined in "%s". Use one of "%s", "%s" or "%s".' ), $config->transport, static::getConfig()->getConfigFile(), LocalCommandFile::TRANSPORT, - RemoteCommandFile::TRANSPORT + RemoteCommandFile::TRANSPORT, + ApiCommandTransport::TRANSPORT ); } From 64b3811ab0523456e9d5053a6da44c916afd68a1 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 1 Sep 2016 10:49:56 +0200 Subject: [PATCH 22/22] Display transport type from config in the config overview refs #11398 --- modules/monitoring/application/views/scripts/config/index.phtml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/monitoring/application/views/scripts/config/index.phtml b/modules/monitoring/application/views/scripts/config/index.phtml index 091aaf70f..79a8ccd3c 100644 --- a/modules/monitoring/application/views/scripts/config/index.phtml +++ b/modules/monitoring/application/views/scripts/config/index.phtml @@ -92,7 +92,7 @@ ); ?> (translate('Type: %s'), - $config->host !== null ? $this->translate('Remote') : $this->translate('Local') + ucfirst($config->get('transport', 'local')) ) ?>)