From 3d3fbb123da45abef07601e7a4dbf292f495151f Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 15 Aug 2013 15:34:48 +0200 Subject: [PATCH] Monitoring/Commands: Add help messages to the custom notification form refs #4524 --- .../Protocol/Commandpipe/CommandPipe.php | 52 ++++----- .../Commandpipe/CustomNotification.php | 109 ++++++++++++++++++ .../controllers/CommandController.php | 7 +- .../forms/Command/CustomNotificationForm.php | 85 +++++++++++--- 4 files changed, 202 insertions(+), 51 deletions(-) create mode 100644 library/Icinga/Protocol/Commandpipe/CustomNotification.php diff --git a/library/Icinga/Protocol/Commandpipe/CommandPipe.php b/library/Icinga/Protocol/Commandpipe/CommandPipe.php index 1322c00e5..c20c18a46 100644 --- a/library/Icinga/Protocol/Commandpipe/CommandPipe.php +++ b/library/Icinga/Protocol/Commandpipe/CommandPipe.php @@ -2,24 +2,24 @@ // {{{ICINGA_LICENSE_HEADER}}} /** * This file is part of Icinga 2 Web. - * + * * Icinga 2 Web - Head for multiple monitoring backends. * Copyright (C) 2013 Icinga Development Team - * + * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * + * * @copyright 2013 Icinga Development Team * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @author Icinga Development Team @@ -648,31 +648,31 @@ class CommandPipe } /** - * Send a custom host or service notification + * Send custom host and/or service notifications * - * @param $objects monitoring objects to send this notification to - * @param Comment $comment comment to use in the notification - * @param int [$...] Optional list of Notification flags which will be used as the option parameter + * @param array $objects Affected monitoring objects + * @param CustomNotification $notification */ - public function sendCustomNotification($objects, Comment $comment, $optionsVarList = 0/*, ...*/) + public function sendCustomNotification(array $objects, CustomNotification $notification) { - $args = func_get_args(); - // logical OR for all notification options - for ($i = 3; $i < count($args); $i++) { - $optionsVarList |= $args[$i]; - } - - foreach ($objects as $object) { - $type = $this->getObjectType($object); - $msg = 'SEND_CUSTOM_'.(($type == self::TYPE_SERVICE) ? 'SVC' : 'HOST' ).'_NOTIFICATION'; - $msg .= ';'.$object->host_name; - if ($type == self::TYPE_SERVICE) { - $msg .= ';'.$object->service_description; + foreach ($objects as $hostOrService) { + if (isset($hostOrService->service_description) && isset($hostOrService->host_name)) { + // Assume service + $command = sprintf( + $notification->getFormatString(self::TYPE_SERVICE), + $hostOrService->host_name, + $hostOrService->service_description + ); + } elseif (isset($hostOrService->host_name)) { + // Assume host + $command = sprintf( + $notification->getFormatString(self::TYPE_HOST), + $hostOrService->host_name + ); + } else { + continue; } - $msg .= ';'.$optionsVarList; - $msg .= ';'.$comment->author; - $msg .= ';'.$comment->comment; - $this->send($msg); + $this->send($command); } } diff --git a/library/Icinga/Protocol/Commandpipe/CustomNotification.php b/library/Icinga/Protocol/Commandpipe/CustomNotification.php new file mode 100644 index 000000000..e21bd9fa0 --- /dev/null +++ b/library/Icinga/Protocol/Commandpipe/CustomNotification.php @@ -0,0 +1,109 @@ +author = $author; + $this->comment = $comment; + $this->forced = $forced; + $this->broadcast = $broadcast; + } + + /** + * Get Custom Notification command format string according to if its sent to a host or a service + * + * @param string $type Identifier for either host or service + * + * @return string + * + * @throws InvalidCommandException When the given type is unknown + * @see \Icinga\Protocol\Commandpipe\CommandPipe::TYPE_HOST + * @see \Icinga\Protocol\Commandpipe\CommandPipe::TYPE_SERVICE + */ + public function getFormatString($type) + { + switch ($type) { + case CommandPipe::TYPE_HOST: + $format = '%s'; + break; + case CommandPipe::TYPE_SERVICE: + $format = '%s;%s'; + break; + default: + throw new InvalidCommandException('Custom Notifications can only apply on hosts and services'); + } + + $options = 0; + if ($this->forced) { + $options |= self::NOTIFY_FORCED; + } + if ($this->broadcast) { + $options |= self::NOTIFY_BROADCAST; + } + + // Build the command + $command = 'SEND_CUSTOM_' . $type . '_NOTIFICATION;' + . $format . ';' + . $options . ';' + . $this->author . ';' + . $this->comment; + return $command; + } +} diff --git a/modules/monitoring/application/controllers/CommandController.php b/modules/monitoring/application/controllers/CommandController.php index 0033a7730..68c3c153b 100644 --- a/modules/monitoring/application/controllers/CommandController.php +++ b/modules/monitoring/application/controllers/CommandController.php @@ -431,12 +431,7 @@ class Monitoring_CommandController extends ModuleActionController $this->setForm($form); if ($form->IsSubmittedAndValid() === true) { - $author = $this->getRequest()->getUser()->getUsername(); - $this->target->sendCustomNotification( - $this->view->objects, - new Comment($author, $form->getComment()), - $form->getOptions() - ); + $this->target->sendCustomNotification($this->view->objects, $form->getCustomNotification()); } } diff --git a/modules/monitoring/application/forms/Command/CustomNotificationForm.php b/modules/monitoring/application/forms/Command/CustomNotificationForm.php index 7244fb759..c62a2e4d5 100644 --- a/modules/monitoring/application/forms/Command/CustomNotificationForm.php +++ b/modules/monitoring/application/forms/Command/CustomNotificationForm.php @@ -28,7 +28,8 @@ namespace Monitoring\Form\Command; -use Zend_Form_Element_Hidden; +use \Icinga\Web\Form\Element\Note; +use \Icinga\Protocol\Commandpipe\CustomNotification; /** * For for command CustomNotification @@ -36,11 +37,23 @@ use Zend_Form_Element_Hidden; class CustomNotificationForm extends CommandForm { /** - * Interface method to build the form - * @see CommandForm::create + * Create the form's elements */ protected function create() { + $this->addElement( + new Note( + array( + 'name' => 'commanddescription', + 'value' => t( + 'This command is used to send a custom notification about hosts or services. Useful in ' + . 'emergencies when you need to notify admins of an issue regarding a monitored system or ' + . 'service.' + ) + ) + ) + ); + $this->addElement($this->createAuthorField()); $this->addElement( @@ -52,14 +65,38 @@ class CustomNotificationForm extends CommandForm 'required' => true ) ); + $this->addElement( + new Note( + array( + 'name' => 'commentnote', + 'value' => t( + 'If you work with other administrators, you may find it useful to share information ' + . 'about a host or service that is having problems if more than one of you may be working on ' + . 'it. Make sure you enter a brief description of what you are doing.' + ) + ) + ) + ); $this->addElement( 'checkbox', - 'force', + 'forced', array( 'label' => t('Forced') ) ); + $this->addElement( + new Note( + array( + 'name' => 'forcenote', + 'value' => t( + 'Custom notifications normally follow the regular notification logic in Icinga. Selecting this ' + . 'option will force the notification to be sent out, regardless of the time restrictions, ' + . 'whether or not notifications are enabled, etc.' + ) + ) + ) + ); $this->addElement( 'checkbox', @@ -68,26 +105,36 @@ class CustomNotificationForm extends CommandForm 'label' => t('Broadcast') ) ); + $this->addElement( + new Note( + array( + 'name' => 'broadcastnote', + 'value' => t( + 'Selecting this option causes the notification to be sent out to all normal (non-escalated) ' + . ' and escalated contacts. These options allow you to override the normal notification logic ' + . 'if you need to get an important message out.' + ) + ) + ) + ); - $this->setSubmitLabel(t('Send custom notification')); + $this->setSubmitLabel(t('Send Custom Notification')); parent::create(); } - public function getComment() + /** + * Create Custom Notification from request data + * + * @return \Icinga\Protocol\Commandpipe\CustomNotification + */ + public function getCustomNotification() { - return $this->getValue('comment'); - } - - public function getOptions() - { - $value = 0; - if ($this->getValue('force')) { - $value |= 2; - } - if ($this->getValue('broadcast')) { - $value |= 1; - } - return $value; + return new CustomNotification( + $this->getAuthorName(), + $this->getValue('comment'), + $this->getValue('forced'), + $this->getValue('broadcast') + ); } }