| 1 |
<?php |
| 2 |
/* Copyright (C) 2017 Icinga Development Team <[email protected]> */ |
| 3 |
|
| 4 |
namespace Icinga\Module\Toplevelview\Tree; |
| 5 |
|
| 6 |
/** |
| 7 |
* TLVStatus represents the status for a TLVTreeNode that is shown in the view |
| 8 |
*/ |
| 9 |
class TLVStatus |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Properties track each tree nodes Icinga states |
| 13 |
*/ |
| 14 |
protected $properties = [ |
| 15 |
'critical_unhandled' => null, |
| 16 |
'critical_handled' => null, |
| 17 |
'warning_unhandled' => null, |
| 18 |
'warning_handled' => null, |
| 19 |
'unknown_unhandled' => null, |
| 20 |
'unknown_handled' => null, |
| 21 |
'downtime_handled' => null, |
| 22 |
'downtime_active' => null, |
| 23 |
'ok' => null, |
| 24 |
'missing' => null, |
| 25 |
'total' => null, |
| 26 |
]; |
| 27 |
|
| 28 |
/** |
| 29 |
* statusPriority decribes the priority from worst to best |
| 30 |
*/ |
| 31 |
protected static $statusPriority = [ |
| 32 |
'critical_unhandled', |
| 33 |
'warning_unhandled', |
| 34 |
'unknown_unhandled', |
| 35 |
'downtime_handled', |
| 36 |
'critical_handled', |
| 37 |
'warning_handled', |
| 38 |
'unknown_handled', |
| 39 |
'ok', |
| 40 |
'missing', |
| 41 |
]; |
| 42 |
|
| 43 |
/** |
| 44 |
* meta tracks get overall count of hosts and services if this status object |
| 45 |
*/ |
| 46 |
protected $meta = []; |
| 47 |
|
| 48 |
/** |
| 49 |
* merge merges another TLVStatus object's properties into this object |
| 50 |
*/ |
| 51 |
public function merge(TLVStatus $status) |
| 52 |
{ |
| 53 |
$properties = $status->getProperties(); |
| 54 |
foreach (array_keys($this->properties) as $key) { |
| 55 |
if ($this->properties[$key] === null) { |
| 56 |
$this->properties[$key] = $properties[$key]; |
| 57 |
} else { |
| 58 |
$this->properties[$key] += $properties[$key]; |
| 59 |
} |
| 60 |
} |
| 61 |
return $this; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* get returns the given key's value from the properties |
| 66 |
* |
| 67 |
* @param string $key key of the property |
| 68 |
*/ |
| 69 |
public function get($key) |
| 70 |
{ |
| 71 |
return $this->properties[$key]; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* set sets the given key/value in the properties |
| 76 |
* |
| 77 |
* @param string $key key of the property |
| 78 |
* @param int $value value to set to property to |
| 79 |
*/ |
| 80 |
public function set($key, $value) |
| 81 |
{ |
| 82 |
$this->properties[$key] = (int) $value; |
| 83 |
return $this; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* getProperties returns all properties |
| 88 |
*/ |
| 89 |
public function getProperties() |
| 90 |
{ |
| 91 |
return $this->properties; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* add adds the given value (integer) to the given property |
| 96 |
* |
| 97 |
* @param string $key key of the property |
| 98 |
* @param int $value value to add to the property |
| 99 |
*/ |
| 100 |
public function add($key, $value = 1) |
| 101 |
{ |
| 102 |
if ($this->properties[$key] === null) { |
| 103 |
$this->properties[$key] = 0; |
| 104 |
} |
| 105 |
$this->properties[$key] += (int) $value; |
| 106 |
return $this; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* zero sets all properties to zero (0) |
| 111 |
*/ |
| 112 |
public function zero() |
| 113 |
{ |
| 114 |
foreach (array_keys($this->properties) as $key) { |
| 115 |
$this->properties[$key] = 0; |
| 116 |
} |
| 117 |
return $this; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* getOverall returns the worst state of this TLVStatus, |
| 122 |
* given the statusPriority. |
| 123 |
* |
| 124 |
* @return string |
| 125 |
*/ |
| 126 |
public function getOverall(): string |
| 127 |
{ |
| 128 |
foreach (static::$statusPriority as $key) { |
| 129 |
if ($this->properties[$key] !== null && $this->properties[$key] > 0) { |
| 130 |
return $this->cssFriendly($key); |
| 131 |
} |
| 132 |
} |
| 133 |
return 'missing'; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* cssFriendly transforms the given key to be CSS friendly, |
| 138 |
* meaning using spaces between the state and the handled indicator |
| 139 |
*/ |
| 140 |
protected function cssFriendly($key): string |
| 141 |
{ |
| 142 |
return str_replace('_', ' ', $key); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* getMeta returns the given key's value from the metadata |
| 147 |
*/ |
| 148 |
public function getMeta($key) |
| 149 |
{ |
| 150 |
if (array_key_exists($key, $this->meta)) { |
| 151 |
return $this->meta[$key]; |
| 152 |
} else { |
| 153 |
return null; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* setMeta sets the given key/value in the metadata |
| 159 |
*/ |
| 160 |
public function setMeta($key, $value) |
| 161 |
{ |
| 162 |
$this->meta[$key] = $value; |
| 163 |
} |
| 164 |
} |