Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
5588562
Remove dependency stuff
jrauh01 Feb 18, 2026
f3c3298
Add types
jrauh01 Jan 30, 2026
fdb0610
Add empty line above `return` and `exit`
jrauh01 Jan 30, 2026
d0d7ac4
Use imports
jrauh01 Feb 18, 2026
dc86dd2
Use match expressions
jrauh01 Jan 30, 2026
d9f5159
Use modern string functions
jrauh01 Feb 6, 2026
5d03069
Use arrow functions
jrauh01 Feb 17, 2026
a476f24
Use variadic argument instead of `func_get_args()`
jrauh01 Feb 17, 2026
447de6b
Use `Attributes` explicitly
jrauh01 Jan 30, 2026
972b74e
Use first class callables
jrauh01 Feb 6, 2026
504a283
Add phpdocs for deprecations
jrauh01 Jan 30, 2026
3072dfa
Replace deprecated stuff
jrauh01 Jan 30, 2026
1d80e22
Inline stuff
jrauh01 Feb 16, 2026
79dabd6
Remove unsed classes
jrauh01 Jan 30, 2026
6ef69dd
Remove superfluous else cases
jrauh01 Feb 18, 2026
afb6624
Remove superfluous code
jrauh01 Feb 17, 2026
9c4436c
Remove superfluous variables
jrauh01 Feb 17, 2026
f3a70ef
Remove trailing commas from arrays
jrauh01 Feb 16, 2026
120ae3d
Reformat query builder chains for consistent, more readable formatting
jrauh01 Feb 17, 2026
c8449d4
Reformat arrays
jrauh01 Feb 17, 2026
7e91194
Reformat table column definitions for consistency and readability
jrauh01 Feb 17, 2026
50a0570
Reformat stuff
jrauh01 Feb 17, 2026
842688d
Comment out unreachable or unused code
jrauh01 Feb 17, 2026
32a5a20
Sort imports
jrauh01 Jan 30, 2026
6ae68c3
Translate german comments to englisch
jrauh01 Feb 3, 2026
ae8da29
Fix potential operator precedence bug
jrauh01 Feb 2, 2026
9e014e7
Fix `updateDb()` success condition
jrauh01 Feb 3, 2026
63b5cd1
Add enum `ResultStatus`
jrauh01 Feb 5, 2026
018ff6f
Make `CheckPluginState` an enum
jrauh01 Feb 5, 2026
c08bff1
Make `MonitoringStateTrigger` an enum
jrauh01 Feb 5, 2026
0cad039
Make `ObjectType` an enum
jrauh01 Feb 5, 2026
612ce08
Change method visibility to `protected` like the parent method
jrauh01 Feb 6, 2026
5c420f4
Add abstract class `PercentObjectsTable` to bundle redundant methods
jrauh01 Feb 11, 2026
5325047
Use `??=` operator
jrauh01 Feb 16, 2026
2bf8fd4
Use `??` operator
jrauh01 Feb 17, 2026
c6a5147
Use `?->` operator
jrauh01 Feb 2, 2026
a263dab
Use ternary operator
jrauh01 Feb 13, 2026
8e1a243
Revert "Fix potential operator precedence bug"
jrauh01 Feb 20, 2026
6cd2c04
Remove PhpDoc annotation alignment
jrauh01 Mar 4, 2026
9cbb77e
Use ?Type shorthand for nullable PHPDoc annotations
jrauh01 Mar 4, 2026
9574f5b
fixup! Add types
jrauh01 Mar 20, 2026
538bb00
Drop `@var` property docblocks that only repeat the native type
jrauh01 May 18, 2026
9f0aa3f
fixup! Comment out unreachable or unused code
jrauh01 May 18, 2026
b6c6b7f
FIXUP and REWRITE: Comment out unused and unreachable code
jrauh01 May 18, 2026
15834ab
fixup! Use ternary operator
jrauh01 May 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
150 changes: 94 additions & 56 deletions application/clicommands/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Icinga\Module\Vspheredb\Clicommands;

use Exception;
use gipfl\Cli\Screen;
use Icinga\Date\DateFormatter;
use Icinga\Exception\NotFoundError;
Expand All @@ -12,10 +13,10 @@
use Icinga\Module\Vspheredb\Db;
use Icinga\Module\Vspheredb\Db\CheckRelatedLookup;
use Icinga\Module\Vspheredb\DbObject\BaseDbObject;
use Icinga\Module\Vspheredb\Monitoring\CheckPluginState;
use Icinga\Module\Vspheredb\Monitoring\CheckRunner;
use Icinga\Module\Vspheredb\Monitoring\Health\ServerConnectionInfo;
use Icinga\Module\Vspheredb\Monitoring\Health\VCenterInfo;
use Icinga\Module\Vspheredb\Monitoring\Rule\Enum\CheckPluginState;
use InvalidArgumentException;
use Ramsey\Uuid\Uuid;

Expand All @@ -28,18 +29,20 @@ class CheckCommand extends Command
{
use CheckPluginHelper;

/** @var Db */
protected $db;
protected ?Db $db = null;

/**
* Check vSphereDB daemon health
*
* @return void
*/
public function healthAction()
public function healthAction(): void
{
$this->run(function () {
$migrations = Db::migrationsForDb($this->db());
if (! $migrations->hasSchema()) {
$this->addProblem('CRITICAL', 'Database has no vSphereDB schema');

return resolve(null);
}
if ($migrations->hasPendingMigrations()) {
Expand All @@ -57,15 +60,15 @@ public function healthAction()
}

if (count($vCenters) > 1) {
if ($this->getState() === 0) {
$this->prependMessage('All vCenters/ESXi Hosts are connected');
} else {
$this->prependMessage('There are problems with some vCenters/ESXi Host connections');
}
$this->prependMessage(
$this->getState() === 0
? 'All vCenters/ESXi Hosts are connected'
: 'There are problems with some vCenters/ESXi Host connections'
);
}
}, function (\Exception $e) {
}, function (Exception $e) {
$message = $e->getMessage();
if (preg_match('/^Unable to connect/', $message)) {
if (str_starts_with($message, 'Unable to connect')) {
$message = "Daemon not running? $message";
}
$this->addProblem('CRITICAL', $message);
Expand All @@ -82,8 +85,10 @@ public function healthAction()
* USAGE
*
* icingacli vspheredb check vcenterconnection --vCenter <id>
*
* @return void
*/
public function vcenterconnectionAction()
public function vcenterconnectionAction(): void
{
$this->run(function () {
$vcenter = VCenterInfo::fetchOne(
Expand All @@ -105,20 +110,16 @@ public function vcenterconnectionAction()
* USAGE
*
* icingacli vspheredb check host [--name <name>|--uuid <uuid>] [--ruleset <set>] [--rule [<ruleset>/]<rule>]
*
* @return void
*/
public function hostAction()
public function hostAction(): void
{
$this->run(function () {
$uuid = $this->params->get('uuid');
if ($uuid !== null) {
$params = [
'uuid' => Uuid::fromString($uuid)->getBytes()
];
} else {
$params = [
'host_name' => $this->params->getRequired('name')
];
}
$params = $uuid !== null
? ['uuid' => Uuid::fromString($uuid)->getBytes()]
: ['host_name' => $this->params->getRequired('name')];
$host = $this->lookup()->findOneBy('HostSystem', $params);
$this->runChecks($host);
});
Expand All @@ -130,8 +131,10 @@ public function hostAction()
* USAGE
*
* icingacli vspheredb check hosts
*
* @return void
*/
public function hostsAction()
public function hostsAction(): void
{
$this->showOverallStatusForProblems(
$this->lookup()->listNonGreenObjects('HostSystem')
Expand All @@ -144,8 +147,10 @@ public function hostsAction()
* USAGE
*
* icingacli vspheredb check vm [--name <name>|--uuid <uuid>] [--ruleset <set>] [--rule [<ruleset>/]<rule>]
*
* @return void
*/
public function vmAction()
public function vmAction(): void
{
$this->run(function () {
$uuid = $this->params->get('uuid');
Expand Down Expand Up @@ -174,8 +179,10 @@ public function vmAction()
* USAGE
*
* icingacli vspheredb check vms
*
* @return void
*/
public function vmsAction()
public function vmsAction(): void
{
$this->showOverallStatusForProblems(
$this->lookup()->listNonGreenObjects('VirtualMachine')
Expand All @@ -188,20 +195,16 @@ public function vmsAction()
* USAGE
*
* icingacli vspheredb check datastore [--name <name>|--uuid <uuid>] [--ruleset <set>] [--rule [<ruleset>/]<rule>]
*
* @return void
*/
public function datastoreAction()
public function datastoreAction(): void
{
$this->run(function () {
$uuid = $this->params->get('uuid');
if ($uuid !== null) {
$params = [
'uuid' => Uuid::fromString($uuid)->getBytes()
];
} else {
$params = [
'object_name' => $this->params->getRequired('name')
];
}
$params = $uuid !== null
? ['uuid' => Uuid::fromString($uuid)->getBytes()]
: ['object_name' => $this->params->getRequired('name')];
$datastore = $this->lookup()->findOneBy('Datastore', $params);
$this->runChecks($datastore);
});
Expand All @@ -213,15 +216,22 @@ public function datastoreAction()
* USAGE
*
* icingacli vspheredb check datastores
*
* @return void
*/
public function datastoresAction()
public function datastoresAction(): void
{
$this->showOverallStatusForProblems(
$this->lookup()->listNonGreenObjects('Datastore')
);
}

protected function runChecks(BaseDbObject $object)
/**
* @param BaseDbObject $object
*
* @return never
*/
protected function runChecks(BaseDbObject $object): never
{
$runner = new CheckRunner($this->db());
if ($section = $this->params->get(CheckRunner::RULESET_NAME_PARAMETER)) {
Expand All @@ -244,10 +254,19 @@ protected function runChecks(BaseDbObject $object)
}
$result = $runner->check($object);
echo $this->colorizeOutput($result->getOutput()) . PHP_EOL;

exit($result->getState()->getExitCode());
}

protected static function assertString($string, string $label)
/**
* @param mixed $string
* @param string $label
*
* @return void
*
* @throws InvalidArgumentException
*/
protected static function assertString(mixed $string, string $label): void
{
if (! is_string($string)) {
throw new InvalidArgumentException("$label must be a string");
Expand All @@ -257,9 +276,10 @@ protected static function assertString($string, string $label)
/**
* @param VCenterInfo $vcenter
* @param array<int, array<int, ServerConnectionInfo>> $connections
*
* @return void
*/
protected function checkVCenterConnection(VCenterInfo $vcenter, array $connections)
protected function checkVCenterConnection(VCenterInfo $vcenter, array $connections): void
{
$vcenterId = $vcenter->id;
$prefix = sprintf('%s, %s: ', $vcenter->name, $vcenter->software);
Expand All @@ -282,7 +302,10 @@ protected function checkVCenterConnection(VCenterInfo $vcenter, array $connectio
}
}

protected function checkDaemonStatus()
/**
* @return void
*/
protected function checkDaemonStatus(): void
{
$db = $this->db()->getDbAdapter();
$daemon = $db->fetchRow(
Expand All @@ -301,16 +324,26 @@ protected function checkDaemonStatus()
}
}

/**
* @param string $string
*
* @return string
*/
protected function colorizeOutput(string $string): string
{
$screen = Screen::factory();
$pattern = '/\[(OK|WARNING|CRITICAL|UNKNOWN)]\s/';
return preg_replace_callback($pattern, function ($match) use ($screen) {
return '[' . $screen->colorize($match[1], (new CheckPluginState($match[1]))->getColor()) . '] ';
return '[' . $screen->colorize($match[1], CheckPluginState::from($match[1])->color()) . '] ';
}, $string);
}

protected function showOverallStatusForProblems($problems)
/**
* @param array $problems
*
* @return void
*/
protected function showOverallStatusForProblems(array $problems): void
{
$this->run(function () use ($problems) {
if (empty($problems)) {
Expand All @@ -324,7 +357,13 @@ protected function showOverallStatusForProblems($problems)
});
}

protected function addProblematicObjectNames($color, $objects)
/**
* @param string $color
* @param array $objects
*
* @return void
*/
protected function addProblematicObjectNames(string $color, array $objects): void
{
$showMax = 5;
$stateName = $this->getStateForColor($color);
Expand Down Expand Up @@ -357,27 +396,26 @@ protected function addProblematicObjectNames($color, $objects)
*/
protected function getStateForColor(string $color): string
{
$colors = [
'green' => 'OK',
'gray' => 'CRITICAL',
'yellow' => 'WARNING',
'red' => 'CRITICAL',
];

return $colors[$color];
return match ($color) {
'green' => 'OK',
'gray', 'red' => 'CRITICAL',
'yellow' => 'WARNING'
};
}

/**
* @return CheckRelatedLookup
*/
protected function lookup(): CheckRelatedLookup
{
return new CheckRelatedLookup($this->db());
}

/**
* @return Db
*/
protected function db(): Db
{
if ($this->db === null) {
$this->db = Db::newConfiguredInstance();
}

return $this->db;
return $this->db ??= Db::newConfiguredInstance();
}
}
Loading
Loading