]> granicus.if.org Git - icinga2/blob - plugins/thresholds.cpp
Change B/s unit to B to comply with Nagios plugin spec
[icinga2] / plugins / thresholds.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 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
20 #include "thresholds.h"
21 #include <boost/algorithm/string.hpp>
22 #include <boost/lexical_cast.hpp>
23 #include <iostream>
24
25 using namespace boost::algorithm;
26
27 threshold::threshold()
28         : set(false)
29 {}
30
31 threshold::threshold(CONST std::wstring& stri)
32 {
33         if (stri.empty())
34                 throw std::invalid_argument("Threshold must not be empty");
35
36         std::wstring str = stri;
37
38         //kill whitespace
39         boost::algorithm::trim(str);
40
41         bool low = (str.at(0) == L'!');
42         if (low)
43                 str = std::wstring(str.begin() + 1, str.end());
44
45         bool pc = false;
46
47         if (str.at(0) == L'[' && str.at(str.length() - 1) == L']') {//is range
48                 str = std::wstring(str.begin() + 1, str.end() - 1);
49                 std::vector<std::wstring> svec;
50                 boost::split(svec, str, boost::is_any_of(L"-"));
51                 if (svec.size() != 2)
52                         throw std::invalid_argument("Threshold range requires two arguments");
53                 std::wstring str1 = svec.at(0), str2 = svec.at(1);
54
55                 if (str1.at(str1.length() - 1) == L'%' && str2.at(str2.length() - 1) == L'%') {
56                         pc = true;
57                         str1 = std::wstring(str1.begin(), str1.end() - 1);
58                         str2 = std::wstring(str2.begin(), str2.end() - 1);
59                 }
60
61                 try {
62                         boost::algorithm::trim(str1);
63                         lower = boost::lexical_cast<DOUBLE>(str1);
64                         boost::algorithm::trim(str2);
65                         upper = boost::lexical_cast<DOUBLE>(str2);
66                         legal = !low; perc = pc; set = true;
67                 } catch (CONST boost::bad_lexical_cast&) {
68                         throw std::invalid_argument("Unknown Threshold type");
69                 }
70         } else { //not range
71                 if (str.at(str.length() - 1) == L'%') {
72                         pc = true;
73                         str = std::wstring(str.begin(), str.end() - 1);
74                 }
75                 try {
76                         boost::algorithm::trim(str);
77                         lower = upper = boost::lexical_cast<DOUBLE>(str);
78                         legal = !low; perc = pc; set = true;
79                 } catch (CONST boost::bad_lexical_cast&) {
80                         throw std::invalid_argument("Unknown Threshold type");
81                 }
82         }
83 }
84
85 //return TRUE if the threshold is broken
86 BOOL threshold::rend(CONST DOUBLE val, CONST DOUBLE max)
87 {
88         DOUBLE upperAbs = upper;
89         DOUBLE lowerAbs = lower;
90
91         if (perc) {
92                 upperAbs = upper / 100.0 * max;
93                 lowerAbs = lower / 100.0 * max;
94         }
95
96         if (!set)
97                 return set;
98         if (lowerAbs == upperAbs)
99                 return val > upperAbs == legal;
100         else
101                 return (val < lowerAbs || upperAbs < val) != legal;
102 }
103
104 //returns a printable string of the threshold
105 std::wstring threshold::pString(CONST DOUBLE max)
106 {
107         if (!set)
108                 return L"";
109         //transform percentages to abolute values
110         DOUBLE lowerAbs = lower;
111         DOUBLE upperAbs = upper;
112         if (perc) {
113                 lowerAbs = lower / 100.0 * max;
114                 upperAbs = upper / 100.0 * max;
115         }
116
117         std::wstring s, lowerStr = removeZero(lowerAbs), 
118                                         upperStr = removeZero(upperAbs);
119
120         if (lower != upper) {
121                 s.append(L"[").append(lowerStr).append(L"-")
122                 .append(upperStr).append(L"]");
123         } else 
124                 s.append(lowerStr);
125         
126         return s;
127 }
128
129 std::wstring removeZero(DOUBLE val)
130 {
131         std::wstring ret = boost::lexical_cast<std::wstring>(val);
132         INT pos = ret.length();
133         if (ret.find_first_of(L".") == std::string::npos)
134                 return ret;
135         for (std::wstring::reverse_iterator rit = ret.rbegin(); rit != ret.rend(); ++rit) {
136                 if (*rit == L'.') {
137                         return ret.substr(0, pos - 1);
138                 }
139                 if (*rit != L'0') {
140                         return ret.substr(0, pos);
141                 }
142                 pos--;
143         }
144         return L"0";
145 }
146
147 std::vector<std::wstring> splitMultiOptions(std::wstring str)
148 {
149         std::vector<std::wstring> sVec;
150         boost::split(sVec, str, boost::is_any_of(L","));
151         return sVec;
152 }
153
154 Bunit parseBUnit(CONST std::wstring& str)
155 {
156         std::wstring wstr = to_upper_copy(str);
157
158         if (wstr == L"B")
159                 return BunitB;
160         if (wstr == L"KB")
161                 return BunitkB;
162         if (wstr == L"MB")
163                 return BunitMB;
164         if (wstr == L"GB")
165                 return BunitGB;
166         if (wstr == L"TB")
167                 return BunitTB;
168
169         throw std::invalid_argument("Unknown unit type");
170 }
171
172 std::wstring BunitStr(CONST Bunit& unit) 
173 {
174         switch (unit) {
175         case BunitB:
176                 return L"B";
177         case BunitkB:
178                 return L"kB";
179         case BunitMB:
180                 return L"MB";
181         case BunitGB:
182                 return L"GB";
183         case BunitTB:
184                 return L"TB";
185         }
186         return NULL;
187 }
188
189 Tunit parseTUnit(CONST std::wstring& str) {
190         std::wstring wstr = to_lower_copy(str);
191
192         if (wstr == L"ms")
193                 return TunitMS;
194         if (wstr == L"s")
195                 return TunitS;
196         if (wstr == L"m")
197                 return TunitM;
198         if (wstr == L"h")
199                 return TunitH;
200
201         throw std::invalid_argument("Unknown unit type");
202 }
203
204 std::wstring TunitStr(CONST Tunit& unit) 
205 {
206         switch (unit) {
207         case TunitMS:
208                 return L"ms";
209         case TunitS:
210                 return L"s";
211         case TunitM:
212                 return L"m";
213         case TunitH:
214                 return L"h";
215         }
216         return NULL;
217 }
218
219 VOID die(DWORD err)
220 {
221         if (!err)
222                 err = GetLastError();
223         LPWSTR mBuf = NULL;
224         if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
225                 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&mBuf, 0, NULL))
226                         std::wcout << "Failed to format error message, last error was: " << err << '\n';
227         else
228                 std::wcout << mBuf << std::endl;
229 }