diff --git a/README.md b/README.md index 449d80d..cbb1a5d 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ children: service: ssh - name: Tile 2 hostgroup: linux-servers - - name: Tile 3 + - servicegroup: http ``` With a caching layer, this view can aggregate thousands of status objects and make diff --git a/application/views/helpers/Tree.php b/application/views/helpers/Tree.php index 386b3e3..85ee47b 100644 --- a/application/views/helpers/Tree.php +++ b/application/views/helpers/Tree.php @@ -37,6 +37,15 @@ public function tree(TLVTreeNode $node, $classes = array(), $level = 0) 'host.name' => $node->get('host') ] ); + } elseif ($type === 'servicegroup') { + $icon = 'services'; + $url = Url::fromPath( + 'icingadb/servicegroup', + array( + 'name' => $node->get('servicegroup'), + 'sort' => 'service.state.severity desc' + ) + ); } elseif ($type === 'hostgroup') { $icon = 'cubes'; $url = Url::fromPath( diff --git a/doc/01-Introduction.md b/doc/01-Introduction.md index 7ce78de..2dc9436 100644 --- a/doc/01-Introduction.md +++ b/doc/01-Introduction.md @@ -22,7 +22,7 @@ children: service: ssh - name: Tile 2 hostgroup: linux-servers - - name: Tile 3 + - servicegroup: http ``` **Hint:** Top Level View can use additional status logic for its views, see later chapters on details. diff --git a/doc/21-Config-Format.md b/doc/21-Config-Format.md index 524fc42..a736ec4 100644 --- a/doc/21-Config-Format.md +++ b/doc/21-Config-Format.md @@ -29,6 +29,7 @@ children: - name: Tile 2 hostgroup: 'linux-servers' - name: Tile 3 + servicegroup: 'http' - name: Section 2 children: - name: Tile 1 @@ -86,9 +87,17 @@ Attributes: Brings in the host group summary state. Attributes: -* `hostgroup: linux-servers` hostname in Icinga +* `hostgroup: linux-servers` host group name in Icinga * `type: hostgroup` (optional - detected by key attribute) +### Icinga Service group + +Brings in the service group summary state. + +Attributes: +* `servicegroup: linux-servers` service group name in Icinga +* `type: servicegroup` (optional - detected by key attribute) + ## Options Additional options are available to control status behavior of the respective view. diff --git a/library/Toplevelview/Tree/TLVServiceGroupNode.php b/library/Toplevelview/Tree/TLVServiceGroupNode.php new file mode 100644 index 0000000..db619d5 --- /dev/null +++ b/library/Toplevelview/Tree/TLVServiceGroupNode.php @@ -0,0 +1,113 @@ + */ + +namespace Icinga\Module\Toplevelview\Tree; + +use Icinga\Application\Benchmark; +use Icinga\Exception\NotFoundError; +use Icinga\Module\Icingadb\Model\ServicegroupSummary; +use ipl\Stdlib\Filter; +use stdClass; + +/** + * TLVServiceGroupNode represents a Servicegroup in the tree + */ +class TLVServiceGroupNode extends TLVIcingaNode +{ + protected $type = 'servicegroup'; + + protected $key = 'servicegroup'; + + protected static $titleKey = 'servicegroup'; + + public function getTitle(): string + { + $key = $this->getKey(); + $obj = $this->root->getFetched($this->type, $key); + + $n = $this->get($this->key); + + if (isset($obj->display_name)) { + $n = $obj->display_name; + } + + return sprintf('%s', $n); + } + + public static function fetch(TLVTree $root): void + { + Benchmark::measure('Begin fetching servicegroups'); + + if (! array_key_exists('servicegroup', $root->registeredObjects) or empty($root->registeredObjects['servicegroup'])) { + throw new NotFoundError('No servicegroups registered to fetch!'); + } + + $hgFilter = Filter::any(); + foreach (array_keys($root->registeredObjects['servicegroup']) as $name) { + $hgFilter->add(Filter::equal('servicegroup_name', $name)); + } + + $servicegroups = ServicegroupSummary::on($root->getDb()); + + $servicegroups->filter($hgFilter); + + foreach ($servicegroups as $servicegroup) { + // TODO We cannot store the ORM Models with json_encore + // Thus I'm converting things to objects that can be stored + // Maybe there's a better way? iterator_to_array does not work. + $sg = new stdClass; + $sg->display_name = $servicegroup->display_name; + $sg->services_total = $servicegroup->services_total; + $sg->services_ok = $servicegroup->services_ok; + $sg->services_warning_handled = $servicegroup->services_warning_handled; + $sg->services_warning_unhandled = $servicegroup->services_warning_unhandled; + $sg->services_critical_handled = $servicegroup->services_critical_handled; + $sg->services_critical_unhandled = $servicegroup->services_critical_unhandled; + $sg->services_unknown_handled = $servicegroup->services_unknown_handled; + $sg->services_unknown_unhandled = $servicegroup->services_unknown_unhandled; + + $root->registeredObjects['servicegroup'][$servicegroup->name] = $sg; + } + + Benchmark::measure('Finished fetching servicegroups'); + } + + /** + * getStatus returns the current status for the Servicegroup + * + * @return TLVStatus + */ + public function getStatus(): TLVStatus + { + if ($this->status !== null) { + return $this->status; + } + + $this->status = $status = new TLVStatus(); + $key = $this->getKey(); + + $servicegroup = $this->root->getFetched($this->type, $key); + + if ($servicegroup === null) { + $this->status->add('missing', 1); + return $this->status; + } + + $status->set('total', $servicegroup->services_total); + $status->set('ok', $servicegroup->services_ok); + + $status->set('critical_handled', $servicegroup->services_critical_handled); + $status->set('critical_unhandled', $servicegroup->services_critical_unhandled); + $status->set('warning_handled', $servicegroup->services_warning_handled); + $status->set('warning_unhandled', $servicegroup->services_warning_unhandled); + $status->set('unknown_handled', $servicegroup->services_unknown_handled); + $status->set('unknown_unhandled', $servicegroup->services_unknown_unhandled); + + // extra metadata for view + $status->setMeta('services_total', $servicegroup->services_total); + + $status->set('missing', 0); + + return $this->status; + } +} diff --git a/library/Toplevelview/Tree/TLVTreeNode.php b/library/Toplevelview/Tree/TLVTreeNode.php index 5b5207a..9cfd3b9 100644 --- a/library/Toplevelview/Tree/TLVTreeNode.php +++ b/library/Toplevelview/Tree/TLVTreeNode.php @@ -66,6 +66,7 @@ class TLVTreeNode extends TreeNode 'host' => 'Icinga\\Module\\Toplevelview\\Tree\\TLVHostNode', 'service' => 'Icinga\\Module\\Toplevelview\\Tree\\TLVServiceNode', 'hostgroup' => 'Icinga\\Module\\Toplevelview\\Tree\\TLVHostGroupNode', + 'servicegroup' => 'Icinga\\Module\\Toplevelview\\Tree\\TLVServiceGroupNode', ); /** @@ -79,6 +80,7 @@ class TLVTreeNode extends TreeNode 'service' => array('host', 'service'), 'host' => 'host', 'hostgroup' => 'hostgroup', + 'servicegroup' => 'servicegroup', ); /**