From 8ca6cc32b2c789a3149861159ad258f2cb9491e3 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:39:39 +0200 Subject: Adding upstream version 2.11.4. Signed-off-by: Daniel Baumann --- .../Monitoring/Command/IcingaApiCommand.php | 126 ++++++ .../library/Monitoring/Command/IcingaCommand.php | 21 + .../Instance/DisableNotificationsExpireCommand.php | 42 ++ .../Instance/ToggleInstanceFeatureCommand.php | 122 ++++++ .../Command/Object/AcknowledgeProblemCommand.php | 144 +++++++ .../Command/Object/AddCommentCommand.php | 80 ++++ .../Object/ApiScheduleHostDowntimeCommand.php | 40 ++ .../Monitoring/Command/Object/CommandAuthor.php | 38 ++ .../Command/Object/DeleteCommentCommand.php | 110 +++++ .../Command/Object/DeleteDowntimeCommand.php | 110 +++++ .../Monitoring/Command/Object/ObjectCommand.php | 61 +++ .../Command/Object/ProcessCheckResultCommand.php | 176 ++++++++ .../Object/PropagateHostDowntimeCommand.php | 48 +++ .../Object/RemoveAcknowledgementCommand.php | 21 + .../Command/Object/ScheduleHostCheckCommand.php | 48 +++ .../Command/Object/ScheduleHostDowntimeCommand.php | 75 ++++ .../Command/Object/ScheduleServiceCheckCommand.php | 92 ++++ .../Object/ScheduleServiceDowntimeCommand.php | 190 ++++++++ .../Object/SendCustomNotificationCommand.php | 82 ++++ .../Command/Object/ToggleObjectFeatureCommand.php | 113 +++++ .../Command/Object/WithCommentCommand.php | 42 ++ .../Command/Renderer/IcingaApiCommandRenderer.php | 324 ++++++++++++++ .../Renderer/IcingaCommandFileCommandRenderer.php | 478 +++++++++++++++++++++ .../Renderer/IcingaCommandRendererInterface.php | 11 + .../Command/Transport/ApiCommandTransport.php | 291 +++++++++++++ .../Command/Transport/CommandTransport.php | 170 ++++++++ .../Transport/CommandTransportInterface.php | 22 + .../Command/Transport/LocalCommandFile.php | 168 ++++++++ .../Command/Transport/RemoteCommandFile.php | 465 ++++++++++++++++++++ 29 files changed, 3710 insertions(+) create mode 100644 modules/monitoring/library/Monitoring/Command/IcingaApiCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/IcingaCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Instance/DisableNotificationsExpireCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Instance/ToggleInstanceFeatureCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Object/AcknowledgeProblemCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Object/AddCommentCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Object/ApiScheduleHostDowntimeCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Object/CommandAuthor.php create mode 100644 modules/monitoring/library/Monitoring/Command/Object/DeleteCommentCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Object/DeleteDowntimeCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Object/ObjectCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Object/ProcessCheckResultCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Object/PropagateHostDowntimeCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Object/RemoveAcknowledgementCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Object/ScheduleHostCheckCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Object/ScheduleHostDowntimeCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Object/ScheduleServiceCheckCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Object/ScheduleServiceDowntimeCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Object/SendCustomNotificationCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Object/ToggleObjectFeatureCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Object/WithCommentCommand.php create mode 100644 modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php create mode 100644 modules/monitoring/library/Monitoring/Command/Renderer/IcingaCommandFileCommandRenderer.php create mode 100644 modules/monitoring/library/Monitoring/Command/Renderer/IcingaCommandRendererInterface.php create mode 100644 modules/monitoring/library/Monitoring/Command/Transport/ApiCommandTransport.php create mode 100644 modules/monitoring/library/Monitoring/Command/Transport/CommandTransport.php create mode 100644 modules/monitoring/library/Monitoring/Command/Transport/CommandTransportInterface.php create mode 100644 modules/monitoring/library/Monitoring/Command/Transport/LocalCommandFile.php create mode 100644 modules/monitoring/library/Monitoring/Command/Transport/RemoteCommandFile.php (limited to 'modules/monitoring/library/Monitoring/Command') diff --git a/modules/monitoring/library/Monitoring/Command/IcingaApiCommand.php b/modules/monitoring/library/Monitoring/Command/IcingaApiCommand.php new file mode 100644 index 0000000..c33157f --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/IcingaApiCommand.php @@ -0,0 +1,126 @@ +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; + } + + /** + * Get whether another Icinga API command should be sent after this one + * + * @return bool + */ + public function hasNext() + { + return $this->next !== null; + } + + /** + * Get the next Icinga API command + * + * @return IcingaApiCommand + */ + public function getNext() + { + return $this->next; + } + + /** + * Set the next Icinga API command + * + * @param IcingaApiCommand $next + * + * @return IcingaApiCommand + */ + public function setNext(IcingaApiCommand $next) + { + $this->next = $next; + return $next; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/IcingaCommand.php b/modules/monitoring/library/Monitoring/Command/IcingaCommand.php new file mode 100644 index 0000000..49ce586 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/IcingaCommand.php @@ -0,0 +1,21 @@ +expireTime = (int) $expireTime; + return $this; + } + + /** + * Get the date and time when notifications should be re-enabled after disabling + * + * @return int|null Unix timestamp + */ + public function getExpireTime() + { + return $this->expireTime; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Instance/ToggleInstanceFeatureCommand.php b/modules/monitoring/library/Monitoring/Command/Instance/ToggleInstanceFeatureCommand.php new file mode 100644 index 0000000..8a8a8ca --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Instance/ToggleInstanceFeatureCommand.php @@ -0,0 +1,122 @@ +feature = (string) $feature; + return $this; + } + + /** + * Get the feature that is to be enabled or disabled + * + * @return string + */ + public function getFeature() + { + return $this->feature; + } + + /** + * Set whether the feature should be enabled or disabled + * + * @param bool $enabled + * + * @return $this + */ + public function setEnabled($enabled = true) + { + $this->enabled = (bool) $enabled; + return $this; + } + + /** + * Get whether the feature should be enabled or disabled + * + * @return bool + */ + public function getEnabled() + { + return $this->enabled; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Object/AcknowledgeProblemCommand.php b/modules/monitoring/library/Monitoring/Command/Object/AcknowledgeProblemCommand.php new file mode 100644 index 0000000..2001e78 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Object/AcknowledgeProblemCommand.php @@ -0,0 +1,144 @@ +sticky = (bool) $sticky; + return $this; + } + + /** + * Is the acknowledgement sticky? + * + * @return bool + */ + public function getSticky() + { + return $this->sticky; + } + + /** + * Set whether to send a notification about the acknowledgement + * + * @param bool $notify + * + * @return $this + */ + public function setNotify($notify = true) + { + $this->notify = (bool) $notify; + return $this; + } + + /** + * Get whether to send a notification about the acknowledgement + * + * @return bool + */ + public function getNotify() + { + return $this->notify; + } + + /** + * Set whether the comment associated with the acknowledgement is persistent + * + * @param bool $persistent + * + * @return $this + */ + public function setPersistent($persistent = true) + { + $this->persistent = (bool) $persistent; + return $this; + } + + /** + * Is the comment associated with the acknowledgement is persistent? + * + * @return bool + */ + public function getPersistent() + { + return $this->persistent; + } + + /** + * Set the time when the acknowledgement should expire + * + * @param int $expireTime + * + * @return $this + */ + public function setExpireTime($expireTime) + { + $this->expireTime = (int) $expireTime; + return $this; + } + + /** + * Get the time when the acknowledgement should expire + * + * @return int|null + */ + public function getExpireTime() + { + return $this->expireTime; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Object/AddCommentCommand.php b/modules/monitoring/library/Monitoring/Command/Object/AddCommentCommand.php new file mode 100644 index 0000000..9e3151f --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Object/AddCommentCommand.php @@ -0,0 +1,80 @@ +persistent = $persistent; + return $this; + } + + /** + * Is the comment persistent? + * + * @return bool + */ + public function getPersistent() + { + return $this->persistent; + } + + /** + * Set the time when the acknowledgement should expire + * + * @param int $expireTime + * + * @return $this + */ + public function setExpireTime($expireTime) + { + $this->expireTime = (int) $expireTime; + + return $this; + } + + /** + * Get the time when the acknowledgement should expire + * + * @return int|null + */ + public function getExpireTime() + { + return $this->expireTime; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Object/ApiScheduleHostDowntimeCommand.php b/modules/monitoring/library/Monitoring/Command/Object/ApiScheduleHostDowntimeCommand.php new file mode 100644 index 0000000..6495375 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Object/ApiScheduleHostDowntimeCommand.php @@ -0,0 +1,40 @@ += 2.11.0 that + * sends all_services and child_options in a single request + */ +class ApiScheduleHostDowntimeCommand extends ScheduleHostDowntimeCommand +{ + /** @var int Whether no, triggered, or non-triggered child downtimes should be scheduled */ + protected $childOptions; + + protected $forAllServicesNative = true; + + /** + * Get child options, i.e. whether no, triggered, or non-triggered child downtimes should be scheduled + * + * @return int + */ + public function getChildOptions() + { + return $this->childOptions; + } + + /** + * Set child options, i.e. whether no, triggered, or non-triggered child downtimes should be scheduled + * + * @param int $childOptions + * + * @return $this + */ + public function setChildOptions($childOptions) + { + $this->childOptions = $childOptions; + + return $this; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Object/CommandAuthor.php b/modules/monitoring/library/Monitoring/Command/Object/CommandAuthor.php new file mode 100644 index 0000000..577e3df --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Object/CommandAuthor.php @@ -0,0 +1,38 @@ +author = (string) $author; + return $this; + } + + /** + * Get the author + * + * @return string + */ + public function getAuthor() + { + return $this->author; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Object/DeleteCommentCommand.php b/modules/monitoring/library/Monitoring/Command/Object/DeleteCommentCommand.php new file mode 100644 index 0000000..348175a --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Object/DeleteCommentCommand.php @@ -0,0 +1,110 @@ +commentId; + } + + /** + * Set the ID of the comment that is to be deleted + * + * @param int $commentId + * + * @return $this + */ + public function setCommentId($commentId) + { + $this->commentId = (int) $commentId; + 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 + * + * @return boolean + */ + public function getIsService() + { + return $this->isService; + } + + /** + * Set whether the command affects a service comment + * + * @param bool $isService + * + * @return $this + */ + public function setIsService($isService = true) + { + $this->isService = (bool) $isService; + return $this; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Object/DeleteDowntimeCommand.php b/modules/monitoring/library/Monitoring/Command/Object/DeleteDowntimeCommand.php new file mode 100644 index 0000000..a314864 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Object/DeleteDowntimeCommand.php @@ -0,0 +1,110 @@ +downtimeId; + } + + /** + * Set the ID of the downtime that is to be deleted + * + * @param int $downtimeId + * + * @return $this + */ + public function setDowntimeId($downtimeId) + { + $this->downtimeId = (int) $downtimeId; + 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 + * + * @return bool + */ + public function getIsService() + { + return $this->isService; + } + + /** + * Set whether the command affects a service + * + * @param bool $isService + * + * @return $this + */ + public function setIsService($isService = true) + { + $this->isService = (bool) $isService; + return $this; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Object/ObjectCommand.php b/modules/monitoring/library/Monitoring/Command/Object/ObjectCommand.php new file mode 100644 index 0000000..43ab645 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Object/ObjectCommand.php @@ -0,0 +1,61 @@ +assertOneOf($this->allowedObjects); + $this->object = $object; + return $this; + } + + /** + * Get the involved object + * + * @return MonitoredObject + */ + public function getObject() + { + return $this->object; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Object/ProcessCheckResultCommand.php b/modules/monitoring/library/Monitoring/Command/Object/ProcessCheckResultCommand.php new file mode 100644 index 0000000..cd2db33 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Object/ProcessCheckResultCommand.php @@ -0,0 +1,176 @@ + array( + self::HOST_UP, self::HOST_DOWN, self::HOST_UNREACHABLE + ), + self::TYPE_SERVICE => array( + self::SERVICE_OK, self::SERVICE_WARNING, self::SERVICE_CRITICAL, self::SERVICE_UNKNOWN + ) + ); + + /** + * Status code of the host or service check result + * + * @var int + */ + protected $status; + + /** + * Text output of the host or service check result + * + * @var string + */ + protected $output; + + /** + * Optional performance data of the host or service check result + * + * @var string + */ + protected $performanceData; + + + /** + * Set the status code of the host or service check result + * + * @param int $status + * + * @return $this + * + * @throws LogicException If the object is null + * @throws InvalidArgumentException If status is not one of the valid status codes for the object's type + */ + public function setStatus($status) + { + if ($this->object === null) { + throw new LogicException('You\'re required to call setObject() before calling setStatus()'); + } + $status = (int) $status; + if (! in_array($status, self::$statusCodes[$this->object->getType()])) { + throw new InvalidArgumentException(sprintf( + 'The status code %u you provided is not one of the valid status codes for type %s', + $status, + $this->object->getType() + )); + } + $this->status = $status; + return $this; + } + + /** + * Get the status code of the host or service check result + * + * @return int + */ + public function getStatus() + { + return $this->status; + } + + /** + * Set the text output of the host or service check result + * + * @param string $output + * + * @return $this + */ + public function setOutput($output) + { + $this->output = (string) $output; + return $this; + } + + /** + * Get the text output of the host or service check result + * + * @return string + */ + public function getOutput() + { + return $this->output; + } + + /** + * Set the performance data of the host or service check result + * + * @param string $performanceData + * + * @return $this + */ + public function setPerformanceData($performanceData) + { + $this->performanceData = (string) $performanceData; + return $this; + } + + /** + * Get the performance data of the host or service check result + * + * @return string + */ + public function getPerformanceData() + { + return $this->performanceData; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Object/PropagateHostDowntimeCommand.php b/modules/monitoring/library/Monitoring/Command/Object/PropagateHostDowntimeCommand.php new file mode 100644 index 0000000..3fd350c --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Object/PropagateHostDowntimeCommand.php @@ -0,0 +1,48 @@ +triggered = (bool) $triggered; + return $this; + } + + /** + * Get whether the downtime for child hosts are all set to be triggered by this' host downtime + * + * @return bool + */ + public function getTriggered() + { + return $this->triggered; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Object/RemoveAcknowledgementCommand.php b/modules/monitoring/library/Monitoring/Command/Object/RemoveAcknowledgementCommand.php new file mode 100644 index 0000000..31c8180 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Object/RemoveAcknowledgementCommand.php @@ -0,0 +1,21 @@ +ofAllServices = (bool) $ofAllServices; + return $this; + } + + /** + * Get whether to schedule a check of all services associated with a particular host + * + * @return bool + */ + public function getOfAllServices() + { + return $this->ofAllServices; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Object/ScheduleHostDowntimeCommand.php b/modules/monitoring/library/Monitoring/Command/Object/ScheduleHostDowntimeCommand.php new file mode 100644 index 0000000..3ac37d3 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Object/ScheduleHostDowntimeCommand.php @@ -0,0 +1,75 @@ +forAllServices = (bool) $forAllServices; + return $this; + } + + /** + * Get whether to schedule a downtime for all services associated with a particular host + * + * @return bool + */ + public function getForAllServices() + { + return $this->forAllServices; + } + + /** + * Get whether to send the all_services API parameter + * + * @return bool + */ + public function isForAllServicesNative() + { + return $this->forAllServicesNative; + } + + /** + * Get whether to send the all_services API parameter + * + * @param bool $forAllServicesNative + * + * @return $this + */ + public function setForAllServicesNative($forAllServicesNative = true) + { + $this->forAllServicesNative = (bool) $forAllServicesNative; + + return $this; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Object/ScheduleServiceCheckCommand.php b/modules/monitoring/library/Monitoring/Command/Object/ScheduleServiceCheckCommand.php new file mode 100644 index 0000000..8880984 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Object/ScheduleServiceCheckCommand.php @@ -0,0 +1,92 @@ +checkTime = (int) $checkTime; + return $this; + } + + /** + * Get the time when the next check of a host or service is to be scheduled + * + * @return int Unix timestamp + */ + public function getCheckTime() + { + return $this->checkTime; + } + + /** + * Set whether the check is forced + * + * @param bool $forced + * + * @return $this + */ + public function setForced($forced = true) + { + $this->forced = (bool) $forced; + return $this; + } + + /** + * Get whether the check is forced + * + * @return bool + */ + public function getForced() + { + return $this->forced; + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return 'ScheduleCheck'; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Object/ScheduleServiceDowntimeCommand.php b/modules/monitoring/library/Monitoring/Command/Object/ScheduleServiceDowntimeCommand.php new file mode 100644 index 0000000..a023ab5 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Object/ScheduleServiceDowntimeCommand.php @@ -0,0 +1,190 @@ +start = (int) $start; + return $this; + } + + /** + * Get the time when the downtime should start + * + * @return int Unix timestamp + */ + public function getStart() + { + return $this->start; + } + + /** + * Set the time when the downtime should end + * + * @param int $end Unix timestamp + * + * @return $this + */ + public function setEnd($end) + { + $this->end = (int) $end; + return $this; + } + + /** + * Get the time when the downtime should end + * + * @return int Unix timestamp + */ + public function getEnd() + { + return $this->end; + } + + /** + * Set whether it's a fixed or flexible downtime + * + * @param boolean $fixed + * + * @return $this + */ + public function setFixed($fixed = true) + { + $this->fixed = (bool) $fixed; + return $this; + } + + /** + * Is the downtime fixed? + * + * @return boolean + */ + public function getFixed() + { + return $this->fixed; + } + + /** + * Set the ID of the downtime which triggers this downtime + * + * @param int $triggerId + * + * @return $this + */ + public function setTriggerId($triggerId) + { + $this->triggerId = (int) $triggerId; + return $this; + } + + /** + * Get the ID of the downtime which triggers this downtime + * + * @return int|null + */ + public function getTriggerId() + { + return $this->triggerId; + } + + /** + * Set the duration in seconds the downtime must last if it's a flexible downtime + * + * @param int $duration + * + * @return $this + */ + public function setDuration($duration) + { + $this->duration = (int) $duration; + return $this; + } + + /** + * Get the duration in seconds the downtime must last if it's a flexible downtime + * + * @return int|null + */ + public function getDuration() + { + return $this->duration; + } + + /** + * (non-PHPDoc) + * @see \Icinga\Module\Monitoring\Command\Object\IcingaCommand::getName() For the method documentation. + */ + public function getName() + { + return 'ScheduleDowntime'; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Object/SendCustomNotificationCommand.php b/modules/monitoring/library/Monitoring/Command/Object/SendCustomNotificationCommand.php new file mode 100644 index 0000000..ac8889c --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Object/SendCustomNotificationCommand.php @@ -0,0 +1,82 @@ +forced; + } + + /** + * Set whether to force the notification + * + * @param bool $forced + * + * @return $this + */ + public function setForced($forced = true) + { + $this->forced = $forced; + return $this; + } + + /** + * Get whether to broadcast the notification + * + * @return bool + */ + public function getBroadcast() + { + return $this->broadcast; + } + + /** + * Set whether to broadcast the notification + * + * @param bool $broadcast + * + * @return $this + */ + public function setBroadcast($broadcast = true) + { + $this->broadcast = $broadcast; + return $this; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Object/ToggleObjectFeatureCommand.php b/modules/monitoring/library/Monitoring/Command/Object/ToggleObjectFeatureCommand.php new file mode 100644 index 0000000..e3ba8a2 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Object/ToggleObjectFeatureCommand.php @@ -0,0 +1,113 @@ +feature = (string) $feature; + return $this; + } + + /** + * Get the feature that is to be enabled or disabled + * + * @return string + */ + public function getFeature() + { + return $this->feature; + } + + /** + * Set whether the feature should be enabled or disabled + * + * @param bool $enabled + * + * @return $this + */ + public function setEnabled($enabled = true) + { + $this->enabled = (bool) $enabled; + return $this; + } + + /** + * Get whether the feature should be enabled or disabled + * + * @return bool + */ + public function getEnabled() + { + return $this->enabled; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Object/WithCommentCommand.php b/modules/monitoring/library/Monitoring/Command/Object/WithCommentCommand.php new file mode 100644 index 0000000..aa2e439 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Object/WithCommentCommand.php @@ -0,0 +1,42 @@ +comment = (string) $comment; + return $this; + } + + /** + * Get the comment + * + * @return string + */ + public function getComment() + { + return $this->comment; + } +} 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 0000000..8370314 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaApiCommandRenderer.php @@ -0,0 +1,324 @@ +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() + ); + + if ($command->getExpireTime() !== null) { + $data['expiry'] = $command->getExpireTime(); + } + + $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' => $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() + ); + $commandData = $data; + + if ($command instanceof PropagateHostDowntimeCommand) { + $commandData['child_options'] = $command->getTriggered() ? 1 : 2; + } elseif ($command instanceof ApiScheduleHostDowntimeCommand) { + // We assume that it has previously been verified that the Icinga version is + // equal to or greater than 2.11.0 + $commandData['child_options'] = $command->getChildOptions(); + } + + $allServicesCompat = false; + if ($command instanceof ScheduleHostDowntimeCommand) { + if ($command->isForAllServicesNative()) { + // We assume that it has previously been verified that the Icinga version is + // equal to or greater than 2.11.0 + $commandData['all_services'] = $command->getForAllServices(); + } else { + $allServicesCompat = $command->getForAllServices(); + } + } + + $this->applyFilter($commandData, $command->getObject()); + $apiCommand = IcingaApiCommand::create($endpoint, $commandData); + + if ($allServicesCompat) { + $commandData = $data + [ + 'type' => 'Service', + 'filter' => 'host.name == host_name', + 'filter_vars' => [ + 'host_name' => $command->getObject()->getName() + ] + ]; + $apiCommand->setNext(IcingaApiCommand::create($endpoint, $commandData)); + } + + return $apiCommand; + } + + public function renderAcknowledgeProblem(AcknowledgeProblemCommand $command) + { + $endpoint = 'actions/acknowledge-problem'; + $data = array( + 'author' => $command->getAuthor(), + 'comment' => $command->getComment(), + 'sticky' => $command->getSticky(), + 'notify' => $command->getNotify(), + 'persistent' => $command->getPersistent() + ); + if ($command->getExpireTime() !== null) { + $data['expiry'] = $command->getExpireTime(); + } + $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 = [ + 'author' => $command->getAuthor(), + 'comment' => $command->getCommentName() + ]; + return IcingaApiCommand::create($endpoint, $data); + } + + public function renderDeleteDowntime(DeleteDowntimeCommand $command) + { + $endpoint = 'actions/remove-downtime'; + $data = [ + 'author' => $command->getAuthor(), + 'downtime' => $command->getDowntimeName() + ]; + return IcingaApiCommand::create($endpoint, $data); + } + + public function renderRemoveAcknowledgement(RemoveAcknowledgementCommand $command) + { + $endpoint = 'actions/remove-acknowledgement'; + $data = ['author' => $command->getAuthor()]; + $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); + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Renderer/IcingaCommandFileCommandRenderer.php b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaCommandFileCommandRenderer.php new file mode 100644 index 0000000..97d1314 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaCommandFileCommandRenderer.php @@ -0,0 +1,478 @@ +getName(); + if (! method_exists($this, $renderMethod)) { + die($renderMethod); + } + if ($now === null) { + $now = time(); + } + return sprintf('[%u] %s', $now, $this->escape($this->$renderMethod($command))); + } + + public function renderAddComment(AddCommentCommand $command) + { + $object = $command->getObject(); + if ($command->getObject()->getType() === $command::TYPE_HOST) { + /** @var \Icinga\Module\Monitoring\Object\Host $object */ + $commandString = sprintf( + 'ADD_HOST_COMMENT;%s', + $object->getName() + ); + } else { + /** @var \Icinga\Module\Monitoring\Object\Service $object */ + $commandString = sprintf( + 'ADD_SVC_COMMENT;%s;%s', + $object->getHost()->getName(), + $object->getName() + ); + } + return sprintf( + '%s;%u;%s;%s', + $commandString, + $command->getPersistent(), + $command->getAuthor(), + $command->getComment() + ); + } + + public function renderSendCustomNotification(SendCustomNotificationCommand $command) + { + $object = $command->getObject(); + if ($command->getObject()->getType() === $command::TYPE_HOST) { + /** @var \Icinga\Module\Monitoring\Object\Host $object */ + $commandString = sprintf( + 'SEND_CUSTOM_HOST_NOTIFICATION;%s', + $object->getName() + ); + } else { + /** @var \Icinga\Module\Monitoring\Object\Service $object */ + $commandString = sprintf( + 'SEND_CUSTOM_SVC_NOTIFICATION;%s;%s', + $object->getHost()->getName(), + $object->getName() + ); + } + $options = 0; // 0 for no options + if ($command->getBroadcast() === true) { + $options |= 1; + } + if ($command->getForced() === true) { + $options |= 2; + } + return sprintf( + '%s;%u;%s;%s', + $commandString, + $options, + $command->getAuthor(), + $command->getComment() + ); + } + + public function renderProcessCheckResult(ProcessCheckResultCommand $command) + { + $object = $command->getObject(); + if ($command->getObject()->getType() === $command::TYPE_HOST) { + /** @var \Icinga\Module\Monitoring\Object\Host $object */ + $commandString = sprintf( + 'PROCESS_HOST_CHECK_RESULT;%s', + $object->getName() + ); + } else { + /** @var \Icinga\Module\Monitoring\Object\Service $object */ + $commandString = sprintf( + 'PROCESS_SERVICE_CHECK_RESULT;%s;%s', + $object->getHost()->getName(), + $object->getName() + ); + } + $output = $command->getOutput(); + if ($command->getPerformanceData() !== null) { + $output .= '|' . $command->getPerformanceData(); + } + return sprintf( + '%s;%u;%s', + $commandString, + $command->getStatus(), + $output + ); + } + + public function renderScheduleCheck(ScheduleServiceCheckCommand $command) + { + $object = $command->getObject(); + if ($command->getObject()->getType() === $command::TYPE_HOST) { + /** @var \Icinga\Module\Monitoring\Object\Host $object */ + /** @var \Icinga\Module\Monitoring\Command\Object\ScheduleHostCheckCommand $command */ + if ($command->getOfAllServices() === true) { + if ($command->getForced() === true) { + $commandName = 'SCHEDULE_FORCED_HOST_SVC_CHECKS'; + } else { + $commandName = 'SCHEDULE_HOST_SVC_CHECKS'; + } + } else { + if ($command->getForced() === true) { + $commandName = 'SCHEDULE_FORCED_HOST_CHECK'; + } else { + $commandName = 'SCHEDULE_HOST_CHECK'; + } + } + $commandString = sprintf( + '%s;%s', + $commandName, + $object->getName() + ); + } else { + /** @var \Icinga\Module\Monitoring\Object\Service $object */ + $commandString = sprintf( + '%s;%s;%s', + $command->getForced() === true ? 'SCHEDULE_FORCED_SVC_CHECK' : 'SCHEDULE_SVC_CHECK', + $object->getHost()->getName(), + $object->getName() + ); + } + return sprintf( + '%s;%u', + $commandString, + $command->getCheckTime() + ); + } + + public function renderScheduleDowntime(ScheduleServiceDowntimeCommand $command) + { + $object = $command->getObject(); + if ($command->getObject()->getType() === $command::TYPE_HOST) { + /** @var \Icinga\Module\Monitoring\Object\Host $object */ + /** @var \Icinga\Module\Monitoring\Command\Object\ScheduleHostDowntimeCommand $command */ + if ($command instanceof PropagateHostDowntimeCommand) { + /** @var \Icinga\Module\Monitoring\Command\Object\PropagateHostDowntimeCommand $command */ + $commandName = $command->getTriggered() === true ? 'SCHEDULE_AND_PROPAGATE_TRIGGERED_HOST_DOWNTIME' + : 'SCHEDULE_AND_PROPAGATE_HOST_DOWNTIME'; + } elseif ($command->getForAllServices() === true) { + $commandName = 'SCHEDULE_HOST_SVC_DOWNTIME'; + } else { + $commandName = 'SCHEDULE_HOST_DOWNTIME'; + } + $commandString = sprintf( + '%s;%s', + $commandName, + $object->getName() + ); + } else { + /** @var \Icinga\Module\Monitoring\Object\Service $object */ + $commandString = sprintf( + '%s;%s;%s', + 'SCHEDULE_SVC_DOWNTIME', + $object->getHost()->getName(), + $object->getName() + ); + } + return sprintf( + '%s;%u;%u;%u;%u;%u;%s;%s', + $commandString, + $command->getStart(), + $command->getEnd(), + $command->getFixed(), + $command->getTriggerId(), + $command->getDuration(), + $command->getAuthor(), + $command->getComment() + ); + } + + public function renderAcknowledgeProblem(AcknowledgeProblemCommand $command) + { + $object = $command->getObject(); + if ($command->getObject()->getType() === $command::TYPE_HOST) { + /** @var \Icinga\Module\Monitoring\Object\Host $object */ + $commandString = sprintf( + '%s;%s', + $command->getExpireTime() !== null ? 'ACKNOWLEDGE_HOST_PROBLEM_EXPIRE' : 'ACKNOWLEDGE_HOST_PROBLEM', + $object->getName() + ); + } else { + /** @var \Icinga\Module\Monitoring\Object\Service $object */ + $commandString = sprintf( + '%s;%s;%s', + $command->getExpireTime() !== null ? 'ACKNOWLEDGE_SVC_PROBLEM_EXPIRE' : 'ACKNOWLEDGE_SVC_PROBLEM', + $object->getHost()->getName(), + $object->getName() + ); + } + $commandString = sprintf( + '%s;%u;%u;%u', + $commandString, + $command->getSticky() ? 2 : 0, + $command->getNotify(), + $command->getPersistent() + ); + if ($command->getExpireTime() !== null) { + $commandString = sprintf( + '%s;%u', + $commandString, + $command->getExpireTime() + ); + } + return sprintf( + '%s;%s;%s', + $commandString, + $command->getAuthor(), + $command->getComment() + ); + } + + public function renderToggleObjectFeature(ToggleObjectFeatureCommand $command) + { + if ($command->getEnabled() === true) { + $commandPrefix = 'ENABLE'; + } else { + $commandPrefix = 'DISABLE'; + } + switch ($command->getFeature()) { + case ToggleObjectFeatureCommand::FEATURE_ACTIVE_CHECKS: + $commandFormat = sprintf('%s_%%s_CHECK', $commandPrefix); + break; + case ToggleObjectFeatureCommand::FEATURE_PASSIVE_CHECKS: + $commandFormat = sprintf('%s_PASSIVE_%%s_CHECKS', $commandPrefix); + break; + case ToggleObjectFeatureCommand::FEATURE_OBSESSING: + if ($command->getEnabled() === true) { + $commandPrefix = 'START'; + } else { + $commandPrefix = 'STOP'; + } + $commandFormat = sprintf('%s_OBSESSING_OVER_%%s', $commandPrefix); + break; + case ToggleObjectFeatureCommand::FEATURE_NOTIFICATIONS: + $commandFormat = sprintf('%s_%%s_NOTIFICATIONS', $commandPrefix); + break; + case ToggleObjectFeatureCommand::FEATURE_EVENT_HANDLER: + $commandFormat = sprintf('%s_%%s_EVENT_HANDLER', $commandPrefix); + break; + case ToggleObjectFeatureCommand::FEATURE_FLAP_DETECTION: + $commandFormat = sprintf('%s_%%s_FLAP_DETECTION', $commandPrefix); + break; + default: + throw new InvalidArgumentException($command->getFeature()); + } + $object = $command->getObject(); + if ($object->getType() === ToggleObjectFeatureCommand::TYPE_HOST) { + /** @var \Icinga\Module\Monitoring\Object\Host $object */ + $commandString = sprintf( + $commandFormat . ';%s', + 'HOST', + $object->getName() + ); + } else { + /** @var \Icinga\Module\Monitoring\Object\Service $object */ + $commandString = sprintf( + $commandFormat . ';%s;%s', + 'SVC', + $object->getHost()->getName(), + $object->getName() + ); + } + return $commandString; + } + + public function renderDeleteComment(DeleteCommentCommand $command) + { + return sprintf( + '%s;%u', + $command->getIsService() ? 'DEL_SVC_COMMENT' : 'DEL_HOST_COMMENT', + $command->getCommentId() + ); + } + + public function renderDeleteDowntime(DeleteDowntimeCommand $command) + { + return sprintf( + '%s;%u', + $command->getIsService() ? 'DEL_SVC_DOWNTIME' : 'DEL_HOST_DOWNTIME', + $command->getDowntimeId() + ); + } + + public function renderRemoveAcknowledgement(RemoveAcknowledgementCommand $command) + { + $object = $command->getObject(); + if ($command->getObject()->getType() === $command::TYPE_HOST) { + /** @var \Icinga\Module\Monitoring\Object\Host $object */ + $commandString = sprintf( + '%s;%s', + 'REMOVE_HOST_ACKNOWLEDGEMENT', + $object->getName() + ); + } else { + /** @var \Icinga\Module\Monitoring\Object\Service $object */ + $commandString = sprintf( + '%s;%s;%s', + 'REMOVE_SVC_ACKNOWLEDGEMENT', + $object->getHost()->getName(), + $object->getName() + ); + } + return $commandString; + } + + public function renderDisableNotificationsExpire(DisableNotificationsExpireCommand $command) + { + return sprintf( + '%s;%u;%u', + 'DISABLE_NOTIFICATIONS_EXPIRE_TIME', + time(), + $command->getExpireTime() + ); + } + + public function renderToggleInstanceFeature(ToggleInstanceFeatureCommand $command) + { + switch ($command->getFeature()) { + case ToggleInstanceFeatureCommand::FEATURE_ACTIVE_HOST_CHECKS: + case ToggleInstanceFeatureCommand::FEATURE_ACTIVE_SERVICE_CHECKS: + case ToggleInstanceFeatureCommand::FEATURE_HOST_OBSESSING: + case ToggleInstanceFeatureCommand::FEATURE_SERVICE_OBSESSING: + case ToggleInstanceFeatureCommand::FEATURE_PASSIVE_HOST_CHECKS: + case ToggleInstanceFeatureCommand::FEATURE_PASSIVE_SERVICE_CHECKS: + if ($command->getEnabled() === true) { + $commandPrefix = 'START'; + } else { + $commandPrefix = 'STOP'; + } + break; + case ToggleInstanceFeatureCommand::FEATURE_EVENT_HANDLERS: + case ToggleInstanceFeatureCommand::FEATURE_FLAP_DETECTION: + case ToggleInstanceFeatureCommand::FEATURE_NOTIFICATIONS: + case ToggleInstanceFeatureCommand::FEATURE_PERFORMANCE_DATA: + if ($command->getEnabled() === true) { + $commandPrefix = 'ENABLE'; + } else { + $commandPrefix = 'DISABLE'; + } + break; + default: + throw new InvalidArgumentException($command->getFeature()); + } + switch ($command->getFeature()) { + case ToggleInstanceFeatureCommand::FEATURE_ACTIVE_HOST_CHECKS: + $commandString = sprintf( + '%s_%s', + $commandPrefix, + 'EXECUTING_HOST_CHECKS' + ); + break; + case ToggleInstanceFeatureCommand::FEATURE_ACTIVE_SERVICE_CHECKS: + $commandString = sprintf( + '%s_%s', + $commandPrefix, + 'EXECUTING_SVC_CHECKS' + ); + break; + case ToggleInstanceFeatureCommand::FEATURE_EVENT_HANDLERS: + $commandString = sprintf( + '%s_%s', + $commandPrefix, + 'EVENT_HANDLERS' + ); + break; + case ToggleInstanceFeatureCommand::FEATURE_FLAP_DETECTION: + $commandString = sprintf( + '%s_%s', + $commandPrefix, + 'FLAP_DETECTION' + ); + break; + case ToggleInstanceFeatureCommand::FEATURE_NOTIFICATIONS: + $commandString = sprintf( + '%s_%s', + $commandPrefix, + 'NOTIFICATIONS' + ); + break; + case ToggleInstanceFeatureCommand::FEATURE_HOST_OBSESSING: + $commandString = sprintf( + '%s_%s', + $commandPrefix, + 'OBSESSING_OVER_HOST_CHECKS' + ); + break; + case ToggleInstanceFeatureCommand::FEATURE_SERVICE_OBSESSING: + $commandString = sprintf( + '%s_%s', + $commandPrefix, + 'OBSESSING_OVER_SVC_CHECKS' + ); + break; + case ToggleInstanceFeatureCommand::FEATURE_PASSIVE_HOST_CHECKS: + $commandString = sprintf( + '%s_%s', + $commandPrefix, + 'ACCEPTING_PASSIVE_HOST_CHECKS' + ); + break; + case ToggleInstanceFeatureCommand::FEATURE_PASSIVE_SERVICE_CHECKS: + $commandString = sprintf( + '%s_%s', + $commandPrefix, + 'ACCEPTING_PASSIVE_SVC_CHECKS' + ); + break; + case ToggleInstanceFeatureCommand::FEATURE_PERFORMANCE_DATA: + $commandString = sprintf( + '%s_%s', + $commandPrefix, + 'PERFORMANCE_DATA' + ); + break; + default: + throw new InvalidArgumentException($command->getFeature()); + } + return $commandString; + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Renderer/IcingaCommandRendererInterface.php b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaCommandRendererInterface.php new file mode 100644 index 0000000..e3ef6ba --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaCommandRendererInterface.php @@ -0,0 +1,11 @@ +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); + } + + protected function sendCommand(IcingaApiCommand $command) + { + Logger::debug( + 'Sending Icinga command "%s" to the API "%s:%u"', + $command->getEndpoint(), + $this->getHost(), + $this->getPort() + ); + + $data = $command->getData(); + $payload = Json::encode($data); + AuditHook::logActivity( + 'monitoring/command', + "Issued command {$command->getEndpoint()} with the following payload: $payload", + $data + ); + + try { + $response = RestRequest::post($this->getUriFor($command->getEndpoint())) + ->authenticateWith($this->getUsername(), $this->getPassword()) + ->sendJson() + ->noStrictSsl() + ->setPayload($command->getData()) + ->send(); + } catch (JsonDecodeException $e) { + throw new CommandTransportException( + 'Got invalid JSON response from the Icinga 2 API: %s', + $e->getMessage() + ); + } + + 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 (! empty($result) + && ($result['code'] < 200 || $result['code'] >= 300) + ) { + throw new CommandTransportException( + 'Can\'t send external Icinga command: %u %s', + $result['code'], + $result['status'] + ); + } + if ($command->hasNext()) { + $this->sendCommand($command->getNext()); + } + } + + /** + * 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) + { + $this->sendCommand($this->renderer->render($command)); + } + + /** + * Try to connect to the API + * + * @throws CommandTransportException In case of failure + */ + public function probe() + { + $request = RestRequest::get($this->getUriFor(null)) + ->authenticateWith($this->getUsername(), $this->getPassword()) + ->noStrictSsl(); + + try { + $response = $request->send(); + } catch (CurlException $e) { + throw new CommandTransportException( + 'Couldn\'t connect to the Icinga 2 API: %s', + $e->getMessage() + ); + } catch (JsonDecodeException $e) { + throw new CommandTransportException( + 'Got invalid JSON response from the Icinga 2 API: %s', + $e->getMessage() + ); + } + + if (isset($response['error'])) { + throw new CommandTransportException( + 'Can\'t connect to the Icinga 2 API: %u %s', + $response['error'], + $response['status'] + ); + } + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Transport/CommandTransport.php b/modules/monitoring/library/Monitoring/Command/Transport/CommandTransport.php new file mode 100644 index 0000000..aa47547 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Transport/CommandTransport.php @@ -0,0 +1,170 @@ +isEmpty()) { + throw new ConfigurationError( + mt('monitoring', 'No command transports have been configured in "%s".'), + $config->getConfigFile() + ); + } + + static::$config = $config; + } + + return static::$config; + } + + /** + * Create a transport from config + * + * @param ConfigObject $config + * + * @return LocalCommandFile|RemoteCommandFile + * + * @throws ConfigurationError + */ + public static function createTransport(ConfigObject $config) + { + $config = clone $config; + switch (strtolower($config->transport)) { + 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(); + break; + default: + throw new ConfigurationError( + mt( + 'monitoring', + 'Cannot create command transport "%s". Invalid transport' + . ' defined in "%s". Use one of "%s", "%s" or "%s".' + ), + $config->transport, + static::getConfig()->getConfigFile(), + LocalCommandFile::TRANSPORT, + RemoteCommandFile::TRANSPORT, + ApiCommandTransport::TRANSPORT + ); + } + + unset($config->transport); + foreach ($config as $key => $value) { + $method = 'set' . ucfirst($key); + if (! method_exists($transport, $method)) { + // Ignore settings from config that don't have a setter on the transport instead of throwing an + // exception here because the transport should throw an exception if it's not fully set up + // when being about to send a command + continue; + } + + $transport->$method($value); + } + + return $transport; + } + + /** + * Send the given command over an appropriate Icinga command transport + * + * This will try one configured transport after another until the command has been successfully sent. + * + * @param IcingaCommand $command The command to send + * @param int|null $now Timestamp of the command or null for now + * + * @throws CommandTransportException If sending the Icinga command failed + */ + public function send(IcingaCommand $command, $now = null) + { + $errors = array(); + + foreach (static::getConfig() as $name => $transportConfig) { + $transport = static::createTransport($transportConfig); + if ($this->transferPossible($command, $transport)) { + try { + $transport->send($command, $now); + } catch (Exception $e) { + Logger::error($e); + $errors[] = sprintf('%s: %s.', $name, rtrim($e->getMessage(), '.')); + continue; // Try the next transport + } + + return; // The command was successfully sent + } + } + + if (! empty($errors)) { + throw new CommandTransportException(implode("\n", $errors)); + } + + throw new CommandTransportException( + mt( + 'monitoring', + 'Failed to send external Icinga command. No transport has been configured' + . ' for this instance. Please contact your Icinga Web administrator.' + ) + ); + } + + /** + * Return whether it is possible to send the given command using the given transport + * + * @param IcingaCommand $command + * @param CommandTransportInterface $transport + * + * @return bool + */ + protected function transferPossible($command, $transport) + { + if (! method_exists($transport, 'getInstance') || !$command instanceof ObjectCommand) { + return true; + } + + $transportInstance = $transport->getInstance(); + if (! $transportInstance || $transportInstance === 'none') { + return true; + } + + return strtolower($transportInstance) === strtolower($command->getObject()->instance_name); + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Transport/CommandTransportInterface.php b/modules/monitoring/library/Monitoring/Command/Transport/CommandTransportInterface.php new file mode 100644 index 0000000..e9cb086 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Transport/CommandTransportInterface.php @@ -0,0 +1,22 @@ +renderer = new IcingaCommandFileCommandRenderer(); + } + + /** + * Set the name of the Icinga instance this transport will transfer commands to + * + * @param string $name + * + * @return $this + */ + public function setInstance($name) + { + $this->instanceName = $name; + return $this; + } + + /** + * Return the name of the Icinga instance this transport will transfer commands to + * + * @return string + */ + public function getInstance() + { + return $this->instanceName; + } + + /** + * Set the path to the local Icinga command file + * + * @param string $path + * + * @return $this + */ + public function setPath($path) + { + $this->path = (string) $path; + return $this; + } + + /** + * Get the path to the local Icinga command file + * + * @return string + */ + public function getPath() + { + return $this->path; + } + + /** + * Set the mode used to open the icinga command file + * + * @param string $openMode + * + * @return $this + */ + public function setOpenMode($openMode) + { + $this->openMode = (string) $openMode; + return $this; + } + + /** + * Get the mode used to open the icinga command file + * + * @return string + */ + public function getOpenMode() + { + return $this->openMode; + } + + /** + * Write the command to the local Icinga command file + * + * @param IcingaCommand $command + * @param int|null $now + * + * @throws ConfigurationError + * @throws CommandTransportException + */ + public function send(IcingaCommand $command, $now = null) + { + if (! isset($this->path)) { + throw new ConfigurationError( + 'Can\'t send external Icinga Command. Path to the local command file is missing' + ); + } + $commandString = $this->renderer->render($command, $now); + Logger::debug( + 'Sending external Icinga command "%s" to the local command file "%s"', + $commandString, + $this->path + ); + try { + $file = new File($this->path, $this->openMode); + $file->fwrite($commandString . "\n"); + } catch (Exception $e) { + $message = $e->getMessage(); + if ($e instanceof RuntimeException && ($pos = strrpos($message, ':')) !== false) { + // Assume RuntimeException thrown by SplFileObject in the format: __METHOD__ . "({$filename}): Message" + $message = substr($message, $pos + 1); + } + throw new CommandTransportException( + 'Can\'t send external Icinga command to the local command file "%s": %s', + $this->path, + $message + ); + } + } +} diff --git a/modules/monitoring/library/Monitoring/Command/Transport/RemoteCommandFile.php b/modules/monitoring/library/Monitoring/Command/Transport/RemoteCommandFile.php new file mode 100644 index 0000000..5426bb9 --- /dev/null +++ b/modules/monitoring/library/Monitoring/Command/Transport/RemoteCommandFile.php @@ -0,0 +1,465 @@ +renderer = new IcingaCommandFileCommandRenderer(); + } + + /** + * Set the name of the Icinga instance this transport will transfer commands to + * + * @param string $name + * + * @return $this + */ + public function setInstance($name) + { + $this->instanceName = $name; + return $this; + } + + /** + * Return the name of the Icinga instance this transport will transfer commands to + * + * @return string + */ + public function getInstance() + { + return $this->instanceName; + } + + /** + * Set the remote host + * + * @param string $host + * + * @return $this + */ + public function setHost($host) + { + $this->host = (string) $host; + return $this; + } + + /** + * Get the remote host + * + * @return string + */ + public function getHost() + { + return $this->host; + } + + /** + * Set the port to connect to on the remote host + * + * @param int $port + * + * @return $this + */ + public function setPort($port) + { + $this->port = (int) $port; + return $this; + } + + /** + * Get the port to connect on the remote host + * + * @return int + */ + public function getPort() + { + return $this->port; + } + + /** + * Set the user to log in as on the remote host + * + * @param string $user + * + * @return $this + */ + public function setUser($user) + { + $this->user = (string) $user; + return $this; + } + + /** + * Get the user to log in as on the remote host + * + * Defaults to current PHP process' user + * + * @return string|null + */ + public function getUser() + { + return $this->user; + } + + /** + * Set the path to the private key file + * + * @param string $privateKey + * + * @return $this + */ + public function setPrivateKey($privateKey) + { + $this->privateKey = (string) $privateKey; + return $this; + } + + /** + * Get the path to the private key + * + * @return string + */ + public function getPrivateKey() + { + return $this->privateKey; + } + + /** + * Use a given resource to set the user and the key + * + * @param string + * + * @throws ConfigurationError + */ + public function setResource($resource = null) + { + $config = ResourceFactory::getResourceConfig($resource); + + if (! isset($config->user)) { + throw new ConfigurationError( + t("Can't send external Icinga Command. Remote user is missing") + ); + } + if (! isset($config->private_key)) { + throw new ConfigurationError( + t("Can't send external Icinga Command. The private key for the remote user is missing") + ); + } + + $this->setUser($config->user); + $this->setPrivateKey($config->private_key); + } + + /** + * Set the path to the Icinga command file on the remote host + * + * @param string $path + * + * @return $this + */ + public function setPath($path) + { + $this->path = (string) $path; + return $this; + } + + /** + * Get the path to the Icinga command file on the remote host + * + * @return string + */ + public function getPath() + { + return $this->path; + } + + /** + * Write the command to the Icinga command file on the remote host + * + * @param IcingaCommand $command + * @param int|null $now + * + * @throws ConfigurationError + * @throws CommandTransportException + */ + public function send(IcingaCommand $command, $now = null) + { + if (! isset($this->path)) { + throw new ConfigurationError( + 'Can\'t send external Icinga Command. Path to the remote command file is missing' + ); + } + if (! isset($this->host)) { + throw new ConfigurationError('Can\'t send external Icinga Command. Remote host is missing'); + } + $commandString = $this->renderer->render($command, $now); + Logger::debug( + 'Sending external Icinga command "%s" to the remote command file "%s:%u%s"', + $commandString, + $this->host, + $this->port, + $this->path + ); + return $this->sendCommandString($commandString); + } + + /** + * Get the SSH command + * + * @return string + */ + protected function sshCommand() + { + $cmd = sprintf( + 'exec ssh -o BatchMode=yes -p %u', + $this->port + ); + // -o BatchMode=yes for disabling interactive authentication methods + + if (isset($this->user)) { + $cmd .= ' -l ' . escapeshellarg($this->user); + } + + if (isset($this->privateKey)) { + // TODO: StrictHostKeyChecking=no for compat only, must be removed + $cmd .= ' -o StrictHostKeyChecking=no' + . ' -i ' . escapeshellarg($this->privateKey); + } + + $cmd .= sprintf( + ' %s "cat > %s"', + escapeshellarg($this->host), + escapeshellarg($this->path) + ); + + return $cmd; + } + + /** + * Send the command over SSH + * + * @param string $commandString + * + * @throws CommandTransportException + */ + protected function sendCommandString($commandString) + { + if ($this->isSshAlive()) { + $ret = fwrite($this->sshPipes[0], $commandString . "\n"); + if ($ret === false) { + $this->throwSshFailure('Cannot write to the remote command pipe'); + } elseif ($ret !== strlen($commandString) + 1) { + $this->throwSshFailure( + 'Failed to write the whole command to the remote command pipe' + ); + } + } else { + $this->throwSshFailure(); + } + } + + /** + * Get the pipes of the SSH subprocess + * + * @return array + */ + protected function getSshPipes() + { + if ($this->sshPipes === null) { + $this->forkSsh(); + } + + return $this->sshPipes; + } + + /** + * Get the SSH subprocess + * + * @return resource + */ + protected function getSshProcess() + { + if ($this->sshProcess === null) { + $this->forkSsh(); + } + + return $this->sshProcess; + } + + /** + * Get the status of the SSH subprocess + * + * @param string $what + * + * @return mixed + */ + protected function getSshProcessStatus($what = null) + { + $status = proc_get_status($this->getSshProcess()); + if ($what === null) { + return $status; + } else { + return $status[$what]; + } + } + + /** + * Get whether the SSH subprocess is alive + * + * @return bool + */ + protected function isSshAlive() + { + return $this->getSshProcessStatus('running'); + } + + /** + * Fork SSH subprocess + * + * @throws CommandTransportException If fork fails + */ + protected function forkSsh() + { + $descriptors = array( + 0 => array('pipe', 'r'), + 1 => array('pipe', 'w'), + 2 => array('pipe', 'w') + ); + + $this->sshProcess = proc_open($this->sshCommand(), $descriptors, $this->sshPipes); + + if (! is_resource($this->sshProcess)) { + throw new CommandTransportException( + 'Can\'t send external Icinga command: Failed to fork SSH' + ); + } + } + + /** + * Read from STDERR + * + * @return string + */ + protected function readStderr() + { + return stream_get_contents($this->sshPipes[2]); + } + + /** + * Throw SSH failure + * + * @param string $msg + * + * @throws CommandTransportException + */ + protected function throwSshFailure($msg = 'Can\'t send external Icinga command') + { + throw new CommandTransportException( + '%s: %s', + $msg, + $this->readStderr() . var_export($this->getSshProcessStatus(), true) + ); + } + + /** + * Close SSH pipes and SSH subprocess + */ + public function __destruct() + { + if (is_resource($this->sshProcess)) { + fclose($this->sshPipes[0]); + fclose($this->sshPipes[1]); + fclose($this->sshPipes[2]); + + proc_close($this->sshProcess); + } + } +} -- cgit v1.2.3