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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
|
<?php
namespace Icinga\Module\Businessprocess;
use Icinga\Exception\ProgrammingError;
use ipl\Html\Html;
use ipl\Web\Widget\Icon;
abstract class Node
{
const FLAG_DOWNTIME = 1;
const FLAG_ACK = 2;
const FLAG_MISSING = 4;
const FLAG_NONE = 8;
const SHIFT_FLAGS = 4;
const ICINGA_OK = 0;
const ICINGA_WARNING = 1;
const ICINGA_CRITICAL = 2;
const ICINGA_UNKNOWN = 3;
const ICINGA_UP = 0;
const ICINGA_DOWN = 1;
const ICINGA_UNREACHABLE = 2;
const ICINGA_PENDING = 99;
const NODE_EMPTY = 128;
/** @var bool Whether to treat acknowledged hosts/services always as UP/OK */
protected static $ackIsOk = false;
/** @var bool Whether to treat hosts/services in downtime always as UP/OK */
protected static $downtimeIsOk = false;
protected $sortStateToStateMap = array(
4 => self::ICINGA_CRITICAL,
3 => self::ICINGA_UNKNOWN,
2 => self::ICINGA_WARNING,
1 => self::ICINGA_PENDING,
0 => self::ICINGA_OK
);
protected $stateToSortStateMap = array(
self::ICINGA_PENDING => 1,
self::ICINGA_UNKNOWN => 3,
self::ICINGA_CRITICAL => 4,
self::ICINGA_WARNING => 2,
self::ICINGA_OK => 0,
self::NODE_EMPTY => 0
);
/** @var ?string Alias of the node */
protected $alias;
/**
* Main business process object
*
* @var BpConfig
*/
protected $bp;
/**
* Parent nodes
*
* @var array
*/
protected $parents = array();
/**
* Node identifier
*
* @var string
*/
protected $name;
/**
* Node state
*
* @var ?int
*/
protected $state;
/**
* Whether this nodes state has been acknowledged
*
* @var bool
*/
protected $ack;
/**
* Whether this node is in a scheduled downtime
*
* @var bool
*/
protected $downtime;
// obsolete
protected $duration;
/**
* This node's icon
*
* @var ?string
*/
protected $icon;
/**
* Last state change, unix timestamp
*
* @var int
*/
protected $lastStateChange;
protected $missing = false;
protected $empty = false;
protected $className = 'unknown';
protected $stateNames = array(
'OK',
'WARNING',
'CRITICAL',
'UNKNOWN',
99 => 'PENDING',
128 => 'EMPTY'
);
/**
* Set whether to treat acknowledged hosts/services always as UP/OK
*
* @param bool $ackIsOk
*/
public static function setAckIsOk($ackIsOk = true)
{
self::$ackIsOk = $ackIsOk;
}
/**
* Set whether to treat hosts/services in downtime always as UP/OK
*
* @param bool $downtimeIsOk
*/
public static function setDowntimeIsOk($downtimeIsOk = true)
{
self::$downtimeIsOk = $downtimeIsOk;
}
public function setBpConfig(BpConfig $bp)
{
$this->bp = $bp;
return $this;
}
public function getBpConfig()
{
return $this->bp;
}
public function setMissing($missing = true)
{
$this->missing = $missing;
return $this;
}
public function isProblem()
{
return $this->getState() > 0;
}
public function hasBeenChanged()
{
return false;
}
public function isMissing()
{
return $this->missing;
}
public function hasMissingChildren()
{
return count($this->getMissingChildren()) > 0;
}
public function getMissingChildren()
{
return array();
}
public function hasInfoUrl()
{
return false;
}
public function setState($state)
{
$this->state = (int) $state;
$this->missing = false;
return $this;
}
/**
* Forget my state
*
* @return $this
*/
public function clearState()
{
$this->state = null;
return $this;
}
public function setAck($ack = true)
{
$this->ack = $ack;
return $this;
}
public function setDowntime($downtime = true)
{
$this->downtime = $downtime;
return $this;
}
public function getStateName($state = null)
{
$states = $this->enumStateNames();
if ($state === null) {
return $states[ $this->getState() ];
} else {
return $states[ $state ];
}
}
public function enumStateNames()
{
return $this->stateNames;
}
public function getState()
{
if ($this->state === null) {
throw new ProgrammingError(
sprintf(
'Node %s is unable to retrieve it\'s state',
$this->name
)
);
}
return $this->state;
}
public function getSortingState($state = null)
{
if ($state === null) {
$state = $this->getState();
}
if (self::$ackIsOk && $this->isAcknowledged()) {
$state = self::ICINGA_OK;
}
if (self::$downtimeIsOk && $this->isInDowntime()) {
$state = self::ICINGA_OK;
}
$sort = $this->stateToSortState($state);
$sort = ($sort << self::SHIFT_FLAGS)
+ ($this->isInDowntime() ? self::FLAG_DOWNTIME : 0)
+ ($this->isAcknowledged() ? self::FLAG_ACK : 0);
if (! ($sort & (self::FLAG_DOWNTIME | self::FLAG_ACK))) {
$sort |= self::FLAG_NONE;
}
return $sort;
}
public function getLastStateChange()
{
return $this->lastStateChange;
}
public function setLastStateChange($timestamp)
{
$this->lastStateChange = $timestamp;
return $this;
}
public function addParent(Node $parent)
{
$this->parents[] = $parent;
return $this;
}
public function getDuration()
{
return $this->duration;
}
public function isHandled()
{
return $this->isInDowntime() || $this->isAcknowledged();
}
public function isInDowntime()
{
if ($this->downtime === null) {
$this->getState();
}
return $this->downtime;
}
public function isAcknowledged()
{
if ($this->ack === null) {
$this->getState();
}
return $this->ack;
}
public function getChildren($filter = null)
{
return array();
}
public function countChildren($filter = null)
{
return count($this->getChildren($filter));
}
public function hasChildren($filter = null)
{
return $this->countChildren($filter) > 0;
}
public function isEmpty()
{
return $this->countChildren() === 0;
}
public function hasAlias()
{
return $this->alias !== null;
}
/**
* Get the alias of the node
*
* @return ?string
*/
public function getAlias()
{
return $this->alias;
}
/**
* Set the alias of the node
*
* @param string $alias
*
* @return $this
*/
public function setAlias($alias)
{
$this->alias = $alias;
return $this;
}
/**
* Get the label of the node
*
* @return ?string
*/
public function getLabel()
{
return ($this->alias ?? $this->name)
. ' (' . implode(' ยป ', $this->getPaths()[0]) . ')';
}
public function hasParents()
{
return count($this->parents) > 0;
}
public function hasParentName($name)
{
foreach ($this->getParents() as $parent) {
if ($parent->getName() === $name) {
return true;
}
}
return false;
}
public function removeParent($name)
{
$this->parents = array_filter(
$this->parents,
function (BpNode $parent) use ($name) {
return $parent->getName() !== $name;
}
);
return $this;
}
/**
* @return BpNode[]
*/
public function getParents()
{
return $this->parents;
}
/**
* @param BpConfig $rootConfig
*
* @return array
*/
public function getPaths($rootConfig = null)
{
$differentConfig = false;
if ($rootConfig === null) {
$rootConfig = $this->getBpConfig();
} else {
$differentConfig = $this->getBpConfig()->getName() !== $rootConfig->getName();
}
$paths = [];
foreach ($this->parents as $parent) {
foreach ($parent->getPaths($rootConfig) as $path) {
$path[] = $differentConfig ? $this->getIdentifier() : $this->getName();
$paths[] = $path;
}
}
if (! $this instanceof ImportedNode && $this->getBpConfig()->hasRootNode($this->getName())) {
$paths[] = [$differentConfig ? $this->getIdentifier() : $this->getName()];
} elseif (! $this->hasParents()) {
$paths[] = ['__unbound__', $differentConfig ? $this->getIdentifier() : $this->getName()];
}
return $paths;
}
protected function stateToSortState($state)
{
if (array_key_exists($state, $this->stateToSortStateMap)) {
return $this->stateToSortStateMap[$state];
}
throw new ProgrammingError(
'Got invalid state for node %s: %s',
$this->getName(),
var_export($state, true) . var_export($this->stateToSortStateMap, true)
);
}
protected function sortStateTostate($sortState)
{
$sortState = $sortState >> self::SHIFT_FLAGS;
if (array_key_exists($sortState, $this->sortStateToStateMap)) {
return $this->sortStateToStateMap[$sortState];
}
throw new ProgrammingError('Got invalid sorting state %s', $sortState);
}
public function getObjectClassName()
{
return $this->className;
}
public function getLink()
{
return Html::tag('a', ['href' => '#', 'class' => 'toggle'], new Icon('caret-down'));
}
public function getIcon(): Icon
{
return new Icon($this->icon ?? 'circle-exclamation');
}
public function operatorHtml()
{
return ' ';
}
public function getName()
{
return $this->name;
}
/**
* Get the Node operators
*
* @return array
*/
public static function getOperators(): array
{
return [
'&' => t('AND'),
'|' => t('OR'),
'^' => t('XOR'),
'!' => t('NOT'),
'%' => t('DEGRADED'),
'1' => t('MIN 1'),
'2' => t('MIN 2'),
'3' => t('MIN 3'),
'4' => t('MIN 4'),
'5' => t('MIN 5'),
'6' => t('MIN 6'),
'7' => t('MIN 7'),
'8' => t('MIN 8'),
'9' => t('MIN 9'),
];
}
public function getIdentifier()
{
return '@' . $this->getBpConfig()->getName() . ':' . $this->getName();
}
public function __toString()
{
return $this->getName();
}
public function __destruct()
{
unset($this->parents);
}
/**
* Export the node to array
*
* @param array $parent The node's parent. Used to construct the path to the node
* @param bool $flat If false, children will be added to the array key children, else the array will be flat
*
* @return array
*/
public function toArray(array $parent = null, $flat = false)
{
$data = [
'name' => $this->getAlias(),
'state' => $this->getStateName(),
'since' => $this->getLastStateChange(),
'in_downtime' => $this->isInDowntime() ? true : false,
'is_acknowledged' => $this->isAcknowledged() ? true : false
];
if ($parent !== null) {
$data['path'] = $parent['path'] . '!' . $this->getAlias();
} else {
$data['path'] = $this->getAlias();
}
$children = [];
foreach ($this->getChildren() as $node) {
if ($flat) {
$children = array_merge($children, $node->toArray($data, $flat));
} else {
$children[] = $node->toArray($data, $flat);
}
}
if ($flat) {
$data = [$data];
if (! empty($children)) {
$data = array_merge($data, $children);
}
} else {
$data['children'] = $children;
}
return $data;
}
}
|