From d5f222b7ebf4d2c2d47d20a25adcc9aadf67fbd5 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:30:50 +0200 Subject: Adding upstream version 0.3.3. Signed-off-by: Daniel Baumann --- application/clicommands/ConvertCommand.php | 104 +++++++++++++++++++++++++++++ application/clicommands/LegacyCommand.php | 92 +++++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 application/clicommands/ConvertCommand.php create mode 100644 application/clicommands/LegacyCommand.php (limited to 'application/clicommands') diff --git a/application/clicommands/ConvertCommand.php b/application/clicommands/ConvertCommand.php new file mode 100644 index 0000000..a05538b --- /dev/null +++ b/application/clicommands/ConvertCommand.php @@ -0,0 +1,104 @@ + */ + +namespace Icinga\Module\Toplevelview\Clicommands; + +use Icinga\Exception\ConfigurationError; +use Icinga\Module\Toplevelview\Command; +use Icinga\Module\Toplevelview\Config\ConfigEmitter; +use Icinga\Module\Toplevelview\Legacy\LegacyDbHelper; +use Icinga\Module\Toplevelview\ViewConfig; +use Zend_Db_Adapter_Pdo_Sqlite; + +/** + * Converts a TopLevelView database into the new YAML configuration format + */ +class ConvertCommand extends Command +{ + protected $dbConnections = array(); + + public function init() + { + parent::init(); + + if (! extension_loaded('pdo_sqlite')) { + throw new ConfigurationError('You need the PHP extension "pdo_sqlite" in order to convert TopLevelView'); + } + } + + /** + * List all hierarchies in the database + * + * Arguments: + * --db SQLite3 data from from old TopLevelView module + */ + public function listAction() + { + $dbFile = $this->params->getRequired('db'); + $db = $this->sqlite($dbFile); + + $helper = new LegacyDbHelper($db); + foreach ($helper->fetchHierarchies() as $root) { + printf("[%d] %s\n", $root['id'], $root['name']); + } + } + + /** + * Generate a YAML config file for Icinga Web 2 module + * + * Arguments: + * --db SQLite3 data from from old TopLevelView module + * --id Database id to export (see list) + * --output Write to file (default '-' for stdout) + * --name If name is specified instead of file, + * config is saved under that name + */ + public function convertAction() + { + $dbFile = $this->params->getRequired('db'); + $db = $this->sqlite($dbFile); + + $id = $this->params->getRequired('id'); + + $output = $this->params->get('output', null); + $name = $this->params->get('name', null); + $format = $this->params->get('format', 'yaml'); + + $helper = new LegacyDbHelper($db, $this->monitoringBackend()); + $tree = $helper->fetchTree($id); + + $emitter = ConfigEmitter::fromLegacyTree($tree); + + if ($name !== null and $output === null) { + $viewConfig = new ViewConfig(); + $viewConfig->setConfigDir(); + $viewConfig->setFormat(ViewConfig::FORMAT_YAML); + $viewConfig->setName($name); + $viewConfig->setText($emitter->emitYAML($format)); + $viewConfig->store(); + printf("Saved as config %s\n", $name); + exit(0); + } + + $text = $emitter->emit($format); + if ($output === null || $output === '-') { + echo $text; + } else { + file_put_contents($output, $text); + } + } + + /** + * Sets up the Zend PDO resource for SQLite + * + * @param string $file + * + * @return Zend_Db_Adapter_Pdo_Sqlite + */ + protected function sqlite($file) + { + return new Zend_Db_Adapter_Pdo_Sqlite(array( + 'dbname' => $file, + )); + } +} diff --git a/application/clicommands/LegacyCommand.php b/application/clicommands/LegacyCommand.php new file mode 100644 index 0000000..f9784be --- /dev/null +++ b/application/clicommands/LegacyCommand.php @@ -0,0 +1,92 @@ + */ + +namespace Icinga\Module\Toplevelview\Clicommands; + +use Icinga\Exception\ConfigurationError; +use Icinga\Module\Monitoring\Backend\MonitoringBackend; +use Icinga\Module\Toplevelview\Command; +use Icinga\Module\Toplevelview\Legacy\LegacyDbHelper; +use Zend_Db_Adapter_Pdo_Sqlite; + +/** + * Tools for the legacy DB + */ +class LegacyCommand extends Command +{ + public function init() + { + parent::init(); + + if (! extension_loaded('pdo_sqlite')) { + throw new ConfigurationError('You need the PHP extension "pdo_sqlite" in order to convert TopLevelView'); + } + } + + /** + * Delete unreferenced objects from the database + * + * Arguments: + * --db SQLite3 data from from old TopLevelView module + * --noop Only show counts, don't delete + */ + public function cleanupAction() + { + $dbFile = $this->params->getRequired('db'); + $noop = $this->params->shift('noop'); + $db = $this->sqlite($dbFile); + + $helper = new LegacyDbHelper($db); + + $result = $helper->cleanupUnreferencedObjects($noop); + foreach ($result as $type => $c) { + printf("%s: %d\n", $type, $c); + } + } + + /** + * Migrate database ids from an IDO to another IDO + * + * Arguments: + * --db SQLite3 data from from old TopLevelView module + * --target Target database path (will be overwritten) + * --old OLD IDO backend (configured in monitoring module) + * --new New IDO backend (configured in monitoring module) (optional) + * --purge Remove unresolvable data during update (see log) + */ + public function idomigrateAction() + { + $dbFile = $this->params->getRequired('db'); + $old = $this->params->getRequired('old'); + $target = $this->params->getRequired('target'); + $new = $this->params->get('new'); + $purge = $this->params->shift('purge'); + + $db = $this->sqlite($dbFile); + + $helper = new LegacyDbHelper($db, MonitoringBackend::instance($new)); + $helper->setOldBackend(MonitoringBackend::instance($old)); + + // Use the copy as db + $helper->setDb($helper->copySqliteDb($db, $target)); + + $result = $helper->migrateObjectIds(false, $purge); + foreach ($result as $type => $c) { + printf("%s: %d\n", $type, $c); + } + } + + /** + * Sets up the Zend PDO resource for SQLite + * + * @param string $file + * + * @return Zend_Db_Adapter_Pdo_Sqlite + */ + protected function sqlite($file) + { + return new Zend_Db_Adapter_Pdo_Sqlite(array( + 'dbname' => $file, + )); + } +} -- cgit v1.2.3