2018-04-27 15:05:34 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Clicommands;
|
|
|
|
|
2018-09-05 13:32:17 +02:00
|
|
|
use Icinga\Application\Benchmark;
|
2018-04-27 15:05:34 +02:00
|
|
|
use Icinga\Module\Director\Cli\Command;
|
2018-09-05 13:32:17 +02:00
|
|
|
use Icinga\Module\Director\Hook\ImportSourceHook;
|
2018-04-27 15:05:34 +02:00
|
|
|
use Icinga\Module\Director\Objects\ImportSource;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deal with Director Import Sources
|
|
|
|
*
|
|
|
|
* Use this command to check or trigger your defined Import Sources
|
|
|
|
*/
|
|
|
|
class ImportsourceCommand extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* List defined Import Sources
|
|
|
|
*
|
|
|
|
* This shows a table with your defined Import Sources, their IDs and
|
|
|
|
* current state. As triggering Imports requires an ID, this is where
|
|
|
|
* you can look up the desired ID.
|
|
|
|
*
|
|
|
|
* USAGE
|
|
|
|
*
|
|
|
|
* icingacli director importsource list
|
|
|
|
*/
|
|
|
|
public function listAction()
|
|
|
|
{
|
|
|
|
$sources = ImportSource::loadAll($this->db());
|
|
|
|
if (empty($sources)) {
|
|
|
|
echo "No Import Source has been defined\n";
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%4s | %s\n", 'ID', 'Import Source name');
|
|
|
|
printf("-----+%s\n", str_repeat('-', 64));
|
|
|
|
|
|
|
|
foreach ($sources as $source) {
|
|
|
|
$state = $source->get('import_state');
|
|
|
|
printf("%4d | %s\n", $source->get('id'), $source->get('source_name'));
|
|
|
|
printf(" | -> %s%s\n", $state, $state === 'failing' ? ': ' . $source->get('last_error_message') : '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check a given Import Source for changes
|
|
|
|
*
|
|
|
|
* This command fetches data from the given Import Source and compares it
|
|
|
|
* to the most recently imported data.
|
|
|
|
*
|
|
|
|
* USAGE
|
|
|
|
*
|
|
|
|
* icingacli director importsource check --id <id>
|
|
|
|
*
|
|
|
|
* OPTIONS
|
|
|
|
*
|
|
|
|
* --id <id> An Import Source ID. Use the list command to figure out
|
|
|
|
* --benchmark Show timing and memory usage details
|
|
|
|
*/
|
|
|
|
public function checkAction()
|
|
|
|
{
|
|
|
|
$source = $this->getImportSource();
|
|
|
|
$source->checkForChanges();
|
2018-04-27 18:08:22 +02:00
|
|
|
$this->showImportStateDetails($source);
|
2018-04-27 15:05:34 +02:00
|
|
|
}
|
|
|
|
|
2018-09-05 13:32:17 +02:00
|
|
|
/**
|
|
|
|
* Fetch current data from a given Import Source
|
|
|
|
*
|
|
|
|
* This command fetches data from the given Import Source and outputs
|
|
|
|
* them as plain JSON
|
|
|
|
*
|
|
|
|
* USAGE
|
|
|
|
*
|
|
|
|
* icingacli director importsource fetch --id <id>
|
|
|
|
*
|
|
|
|
* OPTIONS
|
|
|
|
*
|
|
|
|
* --id <id> An Import Source ID. Use the list command to figure out
|
|
|
|
* --benchmark Show timing and memory usage details
|
|
|
|
*/
|
|
|
|
public function fetchAction()
|
|
|
|
{
|
|
|
|
$source = $this->getImportSource();
|
|
|
|
$source->checkForChanges();
|
|
|
|
$hook = ImportSourceHook::forImportSource($source);
|
|
|
|
Benchmark::measure('Ready to fetch data');
|
|
|
|
$data = $hook->fetchData();
|
2019-09-06 11:20:53 +02:00
|
|
|
$source->applyModifiers($data);
|
2018-09-05 13:32:17 +02:00
|
|
|
Benchmark::measure(sprintf('Got %d rows, ready to dump JSON', count($data)));
|
|
|
|
echo json_encode($data, JSON_PRETTY_PRINT);
|
|
|
|
}
|
|
|
|
|
2018-04-27 15:05:34 +02:00
|
|
|
/**
|
|
|
|
* Trigger an Import Run for a given Import Source
|
|
|
|
*
|
|
|
|
* This command fetches data from the given Import Source and stores it to
|
|
|
|
* the Director DB, so that the next related Sync Rule run can work with
|
|
|
|
* fresh data. In case data didn't change, nothing is going to be stored.
|
|
|
|
*
|
|
|
|
* USAGE
|
|
|
|
*
|
|
|
|
* icingacli director importsource run --id <id>
|
|
|
|
*
|
|
|
|
* OPTIONS
|
|
|
|
*
|
|
|
|
* --id <id> An Import Source ID. Use the list command to figure out
|
|
|
|
* --benchmark Show timing and memory usage details
|
|
|
|
*/
|
|
|
|
public function runAction()
|
|
|
|
{
|
|
|
|
$source = $this->getImportSource();
|
|
|
|
|
|
|
|
if ($source->runImport()) {
|
|
|
|
print "New data has been imported\n";
|
2018-04-27 18:08:22 +02:00
|
|
|
$this->showImportStateDetails($source);
|
2018-04-27 15:05:34 +02:00
|
|
|
} else {
|
|
|
|
print "Nothing has been changed, imported data is still up to date\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ImportSource
|
|
|
|
*/
|
|
|
|
protected function getImportSource()
|
|
|
|
{
|
2018-10-08 06:57:02 +02:00
|
|
|
return ImportSource::loadWithAutoIncId(
|
|
|
|
(int) $this->params->getRequired('id'),
|
|
|
|
$this->db()
|
|
|
|
);
|
2018-04-27 15:05:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ImportSource $source
|
|
|
|
* @throws \Icinga\Exception\IcingaException
|
|
|
|
*/
|
2018-04-27 18:08:22 +02:00
|
|
|
protected function showImportStateDetails(ImportSource $source)
|
2018-04-27 15:05:34 +02:00
|
|
|
{
|
|
|
|
echo $this->getImportStateDescription($source) . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ImportSource $source
|
|
|
|
* @return string
|
|
|
|
* @throws \Icinga\Exception\IcingaException
|
|
|
|
*/
|
|
|
|
protected function getImportStateDescription(ImportSource $source)
|
|
|
|
{
|
|
|
|
switch ($source->get('import_state')) {
|
|
|
|
case 'unknown':
|
|
|
|
return "It's currently unknown whether we are in sync with this"
|
|
|
|
. ' Import Source. You should either check for changes or'
|
|
|
|
. ' trigger a new Import Run.';
|
|
|
|
case 'in-sync':
|
|
|
|
return 'This Import Source is in sync';
|
|
|
|
case 'pending-changes':
|
|
|
|
return 'There are pending changes for this Import Source. You'
|
|
|
|
. ' should trigger a new Import Run.';
|
|
|
|
case 'failing':
|
|
|
|
return 'This Import Source failed: ' . $source->get('last_error_message');
|
|
|
|
default:
|
|
|
|
return 'This Import Source has an invalid state: ' . $source->get('import_state');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|