| 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\Host; |
| 9 |
use ipl\Stdlib\Filter; |
| 10 |
use stdClass; |
| 11 |
|
| 12 |
/** |
| 13 |
* TLVHostNode represents a Host in the tree |
| 14 |
*/ |
| 15 |
class TLVHostNode extends TLVIcingaNode |
| 16 |
{ |
| 17 |
protected $type = 'host'; |
| 18 |
|
| 19 |
protected $key = 'host'; |
| 20 |
|
| 21 |
protected static $titleKey = 'host'; |
| 22 |
|
| 23 |
public function getTitle(): string |
| 24 |
{ |
| 25 |
$key = $this->getKey(); |
| 26 |
$obj = $this->root->getFetched($this->type, $key); |
| 27 |
|
| 28 |
$n = $this->get($this->key); |
| 29 |
|
| 30 |
if (isset($obj->display_name)) { |
| 31 |
$n = $obj->display_name; |
| 32 |
} |
| 33 |
|
| 34 |
return sprintf('%s', $n); |
| 35 |
} |
| 36 |
|
| 37 |
public static function fetch(TLVTree $root): void |
| 38 |
{ |
| 39 |
Benchmark::measure('Begin fetching hosts'); |
| 40 |
|
| 41 |
if (! array_key_exists('host', $root->registeredObjects) or empty($root->registeredObjects['host'])) { |
| 42 |
throw new NotFoundError('No hosts registered to fetch!'); |
| 43 |
} |
| 44 |
|
| 45 |
$hostnameFilter = Filter::any(); |
| 46 |
foreach (array_keys($root->registeredObjects['host']) as $name) { |
| 47 |
$hostnameFilter->add(Filter::equal('name', $name)); |
| 48 |
} |
| 49 |
|
| 50 |
$hosts = Host::on($root->getDb())->with([ |
| 51 |
'state' |
| 52 |
]); |
| 53 |
|
| 54 |
$hosts->filter($hostnameFilter); |
| 55 |
|
| 56 |
foreach ($hosts as $host) { |
| 57 |
// TODO We cannot store the ORM Models with json_encore |
| 58 |
// Thus I'm converting things to objects that can be stored |
| 59 |
// Maybe there's a better way? iterator_to_array does not work. |
| 60 |
$h = new stdClass; |
| 61 |
$h->state = new stdClass; |
| 62 |
$h->display_name = $host->display_name; |
| 63 |
$h->notifications_enabled = $host->notifications_enabled; |
| 64 |
$h->state->hard_state = $host->state->hard_state; |
| 65 |
$h->state->is_flapping = $host->state->is_flapping; |
| 66 |
$h->state->is_handled = $host->state->is_handled; |
| 67 |
$h->state->in_downtime = $host->state->in_downtime; |
| 68 |
|
| 69 |
$root->registeredObjects['host'][$host->name] = $h; |
| 70 |
} |
| 71 |
|
| 72 |
Benchmark::measure('Finished fetching hosts'); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* getStatus returns the current status for the Host |
| 77 |
* |
| 78 |
* @return TLVStatus |
| 79 |
*/ |
| 80 |
public function getStatus(): TLVStatus |
| 81 |
{ |
| 82 |
if ($this->status !== null) { |
| 83 |
return $this->status; |
| 84 |
} |
| 85 |
|
| 86 |
$this->status = $status = new TLVStatus(); |
| 87 |
$key = $this->getKey(); |
| 88 |
|
| 89 |
$host = $this->root->getFetched($this->type, $key); |
| 90 |
|
| 91 |
if ($host === null) { |
| 92 |
$this->status->add('missing', 1); |
| 93 |
return $this->status; |
| 94 |
} |
| 95 |
|
| 96 |
$status->zero(); |
| 97 |
$status->add('total'); |
| 98 |
|
| 99 |
// We only care about the hard state in TLV |
| 100 |
$state = $host->state->hard_state; |
| 101 |
|
| 102 |
// Check if the downtime is enabled if set_downtime_if_notification_disabled is true |
| 103 |
$downtime_if_no_notifications = $host->notifications_enabled === false && |
| 104 |
$this->getRoot()->get('set_downtime_if_notification_disabled'); |
| 105 |
|
| 106 |
if ($host->state->in_downtime || $downtime_if_no_notifications) { |
| 107 |
// Set downtime if notifications are disabled for the host |
| 108 |
$status->add('downtime_active'); |
| 109 |
$state = 10; |
| 110 |
$handled = ''; |
| 111 |
} elseif ($host->state->is_handled || |
| 112 |
$this->getRoot()->get('override_host_problem_to_handled')) { |
| 113 |
// Set the state to handled if it actually is handled, and the option is set to override the state |
| 114 |
$handled = '_handled'; |
| 115 |
} else { |
| 116 |
$handled = '_unhandled'; |
| 117 |
} |
| 118 |
|
| 119 |
if ($state === 0) { |
| 120 |
$status->add('ok'); |
| 121 |
} elseif ($state === 1 || $state === 2) { |
| 122 |
$status->add('critical' . $handled); |
| 123 |
} elseif ($state === 10) { |
| 124 |
$status->add('downtime_handled'); |
| 125 |
} else { |
| 126 |
$status->add('unknown_handled'); |
| 127 |
} |
| 128 |
|
| 129 |
return $this->status; |
| 130 |
} |
| 131 |
} |