diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:31 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:31 +0000 |
commit | f66ab8dae2f3d0418759f81a3a64dc9517a62449 (patch) | |
tree | fbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/CheckPlugin/PluginState.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.tar.xz icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.zip |
Adding upstream version 1.10.2.upstream/1.10.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Director/CheckPlugin/PluginState.php')
-rw-r--r-- | library/Director/CheckPlugin/PluginState.php | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/library/Director/CheckPlugin/PluginState.php b/library/Director/CheckPlugin/PluginState.php new file mode 100644 index 0000000..d68ec70 --- /dev/null +++ b/library/Director/CheckPlugin/PluginState.php @@ -0,0 +1,114 @@ +<?php + +namespace Icinga\Module\Director\CheckPlugin; + +use Icinga\Exception\ProgrammingError; + +class PluginState +{ + protected static $stateCodes = [ + 'UNKNOWN' => 3, + 'CRITICAL' => 2, + 'WARNING' => 1, + 'OK' => 0, + ]; + + protected static $stateNames = [ + 'OK', + 'WARNING', + 'CRITICAL', + 'UNKNOWN', + ]; + + protected static $sortSeverity = [0, 1, 3, 2]; + + /** @var int */ + protected $state; + + public function __construct($state) + { + $this->set($state); + } + + public function isProblem() + { + return $this->state > 0; + } + + public function set($state) + { + $this->state = $this->getNumericStateFor($state); + } + + public function getNumeric() + { + return $this->state; + } + + public function getSortSeverity() + { + return static::getSortSeverityFor($this->getNumeric()); + } + + public function getName() + { + return self::$stateNames[$this->getNumeric()]; + } + + public function raise(PluginState $state) + { + if ($this->getSortSeverity() < $state->getSortSeverity()) { + $this->state = $state->getNumeric(); + } + + return $this; + } + + public static function create($state) + { + return new static($state); + } + + public static function ok() + { + return new static(0); + } + + public static function warning() + { + return new static(1); + } + + public static function critical() + { + return new static(2); + } + + public static function unknown() + { + return new static(3); + } + + protected static function getNumericStateFor($state) + { + if ((is_int($state) || ctype_digit($state)) && $state >= 0 && $state <= 3) { + return (int) $state; + } elseif (is_string($state) && array_key_exists($state, self::$stateCodes)) { + return self::$stateCodes[$state]; + } else { + throw new ProgrammingError('Expected valid state, got: %s', $state); + } + } + + protected static function getSortSeverityFor($state) + { + if (array_key_exists($state, self::$sortSeverity)) { + return self::$sortSeverity[$state]; + } else { + throw new ProgrammingError( + 'Unable to retrieve sort severity for invalid state: %s', + $state + ); + } + } +} |