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 5fb7639 commit d0c86d6
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions modules/migrate/application/clicommands/ToicingadbCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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=<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 */
if ($dashboardUrlString !== null) {

Check failure on line 353 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Variable $dashboardUrlString might not be defined.

Check failure on line 353 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Variable $dashboardUrlString might not be defined.

Check failure on line 353 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Variable $dashboardUrlString might not be defined.

Check failure on line 353 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Variable $dashboardUrlString might not be defined.

Check failure on line 353 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Variable $dashboardUrlString might not be defined.

Check failure on line 353 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Variable $dashboardUrlString might not be defined.
$dashboardUrlString = $dashboardConfig->get('url');
$dashBoardUrl = Url::fromPath($dashboardUrlString, [], new Request());

Check failure on line 355 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Parameter #1 $url of static method Icinga\Web\Url::fromPath() expects string, mixed given.

Check failure on line 355 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Parameter #1 $url of static method Icinga\Web\Url::fromPath() expects string, mixed given.

Check failure on line 355 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Parameter #1 $url of static method Icinga\Web\Url::fromPath() expects string, mixed given.

Check failure on line 355 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Parameter #1 $url of static method Icinga\Web\Url::fromPath() expects string, mixed given.

Check failure on line 355 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Parameter #1 $url of static method Icinga\Web\Url::fromPath() expects string, mixed given.

Check failure on line 355 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Parameter #1 $url of static method Icinga\Web\Url::fromPath() expects string, mixed given.
if (fnmatch('monitioring*', $dashboardUrlString)) {

Check failure on line 356 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Parameter #2 $filename of function fnmatch expects string, mixed given.

Check failure on line 356 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Parameter #2 $filename of function fnmatch expects string, mixed given.

Check failure on line 356 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Parameter #2 $filename of function fnmatch expects string, mixed given.

Check failure on line 356 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Parameter #2 $filename of function fnmatch expects string, mixed given.

Check failure on line 356 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Parameter #2 $filename of function fnmatch expects string, mixed given.

Check failure on line 356 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Parameter #2 $filename of function fnmatch expects string, mixed given.
$dashboardConfig->url = UrlMigrator::transformUrl($dashBoardUrl)->getAbsoluteUrl();
}

if (fnmatch('icingadb*', $dashboardUrlString)) {

Check failure on line 360 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Parameter #2 $filename of function fnmatch expects string, mixed given.

Check failure on line 360 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Parameter #2 $filename of function fnmatch expects string, mixed given.

Check failure on line 360 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Parameter #2 $filename of function fnmatch expects string, mixed given.

Check failure on line 360 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Parameter #2 $filename of function fnmatch expects string, mixed given.

Check failure on line 360 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Parameter #2 $filename of function fnmatch expects string, mixed given.

Check failure on line 360 in modules/migrate/application/clicommands/ToicingadbCommand.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Parameter #2 $filename of function fnmatch expects string, mixed given.
$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
*
Expand Down

0 comments on commit d0c86d6

Please sign in to comment.