| 1 |
<?php |
| 2 |
/* Copyright (C) 2017 Icinga Development Team <[email protected]> */ |
| 3 |
|
| 4 |
namespace Icinga\Module\Toplevelview\Tree; |
| 5 |
|
| 6 |
use Icinga\Application\Benchmark; |
| 7 |
use Icinga\Exception\NotFoundError; |
| 8 |
use Icinga\Module\Icingadb\Model\Service; |
| 9 |
use ipl\Stdlib\Filter; |
| 10 |
use stdClass; |
| 11 |
|
| 12 |
/** |
| 13 |
* TLVServiceNode represents a Service in the tree |
| 14 |
*/ |
| 15 |
class TLVServiceNode extends TLVIcingaNode |
| 16 |
{ |
| 17 |
protected $type = 'service'; |
| 18 |
|
| 19 |
protected $key = '{host}!{service}'; |
| 20 |
|
| 21 |
public function getTitle(): string |
| 22 |
{ |
| 23 |
$key = $this->getKey(); |
| 24 |
$obj = $this->root->getFetched($this->type, $key); |
| 25 |
|
| 26 |
$name = $this->get($this->type); |
| 27 |
$hostname = $this->get('host'); |
| 28 |
|
| 29 |
if (isset($obj->display_name)) { |
| 30 |
$name = $obj->display_name; |
| 31 |
} |
| 32 |
|
| 33 |
if (isset($obj->display_name)) { |
| 34 |
$hostname = $obj->host->display_name; |
| 35 |
} |
| 36 |
|
| 37 |
return sprintf('%s: %s', $hostname, $name); |
| 38 |
} |
| 39 |
|
| 40 |
public function register() |
| 41 |
{ |
| 42 |
// also register host, because that's what we fetch data with |
| 43 |
$hostDummy = new TLVHostNode(); |
| 44 |
$this->root->registerObject($hostDummy->getType(), $this->get('host'), get_class($hostDummy)); |
| 45 |
|
| 46 |
// register myself |
| 47 |
return parent::register(); |
| 48 |
} |
| 49 |
|
| 50 |
public function getKey(): string |
| 51 |
{ |
| 52 |
return sprintf('%s!%s', $this->properties['host'], $this->properties['service']); |
| 53 |
} |
| 54 |
|
| 55 |
public static function fetch(TLVTree $root): void |
| 56 |
{ |
| 57 |
Benchmark::measure('Begin fetching services'); |
| 58 |
|
| 59 |
if (! array_key_exists('service', $root->registeredObjects) or empty($root->registeredObjects['service'])) { |
| 60 |
throw new NotFoundError('No services registered to fetch!'); |
| 61 |
} |
| 62 |
|
| 63 |
$hostnameFilter = Filter::any(); |
| 64 |
|
| 65 |
foreach (array_keys($root->registeredObjects['host']) as $name) { |
| 66 |
$hostnameFilter->add(Filter::equal('host.name', $name)); |
| 67 |
} |
| 68 |
|
| 69 |
$services = Service::on($root->getDb())->with([ |
| 70 |
'host', |
| 71 |
'state' |
| 72 |
]); |
| 73 |
|
| 74 |
$services->filter($hostnameFilter); |
| 75 |
|
| 76 |
foreach ($services as $service) { |
| 77 |
// TODO We cannot store the ORM Models with json_encore |
| 78 |
// Thus I'm converting things to objects that can be stored |
| 79 |
// Maybe there's a better way? iterator_to_array does not work. |
| 80 |
$s = new stdClass; |
| 81 |
$s->state = new stdClass; |
| 82 |
$s->host = new stdClass; |
| 83 |
$s->name = $service->name; |
| 84 |
$s->display_name = $service->display_name; |
| 85 |
$s->notifications_enabled = $service->notifications_enabled; |
| 86 |
$s->host->display_name = $service->host->display_name; |
| 87 |
$s->state->hard_state = $service->state->hard_state; |
| 88 |
$s->state->is_flapping = $service->state->is_flapping; |
| 89 |
$s->state->is_handled = $service->state->is_handled; |
| 90 |
$s->state->in_downtime = $service->state->in_downtime; |
| 91 |
|
| 92 |
$key = sprintf('%s!%s', $service->host->name, $service->name); |
| 93 |
if (array_key_exists($key, $root->registeredObjects['service'])) { |
| 94 |
$root->registeredObjects['service'][$key] = $s; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
Benchmark::measure('Finished fetching services'); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* getStatus returns the current status for the Service |
| 103 |
* |
| 104 |
* @return TLVStatus |
| 105 |
*/ |
| 106 |
public function getStatus(): TLVStatus |
| 107 |
{ |
| 108 |
if ($this->status !== null) { |
| 109 |
return $this->status; |
| 110 |
} |
| 111 |
|
| 112 |
$this->status = $status = new TLVStatus(); |
| 113 |
$key = $this->getKey(); |
| 114 |
|
| 115 |
$service = $this->root->getFetched($this->type, $key); |
| 116 |
|
| 117 |
if ($service === null) { |
| 118 |
$status->add('missing', 1); |
| 119 |
return $this->status; |
| 120 |
} |
| 121 |
|
| 122 |
$status->zero(); |
| 123 |
$status->add('total'); |
| 124 |
|
| 125 |
// We only care about the hard state in TLV |
| 126 |
$state = $service->state->hard_state; |
| 127 |
|
| 128 |
// Get the service's current handled state |
| 129 |
$isHandled = $service->state->is_handled; |
| 130 |
|
| 131 |
// In TLV flapping means the state is handled |
| 132 |
$isHandled = $isHandled || $service->state->is_flapping; |
| 133 |
|
| 134 |
// Check if the downtime is enabled if set_downtime_if_notification_disabled is true |
| 135 |
$downtime_if_no_notifications = $service->notifications_enabled === false && |
| 136 |
$this->getRoot()->get('set_downtime_if_notification_disabled'); |
| 137 |
|
| 138 |
if ($service->state->in_downtime || $downtime_if_no_notifications) { |
| 139 |
$status->add('downtime_active'); |
| 140 |
if ($state !== 0) { |
| 141 |
$state = 10; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
if ($isHandled) { |
| 146 |
$handled = '_handled'; |
| 147 |
} else { |
| 148 |
$handled = '_unhandled'; |
| 149 |
} |
| 150 |
|
| 151 |
if ($state === 0 || $state === 99) { |
| 152 |
$status->add('ok', 1); |
| 153 |
} elseif ($state === 1) { |
| 154 |
$status->add('warning' . $handled, 1); |
| 155 |
} elseif ($state === 2) { |
| 156 |
$status->add('critical' . $handled, 1); |
| 157 |
} elseif ($state === 10) { |
| 158 |
$status->add('downtime_handled'); |
| 159 |
} else { |
| 160 |
$status->add('unknown' . $handled, 1); |
| 161 |
} |
| 162 |
|
| 163 |
return $this->status; |
| 164 |
} |
| 165 |
} |