2015-06-30 11:19:31 +02:00
|
|
|
<?php
|
|
|
|
|
2015-10-20 22:34:04 +02:00
|
|
|
namespace Icinga\Module\Director\Controllers;
|
|
|
|
|
2016-05-03 08:27:12 +02:00
|
|
|
use Exception;
|
2016-04-08 00:24:20 +02:00
|
|
|
use Icinga\Exception\NotFoundError;
|
2016-10-13 12:05:35 +02:00
|
|
|
use Icinga\Module\Director\Exception\NestingError;
|
2016-06-28 01:55:43 +02:00
|
|
|
use Icinga\Module\Director\IcingaConfig\AgentWizard;
|
2016-02-18 01:45:05 +01:00
|
|
|
use Icinga\Module\Director\Objects\IcingaEndpoint;
|
2016-09-08 13:25:48 +02:00
|
|
|
use Icinga\Module\Director\Objects\IcingaHost;
|
2016-08-23 16:18:54 +02:00
|
|
|
use Icinga\Module\Director\Objects\IcingaService;
|
2017-01-02 10:51:54 +01:00
|
|
|
use Icinga\Module\Director\Objects\IcingaServiceSet;
|
2016-02-18 01:45:05 +01:00
|
|
|
use Icinga\Module\Director\Objects\IcingaZone;
|
2016-02-05 16:37:57 +01:00
|
|
|
use Icinga\Module\Director\Util;
|
2015-06-30 11:19:31 +02:00
|
|
|
use Icinga\Module\Director\Web\Controller\ObjectController;
|
|
|
|
|
2015-10-20 22:34:04 +02:00
|
|
|
class HostController extends ObjectController
|
2015-06-30 11:19:31 +02:00
|
|
|
{
|
2015-12-10 13:00:08 +01:00
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
parent::init();
|
|
|
|
if ($this->object) {
|
2016-02-05 16:37:57 +01:00
|
|
|
$tabs = $this->getTabs();
|
|
|
|
$tabs->add('services', array(
|
2015-12-10 13:00:08 +01:00
|
|
|
'url' => 'director/host/services',
|
|
|
|
'urlParams' => array('name' => $this->object->object_name),
|
|
|
|
'label' => 'Services'
|
|
|
|
));
|
2016-10-13 12:05:35 +02:00
|
|
|
try {
|
|
|
|
if ($this->object->object_type === 'object'
|
|
|
|
&& $this->object->getResolvedProperty('has_agent') === 'y'
|
|
|
|
) {
|
|
|
|
$tabs->add('agent', array(
|
|
|
|
'url' => 'director/host/agent',
|
|
|
|
'urlParams' => array('name' => $this->object->object_name),
|
|
|
|
'label' => 'Agent'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} catch (NestingError $e) {
|
|
|
|
// Ignore nesting errors
|
2016-02-05 16:37:57 +01:00
|
|
|
}
|
2015-12-10 13:00:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-03 16:06:18 +01:00
|
|
|
protected function checkDirectorPermissions()
|
|
|
|
{
|
|
|
|
$this->assertPermission('director/hosts');
|
|
|
|
}
|
|
|
|
|
2016-03-20 11:27:19 +01:00
|
|
|
public function editAction()
|
|
|
|
{
|
|
|
|
parent::editAction();
|
|
|
|
$host = $this->object;
|
|
|
|
$mon = $this->monitoring();
|
|
|
|
if ($host->isObject() && $mon->isAvailable() && $mon->hasHost($host->object_name)) {
|
|
|
|
$this->view->actionLinks .= ' ' . $this->view->qlink(
|
|
|
|
$this->translate('Show'),
|
|
|
|
'monitoring/host/show',
|
|
|
|
array('host' => $host->object_name),
|
|
|
|
array(
|
|
|
|
'class' => 'icon-globe critical',
|
|
|
|
'data-base-target' => '_next'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 13:00:08 +01:00
|
|
|
public function servicesAction()
|
|
|
|
{
|
2016-08-23 16:18:54 +02:00
|
|
|
$db = $this->db();
|
2016-03-18 12:59:26 +01:00
|
|
|
$host = $this->object;
|
|
|
|
|
|
|
|
$this->view->addLink = $this->view->qlink(
|
|
|
|
$this->translate('Add service'),
|
|
|
|
'director/service/add',
|
|
|
|
array('host' => $host->object_name),
|
|
|
|
array('class' => 'icon-plus')
|
2016-11-04 09:25:28 +01:00
|
|
|
) . ' ' . $this->view->qlink(
|
2016-10-20 09:15:42 +02:00
|
|
|
$this->translate('Add service set'),
|
|
|
|
'director/serviceset/add',
|
|
|
|
array('host' => $host->object_name),
|
|
|
|
array('class' => 'icon-plus')
|
2016-11-04 09:25:28 +01:00
|
|
|
);
|
2016-03-18 12:59:26 +01:00
|
|
|
|
2015-12-10 13:00:08 +01:00
|
|
|
$this->getTabs()->activate('services');
|
2016-03-18 13:32:24 +01:00
|
|
|
$this->view->title = sprintf(
|
2016-03-20 13:14:49 +01:00
|
|
|
$this->translate('Services: %s'),
|
2016-03-18 13:32:24 +01:00
|
|
|
$host->object_name
|
|
|
|
);
|
2016-08-23 16:18:54 +02:00
|
|
|
|
|
|
|
$resolver = $this->object->templateResolver();
|
|
|
|
|
|
|
|
$tables = array();
|
|
|
|
$table = $this->loadTable('IcingaHostService')
|
2016-03-18 12:59:26 +01:00
|
|
|
->setHost($host)
|
2016-08-23 16:18:54 +02:00
|
|
|
->setTitle($this->translate('Individual Service objects'))
|
2016-03-18 12:59:26 +01:00
|
|
|
->enforceFilter('host_id', $host->id)
|
2016-08-23 16:18:54 +02:00
|
|
|
->setConnection($db);
|
|
|
|
|
|
|
|
if (count($table)) {
|
|
|
|
$tables[0] = $table;
|
|
|
|
}
|
|
|
|
|
2016-08-27 15:20:03 +02:00
|
|
|
if ($applied = $host->vars()->get($db->settings()->magic_apply_for)) {
|
2016-08-23 16:18:54 +02:00
|
|
|
$table = $this->loadTable('IcingaHostAppliedForService')
|
|
|
|
->setHost($host)
|
|
|
|
->setDictionary($applied)
|
|
|
|
->setTitle($this->translate('Generated from host vars'));
|
|
|
|
|
|
|
|
if (count($table)) {
|
|
|
|
$tables[1] = $table;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($resolver->fetchResolvedParents() as $parent) {
|
|
|
|
$table = $this->loadTable('IcingaHostService')
|
|
|
|
->setHost($parent)
|
2016-09-08 13:25:48 +02:00
|
|
|
->setInheritedBy($host)
|
2016-08-23 16:18:54 +02:00
|
|
|
->enforceFilter('host_id', $parent->id)
|
|
|
|
->setConnection($db);
|
|
|
|
if (! count($table)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// dup dup
|
|
|
|
$title = sprintf(
|
|
|
|
'Inherited from %s',
|
|
|
|
$parent->object_name
|
|
|
|
);
|
|
|
|
|
|
|
|
$tables[$title] = $table->setTitle($title);
|
|
|
|
}
|
|
|
|
|
2016-10-20 09:15:42 +02:00
|
|
|
$table = $this->loadTable('IcingaHostServiceSet')
|
2017-01-02 10:51:54 +01:00
|
|
|
->setHost($host)
|
|
|
|
->setConnection($db);
|
|
|
|
|
|
|
|
$tables[$this->translate('Service sets')] = $table;
|
|
|
|
|
|
|
|
$title = $this->translate('Applied services');
|
|
|
|
$table = $this->loadTable('IcingaHostAppliedServices')
|
2016-10-20 09:15:42 +02:00
|
|
|
->setHost($host)
|
|
|
|
->setTitle($title)
|
2017-01-02 10:51:54 +01:00
|
|
|
->setConnection($db);
|
2016-10-20 09:15:42 +02:00
|
|
|
|
|
|
|
$tables[$title] = $table;
|
|
|
|
|
2016-08-23 16:18:54 +02:00
|
|
|
$this->view->tables = $tables;
|
|
|
|
}
|
|
|
|
|
2017-01-02 10:51:54 +01:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2016-08-23 16:18:54 +02:00
|
|
|
public function appliedserviceAction()
|
|
|
|
{
|
|
|
|
$db = $this->db();
|
2017-01-02 10:51:54 +01:00
|
|
|
/** @var IcingaHost $host */
|
2016-08-23 16:18:54 +02:00
|
|
|
$host = $this->object;
|
2017-01-02 10:51:54 +01:00
|
|
|
$serviceId = $this->params->get('service_id');
|
|
|
|
$parent = IcingaService::loadWithAutoIncId($serviceId, $db);
|
|
|
|
$serviceName = $parent->object_name;
|
2016-09-08 15:10:42 +02:00
|
|
|
|
2016-08-23 16:18:54 +02:00
|
|
|
$service = IcingaService::create(array(
|
2017-01-02 10:51:54 +01:00
|
|
|
'imports' => $parent,
|
2016-08-23 16:18:54 +02:00
|
|
|
'object_type' => 'apply',
|
|
|
|
'object_name' => $serviceName,
|
|
|
|
'host_id' => $host->id,
|
2016-09-08 15:10:42 +02:00
|
|
|
'vars' => $host->getOverriddenServiceVars($serviceName),
|
2016-08-23 16:18:54 +02:00
|
|
|
), $db);
|
|
|
|
|
|
|
|
$this->view->title = sprintf(
|
|
|
|
$this->translate('Applied service: %s'),
|
|
|
|
$serviceName
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->view->form = $this->loadForm('IcingaService')
|
|
|
|
->setDb($db)
|
|
|
|
->setHost($host)
|
2017-01-02 10:51:54 +01:00
|
|
|
->setApplyGenerated($parent)
|
2016-08-23 16:18:54 +02:00
|
|
|
->setObject($service)
|
|
|
|
;
|
|
|
|
|
2017-01-02 10:51:54 +01:00
|
|
|
$this->commonForServices();
|
2015-12-10 13:00:08 +01:00
|
|
|
}
|
2016-02-05 16:37:57 +01:00
|
|
|
|
2016-09-08 13:25:48 +02:00
|
|
|
public function inheritedserviceAction()
|
|
|
|
{
|
|
|
|
$db = $this->db();
|
|
|
|
$host = $this->object;
|
|
|
|
$serviceName = $this->params->get('service');
|
|
|
|
$from = IcingaHost::load($this->params->get('inheritedFrom'), $this->db());
|
|
|
|
|
|
|
|
$parent = IcingaService::load(
|
|
|
|
array(
|
|
|
|
'object_name' => $serviceName,
|
|
|
|
'host_id' => $from->id
|
|
|
|
),
|
|
|
|
$this->db()
|
|
|
|
);
|
|
|
|
|
|
|
|
$parent->object_name = $from->object_name;
|
|
|
|
|
|
|
|
$service = IcingaService::create(array(
|
|
|
|
'object_type' => 'apply',
|
|
|
|
'object_name' => $serviceName,
|
|
|
|
'host_id' => $host->id,
|
|
|
|
'imports' => array($parent),
|
|
|
|
'vars' => $host->getOverriddenServiceVars($serviceName),
|
|
|
|
), $db);
|
|
|
|
|
|
|
|
$this->view->title = sprintf(
|
|
|
|
$this->translate('Inherited service: %s'),
|
|
|
|
$serviceName
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->view->form = $this->loadForm('IcingaService')
|
|
|
|
->setDb($db)
|
|
|
|
->setHost($host)
|
|
|
|
->setInheritedFrom($from->object_name)
|
2016-10-20 03:09:34 +02:00
|
|
|
->setObject($service);
|
|
|
|
$this->view->form->setResolvedImports();
|
2017-01-02 10:51:54 +01:00
|
|
|
$this->commonForServices();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function servicesetserviceAction()
|
|
|
|
{
|
|
|
|
$db = $this->db();
|
|
|
|
/** @var IcingaHost $host */
|
|
|
|
$host = $this->object;
|
|
|
|
$serviceName = $this->params->get('service');
|
|
|
|
$set = IcingaServiceSet::loadWithAutoIncId((int) $this->params->get('serviceSet'), $this->db());
|
2016-09-08 13:25:48 +02:00
|
|
|
|
2017-01-02 10:51:54 +01:00
|
|
|
$parent = IcingaService::load(
|
|
|
|
array(
|
|
|
|
'object_type' => 'apply',
|
|
|
|
'object_name' => $serviceName,
|
|
|
|
'service_set_id' => (int) $set->id
|
|
|
|
),
|
|
|
|
$this->db()
|
|
|
|
);
|
|
|
|
|
|
|
|
$parent->object_name = $set->object_name;
|
|
|
|
|
|
|
|
$service = IcingaService::create(array(
|
|
|
|
'object_type' => 'apply',
|
|
|
|
'object_name' => $serviceName,
|
|
|
|
'host_id' => $host->id,
|
|
|
|
'imports' => array($parent),
|
|
|
|
'vars' => $host->getOverriddenServiceVars($serviceName),
|
|
|
|
), $db);
|
|
|
|
|
|
|
|
$this->view->title = sprintf(
|
|
|
|
$this->translate('Service "%s" (from set "%s")'),
|
|
|
|
$serviceName,
|
|
|
|
$set->object_name
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->view->form = $this->loadForm('IcingaService')
|
|
|
|
->setDb($db)
|
|
|
|
->setHost($host)
|
|
|
|
->setInheritedFrom($parent->object_name)
|
|
|
|
->setObject($service);
|
|
|
|
$this->view->form->setResolvedImports();
|
|
|
|
$this->commonForServices();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function commonForServices()
|
|
|
|
{
|
|
|
|
$host = $this->object;
|
|
|
|
$this->view->actionLinks = $this->view->qlink(
|
|
|
|
$this->translate('back'),
|
|
|
|
'director/host/services',
|
|
|
|
array('name' => $host->object_name),
|
|
|
|
array('class' => 'icon-left-big')
|
|
|
|
);
|
|
|
|
$this->getTabs()->activate('services');
|
|
|
|
$this->view->form->handleRequest();
|
2016-09-08 13:25:48 +02:00
|
|
|
$this->setViewScript('object/form');
|
|
|
|
}
|
|
|
|
|
2016-02-05 16:37:57 +01:00
|
|
|
public function agentAction()
|
|
|
|
{
|
2016-06-28 01:55:43 +02:00
|
|
|
switch ($this->params->get('download')) {
|
|
|
|
case 'windows-kickstart':
|
|
|
|
header('Content-type: application/octet-stream');
|
|
|
|
header('Content-Disposition: attachment; filename=icinga2-agent-kickstart.ps1');
|
|
|
|
|
|
|
|
$wizard = $this->view->wizard = new AgentWizard($this->object);
|
|
|
|
$wizard->setTicketSalt($this->api()->getTicketSalt());
|
2016-06-28 02:01:05 +02:00
|
|
|
echo preg_replace('/\n/', "\r\n", $wizard->renderWindowsInstaller());
|
2016-06-28 01:55:43 +02:00
|
|
|
exit;
|
2016-11-17 23:23:02 +01:00
|
|
|
case 'linux':
|
|
|
|
header('Content-type: application/octet-stream');
|
|
|
|
header('Content-Disposition: attachment; filename=icinga2-agent-kickstart.bash');
|
|
|
|
|
|
|
|
$wizard = $this->view->wizard = new AgentWizard($this->object);
|
|
|
|
$wizard->setTicketSalt($this->api()->getTicketSalt());
|
|
|
|
echo $wizard->renderLinuxInstaller();
|
|
|
|
exit;
|
2016-06-28 01:55:43 +02:00
|
|
|
}
|
|
|
|
|
2016-05-25 08:11:53 +02:00
|
|
|
$this->gracefullyActivateTab('agent');
|
2016-02-05 16:37:57 +01:00
|
|
|
$this->view->title = 'Agent deployment instructions';
|
|
|
|
// TODO: Fail when no ticket
|
|
|
|
$this->view->certname = $this->object->object_name;
|
2016-05-03 08:27:12 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
$this->view->ticket = Util::getIcingaTicket(
|
|
|
|
$this->view->certname,
|
|
|
|
$this->api()->getTicketSalt()
|
|
|
|
);
|
|
|
|
|
2016-06-28 01:55:43 +02:00
|
|
|
$wizard = $this->view->wizard = new AgentWizard($this->object);
|
|
|
|
$wizard->setTicketSalt($this->api()->getTicketSalt());
|
|
|
|
$this->view->windows = $wizard->renderWindowsInstaller();
|
2016-11-17 23:23:02 +01:00
|
|
|
$this->view->linux = $wizard->renderLinuxInstaller();
|
2016-06-28 01:55:43 +02:00
|
|
|
|
2016-05-03 08:27:12 +02:00
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->view->ticket = 'ERROR';
|
|
|
|
$this->view->error = sprintf(
|
|
|
|
$this->translate(
|
|
|
|
'A ticket for this agent could not have been requested from'
|
|
|
|
. ' your deployment endpoint: %s'
|
|
|
|
),
|
|
|
|
$e->getMessage()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-10 17:38:39 +01:00
|
|
|
$this->view->master = $this->db()->getDeploymentEndpointName();
|
|
|
|
$this->view->masterzone = $this->db()->getMasterZoneName();
|
|
|
|
$this->view->globalzone = $this->db()->getDefaultGlobalZoneName();
|
2016-02-05 16:37:57 +01:00
|
|
|
}
|
2016-02-18 01:45:05 +01:00
|
|
|
|
2016-04-08 00:24:20 +02:00
|
|
|
public function ticketAction()
|
|
|
|
{
|
|
|
|
if (! $this->getRequest()->isApiRequest() || ! $this->object) {
|
|
|
|
throw new NotFoundError('Not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
$host = $this->object;
|
|
|
|
if ($host->getResolvedProperty('has_agent') !== 'y') {
|
|
|
|
throw new NotFoundError('The host "%s" is not an agent', $host->object_name);
|
|
|
|
}
|
|
|
|
|
2016-05-03 08:27:12 +02:00
|
|
|
return $this->sendJson(
|
|
|
|
Util::getIcingaTicket(
|
|
|
|
$host->object_name,
|
|
|
|
$this->api()->getTicketSalt()
|
|
|
|
)
|
|
|
|
);
|
2016-04-08 00:24:20 +02:00
|
|
|
}
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|