Skip to content

Commit 9928845

Browse files
authored
Merge pull request #6 from dachcom-digital/log
refactor checks
2 parents c5e14d4 + 519ae54 commit 9928845

File tree

12 files changed

+193
-151
lines changed

12 files changed

+193
-151
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,19 @@ monitoring:
3939
api_code: 'YOUR_API_CODE'
4040
```
4141
42-
4342
## Fetch Data
4443
```bash
4544
curl --data "apiCode=YOUR_API_CODE" https://www.your-domain.tld/monitoring/fetch
4645
```
4746

47+
## Create Custom Check
48+
Create a tagged `pimcore.monitoring.check` service and implement the `CheckInterface` interface.
49+
4850
## Available Checks
4951
- Pimcore version and revision
5052
- Installed Bundles
5153
- Installed AreaBricks
5254
- Available Users
53-
- Failed logins
5455

5556
## Copyright and license
5657
Copyright: [DACHCOM.DIGITAL](http://dachcom-digital.ch)

UPGRADE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
### Deprecations
1010
- `extensions.isEnabled` has been removed and was replaced by `extensions.isInstalled`
1111
- Check Script has been removed
12+
- `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
13+
14+
### New Features
15+
- It's now possible to add your own check. Just create a tagged `pimcore.monitoring.check` service and implement the `CheckInterface` interface
1216

1317
***
1418

config/routing.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
monitoring.fetch:
22
path: /monitoring/fetch
3-
defaults: { _controller: MonitoringBundle\Controller\WatchDogController::fetchAction }
3+
controller: MonitoringBundle\Controller\CheckController::fetchAction

config/services.yaml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@ services:
44
autowire: true
55
autoconfigure: true
66

7-
MonitoringBundle\Controller\WatchDogController:
7+
MonitoringBundle\Controller\CheckController:
88
autowire: true
99
autoconfigure: true
1010
tags: [ 'controller.service_arguments' ]
1111

12-
MonitoringBundle\Service\WatchDog:
12+
MonitoringBundle\Check\Check:
1313
autowire: true
1414
autoconfigure: true
15+
arguments:
16+
$checks: !tagged_iterator pimcore.monitoring.check
17+
18+
MonitoringBundle\Check\:
19+
autowire: true
20+
autoconfigure: true
21+
resource: '../src/Check/'
22+
exclude: ['../src/Check/Check.php']
23+
tags: [ 'pimcore.monitoring.check' ]

src/Check/BricksCheck.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace MonitoringBundle\Check;
4+
5+
use Pimcore\Extension\Document\Areabrick\AreabrickManager;
6+
7+
class BricksCheck implements CheckInterface
8+
{
9+
public function __construct(protected AreabrickManager $areaBrickManager)
10+
{
11+
}
12+
13+
public function getCheckReportIdentifier(): string
14+
{
15+
return 'bricks';
16+
}
17+
18+
public function getCheckReport(): array
19+
{
20+
$bricks = [];
21+
22+
foreach ($this->areaBrickManager->getBricks() as $brickName => $brickInfo) {
23+
$brick = $this->areaBrickManager->getBrick($brickName);
24+
25+
$desc = $brick->getDescription();
26+
if (empty($desc) && $newDesc = $brick->getId()) {
27+
$desc = $newDesc;
28+
}
29+
30+
$bricks[$brickName] = [
31+
'description' => $desc,
32+
'name' => $brick->getName(),
33+
'isEnabled' => true,
34+
];
35+
}
36+
37+
return $bricks;
38+
}
39+
}

src/Check/Check.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace MonitoringBundle\Check;
4+
5+
class Check
6+
{
7+
public function __construct(protected iterable $checks)
8+
{
9+
}
10+
11+
public function dispatchCheck(): array
12+
{
13+
$checks = [];
14+
/** @var CheckInterface $check */
15+
foreach ($this->checks as $check) {
16+
$checks[$check->getCheckReportIdentifier()] = $check->getCheckReport();
17+
}
18+
19+
return $checks;
20+
}
21+
}

src/Check/CheckInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace MonitoringBundle\Check;
4+
5+
interface CheckInterface
6+
{
7+
public function getCheckReportIdentifier(): string;
8+
9+
public function getCheckReport(): array;
10+
}

src/Check/CoreCheck.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace MonitoringBundle\Check;
4+
5+
use Pimcore\Version;
6+
7+
class CoreCheck implements CheckInterface
8+
{
9+
public function getCheckReportIdentifier(): string
10+
{
11+
return 'core';
12+
}
13+
14+
public function getCheckReport(): array
15+
{
16+
return [
17+
'version' => Version::getVersion(),
18+
'revision' => Version::getRevision()
19+
];
20+
}
21+
}

src/Check/ExtensionCheck.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace MonitoringBundle\Check;
4+
5+
use Pimcore\Extension\Bundle\PimcoreBundleManager;
6+
7+
class ExtensionCheck implements CheckInterface
8+
{
9+
public function __construct(protected PimcoreBundleManager $pimcoreBundleManager)
10+
{
11+
}
12+
13+
public function getCheckReportIdentifier(): string
14+
{
15+
return 'extensions';
16+
}
17+
18+
public function getCheckReport(): array
19+
{
20+
$extensions = [];
21+
22+
foreach ($this->pimcoreBundleManager->getActiveBundles() as $bundle) {
23+
$extensions[] = [
24+
'title' => $bundle->getNiceName(),
25+
'version' => $bundle->getVersion(),
26+
'identifier' => $this->pimcoreBundleManager->getBundleIdentifier($bundle),
27+
'isInstalled' => $this->pimcoreBundleManager->isInstalled($bundle),
28+
];
29+
}
30+
31+
return $extensions;
32+
}
33+
}

src/Check/UserCheck.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace MonitoringBundle\Check;
4+
5+
use MonitoringBundle\Check\CheckInterface;
6+
use Pimcore\Model\User;
7+
use Pimcore\Version;
8+
9+
class UserCheck implements CheckInterface
10+
{
11+
public function getCheckReportIdentifier(): string
12+
{
13+
return 'users';
14+
}
15+
16+
public function getCheckReport(): array
17+
{
18+
$userListing = new User\Listing();
19+
20+
$users = [];
21+
foreach ($userListing->getUsers() as $user) {
22+
23+
if (!$user instanceof User) {
24+
continue;
25+
}
26+
27+
$lastLogin = 0;
28+
29+
try {
30+
$lastLogin = $user->getLastLogin();
31+
} catch (\Throwable) {
32+
// fail silently: "User::$lastLogin must not be accessed before initialization"
33+
}
34+
35+
$users[] = [
36+
'name' => $user->getName(),
37+
'active' => $user->isActive(),
38+
'is_admin' => $user->isAdmin(),
39+
'last_login' => $lastLogin,
40+
];
41+
}
42+
43+
return $users;
44+
}
45+
}

0 commit comments

Comments
 (0)