]> granicus.if.org Git - icinga2/blob - plugins/thresholds.h
Make check plugins decent
[icinga2] / plugins / thresholds.h
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19 #ifndef THRESHOLDS_H
20 #define THRESHOLDS_H
21 #include <string>
22
23 enum Bunit { BunitB = 0, BunitkB = 1, BunitMB = 2, BunitGB = 3, BunitTB = 4 };
24 enum Tunit { TunitMS, TunitS, TunitM, TunitH };
25 enum state { OK = 0, WARNING = 1, CRITICAL = 2 };
26
27 class threshold
28 {
29 public:
30         double lower, upper;
31         //TRUE means everything BELOW upper/outside [lower-upper] is fine
32         bool legal, perc, set;
33
34         threshold(bool l = true)
35                 : set(false), legal(l) {}
36
37         threshold(const double v, const double c, bool l = true, bool p = false)
38                 : lower(v), upper(c), legal(l), perc(p), set(true) {}
39
40         //return TRUE if the threshold is broken
41         bool rend(const double b)
42         {
43                 if (!set)
44                         return set;
45                 if (lower == upper)
46                         return b > upper == legal;
47                 else
48                         return (b < lower || upper < b) != legal;
49         }
50
51         //returns a printable string of the threshold
52         std::wstring pString()
53         {
54                 if (!set)
55                         return L"0";
56
57                 std::wstring s;
58                 if (!legal)
59                         s.append(L"!");
60
61                 if (lower != upper) {
62                         if (perc)
63                                 s.append(L"[").append(std::to_wstring(lower)).append(L"%").append(L"-")
64                                 .append(std::to_wstring(upper)).append(L"%").append(L"]");
65                         else
66                                 s.append(L"[").append(std::to_wstring(lower)).append(L"-")
67                                 .append(std::to_wstring(upper)).append(L"]");
68                 } else {
69                         if (perc)
70                                 s = std::to_wstring(lower).append(L"%");
71                         else
72                                 s = std::to_wstring(lower);
73                 }
74                 return s;
75         }
76 };
77
78 threshold parse(const std::wstring&);
79 Bunit parseBUnit(const wchar_t *);
80 std::wstring BunitStr(const Bunit&);
81 Tunit parseTUnit(const wchar_t *);
82 std::wstring TunitStr(const Tunit&);
83 #endif