Debian Codegraph @

library/Toplevelview/Tree/TLVHostGroupNode.php

raw
1 <?php
2
3 namespace Icinga\Module\Toplevelview\Tree;
4
5 use Icinga\Module\Toplevelview\Model\Hostgroupsummary;
6
7 use Icinga\Application\Benchmark;
8 use Icinga\Exception\NotFoundError;
9
10 use ipl\Stdlib\Filter;
11
12 use stdClass;
13
14 /**
15 * TLVHostGroupNode represents a Hostgroup in the tree
16 */
17 class TLVHostGroupNode extends TLVIcingaNode
18 {
19 protected $type = 'hostgroup';
20
21 protected $key = 'hostgroup';
22
23 protected static $titleKey = 'hostgroup';
24
25 public function getTitle(): string
26 {
27 $key = $this->getKey();
28 $obj = $this->root->getFetched($this->type, $key);
29
30 $n = $this->get($this->key);
31
32 if (isset($obj->display_name)) {
33 $n = $obj->display_name;
34 }
35
36 return sprintf('%s', $n);
37 }
38
39 public static function fetch(TLVTree $root): void
40 {
41 Benchmark::measure('Begin fetching hostgroups');
42
43 if (! array_key_exists('hostgroup', $root->registeredObjects) or empty($root->registeredObjects['hostgroup'])) {
44 throw new NotFoundError('No hostgroups registered to fetch!');
45 }
46
47 $hgFilter = Filter::any();
48 foreach (array_keys($root->registeredObjects['hostgroup']) as $name) {
49 $hgFilter->add(Filter::equal('hostgroup_name', $name));
50 }
51
52 $hostgroups = Hostgroupsummary::on($root->getDb());
53
54 $hostgroups->filter($hgFilter);
55
56 foreach ($hostgroups as $hostgroup) {
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 $hg = new stdClass;
61 $hg->display_name = $hostgroup->display_name;
62 $hg->hosts_total = $hostgroup->hosts_total;
63 $hg->hosts_up = $hostgroup->hosts_up;
64 $hg->hosts_total = $hostgroup->hosts_total;
65 $hg->services_total = $hostgroup->services_total;
66 $hg->hosts_up = $hostgroup->hosts_up;
67 $hg->services_ok = $hostgroup->services_ok;
68 $hg->hosts_down_handled = $hostgroup->hosts_down_handled;
69 $hg->hosts_down_unhandled = $hostgroup->hosts_down_unhandled;
70 $hg->hosts_downtime_handled = $hostgroup->hosts_downtime_handled;
71 $hg->hosts_downtime_active = $hostgroup->hosts_downtime_active;
72 $hg->services_warning_handled = $hostgroup->services_warning_handled;
73 $hg->services_warning_unhandled = $hostgroup->services_warning_unhandled;
74 $hg->services_critical_handled = $hostgroup->services_critical_handled;
75 $hg->services_critical_unhandled = $hostgroup->services_critical_unhandled;
76 $hg->services_unknown_handled = $hostgroup->services_unknown_handled;
77 $hg->services_unknown_unhandled = $hostgroup->services_unknown_unhandled;
78 $hg->services_downtime_handled = $hostgroup->services_downtime_handled;
79 $hg->services_downtime_active = $hostgroup->services_downtime_active;
80
81 $root->registeredObjects['hostgroup'][$hostgroup->name] = $hg;
82 }
83
84 Benchmark::measure('Finished fetching hostgroups');
85 }
86
87 /**
88 * getStatus returns the current status for the Hostgroup
89 *
90 * @return TLVStatus
91 */
92 public function getStatus(): TLVStatus
93 {
94 if ($this->status !== null) {
95 return $this->status;
96 }
97
98 $this->status = $status = new TLVStatus();
99 $key = $this->getKey();
100
101 $hostgroup = $this->root->getFetched($this->type, $key);
102
103 if ($hostgroup === null) {
104 $this->status->add('missing', 1);
105 return $this->status;
106 }
107
108 $status->set('total', $hostgroup->hosts_total + $hostgroup->services_total);
109 $status->set('ok', $hostgroup->hosts_up + $hostgroup->services_ok);
110
111 $status->set('critical_handled', $hostgroup->services_critical_handled);
112 $status->set('critical_unhandled', $hostgroup->services_critical_unhandled);
113
114 // Override the host status to handled if the option is set
115 if ($this->getRoot()->get('override_host_problem_to_handled') === true) {
116 $status->add(
117 'critical_handled',
118 $hostgroup->hosts_down_handled
119 + $hostgroup->hosts_down_unhandled
120 );
121 } else {
122 $status->add(
123 'critical_handled',
124 $hostgroup->hosts_down_handled
125 );
126 $status->add(
127 'critical_unhandled',
128 $hostgroup->hosts_down_unhandled
129 );
130 }
131
132 $status->set('warning_handled', $hostgroup->services_warning_handled);
133 $status->set('warning_unhandled', $hostgroup->services_warning_unhandled);
134 $status->set('unknown_handled', $hostgroup->services_unknown_handled);
135 $status->set('unknown_unhandled', $hostgroup->services_unknown_unhandled);
136
137 $status->set('downtime_handled', $hostgroup->hosts_downtime_handled + $hostgroup->services_downtime_handled);
138 $status->set('downtime_active', $hostgroup->hosts_downtime_active + $hostgroup->services_downtime_active);
139
140 // extra metadata for view
141 $status->setMeta('hosts_total', $hostgroup->hosts_total);
142 $status->setMeta(
143 'hosts_unhandled',
144 $hostgroup->hosts_down_unhandled
145 );
146
147 $status->set('missing', 0);
148
149 return $this->status;
150 }
151 }