icingaweb2-module-businessp.../library/Businessprocess/Node.php

397 lines
7.9 KiB
PHP
Raw Normal View History

<?php
2014-11-30 09:56:58 -05:00
namespace Icinga\Module\Businessprocess;
2015-03-16 04:08:00 -04:00
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Businessprocess\Html\Link;
abstract class Node
{
const FLAG_DOWNTIME = 1;
const FLAG_ACK = 2;
const FLAG_MISSING = 4;
const FLAG_NONE = 8;
const SHIFT_FLAGS = 4;
2015-03-16 04:08:00 -04:00
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;
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,
);
2015-01-28 13:53:55 -05:00
/**
* Main business process object
*
2017-01-11 08:04:45 -05:00
* @var BpConfig
2015-01-28 13:53:55 -05:00
*/
protected $bp;
2015-01-28 13:53:55 -05:00
/**
2015-03-16 04:08:00 -04:00
* Parent nodes
2015-01-28 13:53:55 -05:00
*
2015-03-16 04:08:00 -04:00
* @var array
2015-01-28 13:53:55 -05:00
*/
2015-03-16 04:08:00 -04:00
protected $parents = array();
2015-01-28 13:53:55 -05:00
/**
* Node identifier
*
* @var string
*/
protected $name;
2015-01-28 13:53:55 -05:00
/**
* Node state
*
* @var int
*/
protected $state;
2015-01-28 13:53:55 -05:00
/**
* Whether this nodes state has been acknowledged
*
* @var bool
*/
protected $ack;
2015-01-28 13:53:55 -05:00
/**
* Whether this node is in a scheduled downtime
*
* @var bool
*/
protected $downtime;
2015-01-28 13:53:55 -05:00
// obsolete
protected $duration;
2015-01-28 13:53:55 -05:00
/**
* Last state change, unix timestamp
*
* @var int
*/
protected $lastStateChange;
protected $missing = false;
protected $className = 'unknown';
protected $stateNames = array(
'OK',
'WARNING',
'CRITICAL',
2015-03-16 04:08:00 -04:00
'UNKNOWN',
99 => 'PENDING'
);
2017-01-11 08:04:45 -05:00
abstract public function __construct(BpConfig $bp, $object);
public function setMissing($missing = true)
{
$this->missing = $missing;
return $this;
}
2015-11-19 08:45:33 -05:00
public function isProblem()
2015-11-19 06:58:25 -05:00
{
return $this->getState() > 0;
}
public function hasBeenChanged()
{
return false;
}
public function isMissing()
{
return $this->missing;
}
public function hasMissingChildren()
{
2016-11-22 10:24:34 -05:00
return count($this->getMissingChildren()) > 0;
}
public function getMissingChildren()
{
return array();
}
2014-11-30 05:30:59 -05:00
public function hasInfoUrl()
{
return false;
}
public function setState($state)
{
$this->state = (int) $state;
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) {
2015-03-16 04:08:00 -04:00
throw new ProgrammingError(
sprintf(
'Node %s is unable to retrieve it\'s state',
$this->name
)
);
}
2016-11-29 05:20:54 -05:00
return $this->state;
}
public function getSortingState()
{
$sort = $this->stateToSortState($this->getState());
2015-03-16 04:08:00 -04:00
$sort = ($sort << self::SHIFT_FLAGS)
+ ($this->isInDowntime() ? self::FLAG_DOWNTIME : 0)
+ ($this->isAcknowledged() ? self::FLAG_ACK : 0);
2015-03-16 04:08:00 -04:00
if (! ($sort & (self::FLAG_DOWNTIME | self::FLAG_ACK))) {
$sort |= self::FLAG_NONE;
}
2015-03-16 04:08:00 -04:00
return $sort;
}
2015-01-28 14:01:19 -05:00
public function getLastStateChange()
{
return $this->lastStateChange;
}
public function setLastStateChange($timestamp)
{
$this->lastStateChange = $timestamp;
return $this;
}
2015-03-16 04:08:00 -04:00
public function addParent(Node $parent)
{
2015-03-16 04:08:00 -04:00
$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;
}
2015-10-06 02:59:22 -04:00
public function getChildren($filter = null)
{
2015-10-06 02:59:22 -04:00
return array();
}
2015-10-06 02:59:22 -04:00
public function countChildren($filter = null)
{
2015-10-06 02:59:22 -04:00
return count($this->getChildren($filter));
}
public function hasChildren($filter = null)
{
return $this->countChildren($filter) > 0;
}
public function isEmpty()
{
return $this->countChildren() === 0;
}
2015-02-12 19:54:08 -05:00
public function hasAlias()
{
return false;
}
public function getAlias()
{
return $this->name;
}
2015-03-16 04:08:00 -04:00
public function hasParents()
{
return count($this->parents) > 0;
}
2017-01-03 05:35:09 -05:00
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 BpNode[]
*/
public function getParents()
{
return $this->parents;
}
/**
* @return array
*/
2017-01-03 05:35:09 -05:00
public function getPaths()
{
if ($this->bp->hasRootNode($this->getName())) {
return array(array($this->getName()));
}
$paths = array();
foreach ($this->parents as $parent) {
foreach ($parent->getPaths() as $path) {
// $path[] = $this->getName();
$paths[] = $path;
}
}
2017-01-03 05:35:09 -05:00
// TODO! -> for delete etc
return $paths;
2017-01-03 05:35:09 -05:00
}
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, 1) . var_export($this->stateToSortStateMap, 1)
);
}
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);
2015-03-16 04:08:00 -04:00
}
public function getObjectClassName()
{
return $this->className;
}
/**
* @return Link
*/
public function getLink()
2014-11-30 05:30:59 -05:00
{
return Link::create($this->getAlias(), '#');
2014-11-30 05:30:59 -05:00
}
public function operatorHtml()
{
return '&nbsp;';
}
public function getName()
{
2014-11-30 05:12:03 -05:00
return $this->name;
}
public function __toString()
{
return $this->getName();
}
2014-11-30 05:12:03 -05:00
public function __destruct()
{
unset($this->parents);
}
}