IcingaConfig: auto-generate agent endpoints

This commit is contained in:
Thomas Gelf 2015-12-18 10:53:27 +01:00
parent 76a3e9cece
commit c0206ca2c7
1 changed files with 36 additions and 5 deletions

View File

@ -5,8 +5,8 @@ namespace Icinga\Module\Director\IcingaConfig;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\Db; use Icinga\Module\Director\Db;
use Icinga\Module\Director\Util; use Icinga\Module\Director\Util;
use Icinga\Module\Director\Objects\IcingaCommand;
use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaZone;
use Icinga\Module\Director\Objects\IcingaEndpoint; use Icinga\Module\Director\Objects\IcingaEndpoint;
use Icinga\Web\Hook; use Icinga\Web\Hook;
use Exception; use Exception;
@ -283,15 +283,13 @@ throw $e;
); );
// TODO: combine this with real fetches, this is a test right now
IcingaEndpoint::prefetchAll($this->connection);
$this $this
->createFileFromDb('zone') ->createFileFromDb('zone')
->createFileFromDb('endpoint') ->createFileFromDb('endpoint')
->createFileFromDb('command') ->createFileFromDb('command')
->createFileFromDb('hostGroup') ->createFileFromDb('hostGroup')
->createFileFromDb('host') ->createFileFromDb('host')
->autogenerateAgents()
->createFileFromDb('serviceGroup') ->createFileFromDb('serviceGroup')
->createFileFromDb('service') ->createFileFromDb('service')
->createFileFromDb('userGroup') ->createFileFromDb('userGroup')
@ -353,10 +351,43 @@ throw $e;
return $this; return $this;
} }
protected function autogenerateAgents()
{
$zones = array();
$endpoints = array();
foreach (IcingaHost::prefetchAll($this->connection) as $host) {
$name = $host->object_name;
if ($host->has_agent === 'y') {
$props = array(
'object_name' => $name,
'object_type' => 'object',
);
if ($host->master_should_connect === 'y') {
$props['host'] = $host->address;
$props['zone_id'] = $host->zone_id;
}
$endpoints[] = IcingaEndpoint::create($props);
$zones[] = IcingaZone::create(array(
'object_name' => $name
))->setEndpointList(array($name));
}
}
$this->createFileForObjects('endpoint', $endpoints);
$this->createFileForObjects('zone', $zones);
return $this;
}
protected function createFileFromDb($type) protected function createFileFromDb($type)
{ {
$class = 'Icinga\\Module\\Director\\Objects\\Icinga' . ucfirst($type); $class = 'Icinga\\Module\\Director\\Objects\\Icinga' . ucfirst($type);
$objects = $class::loadAll($this->connection); $objects = $class::prefetchAll($this->connection);
return $this->createFileForObjects($type, $objects);
}
protected function createFileForObjects($type, $objects)
{
if (empty($objects)) return $this; if (empty($objects)) return $this;
$masterZone = $this->getMasterZoneName(); $masterZone = $this->getMasterZoneName();
$globalZone = $this->getGlobalZoneName(); $globalZone = $this->getGlobalZoneName();