Skip to content

Commit

Permalink
Merge pull request #6 from dachcom-digital/log
Browse files Browse the repository at this point in the history
refactor checks
  • Loading branch information
solverat committed Sep 22, 2023
2 parents c5e14d4 + 519ae54 commit 9928845
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 151 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,19 @@ monitoring:
api_code: 'YOUR_API_CODE'
```


## Fetch Data
```bash
curl --data "apiCode=YOUR_API_CODE" https://www.your-domain.tld/monitoring/fetch
```

## Create Custom Check
Create a tagged `pimcore.monitoring.check` service and implement the `CheckInterface` interface.

## Available Checks
- Pimcore version and revision
- Installed Bundles
- Installed AreaBricks
- Available Users
- Failed logins

## Copyright and license
Copyright: [DACHCOM.DIGITAL](http://dachcom-digital.ch)
Expand Down
4 changes: 4 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
### Deprecations
- `extensions.isEnabled` has been removed and was replaced by `extensions.isInstalled`
- Check Script has been removed
- `failed_logins` has been removed since pimcore now longer provide any logging files for that. If you still need it, you need to implement your own check and hook into the security monolog channel for example

### New Features
- It's now possible to add your own check. Just create a tagged `pimcore.monitoring.check` service and implement the `CheckInterface` interface

***

Expand Down
2 changes: 1 addition & 1 deletion config/routing.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
monitoring.fetch:
path: /monitoring/fetch
defaults: { _controller: MonitoringBundle\Controller\WatchDogController::fetchAction }
controller: MonitoringBundle\Controller\CheckController::fetchAction
13 changes: 11 additions & 2 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ services:
autowire: true
autoconfigure: true

MonitoringBundle\Controller\WatchDogController:
MonitoringBundle\Controller\CheckController:
autowire: true
autoconfigure: true
tags: [ 'controller.service_arguments' ]

MonitoringBundle\Service\WatchDog:
MonitoringBundle\Check\Check:
autowire: true
autoconfigure: true
arguments:
$checks: !tagged_iterator pimcore.monitoring.check

MonitoringBundle\Check\:
autowire: true
autoconfigure: true
resource: '../src/Check/'
exclude: ['../src/Check/Check.php']
tags: [ 'pimcore.monitoring.check' ]
39 changes: 39 additions & 0 deletions src/Check/BricksCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace MonitoringBundle\Check;

use Pimcore\Extension\Document\Areabrick\AreabrickManager;

class BricksCheck implements CheckInterface
{
public function __construct(protected AreabrickManager $areaBrickManager)
{
}

public function getCheckReportIdentifier(): string
{
return 'bricks';
}

public function getCheckReport(): array
{
$bricks = [];

foreach ($this->areaBrickManager->getBricks() as $brickName => $brickInfo) {
$brick = $this->areaBrickManager->getBrick($brickName);

$desc = $brick->getDescription();
if (empty($desc) && $newDesc = $brick->getId()) {
$desc = $newDesc;
}

$bricks[$brickName] = [
'description' => $desc,
'name' => $brick->getName(),
'isEnabled' => true,
];
}

return $bricks;
}
}
21 changes: 21 additions & 0 deletions src/Check/Check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace MonitoringBundle\Check;

class Check
{
public function __construct(protected iterable $checks)
{
}

public function dispatchCheck(): array
{
$checks = [];
/** @var CheckInterface $check */
foreach ($this->checks as $check) {
$checks[$check->getCheckReportIdentifier()] = $check->getCheckReport();
}

return $checks;
}
}
10 changes: 10 additions & 0 deletions src/Check/CheckInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace MonitoringBundle\Check;

interface CheckInterface
{
public function getCheckReportIdentifier(): string;

public function getCheckReport(): array;
}
21 changes: 21 additions & 0 deletions src/Check/CoreCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace MonitoringBundle\Check;

use Pimcore\Version;

class CoreCheck implements CheckInterface
{
public function getCheckReportIdentifier(): string
{
return 'core';
}

public function getCheckReport(): array
{
return [
'version' => Version::getVersion(),
'revision' => Version::getRevision()
];
}
}
33 changes: 33 additions & 0 deletions src/Check/ExtensionCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace MonitoringBundle\Check;

use Pimcore\Extension\Bundle\PimcoreBundleManager;

class ExtensionCheck implements CheckInterface
{
public function __construct(protected PimcoreBundleManager $pimcoreBundleManager)
{
}

public function getCheckReportIdentifier(): string
{
return 'extensions';
}

public function getCheckReport(): array
{
$extensions = [];

foreach ($this->pimcoreBundleManager->getActiveBundles() as $bundle) {
$extensions[] = [
'title' => $bundle->getNiceName(),
'version' => $bundle->getVersion(),
'identifier' => $this->pimcoreBundleManager->getBundleIdentifier($bundle),
'isInstalled' => $this->pimcoreBundleManager->isInstalled($bundle),
];
}

return $extensions;
}
}
45 changes: 45 additions & 0 deletions src/Check/UserCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace MonitoringBundle\Check;

use MonitoringBundle\Check\CheckInterface;
use Pimcore\Model\User;
use Pimcore\Version;

class UserCheck implements CheckInterface
{
public function getCheckReportIdentifier(): string
{
return 'users';
}

public function getCheckReport(): array
{
$userListing = new User\Listing();

$users = [];
foreach ($userListing->getUsers() as $user) {

if (!$user instanceof User) {
continue;
}

$lastLogin = 0;

try {
$lastLogin = $user->getLastLogin();
} catch (\Throwable) {
// fail silently: "User::$lastLogin must not be accessed before initialization"
}

$users[] = [
'name' => $user->getName(),
'active' => $user->isActive(),
'is_admin' => $user->isAdmin(),
'last_login' => $lastLogin,
];
}

return $users;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,29 @@

namespace MonitoringBundle\Controller;

use MonitoringBundle\Check\Check;
use MonitoringBundle\Configuration\Configuration;
use MonitoringBundle\Service\WatchDog;
use Pimcore\Controller\FrontendController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

class WatchDogController extends FrontendController
class CheckController extends FrontendController
{
public function __construct(protected Configuration $configuration, protected WatchDog $watchDog)
public function __construct(protected Configuration $configuration)
{
}

/**
* @throws AccessDeniedHttpException
*/
public function fetchAction(Request $request): JsonResponse
public function fetchAction(Request $request, Check $check): JsonResponse
{
if (!$this->checkAuth($request)) {
throw new AccessDeniedHttpException();
}

return new JsonResponse([
'core' => $this->watchDog->getCoreInfo(),
'extensions' => $this->watchDog->getExtensionsInfo(),
'bricks' => $this->watchDog->getBricksInfo(),
'users' => $this->watchDog->getUsersInfo(),
'failed_logins' => $this->watchDog->getFailedLoginsInfo()
], 200);
return new JsonResponse($check->dispatchCheck(), 200);
}

protected function checkAuth(Request $request): bool
Expand Down

0 comments on commit 9928845

Please sign in to comment.