source: nscp/include/checkHelpers.hpp @ 7f9c823

0.4.00.4.10.4.2
Last change on this file since 7f9c823 was 7f9c823, checked in by Michael Medin <michael@…>, 4 years ago

First attempt at serious boostification.
NOTICE! This is NOT a complete edition, it build and runs but many features are disabled and/or broken.
But we have working, sockets and mutexes and conversion functions from boost inside now and more to come...
Also started to build with CMake since it works better then boost.build

  • Property mode set to 100644
File size: 28.4 KB
Line 
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) _T("'") + alias + _T("'=") + value + unit + _T(";") + warn + _T(";") + crit + _T("; ")
27
28namespace checkHolders {
29
30
31        typedef enum { warning, critical} ResultType;
32        typedef enum { above = 1, below = -1, same = 0 } checkResultType;
33        class check_exception {
34                std::wstring error_;
35        public:
36                check_exception(std::wstring error) : error_(error) {}
37                std::wstring getMessage() {
38                        return error_;
39                }
40        };
41
42        struct parse_exception : public check_exception {
43                parse_exception(std::wstring error) : check_exception(error) {}
44        };
45
46        static std::wstring formatAbove(std::wstring str, ResultType what) {
47                if (what == warning)
48                        return str + _T(" > warning");
49                else if (what == critical)
50                        return str + _T(" > critical");
51                return str + _T(" > unknown");
52        }
53
54        static std::wstring formatBelow(std::wstring str, ResultType what) {
55                if (what == warning)
56                        return str + _T(" < warning");
57                else if (what == critical)
58                        return str + _T(" < critical");
59                return str + _T(" < unknown");
60        }
61        static std::wstring formatSame(std::wstring str, ResultType what) {
62                if (what == warning)
63                        return str + _T(" = warning");
64                else if (what == critical)
65                        return str + _T(" = critical");
66                return str + _T(" = unknown");
67        }
68        static std::wstring formatNotSame(std::wstring str, ResultType what) {
69                if (what == warning)
70                        return str + _T(" != warning");
71                else if (what == critical)
72                        return str + _T(" != critical");
73                return str + _T(" != unknown");
74        }
75        static std::wstring formatState(std::wstring str, ResultType what) {
76                if (what == warning)
77                        return str + _T(" (warning)");
78                else if (what == critical)
79                        return str + _T(" (critical)");
80                return str + _T(" (unknown)");
81        }
82        static std::wstring formatNotFound(std::wstring str, ResultType what) {
83                if (what == warning)
84                        return str + _T("not found (warning)");
85                else if (what == critical)
86                        return str + _T("not found (critical)");
87                return str + _T("not found (unknown)");
88        }
89
90        typedef enum {showLong, showShort, showProblems, showUnknown} showType;
91        template <class TContents>
92        struct CheckContainer {
93                typedef CheckContainer<TContents> TThisType;
94                TContents warn;
95                TContents crit;
96                std::wstring data;
97                std::wstring alias;
98
99                showType show;
100                bool perfData;
101
102
103                CheckContainer() : show(showUnknown), perfData(true)
104                {}
105                CheckContainer(std::wstring data_, TContents warn_, TContents crit_)
106                        : data(data_), warn(warn_), crit(crit_), show(showUnknown)
107                {}
108                CheckContainer(std::wstring name_, std::wstring alias_, TContents warn_, TContents crit_)
109                        : data(data_), alias(alias_), warn(warn_), crit(crit_), show(showUnknown)
110                {}
111                CheckContainer(const TThisType &other)
112                        : data(other.data), alias(other.alias), warn(other.warn), crit(other.crit), show(other.show)
113                {}
114                std::wstring getAlias() {
115                        if (alias.empty())
116                                return data;
117                        return alias;
118                }
119                void setDefault(TThisType def) {
120                        if (!warn.hasBounds())
121                                warn = def.warn;
122                        if (!crit.hasBounds())
123                                crit = def.crit;
124                        if (show == showUnknown)
125                                show = def.show;
126                }
127                bool showAll() {
128                        return show != showProblems;
129                }
130                std::wstring gatherPerfData(typename TContents::TValueType &value) {
131                        return crit.gatherPerfData(getAlias(), value, warn, crit);
132                }
133                bool hasBounds() {
134                        return warn.hasBounds() || crit.hasBounds();
135                }
136                void runCheck(typename TContents::TValueType &value, NSCAPI::nagiosReturn &returnCode, std::wstring &message, std::wstring &perf) {
137                        std::wstring tstr;
138                        if (crit.check(value, getAlias(), tstr, critical)) {
139                                //std::wcout << _T("crit") << std::endl;
140                                NSCHelper::escalteReturnCodeToCRIT(returnCode);
141                        } else if (warn.check(value, getAlias(), tstr, warning)) {
142                                //std::wcout << _T("warn") << std::endl;
143                                NSCHelper::escalteReturnCodeToWARN(returnCode);
144                        }else if (show == showLong) {
145                                //std::wcout << _T("long") << std::endl;
146                                tstr = getAlias() + _T(": ") + TContents::toStringLong(value);
147                        }else if (show == showShort) {
148                                //std::wcout << _T("short") << std::endl;
149                                tstr = getAlias() + _T(": ") + TContents::toStringShort(value);
150                        }
151                        if (perfData)
152                                perf += gatherPerfData(value);
153                        if (!message.empty() && !tstr.empty())
154                                message += _T(", ");
155                        if (!tstr.empty())
156                                message += tstr;
157                        //std::wcout << _T("result: ") << tstr << _T("--") << std::endl;
158                }
159        };
160
161        typedef unsigned __int64 disk_size_type;
162        template <typename TType = disk_size_type>
163        class disk_size_handler {
164        public:
165                static std::wstring print(TType value) {
166                        return strEx::itos_as_BKMG(value);
167                }
168                static std::wstring get_perf_unit(TType value) {
169                        return strEx::find_proper_unit_BKMG(value);
170                }
171                static std::wstring print_perf(TType value, std::wstring unit) {
172                        return strEx::format_BKMG(value, unit);
173                }
174                static TType parse(std::wstring s) {
175                        return strEx::stoi64_as_BKMG(s);
176                }
177                static TType parse_percent(std::wstring s) {
178                        return strEx::stoi64(s);
179                }
180                static std::wstring print_percent(TType value) {
181                        return strEx::itos(value) + _T("%");
182                }
183                static std::wstring print_unformated(TType value) {
184                        return strEx::itos(value);
185                }
186
187                static std::wstring key_total() {
188                        return _T("Total: ");
189                }
190                static std::wstring key_lower() {
191                        return _T("Used: ");
192                }
193                static std::wstring key_upper() {
194                        return _T("Free: ");
195                }
196                static std::wstring key_prefix() {
197                        return _T("");
198                }
199                static std::wstring key_postfix() {
200                        return _T("");
201                }
202
203        };
204
205        typedef unsigned __int64 time_type;
206        template <typename TType = time_type>
207        class time_handler {
208        public:
209                static TType parse(std::wstring s) {
210                        return strEx::stoi64_as_time(s);
211                }
212                static TType parse_percent(std::wstring s) {
213                        return strEx::stoi(s);
214                }
215                static std::wstring print(TType value) {
216                        return strEx::itos_as_time(value);
217                }
218                static std::wstring print_percent(TType value) {
219                        return strEx::itos(value) + _T("%");
220                }
221                static std::wstring print_unformated(TType value) {
222                        return strEx::itos(value);
223                }
224                static std::wstring get_perf_unit(TType value) {
225                        return _T("");
226                }
227                static std::wstring print_perf(TType value, std::wstring unit) {
228                        return strEx::itos(value);
229                }
230                static std::wstring key_prefix() {
231                        return _T("");
232                }
233                static std::wstring key_postfix() {
234                        return _T("");
235                }
236
237        };
238
239
240        class int_handler {
241        public:
242                static int parse(std::wstring s) {
243                        return strEx::stoi(s);
244                }
245                static int parse_percent(std::wstring s) {
246                        return strEx::stoi(s);
247                }
248                static std::wstring print(int value) {
249                        return strEx::itos(value);
250                }
251                static std::wstring get_perf_unit(int value) {
252                        return _T("");
253                }
254                static std::wstring print_perf(int value, std::wstring unit) {
255                        return strEx::itos(value);
256                }
257                static std::wstring print_unformated(int value) {
258                        return strEx::itos(value);
259                }
260                static std::wstring print_percent(int value) {
261                        return strEx::itos(value) + _T("%");
262                }
263                static std::wstring key_prefix() {
264                        return _T("");
265                }
266                static std::wstring key_postfix() {
267                        return _T("");
268                }
269        };
270        class int64_handler {
271        public:
272                static __int64 parse(std::wstring s) {
273                        return strEx::stoi64(s);
274                }
275                static __int64 parse_percent(std::wstring s) {
276                        return strEx::stoi(s);
277                }
278                static std::wstring print(__int64 value) {
279                        return boost::lexical_cast<std::wstring>(value);
280                }
281                static std::wstring get_perf_unit(__int64 value) {
282                        return _T("");
283                }
284                static std::wstring print_perf(__int64 value, std::wstring unit) {
285                        return boost::lexical_cast<std::wstring>(value);
286                }
287                static std::wstring print_unformated(__int64 value) {
288                        return boost::lexical_cast<std::wstring>(value);
289                }
290                static std::wstring print_percent(__int64 value) {
291                        return boost::lexical_cast<std::wstring>(value) + _T("%");
292                }
293                static std::wstring key_prefix() {
294                        return _T("");
295                }
296                static std::wstring key_postfix() {
297                        return _T("");
298                }
299        };
300        class double_handler {
301        public:
302                static double parse(std::wstring s) {
303                        return strEx::stod(s);
304                }
305                static double parse_percent(std::wstring s) {
306                        return strEx::stod(s);
307                }
308                static std::wstring get_perf_unit(double value) {
309                        return _T("");
310                }
311                static std::wstring print_perf(double value, std::wstring unit) {
312                        return strEx::itos_non_sci(value);
313                }
314                static std::wstring print(double value) {
315                        return strEx::itos(value);
316                }
317                static std::wstring print_unformated(double value) {
318                        return strEx::itos(value);
319                }
320                static std::wstring print_percent(double value) {
321                        return strEx::itos(value) + _T("%");
322                }
323                static std::wstring key_prefix() {
324                        return _T("");
325                }
326                static std::wstring key_postfix() {
327                        return _T("");
328                }
329        };
330
331        typedef unsigned long state_type;
332        const int state_none      = 0x00;
333        const int state_started   = 0x01;
334        const int state_stopped   = 0x02;
335        const int state_not_found = 0x06;
336
337        class state_handler {
338        public:
339                static state_type parse(std::wstring s) {
340                        state_type ret = state_none;
341                        strEx::splitList lst = strEx::splitEx(s, _T(","));
342                        for (strEx::splitList::const_iterator it = lst.begin(); it != lst.end(); ++it) {
343                                if (*it == _T("started"))
344                                        ret |= state_started;
345                                else if (*it == _T("stopped"))
346                                        ret |= state_stopped;
347                                else if (*it == _T("ignored"))
348                                        ret |= state_none;
349                                else if (*it == _T("not found"))
350                                        ret |= state_not_found;
351                        }
352                        return ret;
353                }
354                static std::wstring print(state_type value) {
355                        if (value == state_started)
356                                return _T("started");
357                        else if (value == state_stopped)
358                                return _T("stopped");
359                        else if (value == state_none)
360                                return _T("none");
361                        else if (value == state_not_found)
362                                return _T("not found");
363                        return _T("unknown");
364                }
365                static std::wstring print_unformated(state_type value) {
366                        return strEx::itos(value);
367                }
368        };
369
370
371        template <typename TType = int, class THandler = int_handler >
372        class NumericBounds {
373        public:
374
375                bool bHasBounds_;
376                TType value_;
377                typedef typename TType TValueType;
378                typedef THandler TFormatType;
379
380                NumericBounds() : bHasBounds_(false), value_(0) {};
381
382                NumericBounds(const NumericBounds & other) {
383                        bHasBounds_ = other.bHasBounds_;
384                        value_ = other.value_;
385                }
386
387                checkResultType check(TType value) const {
388                        if (value == value_)
389                                return same;
390                        else if (value > value_)
391                                return above;
392                        return below;
393                }
394
395                static std::wstring toStringLong(TType value) {
396                        return THandler::key_prefix() + THandler::print(value) + THandler::key_postfix();
397                }
398                static std::wstring toStringShort(TType value) {
399                        return THandler::print(value);
400                }
401                inline bool hasBounds() const {
402                        return bHasBounds_;
403                }
404
405                const NumericBounds & operator=(std::wstring value) {
406                        set(value);
407                        return *this;
408                }
409
410                TType getPerfBound(TType value) {
411                        return value_;
412                }
413                static std::wstring gatherPerfData(std::wstring alias, TType &value, TType warn, TType crit) {
414                        std::wstring unit = THandler::get_perf_unit(min(warn, min(crit, value)));
415                        return MAKE_PERFDATA(alias, THandler::print_perf(value, unit), unit, THandler::print_perf(warn, unit), THandler::print_perf(crit, unit));
416                }
417
418        private:
419                void set(std::wstring s) {
420                        value_ = THandler::parse(s);
421                        bHasBounds_ = true;
422                }
423        };
424
425
426        template <typename TTypeValue, typename TTypeTotal = TTypeValue>
427        struct PercentageValueType {
428                typedef TTypeValue TValueType;
429                TTypeValue value;
430                TTypeTotal total;
431
432                TTypeValue getUpperPercentage() {
433                        return 100-(value*100/total);
434                }
435                TTypeValue getLowerPercentage() {
436                        return (value*100)/total;
437                }
438        };
439
440        template <typename TType = int, class THandler = int_handler >
441        class NumericPercentageBounds {
442        public:
443                typedef enum {
444                        none = 0,
445                        percentage_upper = 1,
446                        percentage_lower = 2,
447                        value_upper = 3,
448                        value_lower = 4,
449                } checkTypes;
450
451                class InternalValue {
452                        NumericPercentageBounds *pParent_;
453                        bool isUpper_;
454                public:
455
456                        InternalValue(bool isUpper) : pParent_(NULL), isUpper_(isUpper) {}
457                        void setParent(NumericPercentageBounds *pParent) {
458                                pParent_ = pParent;
459                        }
460                        const InternalValue & operator=(std::wstring value) {
461                                std::wstring::size_type p = value.find_first_of('%');
462                                if (p != std::wstring::npos) {
463                                        if (isUpper_)
464                                                pParent_->setPercentageUpper(value);
465                                        else
466                                                pParent_->setPercentageLower(value);
467                                } else {
468                                        if (isUpper_)
469                                                pParent_->setUpper(value);
470                                        else
471                                                pParent_->setLower(value);
472                                }
473                                return *this;
474                        }
475
476                };
477
478                checkTypes type_;
479                typename TType::TValueType value_;
480                typedef typename TType TValueType;
481                typedef THandler TFormatType;
482                typedef NumericPercentageBounds<TType, THandler> TMyType;
483                InternalValue upper;
484                InternalValue lower;
485
486                NumericPercentageBounds() : type_(none), upper(true), lower(false) {
487                        upper.setParent(this);
488                        lower.setParent(this);
489                }
490
491                NumericPercentageBounds(const NumericPercentageBounds &other) {
492                        type_ = other.type_;
493                        value_ = other.value_;
494                }
495                checkResultType check(TType value) const {
496                        if (type_ == percentage_lower) {
497                                if (value.getLowerPercentage() == value_)
498                                        return same;
499                                else if (value.getLowerPercentage() > value_)
500                                        return above;
501                        } else if (type_ == percentage_upper) {
502                                if (value.getUpperPercentage() == value_)
503                                        return same;
504                                else if (value.getUpperPercentage() > value_)
505                                        return above;
506                        } else if (type_ == value_lower) {
507                                if (value.value == value_)
508                                        return same;
509                                else if (value.value > value_)
510                                        return above;
511                        } else if (type_ == value_upper) {
512                                if ((value.total-value.value) == value_)
513                                        return same;
514                                else if ((value.total-value.value) > value_)
515                                        return above;
516                        } else {
517                                std::cout << _T("Damn...: ") << type_ << std::endl;
518                                throw _T("Damn...");
519                        }
520                        return below;
521                }
522                static std::wstring toStringShort(TType value) {
523                        return THandler::print(value.value);
524
525                }
526                static std::wstring toStringLong(TType value) {
527                        return
528                                THandler::key_total() + THandler::print(value.total) +
529                                _T(" - ") + THandler::key_lower() + THandler::print(value.value) +
530                                _T(" (") + THandler::print_percent(value.getLowerPercentage()) + _T(")") +
531                                _T(" - ") + THandler::key_upper() + THandler::print(value.total-value.value) +
532                                _T(" (") + THandler::print_percent(value.getUpperPercentage()) + _T(")");
533                }
534                inline bool hasBounds() const {
535                        return type_ != none;
536                }
537                typename TType::TValueType getPerfBound(TType value) {
538                        return value_;
539                }
540                std::wstring gatherPerfData(std::wstring alias, TType &value, typename TType::TValueType warn, typename TType::TValueType crit) {
541                        if (type_ == percentage_upper) {
542                                return
543                                        MAKE_PERFDATA(alias, THandler::print_unformated(value.getUpperPercentage()), _T("%"),
544                                        THandler::print_unformated(warn), THandler::print_unformated(crit));
545                        } else if (type_ == percentage_lower) {
546                                        return
547                                                MAKE_PERFDATA(alias, THandler::print_unformated(value.getLowerPercentage()), _T("%"),
548                                                THandler::print_unformated(warn), THandler::print_unformated(crit));
549                        } else if (type_ == value_upper) {
550                                std::wstring unit = THandler::get_perf_unit(min(warn, min(crit, value.value)));
551                                return
552                                        MAKE_PERFDATA(alias, THandler::print_perf((value.value), unit), unit,
553                                        THandler::print_perf(value.total-warn, unit), THandler::print_perf(value.total-crit, unit));
554                        } else {
555                                std::wstring unit = THandler::get_perf_unit(min(warn, min(crit, value.value)));
556                                return
557                                        MAKE_PERFDATA(alias, THandler::print_perf(value.value, unit), unit,
558                                        THandler::print_perf(warn, unit), THandler::print_perf(crit, unit));
559                        }
560                }
561        private:
562                void setUpper(std::wstring s) {
563                        value_ = THandler::parse(s);
564                        type_ = value_upper;
565                }
566                void setLower(std::wstring s) {
567                        value_ = THandler::parse(s);
568                        type_ = value_lower;
569                }
570                void setPercentageUpper(std::wstring s) {
571                        value_ = THandler::parse_percent(s);
572                        type_ = percentage_upper;
573                }
574                void setPercentageLower(std::wstring s) {
575                        value_ = THandler::parse_percent(s);
576                        type_ = percentage_lower;
577                }
578        };
579
580        template <typename TType = state_type, class THandler = state_handler >
581        class StateBounds {
582        public:
583                TType value_;
584                typedef typename TType TValueType;
585                typedef THandler TFormatType;
586                typedef StateBounds<TType, THandler> TMyType;
587
588                StateBounds() : value_(state_none) {}
589                StateBounds(const StateBounds &other) : value_(other.value_) {}
590
591                bool check(TType value) const {
592                        return (value & value_) != 0;
593                }
594                static std::wstring toStringLong(TType value) {
595                        return THandler::print(value);
596                }
597                static std::wstring toStringShort(TType value) {
598                        return THandler::print(value);
599                }
600                inline bool hasBounds() const {
601                        return value_ != state_none;
602                }
603                TType getPerfBound(TType value) {
604                        return value_;
605                }
606                std::wstring gatherPerfData(std::wstring alias, TType &value, TType warn, TType crit) {
607                        return "";
608                }
609                const StateBounds & operator=(std::wstring value) {
610                        set(value);
611                        return *this;
612                }
613        private:
614                void set(std::wstring s) {
615                        value_ = THandler::parse(s);
616                }
617        };
618
619        template <typename TMaxMinType = int, typename TStateType = state_type>
620        struct MaxMinStateValueType {
621                TMaxMinType count;
622                TStateType state;
623        };
624
625
626        template <class TValueType = MaxMinStateValueType, class TNumericHolder = NumericBounds<int, int_handler>, class TStateHolder = StateBounds<state_type, state_handler> >
627        class MaxMinStateBounds {
628        public:
629                TNumericHolder max;
630                TNumericHolder min;
631                TStateHolder state;
632                typedef MaxMinStateBounds<TValueType, TNumericHolder, TStateHolder > TMyType;
633
634                typedef typename TValueType TValueType;
635
636                MaxMinStateBounds() {}
637                MaxMinStateBounds(const MaxMinStateBounds &other) {
638                        state = other.state;
639                        max = other.max;
640                        min = other.min;
641                }
642                bool hasBounds() {
643                        return state.hasBounds() ||  max.hasBounds() || min.hasBounds();
644                }
645
646                static std::wstring toStringLong(typename TValueType &value) {
647                        return TNumericHolder::toStringLong(value.count) + _T(", ") + TStateHolder::toStringLong(value.state);
648                }
649                static std::wstring toStringShort(typename TValueType &value) {
650                        return TNumericHolder::toStringShort(value.count);
651                }
652/*
653                void formatString(std::wstring &message, typename TValueType &value) {
654                        if (state.hasBounds())
655                                message = state.toString(value.state);
656                        else if (max.hasBounds())
657                                message = max.toString(value.count);
658                        else if (min.hasBounds())
659                                message = max.toString(value.count);
660                }
661                */
662                std::wstring gatherPerfData(std::wstring alias, typename TValueType &value, TMyType &warn, TMyType &crit) {
663                        if (state.hasBounds()) {
664                                // @todo
665                        } else if (max.hasBounds()) {
666                                return max.gatherPerfData(alias, value.count, warn.max.getPerfBound(value.count), crit.max.getPerfBound(value.count));
667                        } else if (min.hasBounds()) {
668                                return min.gatherPerfData(alias, value.count, warn.min.getPerfBound(value.count), crit.min.getPerfBound(value.count));
669                        }
670                        return _T("");
671                }
672                bool check(typename TValueType &value, std::wstring lable, std::wstring &message, ResultType type) {
673                        if ((state.hasBounds())&&(!state.check(value.state))) {
674                                message = lable + _T(": ") + formatState(TStateHolder::toStringShort(value.state), type);
675                                return true;
676                        } else if ((max.hasBounds())&&(max.check(value.count) != below)) {
677                                message = lable + _T(": ") + formatAbove(TNumericHolder::toStringShort(value.count), type);
678                                return true;
679                        } else if ((min.hasBounds())&&(min.check(value.count) != above)) {
680                                message = lable + _T(": ") + formatBelow(TNumericHolder::toStringShort(value.count), type);
681                                return true;
682                        } else {
683                                NSC_DEBUG_MSG_STD(_T("Missing bounds for check: ") + lable);
684                                //std::cout << "No bounds specified..." << std::endl;
685                        }
686                        return false;
687                }
688
689        };
690
691        template <class TStateHolder = StateBounds<state_type, state_handler> >
692        class SimpleStateBounds {
693        public:
694                TStateHolder state;
695                typedef SimpleStateBounds<TStateHolder > TMyType;
696
697                typedef typename TStateHolder::TValueType TValueType;
698
699                SimpleStateBounds() {}
700                SimpleStateBounds(const SimpleStateBounds &other) {
701                        state = other.state;
702                }
703                bool hasBounds() {
704                        return state.hasBounds();
705                }
706                static std::wstring toStringLong(typename TValueType &value) {
707                        return TStateHolder::toStringLong(value);
708                }
709                static std::wstring toStringShort(typename TValueType &value) {
710                        return TStateHolder::toStringShort(value);
711                }
712                std::wstring gatherPerfData(std::wstring alias, typename TValueType &value, TMyType &warn, TMyType &crit) {
713                        if (state.hasBounds()) {
714                                // @todo
715                        }
716                        return _T("");
717                }
718                bool check(typename TValueType &value, std::wstring lable, std::wstring &message, ResultType type) {
719                        if ((state.hasBounds())&&(!state.check(value))) {
720                                message = lable + _T(": ") + formatState(TStateHolder::toStringLong(value), type);
721                                return true;
722                        }
723                        return false;
724                }
725
726        };
727
728        template <class THolder = NumericBounds<int, int_handler> >
729        class MaxMinBounds {
730        public:
731                THolder max;
732                THolder min;
733                typedef MaxMinBounds<THolder > TMyType;
734
735                typedef typename THolder::TValueType TValueType;
736                //              typedef THolder::TFormatType TFormatType;
737
738                MaxMinBounds() {}
739                MaxMinBounds(const MaxMinBounds &other) {
740                        max = other.max;
741                        min = other.min;
742                }
743                bool hasBounds() {
744                        return max.hasBounds() || min.hasBounds();
745                }
746                static std::wstring toStringLong(typename THolder::TValueType &value) {
747                        return THolder::toStringLong(value);
748                }
749                static std::wstring toStringShort(typename THolder::TValueType &value) {
750                        return THolder::toStringShort(value);
751                }
752                std::wstring gatherPerfData(std::wstring alias, typename THolder::TValueType &value, TMyType &warn, TMyType &crit) {
753                        if (max.hasBounds()) {
754                                return max.gatherPerfData(alias, value, warn.max.getPerfBound(value), crit.max.getPerfBound(value));
755                        } else if (min.hasBounds()) {
756                                return min.gatherPerfData(alias, value, warn.min.getPerfBound(value), crit.min.getPerfBound(value));
757                        } else {
758                                NSC_DEBUG_MSG_STD(_T("Missing bounds for maxmin-bounds check: ") + alias);
759                                return min.gatherPerfData(alias, value, 0, 0);
760                        }
761                        return _T("");
762                }
763                bool check(typename THolder::TValueType &value, std::wstring lable, std::wstring &message, ResultType type) {
764                        if ((max.hasBounds())&&(max.check(value) != below)) {
765                                message = lable + _T(": ") + formatAbove(THolder::toStringLong(value), type);
766                                return true;
767                        } else if ((min.hasBounds())&&(min.check(value) != above)) {
768                                message = lable + _T(": ") + formatBelow(THolder::toStringLong(value), type);
769                                return true;
770                        } else {
771                                //std::cout << "No bounds specified..." << std::endl;
772                        }
773                        return false;
774                }
775
776        };
777        typedef MaxMinBounds<NumericBounds<double, double_handler> > MaxMinBoundsDouble;
778        typedef MaxMinBounds<NumericBounds<__int64, int64_handler> > MaxMinBoundsInt64;
779        typedef MaxMinBounds<NumericBounds<int, int_handler> > MaxMinBoundsInteger;
780        typedef MaxMinBounds<NumericBounds<unsigned int, int_handler> > MaxMinBoundsUInteger;
781        typedef MaxMinBounds<NumericBounds<unsigned long int, int_handler> > MaxMinBoundsULongInteger;
782        typedef MaxMinBounds<NumericBounds<disk_size_type, disk_size_handler<disk_size_type> > > MaxMinBoundsDiscSize;
783        typedef MaxMinBounds<NumericBounds<time_type, time_handler<time_type> > > MaxMinBoundsTime;
784
785
786        template <class THolder = NumericBounds<int, int_handler> >
787        class ExactBounds {
788        public:
789                THolder max;
790                THolder min;
791                THolder eq;
792                THolder neq;
793                typedef ExactBounds<THolder > TMyType;
794                typedef typename THolder::TValueType TValueType;
795
796                ExactBounds() {}
797                ExactBounds(const ExactBounds &other) {
798                        max = other.max;
799                        min = other.min;
800                        eq = other.eq;
801                        neq = other.neq;
802                }
803
804                const TMyType& operator=(std::wstring value) {
805                        //value_ = value;
806                        if (value.substr(0,1) == _T(">")) {
807                                max = value.substr(1);
808                        } else if (value.substr(0,2) == _T("<>")) {
809                                neq = value.substr(2);
810                        } else if (value.substr(0,1) == _T("<")) {
811                                min = value.substr(1);
812                        } else if (value.substr(0,1) == _T("=")) {
813                                eq = value.substr(1);
814                        } else if (value.substr(0,2) == _T("!=")) {
815                                neq = value.substr(2);
816                        } else if (value.substr(0,1) == _T("!")) {
817                                neq = value.substr(1);
818                                /*
819                                TODO add support for lists
820                        } else if (value.substr(0,3) == _T("in:")) {
821                                inList = value.substr(3);
822                                */
823                        } else if (value.substr(0,3) == _T("gt:")) {
824                                max = value.substr(3);
825                        } else if (value.substr(0,3) == _T("lt:")) {
826                                min = value.substr(3);
827                        } else if (value.substr(0,3) == _T("ne:")) {
828                                neq = value.substr(3);
829                        } else if (value.substr(0,3) == _T("eq:")) {
830                                eq = value.substr(3);
831                        } else {
832                                throw parse_exception(_T("Unknown filter key: ") + value + _T(" (numeric filters have to have an operator as well ie. foo=>5 or bar==5 foo=gt:6)"));
833                        }
834                        return *this;
835                }
836
837                bool hasBounds() {
838                        return max.hasBounds() || min.hasBounds() || eq.hasBounds() || neq.hasBounds();
839                }
840                static std::wstring toStringLong(typename THolder::TValueType &value) {
841                        return THolder::toStringLong(value);
842                }
843                static std::wstring toStringShort(typename THolder::TValueType &value) {
844                        return THolder::toStringShort(value);
845                }
846                std::wstring gatherPerfData(std::wstring alias, typename THolder::TValueType &value, TMyType &warn, TMyType &crit) {
847                        if (max.hasBounds()) {
848                                return max.gatherPerfData(alias, value, warn.max.getPerfBound(value), crit.max.getPerfBound(value));
849                        } else if (min.hasBounds()) {
850                                return min.gatherPerfData(alias, value, warn.min.getPerfBound(value), crit.min.getPerfBound(value));
851                        } else if (neq.hasBounds()) {
852                                return neq.gatherPerfData(alias, value, warn.neq.getPerfBound(value), crit.neq.getPerfBound(value));
853                        } else if (eq.hasBounds()) {
854                                return eq.gatherPerfData(alias, value, warn.eq.getPerfBound(value), crit.eq.getPerfBound(value));
855                        } else {
856                                NSC_DEBUG_MSG_STD(_T("Missing bounds for: ") + alias);
857                        }
858                }
859                bool check(typename THolder::TValueType &value, std::wstring lable, std::wstring &message, ResultType type) {
860                        if ((max.hasBounds())&&(max.check(value) == above)) {
861                                message = lable + _T(": ") + formatAbove(THolder::toStringLong(value), type);
862                                return true;
863                        } else if ((min.hasBounds())&&(min.check(value) == below)) {
864                                message = lable + _T(": ") + formatBelow(THolder::toStringLong(value), type);
865                                return true;
866                        } else if ((eq.hasBounds())&&(eq.check(value) == same)) {
867                                message = lable + _T(": ") + formatSame(THolder::toStringLong(value), type);
868                                return true;
869                        } else if ((neq.hasBounds())&&(neq.check(value) != same)) {
870                                message = lable + _T(": ") + formatNotSame(THolder::toStringLong(value), type);
871                                return true;
872                        } else {
873                                //std::cout << "No bounds specified..." << std::endl;
874                        }
875                        return false;
876                }
877
878        };
879        typedef ExactBounds<NumericBounds<unsigned long int, int_handler> > ExactBoundsULongInteger;
880
881        //typedef MaxMinBounds<NumericPercentageBounds<PercentageValueType<int ,int>, int_handler> > MaxMinPercentageBoundsInteger;
882        //typedef MaxMinBounds<NumericPercentageBounds<PercentageValueType<__int64, __int64>, int64_handler> > MaxMinPercentageBoundsInt64;
883        //typedef MaxMinBounds<NumericPercentageBounds<PercentageValueType<double, double>, double_handler> > MaxMinPercentageBoundsDouble;
884        typedef MaxMinBounds<NumericPercentageBounds<PercentageValueType<disk_size_type, disk_size_type>, disk_size_handler<> > > MaxMinPercentageBoundsDiskSize;
885        typedef MaxMinBounds<NumericPercentageBounds<PercentageValueType<__int64, __int64>, disk_size_handler<__int64> > > MaxMinPercentageBoundsDiskSizei64;
886
887        typedef MaxMinStateBounds<MaxMinStateValueType<int, state_type>, NumericBounds<int, int_handler>, StateBounds<state_type, state_handler> > MaxMinStateBoundsStateBoundsInteger;
888        typedef SimpleStateBounds<StateBounds<state_type, state_handler> > SimpleBoundsStateBoundsInteger;
889}
890
891
Note: See TracBrowser for help on using the repository browser.