Adjust custom notification handling

refs #4580
This commit is contained in:
Johannes Meyer 2013-09-05 13:30:43 +02:00 committed by Eric Lippmann
parent 52079b2e73
commit 36424b508b
8 changed files with 198 additions and 330 deletions

View File

@ -598,35 +598,6 @@ class CommandPipe
);
}
/**
* Send custom host and/or service notifications
*
* @param array $objects Affected monitoring objects
* @param CustomNotification $notification
*/
public function sendCustomNotification(array $objects, CustomNotification $notification)
{
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;
}
$this->send($command);
}
}
/**
* Disable notifications for all services of the provided hosts
*

View File

@ -1,109 +0,0 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Protocol\Commandpipe;
/**
* Custom notification about hosts or services sent to Icinga's command pipe
*/
class CustomNotification
{
/**
* Notification comment
*
* @var string
*/
private $comment;
/**
* Notification author
*
* @var string
*/
private $author;
/**
* Whether to force the notification to be sent out, regardless of the time restrictions, whether or not
* notifications are enabled, etc.
*
* @var bool
*/
private $forced;
/**
* Whether the notification is sent out to all normal (non-escalated) and escalated contacts
*
* @var bool
*/
private $broadcast;
/**
* 1 = Broadcast (send notification to all normal and all escalated contacts for the host)
*/
const NOTIFY_BROADCAST = 1;
/**
* 2 = Forced (notification is sent out regardless of current time, whether or not notifications are enabled, etc.)
*/
const NOTIFY_FORCED = 2;
/**
* Constructor
*
* @param string $author Notification author
* @param string $comment Notification comment
* @param bool $forced Whether to force the notification to be sent out, regardless of the time
* restrictions, whether or not notifications are enabled, etc.
* @param bool $broadcast Whether the notification is sent out to all normal (non-escalated) and escalated
* contacts
*/
public function __construct($author, $comment, $forced = false, $broadcast = false)
{
$this->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;
}
}

View File

@ -1,163 +0,0 @@
<?php
// {{{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 <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Protocol\Commandpipe;
/**
* Container class containing downtime information
*
*/
class Downtime
{
/**
* Propagate this downtime for all child objects
*/
const TYPE_WITH_CHILDREN = 'AND_PROPAGATE_';
/**
* Propagate this downtime for all child objects as triggered downtime
*/
const TYPE_WITH_CHILDREN_TRIGGERED = 'AND_PROPAGATE_TRIGGERED_';
/**
* Schedule downtime for the services of the given hos
*/
const TYPE_HOST_SVC = 'HOST_SVC';
/**
* Timestamp representing the downtime's start
*
* @var int
*/
public $startTime;
/**
* Timestamp representing the downtime's end
*
* @var int
*/
public $endTime;
/**
* Whether this is a fixed downtime
*
* @var boolean
*/
private $fixed = false;
/**
* The duration of the downtime in seconds if flexible
*
* @var int
*/
public $duration;
/**
* The comment object of the downtime
*
* @var Comment
*/
public $comment;
/**
* The downtime id that triggers this downtime (0 = no triggered downtime)
*
* @var int
*/
public $trigger_id = 0;
/**
* Internal information for the exact type of the downtime
*
* E.g. with children, with children and triggered, services etc.
*
* @var string
*/
private $subtype = '';
/**
* Create a new downtime container
*
* @param int $start A timestamp that defines the downtime's start time
* @param int $end A timestamp that defines the downtime's end time
* @param Comment $comment A comment that will be used when scheduling the downtime
* @param int $duration The duration of this downtime in seconds.
* Duration > 0 will make this a flexible downtime
* @param int $trigger_id An id of the downtime that triggers this downtime.
* 0 means this is not a triggered downtime
*/
public function __construct($start, $end, Comment $comment, $duration = 0, $trigger_id = 0)
{
$this->startTime = $start;
$this->endTime = $end;
$this->comment = $comment;
if ($duration == 0) {
$this->fixed = true;
}
$this->duration = intval($duration);
$this->trigger_id = intval($trigger_id);
}
/**
* Return the SCHEDULE_?_DOWNTIME representing this class for the given $type
*
* @param string $type CommandPipe::TYPE_SERVICE to trigger a service downtime or CommandPipe::TYPE_HOST to
* trigger a host downtime
* @return string A schedule downtime command representing the state of this class
*
*/
public function getFormatString($type)
{
if ($this->subtype == self::TYPE_HOST_SVC) {
$type = "";
}
return 'SCHEDULE_'
. $this->subtype
. $type
. '_DOWNTIME;'
. '%s;'
. ($type == CommandPipe::TYPE_SERVICE ? '%s;' : '')
. $this->startTime . ';'
. $this->endTime . ';'
. ($this->fixed ? '1' : '0') . ';'
. $this->trigger_id . ';'
. $this->duration . ';'
. $this->comment->author . ';'
. $this->comment->content;
}
/**
* Set the exact type of this downtime (see the TYPE_ constants)
*
* @param $type The type of to use for this downtime
*/
public function setType($type)
{
$this->subtype = $type;
}
}

View File

@ -418,7 +418,7 @@ class Monitoring_CommandController extends ActionController
$this->setForm($form);
if ($form->IsSubmittedAndValid() === true) {
$this->target->sendCustomNotification($this->view->objects, $form->getCustomNotification());
$this->target->sendCommand($form->createCommand(), $this->view->objects);
}
}

View File

@ -28,7 +28,8 @@
namespace Icinga\Module\Monitoring\Form\Command;
use \Icinga\Protocol\Commandpipe\CustomNotification;
use Icinga\Protocol\Commandpipe\Comment;
use Icinga\Module\Monitoring\Command\CustomNotificationCommand;
/**
* For for command CustomNotification
@ -97,15 +98,17 @@ class CustomNotificationForm extends CommandForm
}
/**
* Create Custom Notification from request data
* Create the command object to send custom notifications
*
* @return CustomNotification
* @return CustomNotificationCommand
*/
public function getCustomNotification()
public function createCommand()
{
return new CustomNotification(
$this->getAuthorName(),
$this->getValue('comment'),
return new CustomNotificationCommand(
new Comment(
$this->getAuthorName(),
$this->getValue('comment')
),
$this->getValue('forced'),
$this->getValue('broadcast')
);

View File

@ -0,0 +1,165 @@
<?php
// {{{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 <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Monitoring\Command;
use Icinga\Protocol\Commandpipe\Comment;
/**
* Command to send a custom notification
*/
class CustomNotificationCommand extends BaseCommand
{
/**
* The comment associated with this notification
*
* @var Comment
*/
private $comment;
/**
* Whether this notification is forced
*
* @var bool
*/
private $forced;
/**
* Whether this notification is also sent to escalation-contacts
*
* @var bool
*/
private $broadcast;
/**
* Initialise a new custom notification command object
*
* @param Comment $comment The comment for this notification
* @param bool $forced Whether this notificatin is forced
* @param bool $broadcast Whether this notification is sent to all contacts
*/
public function __construct(Comment $comment, $forced = false, $broadcast = false)
{
$this->comment = $comment;
$this->forced = $forced;
$this->broadcast = $broadcast;
}
/**
* Set the comment for this notification
*
* @param Comment $comment
*
* @return self
*/
public function setComment(Comment $comment)
{
$this->comment = $comment;
return $this;
}
/**
* Set whether this notification is forced
*
* @param bool $state
*
* @return self
*/
public function setForced($state)
{
$this->forced = $state;
return $this;
}
/**
* Set whether this notification is sent to all contacts
*
* @param bool $state
*
* @return self
*/
public function setBroadcast($state)
{
$this->broadcast = $state;
return $this;
}
/**
* Return this command's parameters properly arranged in an array
*
* @return array
*
* @see BaseCommand::getParameters()
*/
public function getParameters()
{
$options = 0;
if ($this->forced) {
$options |= 2;
}
if ($this->broadcast) {
$options |= 1;
}
return array_merge(array($options), $this->comment->getParameters(true));
}
/**
* 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
*
* @see BaseCommand::getHostCommand()
*/
public function getHostCommand($hostname)
{
return 'SEND_CUSTOM_HOST_NOTIFICATION;' . implode(';', array_merge(array($hostname), $this->getParameters()));
}
/**
* 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
*
* @see BaseCommand::getServiceCommand()
*/
public function getServiceCommand($hostname, $servicename)
{
return 'SEND_CUSTOM_SVC_NOTIFICATION;' . implode(
';',
array_merge(
array($hostname, $servicename),
$this->getParameters()
)
);
}
}

View File

@ -19,17 +19,16 @@ class CommandPipeLoader extends LibraryLoader {
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/Downtime.php");
require_once("../../library/Icinga/Protocol/Commandpipe/PropertyModifier.php");
require_once("../../library/Icinga/Protocol/Commandpipe/Exception/InvalidCommandException.php");
require_once("../../library/Icinga/Protocol/Commandpipe/Transport/Transport.php");
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');
require_once('../../modules/monitoring/library/Monitoring/Command/AddCommentCommand.php');
require_once('../../modules/monitoring/library/Monitoring/Command/ScheduleDowntimeCommand.php');
require_once('../../modules/monitoring/library/Monitoring/Command/CustomNotificationCommand.php');
}
}
// @codingStandardsIgnoreEnd

View File

@ -10,13 +10,13 @@ CommandPipeLoader::requireLibrary();
use Zend_Config;
use Icinga\Protocol\Commandpipe\Comment;
use Icinga\Protocol\Commandpipe\CustomNotification;
use Icinga\Protocol\Commandpipe\Commandpipe as Commandpipe;
use Icinga\Protocol\Commandpipe\PropertyModifier as MONFLAG;
use Icinga\Protocol\Ldap\Exception;
use Icinga\Module\Monitoring\Command\AcknowledgeCommand;
use Icinga\Module\Monitoring\Command\AddCommentCommand;
use Icinga\Module\Monitoring\Command\ScheduleDowntimeCommand;
use Icinga\Module\Monitoring\Command\CustomNotificationCommand;
if(!defined("EXTCMD_TEST_BIN"))
define("EXTCMD_TEST_BIN", "./bin/extcmd_test");
@ -444,22 +444,23 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase
/**
* Test whether custom servicenotifications are correctly send to the commandpipe without options
*
* @throws \Exception
* @throws Exception
*/
public function testSendCustomServiceNotification()
{
$pipe = $this->getLocalTestPipe();
try {
$notification = new CustomNotification('Author', 'Comment');
$pipe->sendCustomNotification(array(
(object) array(
'host_name' => 'Host',
'service_description' => 'Service'
$notification = new CustomNotificationCommand(new Comment('Author', 'Comment'));
$pipe->sendCommand(
$notification,
array(
(object) array(
'host_name' => 'Host',
'service_description' => 'Service'
)
)
), $notification);
$this->assertCommandSucceeded(
'SEND_CUSTOM_SVC_NOTIFICATION;Host;Service;0;Author;Comment'
);
$this->assertCommandSucceeded('SEND_CUSTOM_SVC_NOTIFICATION;Host;Service;0;Author;Comment');
} catch (Exception $e) {
$this->cleanup();
throw $e;
@ -470,22 +471,23 @@ class CommandPipeTest extends \PHPUnit_Framework_TestCase
/**
* Test whether custom hostnotifications are correctly send to the commandpipe with a varlist of options
*
* @throws \Exception
* @throws Exception
*/
public function testSendCustomHostNotificationWithOptions()
{
$pipe = $this->getLocalTestPipe();
try {
$notification = new CustomNotification('Author', 'Comment', true, true);
$pipe->sendCustomNotification(array(
(object) array(
'host_name' => 'Host',
'service_description' => 'Service'
$notification = new CustomNotificationCommand(new Comment('Author', 'Comment'), true, true);
$pipe->sendCommand(
$notification,
array(
(object) array(
'host_name' => 'Host',
'service_description' => 'Service'
)
)
), $notification);
$this->assertCommandSucceeded(
'SEND_CUSTOM_SVC_NOTIFICATION;Host;Service;3;Author;Comment'
);
$this->assertCommandSucceeded('SEND_CUSTOM_SVC_NOTIFICATION;Host;Service;3;Author;Comment');
} catch (Exception $e) {
$this->cleanup();
throw $e;