forked from Icinga/icingaweb2-module-vspheredb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiskUsageRuleDefinition.php
More file actions
128 lines (109 loc) · 3.79 KB
/
Copy pathDiskUsageRuleDefinition.php
File metadata and controls
128 lines (109 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace Icinga\Module\Vspheredb\Monitoring\Rule\Definition;
use Icinga\Module\Vspheredb\DbObject\BaseDbObject;
use Icinga\Module\Vspheredb\Monitoring\Rule\Enum\ObjectType;
use Icinga\Module\Vspheredb\Monitoring\Rule\Settings;
use Icinga\Module\Vspheredb\Monitoring\SingleCheckResult;
class DiskUsageRuleDefinition extends MonitoringRuleDefinition
{
public const SUPPORTED_OBJECT_TYPES = [
ObjectType::VIRTUAL_MACHINE,
];
public static function getIdentifier(): string
{
return 'DiskUsage';
}
public static function isMultiInstanceRule(): bool
{
return true;
}
public function getLabel(): string
{
return $this->translate('Disk Usage');
}
public function getInternalDefaults(): array
{
return [
'threshold_precedence' => 'best_wins'
];
}
protected function checkDisk($disk, Settings $settings): ?SingleCheckResult
{
if ((int) $disk->capacity === 0) {
return null;
}
if ($filter = $settings->get('disk_path_filter')) {
if (!$this->filterMatchesPath($filter, $disk->disk_path)) {
return null;
}
}
if ($filter = $settings->get('disk_path_ignore')) {
if ($this->filterMatchesPath($filter, $disk->disk_path)) {
return null;
}
}
return MemoryUsageHelper::prepareState($settings, $disk->free_space, $disk->capacity, $disk->disk_path);
}
protected function filterMatchesPath(string $filterString, string $path): bool
{
$parts = explode('|', $filterString);
foreach ($parts as $part) {
if (self::stringMatches($part, $path)) {
return true;
}
}
return false;
}
protected static function stringMatches(string $filterString, string $string): bool
{
if (strpos($filterString, '*') === false) {
return $string === $filterString;
}
$parts = [];
foreach (preg_split('~\*~', $filterString) as $part) {
$parts[] = preg_quote($part, '/');
}
$pattern = '/^' . implode('.*', $parts) . '$/';
return (bool) preg_match($pattern, $string);
}
public function checkObject(BaseDbObject $object, Settings $settings): array
{
$this->assertSupportedObject($object);
$db = $object->getConnection()->getDbAdapter();
$disks = $db->fetchAll($db->select()->from('vm_disk_usage', [
'disk_path',
'capacity',
'free_space',
])->where('vm_disk_usage.vm_uuid = ?', $object->getConnection()->quoteBinary($object->get('uuid'))));
$instanceSettings = [];
/** @var string $key */
foreach ($settings->listMainKeys() as $key) {
$instanceSettings[$key] = $settings->withRemovedKey($key);
}
$results = [];
foreach ($disks as $disk) {
foreach ($instanceSettings as $instance) {
if ($instance->isDisabled()) {
continue;
}
if ($result = $this->checkDisk($disk, $instance)) {
$results[] = $result;
}
}
}
return $results;
}
public function getParameters(): array
{
return [
'disk_path_filter' => ['text', [
'label' => $this->translate('Apply to specific disks only'),
'placeholder' => 'e.g. C:\\, /var/*, C:\\|D:\\|E:\\',
]],
'disk_path_ignore' => ['text', [
'label' => $this->translate('Ignore specific disks'),
'placeholder' => 'e.g. C:\\, */volume-subpaths/*|/var/lib/kubelet/*',
]],
] + MemoryUsageHelper::getParameters();
}
}