| 1 | /************************************************************************** |
|---|
| 2 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> * |
|---|
| 3 | * * |
|---|
| 4 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp * |
|---|
| 5 | * * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify * |
|---|
| 7 | * it under the terms of the GNU General Public License as published by * |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
|---|
| 9 | * (at your option) any later version. * |
|---|
| 10 | * * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, * |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 14 | * GNU General Public License for more details. * |
|---|
| 15 | * * |
|---|
| 16 | * You should have received a copy of the GNU General Public License * |
|---|
| 17 | * along with this program; if not, write to the * |
|---|
| 18 | * Free Software Foundation, Inc., * |
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
|---|
| 20 | ***************************************************************************/ |
|---|
| 21 | #pragma once |
|---|
| 22 | |
|---|
| 23 | #include <string> |
|---|
| 24 | #include <strEx.h> |
|---|
| 25 | |
|---|
| 26 | #define MAKE_PERFDATA(alias, value, unit, warn, crit) "'" + alias + "'=" + value + unit + ";" + warn + ";" + crit + "; " |
|---|
| 27 | |
|---|
| 28 | namespace checkHolders { |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | typedef enum { warning, critical} ResultType; |
|---|
| 32 | typedef enum { above = 1, below = -1, same = 0 } checkResultType; |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | static std::string formatAbove(std::string str, ResultType what) { |
|---|
| 37 | if (what == warning) |
|---|
| 38 | return str + " > warning"; |
|---|
| 39 | else if (what == critical) |
|---|
| 40 | return str + " > critical"; |
|---|
| 41 | return str + " > unknown"; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | static std::string formatBelow(std::string str, ResultType what) { |
|---|
| 45 | if (what == warning) |
|---|
| 46 | return str + " < warning"; |
|---|
| 47 | else if (what == critical) |
|---|
| 48 | return str + " < critical"; |
|---|
| 49 | return str + " < unknown"; |
|---|
| 50 | } |
|---|
| 51 | std::string formatState(std::string str, ResultType what) { |
|---|
| 52 | if (what == warning) |
|---|
| 53 | return str + " (warning)"; |
|---|
| 54 | else if (what == critical) |
|---|
| 55 | return str + " (critical)"; |
|---|
| 56 | return str + " (unknown)"; |
|---|
| 57 | } |
|---|
| 58 | std::string formatNotFound(std::string str, ResultType what) { |
|---|
| 59 | if (what == warning) |
|---|
| 60 | return str + "not found (warning)"; |
|---|
| 61 | else if (what == critical) |
|---|
| 62 | return str + "not found (critical)"; |
|---|
| 63 | return str + "not found (unknown)"; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | typedef enum {showLong, showShort, showProblems, showUnknown} showType; |
|---|
| 67 | template <class TContents> |
|---|
| 68 | struct CheckConatiner { |
|---|
| 69 | typedef CheckConatiner<TContents> TThisType; |
|---|
| 70 | TContents warn; |
|---|
| 71 | TContents crit; |
|---|
| 72 | std::string data; |
|---|
| 73 | std::string alias; |
|---|
| 74 | |
|---|
| 75 | showType show; |
|---|
| 76 | bool perfData; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | CheckConatiner() : show(showUnknown), perfData(true) |
|---|
| 80 | {} |
|---|
| 81 | CheckConatiner(std::string data_, TContents warn_, TContents crit_) |
|---|
| 82 | : data(data_), warn(warn_), crit(crit_), show(showUnknown) |
|---|
| 83 | {} |
|---|
| 84 | CheckConatiner(std::string name_, std::string alias_, TContents warn_, TContents crit_) |
|---|
| 85 | : data(data_), alias(alias_), warn(warn_), crit(crit_), show(showUnknown) |
|---|
| 86 | {} |
|---|
| 87 | CheckConatiner(const TThisType &other) |
|---|
| 88 | : data(other.data), alias(other.alias), warn(other.warn), crit(other.crit), show(other.show) |
|---|
| 89 | {} |
|---|
| 90 | std::string getAlias() { |
|---|
| 91 | if (alias.empty()) |
|---|
| 92 | return data; |
|---|
| 93 | return alias; |
|---|
| 94 | } |
|---|
| 95 | void setDefault(TThisType def) { |
|---|
| 96 | if (!warn.hasBounds()) |
|---|
| 97 | warn = def.warn; |
|---|
| 98 | if (!crit.hasBounds()) |
|---|
| 99 | crit = def.crit; |
|---|
| 100 | if (show == showUnknown) |
|---|
| 101 | show = def.show; |
|---|
| 102 | } |
|---|
| 103 | bool showAll() { |
|---|
| 104 | return show != showProblems; |
|---|
| 105 | } |
|---|
| 106 | std::string gatherPerfData(typename TContents::TValueType &value) { |
|---|
| 107 | return crit.gatherPerfData(getAlias(), value, warn, crit); |
|---|
| 108 | } |
|---|
| 109 | void runCheck(typename TContents::TValueType &value, NSCAPI::nagiosReturn &returnCode, std::string &message, std::string &perf) { |
|---|
| 110 | std::string tstr; |
|---|
| 111 | if (crit.check(value, getAlias(), tstr, critical)) { |
|---|
| 112 | NSCHelper::escalteReturnCodeToCRIT(returnCode); |
|---|
| 113 | } else if (warn.check(value, getAlias(), tstr, warning)) { |
|---|
| 114 | NSCHelper::escalteReturnCodeToWARN(returnCode); |
|---|
| 115 | }else if (show == showLong) { |
|---|
| 116 | tstr = getAlias() + ": " + TContents::toStringLong(value); |
|---|
| 117 | }else if (show == showShort) { |
|---|
| 118 | tstr = getAlias() + ": " + TContents::toStringShort(value); |
|---|
| 119 | } |
|---|
| 120 | if (perfData) |
|---|
| 121 | perf += gatherPerfData(value); |
|---|
| 122 | if (!message.empty() && !tstr.empty()) |
|---|
| 123 | message += ", "; |
|---|
| 124 | if (!tstr.empty()) |
|---|
| 125 | message += tstr; |
|---|
| 126 | } |
|---|
| 127 | }; |
|---|
| 128 | |
|---|
| 129 | typedef unsigned __int64 disk_size_type; |
|---|
| 130 | template <typename TType = disk_size_type> |
|---|
| 131 | class disk_size_handler { |
|---|
| 132 | public: |
|---|
| 133 | static std::string print(TType value) { |
|---|
| 134 | return strEx::itos_as_BKMG(value); |
|---|
| 135 | } |
|---|
| 136 | static std::string print_perf(TType value) { |
|---|
| 137 | return strEx::itos_as_BKMG(value); |
|---|
| 138 | } |
|---|
| 139 | static TType parse(std::string s) { |
|---|
| 140 | return strEx::stoi64_as_BKMG(s); |
|---|
| 141 | } |
|---|
| 142 | static TType parse_percent(std::string s) { |
|---|
| 143 | return strEx::stoi64(s); |
|---|
| 144 | } |
|---|
| 145 | static std::string print_percent(TType value) { |
|---|
| 146 | return strEx::itos(value) + "%"; |
|---|
| 147 | } |
|---|
| 148 | static std::string print_unformated(TType value) { |
|---|
| 149 | return strEx::itos(value); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | static std::string key_total() { |
|---|
| 153 | return "Total: "; |
|---|
| 154 | } |
|---|
| 155 | static std::string key_lower() { |
|---|
| 156 | return "Used: "; |
|---|
| 157 | } |
|---|
| 158 | static std::string key_upper() { |
|---|
| 159 | return "Free: "; |
|---|
| 160 | } |
|---|
| 161 | static std::string key_prefix() { |
|---|
| 162 | return ""; |
|---|
| 163 | } |
|---|
| 164 | static std::string key_postfix() { |
|---|
| 165 | return ""; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | }; |
|---|
| 169 | |
|---|
| 170 | typedef unsigned __int64 time_type; |
|---|
| 171 | template <typename TType = time_type> |
|---|
| 172 | class time_handler { |
|---|
| 173 | public: |
|---|
| 174 | static TType parse(std::string s) { |
|---|
| 175 | return strEx::stoi64_as_time(s); |
|---|
| 176 | } |
|---|
| 177 | static TType parse_percent(std::string s) { |
|---|
| 178 | return strEx::stoi(s); |
|---|
| 179 | } |
|---|
| 180 | static std::string print(TType value) { |
|---|
| 181 | return strEx::itos_as_time(value); |
|---|
| 182 | } |
|---|
| 183 | static std::string print_percent(TType value) { |
|---|
| 184 | return strEx::itos(value) + "%"; |
|---|
| 185 | } |
|---|
| 186 | static std::string print_unformated(TType value) { |
|---|
| 187 | return strEx::itos(value); |
|---|
| 188 | } |
|---|
| 189 | static std::string print_perf(TType value) { |
|---|
| 190 | return strEx::itos(value); |
|---|
| 191 | } |
|---|
| 192 | static std::string key_prefix() { |
|---|
| 193 | return ""; |
|---|
| 194 | } |
|---|
| 195 | static std::string key_postfix() { |
|---|
| 196 | return ""; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | }; |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | class int_handler { |
|---|
| 203 | public: |
|---|
| 204 | static int parse(std::string s) { |
|---|
| 205 | return strEx::stoi(s); |
|---|
| 206 | } |
|---|
| 207 | static int parse_percent(std::string s) { |
|---|
| 208 | return strEx::stoi(s); |
|---|
| 209 | } |
|---|
| 210 | static std::string print(int value) { |
|---|
| 211 | return strEx::itos(value); |
|---|
| 212 | } |
|---|
| 213 | static std::string print_perf(int value) { |
|---|
| 214 | return strEx::itos(value); |
|---|
| 215 | } |
|---|
| 216 | static std::string print_unformated(int value) { |
|---|
| 217 | return strEx::itos(value); |
|---|
| 218 | } |
|---|
| 219 | static std::string print_percent(int value) { |
|---|
| 220 | return strEx::itos(value) + "%"; |
|---|
| 221 | } |
|---|
| 222 | static std::string key_prefix() { |
|---|
| 223 | return ""; |
|---|
| 224 | } |
|---|
| 225 | static std::string key_postfix() { |
|---|
| 226 | return ""; |
|---|
| 227 | } |
|---|
| 228 | }; |
|---|
| 229 | class int64_handler { |
|---|
| 230 | public: |
|---|
| 231 | static __int64 parse(std::string s) { |
|---|
| 232 | return strEx::stoi64(s); |
|---|
| 233 | } |
|---|
| 234 | static __int64 parse_percent(std::string s) { |
|---|
| 235 | return strEx::stoi(s); |
|---|
| 236 | } |
|---|
| 237 | static std::string print(__int64 value) { |
|---|
| 238 | return strEx::itos(value); |
|---|
| 239 | } |
|---|
| 240 | static std::string print_perf(__int64 value) { |
|---|
| 241 | return strEx::itos(value); |
|---|
| 242 | } |
|---|
| 243 | static std::string print_unformated(__int64 value) { |
|---|
| 244 | return strEx::itos(value); |
|---|
| 245 | } |
|---|
| 246 | static std::string print_percent(__int64 value) { |
|---|
| 247 | return strEx::itos(value) + "%"; |
|---|
| 248 | } |
|---|
| 249 | static std::string key_prefix() { |
|---|
| 250 | return ""; |
|---|
| 251 | } |
|---|
| 252 | static std::string key_postfix() { |
|---|
| 253 | return ""; |
|---|
| 254 | } |
|---|
| 255 | }; |
|---|
| 256 | class double_handler { |
|---|
| 257 | public: |
|---|
| 258 | static double parse(std::string s) { |
|---|
| 259 | return strEx::stod(s); |
|---|
| 260 | } |
|---|
| 261 | static double parse_percent(std::string s) { |
|---|
| 262 | return strEx::stod(s); |
|---|
| 263 | } |
|---|
| 264 | static std::string print_perf(double value) { |
|---|
| 265 | return strEx::itos(value); |
|---|
| 266 | } |
|---|
| 267 | static std::string print(double value) { |
|---|
| 268 | return strEx::itos(value); |
|---|
| 269 | } |
|---|
| 270 | static std::string print_unformated(double value) { |
|---|
| 271 | return strEx::itos(value); |
|---|
| 272 | } |
|---|
| 273 | static std::string print_percent(double value) { |
|---|
| 274 | return strEx::itos(value) + "%"; |
|---|
| 275 | } |
|---|
| 276 | static std::string key_prefix() { |
|---|
| 277 | return ""; |
|---|
| 278 | } |
|---|
| 279 | static std::string key_postfix() { |
|---|
| 280 | return ""; |
|---|
| 281 | } |
|---|
| 282 | }; |
|---|
| 283 | |
|---|
| 284 | typedef unsigned long state_type; |
|---|
| 285 | const int state_none = 0x00; |
|---|
| 286 | const int state_started = 0x01; |
|---|
| 287 | const int state_stopped = 0x02; |
|---|
| 288 | |
|---|
| 289 | class state_handler { |
|---|
| 290 | public: |
|---|
| 291 | static state_type parse(std::string s) { |
|---|
| 292 | state_type ret = state_none; |
|---|
| 293 | strEx::splitList lst = strEx::splitEx(s, ","); |
|---|
| 294 | for (strEx::splitList::const_iterator it = lst.begin(); it != lst.end(); ++it) { |
|---|
| 295 | if (*it == "started") |
|---|
| 296 | ret |= state_started; |
|---|
| 297 | else if (*it == "stopped") |
|---|
| 298 | ret |= state_stopped; |
|---|
| 299 | } |
|---|
| 300 | return ret; |
|---|
| 301 | } |
|---|
| 302 | static std::string print(state_type value) { |
|---|
| 303 | if (value == state_started) |
|---|
| 304 | return "started"; |
|---|
| 305 | else if (value == state_stopped) |
|---|
| 306 | return "stopped"; |
|---|
| 307 | return "unknown"; |
|---|
| 308 | } |
|---|
| 309 | static std::string print_unformated(state_type value) { |
|---|
| 310 | return strEx::itos(value); |
|---|
| 311 | } |
|---|
| 312 | }; |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | template <typename TType = int, class THandler = int_handler > |
|---|
| 316 | class NumericBounds { |
|---|
| 317 | public: |
|---|
| 318 | |
|---|
| 319 | bool bHasBounds_; |
|---|
| 320 | TType value_; |
|---|
| 321 | typedef typename TType TValueType; |
|---|
| 322 | typedef THandler TFormatType; |
|---|
| 323 | |
|---|
| 324 | NumericBounds() : bHasBounds_(false), value_(0) {}; |
|---|
| 325 | |
|---|
| 326 | NumericBounds(const NumericBounds & other) { |
|---|
| 327 | bHasBounds_ = other.bHasBounds_; |
|---|
| 328 | value_ = other.value_; |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | checkResultType check(TType value) const { |
|---|
| 332 | if (value == value_) |
|---|
| 333 | return same; |
|---|
| 334 | else if (value > value_) |
|---|
| 335 | return above; |
|---|
| 336 | return below; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | static std::string toStringLong(TType value) { |
|---|
| 340 | return THandler::key_prefix() + THandler::print(value) + THandler::key_postfix(); |
|---|
| 341 | } |
|---|
| 342 | static std::string toStringShort(TType value) { |
|---|
| 343 | return THandler::print(value); |
|---|
| 344 | } |
|---|
| 345 | inline bool hasBounds() const { |
|---|
| 346 | return bHasBounds_; |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | const NumericBounds & operator=(std::string value) { |
|---|
| 350 | set(value); |
|---|
| 351 | return *this; |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | TType getPerfBound(TType value) { |
|---|
| 355 | return value_; |
|---|
| 356 | } |
|---|
| 357 | static std::string gatherPerfData(std::string alias, TType &value, TType warn, TType crit) { |
|---|
| 358 | return MAKE_PERFDATA(alias, THandler::print_perf(value), "", THandler::print_perf(warn), THandler::print_perf(crit)); |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | private: |
|---|
| 362 | void set(std::string s) { |
|---|
| 363 | value_ = THandler::parse(s); |
|---|
| 364 | bHasBounds_ = true; |
|---|
| 365 | } |
|---|
| 366 | }; |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | template <typename TTypeValue, typename TTypeTotal = TTypeValue> |
|---|
| 370 | struct PercentageValueType { |
|---|
| 371 | typedef TTypeValue TValueType; |
|---|
| 372 | TTypeValue value; |
|---|
| 373 | TTypeTotal total; |
|---|
| 374 | |
|---|
| 375 | TTypeValue getUpperPercentage() { |
|---|
| 376 | return 100-(value*100/total); |
|---|
| 377 | } |
|---|
| 378 | TTypeValue getLowerPercentage() { |
|---|
| 379 | return (value*100)/total; |
|---|
| 380 | } |
|---|
| 381 | }; |
|---|
| 382 | |
|---|
| 383 | template <typename TType = int, class THandler = int_handler > |
|---|
| 384 | class NumericPercentageBounds { |
|---|
| 385 | public: |
|---|
| 386 | typedef enum { |
|---|
| 387 | none = 0, |
|---|
| 388 | percentage_upper = 1, |
|---|
| 389 | percentage_lower = 2, |
|---|
| 390 | value_upper = 3, |
|---|
| 391 | value_lower = 4, |
|---|
| 392 | } checkTypes; |
|---|
| 393 | |
|---|
| 394 | class InternalValue { |
|---|
| 395 | NumericPercentageBounds *pParent_; |
|---|
| 396 | bool isUpper_; |
|---|
| 397 | public: |
|---|
| 398 | |
|---|
| 399 | InternalValue(bool isUpper) : pParent_(NULL), isUpper_(isUpper) {} |
|---|
| 400 | void setParent(NumericPercentageBounds *pParent) { |
|---|
| 401 | pParent_ = pParent; |
|---|
| 402 | } |
|---|
| 403 | const InternalValue & operator=(std::string value) { |
|---|
| 404 | std::string::size_type p = value.find_first_of('%'); |
|---|
| 405 | if (p != std::string::npos) { |
|---|
| 406 | if (isUpper_) |
|---|
| 407 | pParent_->setPercentageUpper(value); |
|---|
| 408 | else |
|---|
| 409 | pParent_->setPercentageLower(value); |
|---|
| 410 | } else { |
|---|
| 411 | if (isUpper_) |
|---|
| 412 | pParent_->setUpper(value); |
|---|
| 413 | else |
|---|
| 414 | pParent_->setLower(value); |
|---|
| 415 | } |
|---|
| 416 | return *this; |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | }; |
|---|
| 420 | |
|---|
| 421 | checkTypes type_; |
|---|
| 422 | typename TType::TValueType value_; |
|---|
| 423 | typedef typename TType TValueType; |
|---|
| 424 | typedef THandler TFormatType; |
|---|
| 425 | typedef NumericPercentageBounds<TType, THandler> TMyType; |
|---|
| 426 | InternalValue upper; |
|---|
| 427 | InternalValue lower; |
|---|
| 428 | |
|---|
| 429 | NumericPercentageBounds() : type_(none), upper(true), lower(false) { |
|---|
| 430 | upper.setParent(this); |
|---|
| 431 | lower.setParent(this); |
|---|
| 432 | } |
|---|
| 433 | |
|---|
| 434 | NumericPercentageBounds(const NumericPercentageBounds &other) { |
|---|
| 435 | type_ = other.type_; |
|---|
| 436 | value_ = other.value_; |
|---|
| 437 | } |
|---|
| 438 | checkResultType check(TType value) const { |
|---|
| 439 | if (type_ == percentage_lower) { |
|---|
| 440 | if (value.getLowerPercentage() == value_) |
|---|
| 441 | return same; |
|---|
| 442 | else if (value.getLowerPercentage() > value_) |
|---|
| 443 | return above; |
|---|
| 444 | } else if (type_ == percentage_upper) { |
|---|
| 445 | if (value.getUpperPercentage() == value_) |
|---|
| 446 | return same; |
|---|
| 447 | else if (value.getUpperPercentage() > value_) |
|---|
| 448 | return above; |
|---|
| 449 | } else if (type_ == value_lower) { |
|---|
| 450 | if (value.value == value_) |
|---|
| 451 | return same; |
|---|
| 452 | else if (value.value > value_) |
|---|
| 453 | return above; |
|---|
| 454 | } else if (type_ == value_upper) { |
|---|
| 455 | if ((value.total-value.value) == value_) |
|---|
| 456 | return same; |
|---|
| 457 | else if ((value.total-value.value) > value_) |
|---|
| 458 | return above; |
|---|
| 459 | } else { |
|---|
| 460 | std::cout << "Damn...: " << type_ << std::endl; |
|---|
| 461 | throw "Damn..."; |
|---|
| 462 | } |
|---|
| 463 | return below; |
|---|
| 464 | } |
|---|
| 465 | static std::string toStringShort(TType value) { |
|---|
| 466 | return THandler::print(value.value); |
|---|
| 467 | |
|---|
| 468 | } |
|---|
| 469 | static std::string toStringLong(TType value) { |
|---|
| 470 | return |
|---|
| 471 | THandler::key_total() + THandler::print(value.total) + |
|---|
| 472 | " - " + THandler::key_lower() + THandler::print(value.value) + |
|---|
| 473 | " (" + THandler::print_percent(value.getLowerPercentage()) + ")" + |
|---|
| 474 | " - " + THandler::key_upper() + THandler::print(value.total-value.value) + |
|---|
| 475 | " (" + THandler::print_percent(value.getUpperPercentage()) + ")"; |
|---|
| 476 | } |
|---|
| 477 | inline bool hasBounds() const { |
|---|
| 478 | return type_ != none; |
|---|
| 479 | } |
|---|
| 480 | typename TType::TValueType getPerfBound(TType value) { |
|---|
| 481 | return value_; |
|---|
| 482 | } |
|---|
| 483 | std::string gatherPerfData(std::string alias, TType &value, typename TType::TValueType warn, typename TType::TValueType crit) { |
|---|
| 484 | if (type_ == percentage_upper) { |
|---|
| 485 | return |
|---|
| 486 | MAKE_PERFDATA(alias, THandler::print_unformated(value.getUpperPercentage()), "%", |
|---|
| 487 | THandler::print_unformated(warn), THandler::print_unformated(crit)); |
|---|
| 488 | } else if (type_ == percentage_lower) { |
|---|
| 489 | return |
|---|
| 490 | MAKE_PERFDATA(alias, THandler::print_unformated(value.getLowerPercentage()), "%", |
|---|
| 491 | THandler::print_unformated(warn), THandler::print_unformated(crit)); |
|---|
| 492 | } else { |
|---|
| 493 | return |
|---|
| 494 | MAKE_PERFDATA(alias, THandler::print_perf(value.value), "", |
|---|
| 495 | THandler::print_perf(warn), THandler::print_perf(crit)); |
|---|
| 496 | } |
|---|
| 497 | } |
|---|
| 498 | private: |
|---|
| 499 | void setUpper(std::string s) { |
|---|
| 500 | value_ = THandler::parse(s); |
|---|
| 501 | type_ = value_upper; |
|---|
| 502 | } |
|---|
| 503 | void setLower(std::string s) { |
|---|
| 504 | value_ = THandler::parse(s); |
|---|
| 505 | type_ = value_lower; |
|---|
| 506 | } |
|---|
| 507 | void setPercentageUpper(std::string s) { |
|---|
| 508 | value_ = THandler::parse_percent(s); |
|---|
| 509 | type_ = percentage_upper; |
|---|
| 510 | } |
|---|
| 511 | void setPercentageLower(std::string s) { |
|---|
| 512 | value_ = THandler::parse_percent(s); |
|---|
| 513 | type_ = percentage_lower; |
|---|
| 514 | } |
|---|
| 515 | }; |
|---|
| 516 | |
|---|
| 517 | template <typename TType = state_type, class THandler = state_handler > |
|---|
| 518 | class StateBounds { |
|---|
| 519 | public: |
|---|
| 520 | TType value_; |
|---|
| 521 | typedef typename TType TValueType; |
|---|
| 522 | typedef THandler TFormatType; |
|---|
| 523 | typedef StateBounds<TType, THandler> TMyType; |
|---|
| 524 | |
|---|
| 525 | StateBounds() : value_(state_none) {} |
|---|
| 526 | StateBounds(const StateBounds &other) : value_(other.value_) {} |
|---|
| 527 | |
|---|
| 528 | bool check(TType value) const { |
|---|
| 529 | return (value & value_) != 0; |
|---|
| 530 | } |
|---|
| 531 | static std::string toStringLong(TType value) { |
|---|
| 532 | return THandler::print(value); |
|---|
| 533 | } |
|---|
| 534 | static std::string toStringShort(TType value) { |
|---|
| 535 | return THandler::print(value); |
|---|
| 536 | } |
|---|
| 537 | inline bool hasBounds() const { |
|---|
| 538 | return value_ != state_none; |
|---|
| 539 | } |
|---|
| 540 | TType getPerfBound(TType value) { |
|---|
| 541 | return value_; |
|---|
| 542 | } |
|---|
| 543 | std::string gatherPerfData(std::string alias, TType &value, TType warn, TType crit) { |
|---|
| 544 | return ""; |
|---|
| 545 | } |
|---|
| 546 | const StateBounds & operator=(std::string value) { |
|---|
| 547 | set(value); |
|---|
| 548 | return *this; |
|---|
| 549 | } |
|---|
| 550 | private: |
|---|
| 551 | void set(std::string s) { |
|---|
| 552 | value_ = THandler::parse(s); |
|---|
| 553 | } |
|---|
| 554 | }; |
|---|
| 555 | |
|---|
| 556 | template <typename TMaxMinType = int, typename TStateType = state_type> |
|---|
| 557 | struct MaxMinStateValueType { |
|---|
| 558 | TMaxMinType count; |
|---|
| 559 | TStateType state; |
|---|
| 560 | }; |
|---|
| 561 | |
|---|
| 562 | |
|---|
| 563 | template <class TValueType = MaxMinStateValueType, class TNumericHolder = NumericBounds<int, int_handler>, class TStateHolder = StateBounds<state_type, state_handler> > |
|---|
| 564 | class MaxMinStateBounds { |
|---|
| 565 | public: |
|---|
| 566 | TNumericHolder max; |
|---|
| 567 | TNumericHolder min; |
|---|
| 568 | TStateHolder state; |
|---|
| 569 | typedef MaxMinStateBounds<TValueType, TNumericHolder, TStateHolder > TMyType; |
|---|
| 570 | |
|---|
| 571 | typedef typename TValueType TValueType; |
|---|
| 572 | |
|---|
| 573 | MaxMinStateBounds() {} |
|---|
| 574 | MaxMinStateBounds(const MaxMinStateBounds &other) { |
|---|
| 575 | state = other.state; |
|---|
| 576 | max = other.max; |
|---|
| 577 | min = other.min; |
|---|
| 578 | } |
|---|
| 579 | bool hasBounds() { |
|---|
| 580 | return state.hasBounds() || max.hasBounds() || min.hasBounds(); |
|---|
| 581 | } |
|---|
| 582 | |
|---|
| 583 | static std::string toStringLong(typename TValueType &value) { |
|---|
| 584 | return TNumericHolder::toStringLong(value.count) + ", " + TStateHolder::toStringLong(value.state); |
|---|
| 585 | } |
|---|
| 586 | static std::string toStringShort(typename TValueType &value) { |
|---|
| 587 | return TNumericHolder::toStringShort(value.count); |
|---|
| 588 | } |
|---|
| 589 | /* |
|---|
| 590 | void formatString(std::string &message, typename TValueType &value) { |
|---|
| 591 | if (state.hasBounds()) |
|---|
| 592 | message = state.toString(value.state); |
|---|
| 593 | else if (max.hasBounds()) |
|---|
| 594 | message = max.toString(value.count); |
|---|
| 595 | else if (min.hasBounds()) |
|---|
| 596 | message = max.toString(value.count); |
|---|
| 597 | } |
|---|
| 598 | */ |
|---|
| 599 | std::string gatherPerfData(std::string alias, typename TValueType &value, TMyType &warn, TMyType &crit) { |
|---|
| 600 | if (state.hasBounds()) { |
|---|
| 601 | // @todo |
|---|
| 602 | } else if (max.hasBounds()) { |
|---|
| 603 | return max.gatherPerfData(alias, value.count, warn.max.getPerfBound(value.count), crit.max.getPerfBound(value.count)); |
|---|
| 604 | } else if (min.hasBounds()) { |
|---|
| 605 | return min.gatherPerfData(alias, value.count, warn.min.getPerfBound(value.count), crit.min.getPerfBound(value.count)); |
|---|
| 606 | } |
|---|
| 607 | return ""; |
|---|
| 608 | } |
|---|
| 609 | bool check(typename TValueType &value, std::string lable, std::string &message, ResultType type) { |
|---|
| 610 | if ((state.hasBounds())&&(!state.check(value.state))) { |
|---|
| 611 | message = lable + ": " + formatState(TStateHolder::toStringShort(value.state), type); |
|---|
| 612 | return true; |
|---|
| 613 | } else if ((max.hasBounds())&&(max.check(value.count) != below)) { |
|---|
| 614 | message = lable + ": " + formatAbove(TNumericHolder::toStringShort(value.count), type); |
|---|
| 615 | return true; |
|---|
| 616 | } else if ((min.hasBounds())&&(min.check(value.count) != above)) { |
|---|
| 617 | message = lable + ": " + formatBelow(TNumericHolder::toStringShort(value.count), type); |
|---|
| 618 | return true; |
|---|
| 619 | } else { |
|---|
| 620 | //std::cout << "No bounds specified..." << std::endl; |
|---|
| 621 | } |
|---|
| 622 | return false; |
|---|
| 623 | } |
|---|
| 624 | |
|---|
| 625 | }; |
|---|
| 626 | |
|---|
| 627 | template <class TStateHolder = StateBounds<state_type, state_handler> > |
|---|
| 628 | class SimpleStateBounds { |
|---|
| 629 | public: |
|---|
| 630 | TStateHolder state; |
|---|
| 631 | typedef SimpleStateBounds<TStateHolder > TMyType; |
|---|
| 632 | |
|---|
| 633 | typedef typename TStateHolder::TValueType TValueType; |
|---|
| 634 | |
|---|
| 635 | SimpleStateBounds() {} |
|---|
| 636 | SimpleStateBounds(const SimpleStateBounds &other) { |
|---|
| 637 | state = other.state; |
|---|
| 638 | } |
|---|
| 639 | bool hasBounds() { |
|---|
| 640 | return state.hasBounds(); |
|---|
| 641 | } |
|---|
| 642 | static std::string toStringLong(typename TValueType &value) { |
|---|
| 643 | return TStateHolder::toStringLong(value); |
|---|
| 644 | } |
|---|
| 645 | static std::string toStringShort(typename TValueType &value) { |
|---|
| 646 | return TStateHolder::toStringShort(value); |
|---|
| 647 | } |
|---|
| 648 | std::string gatherPerfData(std::string alias, typename TValueType &value, TMyType &warn, TMyType &crit) { |
|---|
| 649 | if (state.hasBounds()) { |
|---|
| 650 | // @todo |
|---|
| 651 | } |
|---|
| 652 | return ""; |
|---|
| 653 | } |
|---|
| 654 | bool check(typename TValueType &value, std::string lable, std::string &message, ResultType type) { |
|---|
| 655 | if ((state.hasBounds())&&(!state.check(value))) { |
|---|
| 656 | message = lable + ": " + formatState(TStateHolder::toStringLong(value), type); |
|---|
| 657 | return true; |
|---|
| 658 | } else { |
|---|
| 659 | //std::cout << "No bounds specified..." << std::endl; |
|---|
| 660 | } |
|---|
| 661 | return false; |
|---|
| 662 | } |
|---|
| 663 | |
|---|
| 664 | }; |
|---|
| 665 | |
|---|
| 666 | template <class THolder = NumericBounds<int, int_handler> > |
|---|
| 667 | class MaxMinBounds { |
|---|
| 668 | public: |
|---|
| 669 | THolder max; |
|---|
| 670 | THolder min; |
|---|
| 671 | typedef MaxMinBounds<THolder > TMyType; |
|---|
| 672 | |
|---|
| 673 | typedef typename THolder::TValueType TValueType; |
|---|
| 674 | // typedef THolder::TFormatType TFormatType; |
|---|
| 675 | |
|---|
| 676 | MaxMinBounds() {} |
|---|
| 677 | MaxMinBounds(const MaxMinBounds &other) { |
|---|
| 678 | max = other.max; |
|---|
| 679 | min = other.min; |
|---|
| 680 | } |
|---|
| 681 | bool hasBounds() { |
|---|
| 682 | return max.hasBounds() || min.hasBounds(); |
|---|
| 683 | } |
|---|
| 684 | static std::string toStringLong(typename THolder::TValueType &value) { |
|---|
| 685 | return THolder::toStringLong(value); |
|---|
| 686 | } |
|---|
| 687 | static std::string toStringShort(typename THolder::TValueType &value) { |
|---|
| 688 | return THolder::toStringShort(value); |
|---|
| 689 | } |
|---|
| 690 | std::string gatherPerfData(std::string alias, typename THolder::TValueType &value, TMyType &warn, TMyType &crit) { |
|---|
| 691 | if (max.hasBounds()) { |
|---|
| 692 | return max.gatherPerfData(alias, value, warn.max.getPerfBound(value), crit.max.getPerfBound(value)); |
|---|
| 693 | } else if (min.hasBounds()) { |
|---|
| 694 | return min.gatherPerfData(alias, value, warn.min.getPerfBound(value), crit.min.getPerfBound(value)); |
|---|
| 695 | } else { |
|---|
| 696 | NSC_DEBUG_MSG_STD("Missing bounds for maxmin-bounds check: " + alias); |
|---|
| 697 | } |
|---|
| 698 | return ""; |
|---|
| 699 | } |
|---|
| 700 | bool check(typename THolder::TValueType &value, std::string lable, std::string &message, ResultType type) { |
|---|
| 701 | if ((max.hasBounds())&&(max.check(value) != below)) { |
|---|
| 702 | message = lable + ": " + formatAbove(THolder::toStringLong(value), type); |
|---|
| 703 | return true; |
|---|
| 704 | } else if ((min.hasBounds())&&(min.check(value) != above)) { |
|---|
| 705 | message = lable + ": " + formatBelow(THolder::toStringLong(value), type); |
|---|
| 706 | return true; |
|---|
| 707 | } else { |
|---|
| 708 | //std::cout << "No bounds specified..." << std::endl; |
|---|
| 709 | } |
|---|
| 710 | return false; |
|---|
| 711 | } |
|---|
| 712 | |
|---|
| 713 | }; |
|---|
| 714 | typedef MaxMinBounds<NumericBounds<double, double_handler> > MaxMinBoundsDouble; |
|---|
| 715 | typedef MaxMinBounds<NumericBounds<__int64, int64_handler> > MaxMinBoundsInt64; |
|---|
| 716 | typedef MaxMinBounds<NumericBounds<int, int_handler> > MaxMinBoundsInteger; |
|---|
| 717 | typedef MaxMinBounds<NumericBounds<unsigned int, int_handler> > MaxMinBoundsUInteger; |
|---|
| 718 | typedef MaxMinBounds<NumericBounds<unsigned long int, int_handler> > MaxMinBoundsULongInteger; |
|---|
| 719 | typedef MaxMinBounds<NumericBounds<disk_size_type, disk_size_handler<disk_size_type> > > MaxMinBoundsDiscSize; |
|---|
| 720 | typedef MaxMinBounds<NumericBounds<time_type, time_handler<time_type> > > MaxMinBoundsTime; |
|---|
| 721 | |
|---|
| 722 | |
|---|
| 723 | //typedef MaxMinBounds<NumericPercentageBounds<PercentageValueType<int ,int>, int_handler> > MaxMinPercentageBoundsInteger; |
|---|
| 724 | //typedef MaxMinBounds<NumericPercentageBounds<PercentageValueType<__int64, __int64>, int64_handler> > MaxMinPercentageBoundsInt64; |
|---|
| 725 | //typedef MaxMinBounds<NumericPercentageBounds<PercentageValueType<double, double>, double_handler> > MaxMinPercentageBoundsDouble; |
|---|
| 726 | typedef MaxMinBounds<NumericPercentageBounds<PercentageValueType<disk_size_type, disk_size_type>, disk_size_handler<> > > MaxMinPercentageBoundsDiskSize; |
|---|
| 727 | typedef MaxMinBounds<NumericPercentageBounds<PercentageValueType<__int64, __int64>, disk_size_handler<__int64> > > MaxMinPercentageBoundsDiskSizei64; |
|---|
| 728 | |
|---|
| 729 | typedef MaxMinStateBounds<MaxMinStateValueType<int, state_type>, NumericBounds<int, int_handler>, StateBounds<state_type, state_handler> > MaxMinStateBoundsStateBoundsInteger; |
|---|
| 730 | typedef SimpleStateBounds<StateBounds<state_type, state_handler> > SimpleBoundsStateBoundsInteger; |
|---|
| 731 | } |
|---|
| 732 | |
|---|