1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
<?php
/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Monitoring\Command\Renderer;
use Icinga\Module\Monitoring\Backend\MonitoringBackend;
use Icinga\Module\Monitoring\Command\IcingaApiCommand;
use Icinga\Module\Monitoring\Command\Instance\ToggleInstanceFeatureCommand;
use Icinga\Module\Monitoring\Command\Object\AcknowledgeProblemCommand;
use Icinga\Module\Monitoring\Command\Object\AddCommentCommand;
use Icinga\Module\Monitoring\Command\Object\ApiScheduleHostDowntimeCommand;
use Icinga\Module\Monitoring\Command\Object\DeleteCommentCommand;
use Icinga\Module\Monitoring\Command\Object\DeleteDowntimeCommand;
use Icinga\Module\Monitoring\Command\Object\ProcessCheckResultCommand;
use Icinga\Module\Monitoring\Command\Object\PropagateHostDowntimeCommand;
use Icinga\Module\Monitoring\Command\Object\RemoveAcknowledgementCommand;
use Icinga\Module\Monitoring\Command\Object\ScheduleHostDowntimeCommand;
use Icinga\Module\Monitoring\Command\Object\ScheduleServiceCheckCommand;
use Icinga\Module\Monitoring\Command\Object\ScheduleServiceDowntimeCommand;
use Icinga\Module\Monitoring\Command\Object\SendCustomNotificationCommand;
use Icinga\Module\Monitoring\Command\Object\ToggleObjectFeatureCommand;
use Icinga\Module\Monitoring\Command\IcingaCommand;
use Icinga\Module\Monitoring\Object\MonitoredObject;
use InvalidArgumentException;
/**
* Icinga command renderer for the Icinga command file
*/
class IcingaApiCommandRenderer implements IcingaCommandRendererInterface
{
/**
* Name of the Icinga application object
*
* @var string
*/
protected $app = 'app';
/**
* Get the name of the Icinga application object
*
* @return string
*/
public function getApp()
{
return $this->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
*/
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);
}
}
|