diff --git a/modules/migrate/application/clicommands/ToicingadbCommand.php b/modules/migrate/application/clicommands/ToicingadbCommand.php index e4efa598c4..71159701d7 100644 --- a/modules/migrate/application/clicommands/ToicingadbCommand.php +++ b/modules/migrate/application/clicommands/ToicingadbCommand.php @@ -292,6 +292,106 @@ public function restrictionAction(): void Logger::info('Successfully migrated monitoring restrictions'); } + /** + * Migrate the monitoring dashboards to Icinga DB Web dashboards for all the matched users + * + * USAGE + * + * icingacli migrate toicingadb dasboard [options] + * + * REQUIRED OPTIONS: + * + * --user= Migrate monitoring dashboards for all the + * users that are matched. (* all users) + * + * OPTIONS: + * + * --no-backup Migrate without creating a backup. (By Default + * a backup for monitoring dashboards is created) + */ + public function dashboardAction(): void + { + $dashboardsPath = Config::resolvePath('dashboards'); + if (! file_exists($dashboardsPath)) { + Logger::info('There are no dashboards to migrate'); + return; + } + + /** @var string $user */ + $user = $this->params->getRequired('user'); + $noBackup = $this->params->get('no-backup'); + + $rc = 0; + $directories = new DirectoryIterator($dashboardsPath); + + Logger::info( + 'Start monitoring dashboards migration', + $user + ); + + foreach ($directories as $directory) { + /** @var string $userName */ + $userName = $directories->key() === false ? '' : $directories->key(); + if (fnmatch($user, $userName) === false) { + continue; + } + + $dashboardsConfig = $this->readFromIni($directory . '/dashboard.ini', $rc); + + if ($noBackup === null) { + $dashboardsConfig->saveIni($directory . '/dashboard.backup.ini'); + } + + Logger::info( + 'Migrating monitoring dashboards to Icinga DB Web dashboards for user "%s"', + $userName + ); + + /** @var ConfigObject $dashboardConfig */ + foreach ($dashboardsConfig->getConfigObject() as $name => $dashboardConfig) { + /** @var ?string $dashboardUrlString */ + if ($dashboardUrlString !== null) { + $dashboardUrlString = $dashboardConfig->get('url'); + $dashBoardUrl = Url::fromPath($dashboardUrlString, [], new Request()); + if (fnmatch('monitioring*', $dashboardUrlString)) { + $dashboardConfig->url = UrlMigrator::transformUrl($dashBoardUrl)->getAbsoluteUrl(); + } + + if (fnmatch('icingadb*', $dashboardUrlString)) { + $filter = QueryString::parse($dashBoardUrl->getParams()->toString()); + $filter = $this->transformLegacyWildcardFilter($filter); + if ($filter) { + Logger::info( + 'Icinga Db Web filter of dashboard "%s" has changed from "%s" to "%s"', + $name, + rawurldecode($dashBoardUrl->getParams()->toString()), + rawurldecode(QueryString::render($filter)) + ); + $dashBoardUrl->setParams([]); + $dashBoardUrl->setFilter($filter); + + $dashboardConfig->url = $dashBoardUrl->getAbsoluteUrl(); + } + } + } + } + + try { + $dashboardsConfig->saveIni(); + } catch (NotWritableError $error) { + Logger::error('%s: %s', $error->getMessage(), $error->getPrevious()->getMessage()); + $rc = 256; + } + } + + if ($rc > 0) { + Logger::error('Failed to migrate some monitoring dashboards'); + exit($rc); + } + + Logger::info('Successfully migrated dashboards for all the matched users'); + } + /** * Migrate the given config to the given new config path *