Skip to content

Commit

Permalink
ToicingadbCommand: Introduce dashboard action to migrate monitoring…
Browse files Browse the repository at this point in the history
… dashboards
  • Loading branch information
raviks789 committed Oct 30, 2023
1 parent 1c2b451 commit 71206d1
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
105 changes: 105 additions & 0 deletions modules/migrate/application/clicommands/ToicingadbCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,111 @@ 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=<username> 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 */
$dashboardUrlString = $dashboardConfig->get('url');
if ($dashboardUrlString !== null) {
$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) {
$oldFilterString = $dashBoardUrl->getParams()->toString();
$newFilterString = rawurldecode(QueryString::render($filter));

if ($oldFilterString !== $newFilterString) {
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
*
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ parameters:
- '#Call to an undefined method ipl\\Sql\\Connection::exec\(\)#'

scanDirectories:
- vendor
- /vendor

excludePaths:
- library/Icinga/Test
Expand Down

0 comments on commit 71206d1

Please sign in to comment.