Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add servicegroups in views #75

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions application/views/helpers/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion doc/01-Introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions doc/02-Configuartion-and-Behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ This overrides every host problem to be handled.
This helps when alerting is mostly based on service states, and the host
is only a container.

Only applies to host and hostgroup objects.

### Option `set_downtime_if_notification_enabled` (bool)

Set the host or service to "active downtime" if notifications are disabled.
Expand All @@ -69,3 +71,5 @@ Set the host or service to "active downtime" if notifications are disabled.

Since downtime and notification settings are essential for alerting,
Top Level Views tries to integrate these into its status logic.

Only applies to host and service objects.
11 changes: 10 additions & 1 deletion doc/21-Config-Format.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ children:
- name: Tile 2
hostgroup: 'linux-servers'
- name: Tile 3
servicegroup: 'http'
- name: Section 2
children:
- name: Tile 1
Expand Down Expand Up @@ -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: disk` 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.
Expand Down
113 changes: 113 additions & 0 deletions library/Toplevelview/Tree/TLVServiceGroupNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/* Copyright (C) 2024 Icinga Development Team <[email protected]> */

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;
}
}
2 changes: 2 additions & 0 deletions library/Toplevelview/Tree/TLVTreeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);

/**
Expand All @@ -79,6 +80,7 @@ class TLVTreeNode extends TreeNode
'service' => array('host', 'service'),
'host' => 'host',
'hostgroup' => 'hostgroup',
'servicegroup' => 'servicegroup',
);

/**
Expand Down