summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/library/Monitoring/Plugin/ThresholdRange.php
blob: bd27b8b609e09820c3617c04565abe83c3922390 (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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/* Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Monitoring\Plugin;

/**
 * The warning/critical threshold of a measured value
 */
class ThresholdRange
{
    /**
     * The smallest value inside the range (null stands for -∞)
     *
     * @var float|null
     */
    protected $min;

    /**
     * The biggest value inside the range (null stands for ∞)
     *
     * @var float|null
     */
    protected $max;

    /**
     * Whether to invert the result of contains()
     *
     * @var bool
     */
    protected $inverted = false;

    /**
     * The unmodified range as passed to fromString()
     *
     * @var string
     */
    protected $raw;

    /**
     * Create a new instance based on a threshold range conforming to <https://nagios-plugins.org/doc/guidelines.html>
     *
     * @param   string  $rawRange
     *
     * @return  ThresholdRange
     */
    public static function fromString($rawRange)
    {
        $range = new static();
        $range->raw = $rawRange;

        if ($rawRange == '') {
            return $range;
        }

        $rawRange = ltrim($rawRange);
        if (substr($rawRange, 0, 1) === '@') {
            $range->setInverted();
            $rawRange = substr($rawRange, 1);
        }

        if (strpos($rawRange, ':') === false) {
            $min = 0.0;
            $max = floatval(trim($rawRange));
        } else {
            list($min, $max) = explode(':', $rawRange, 2);
            $min = trim($min);
            $max = trim($max);

            switch ($min) {
                case '':
                    $min = 0.0;
                    break;
                case '~':
                    $min = null;
                    break;
                default:
                    $min = floatval($min);
            }

            $max = empty($max) ? null : floatval($max);
        }

        return $range->setMin($min)
            ->setMax($max);
    }

    /**
     * Set the smallest value inside the range (null stands for -∞)
     *
     * @param   float|null  $min
     *
     * @return  $this
     */
    public function setMin($min)
    {
        $this->min = $min;
        return $this;
    }

    /**
     * Get the smallest value inside the range (null stands for -∞)
     *
     * @return  float|null
     */
    public function getMin()
    {
        return $this->min;
    }

    /**
     * Set the biggest value inside the range (null stands for ∞)
     *
     * @param   float|null  $max
     *
     * @return  $this
     */
    public function setMax($max)
    {
        $this->max = $max;
        return $this;
    }

    /**
     * Get the biggest value inside the range (null stands for ∞)
     *
     * @return  float|null
     */
    public function getMax()
    {
        return $this->max;
    }

    /**
     * Set whether to invert the result of contains()
     *
     * @param   bool    $inverted
     *
     * @return  $this
     */
    public function setInverted($inverted = true)
    {
        $this->inverted = $inverted;
        return $this;
    }

    /**
     * Get whether to invert the result of contains()
     *
     * @return  bool
     */
    public function isInverted()
    {
        return $this->inverted;
    }

    /**
     * Return whether $value is inside $this
     *
     * @param   float   $value
     *
     * @return  bool
     */
    public function contains($value)
    {
        return (bool) ($this->inverted ^ (
            ($this->min === null || $this->min <= $value) && ($this->max === null || $this->max >= $value)
        ));
    }

    /**
     * Return the textual representation of $this, suitable for fromString()
     *
     * @return  string
     */
    public function __toString()
    {
        return (string) $this->raw;
    }
}