summaryrefslogtreecommitdiffstats
path: root/library/Director/CheckPlugin/Threshold.php
blob: 76aac4eb39032e8f751ca55e032686451477fae3 (plain)
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
<?php

namespace Icinga\Module\Director\CheckPlugin;

class Threshold
{
    /** @var Range */
    protected $warning;

    /** @var Range */
    protected $critical;

    public function __construct($warning = null, $critical = null)
    {
        if ($warning !== null) {
            $this->warning = Range::wantRange($warning);
        }

        if ($critical !== null) {
            $this->critical = Range::wantRange($critical);
        }
    }

    public static function check($value, $message, $warning = null, $critical = null)
    {
        $threshold = new static($warning, $critical);
        $state = $threshold->checkValue($value);
        return new CheckResult($message, $state);
    }

    public function checkValue($value)
    {
        if ($this->critical !== null) {
            if (! $this->critical->valueIsValid($value)) {
                return PluginState::critical();
            }
        }

        if ($this->warning !== null) {
            if (! $this->warning->valueIsValid($value)) {
                return PluginState::warning();
            }
        }

        return PluginState::ok();
    }
}