Changeset 227


Ignore:
Timestamp:
12/13/09 22:35:12 (2 years ago)
Author:
mickem
Message:

2009-12-13 MickeM

+ Added new command: CheckSingleFile? to check spects of a single file use like so:

CheckSingleFile? file=d:\nrpe_512.pem warn=>100 check=line-count warn=>100 crit=>170 check=size

+ Added option debug to CheckFile2 to enable priting of debug information
+ Added ignore-errors to "ignore" any filesystem related errors (NOTICE this is probably not what you want)
+ Added master-syntax to CheckFile2 to change the overall message like so:

It takes three options (and char data):

  • %list% A list of all "files" (syntax controls this)
  • %files% number of files
  • %matches% number of files matched

CheckFile2 MinWarn=10 MinCrit=10 path=D:\WINDOWS\system32 filter+size=gt:0 truncate=10 ignore-errors "master-syntax=%matches%/%files%"
OK:7177/7...|'found files'=7177;10;10;

Location:
branches/stable
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/stable

    • Property svn:ignore
      •  

        old new  
        1414NSClient++-2005.suo 
        1515*.bak 
         16build.log 
  • branches/stable/AutoBuild.h

    r225 r227  
    33// change the FALSE to TRUE for autoincrement of build number 
    44#define INCREMENT_VERSION TRUE 
    5 #define FILEVER        0,3,7,496 
    6 #define PRODUCTVER     0,3,7,496 
    7 #define STRFILEVER     _T("0.3.7.496") 
    8 #define STRPRODUCTVER  _T("0.3.7.496") 
    9 #define STRPRODUCTDATE  _T("2009-12-06") 
     5#define FILEVER        0,3,7,512 
     6#define PRODUCTVER     0,3,7,512 
     7#define STRFILEVER     _T("0.3.7.512") 
     8#define STRPRODUCTVER  _T("0.3.7.512") 
     9#define STRPRODUCTDATE  _T("2009-12-13") 
    1010#endif // AUTOBUILD_H 
  • branches/stable/changelog

    r225 r227  
    66 * Fix depend onservice LanManWorkStation (old win) 
    77 * Fix RtlStringFromGUID problem on NT4 
     8 
     92009-12-13 MickeM 
     10 + Added new command: CheckSingleFile to check spects of a single file use like so: 
     11   CheckSingleFile file=d:\nrpe_512.pem warn=>100 check=line-count warn=>100 crit=>170 check=size 
     12 + Added option debug to CheckFile2 to enable priting of debug information 
     13 + Added ignore-errors to "ignore" any filesystem related errors (NOTICE this is probably not what you want) 
     14 + Added master-syntax to CheckFile2 to change the overall message like so: 
     15    It takes three options (and char data): 
     16     * %list% A list of all "files" (syntax controls this) 
     17     * %files% number of files 
     18     * %matches% number of files matched 
     19    CheckFile2 MinWarn=10 MinCrit=10 path=D:\WINDOWS\system32 filter+size=gt:0 truncate=10 ignore-errors "master-syntax=%matches%/%files%" 
     20    OK:7177/7...|'found files'=7177;10;10; 
    821 
    9222009-12-06 MickeM 
  • branches/stable/include/checkHelpers.hpp

    r173 r227  
    2727 
    2828namespace checkHolders { 
     29 
    2930 
    3031 
     
    8889  } 
    8990 
     91 
    9092  typedef enum {showLong, showShort, showProblems, showUnknown} showType; 
    9193  template <class TContents> 
    9294  struct CheckContainer { 
    9395    typedef CheckContainer<TContents> TThisType; 
     96    typedef typename TContents::TValueType TPayloadValueType; 
    9497    TContents warn; 
    9598    TContents crit; 
     
    129132    } 
    130133    std::wstring gatherPerfData(typename TContents::TValueType &value) { 
    131       return crit.gatherPerfData(getAlias(), value, warn, crit); 
     134      if (crit.hasBounds()) 
     135        return crit.gatherPerfData(getAlias(), value, warn, crit); 
     136      else if (warn.hasBounds()) 
     137        return warn.gatherPerfData(getAlias(), value, warn, crit); 
     138      else 
     139        return getAlias() + _T(": ERROR"); 
    132140    } 
    133141    bool hasBounds() { 
     
    156164        message += tstr; 
    157165      //std::wcout << _T("result: ") << tstr << _T("--") << std::endl; 
     166    } 
     167  }; 
     168 
     169 
     170  template <class value_type> 
     171  struct check_proxy_interface { 
     172    virtual bool showAll() = 0; 
     173    virtual std::wstring gatherPerfData(value_type &value) = 0; 
     174    virtual bool hasBounds() = 0; 
     175    virtual void runCheck(value_type &value, NSCAPI::nagiosReturn &returnCode, std::wstring &message, std::wstring &perf) = 0; 
     176    virtual void set_warn_bound(std::wstring value) = 0; 
     177    virtual void set_crit_bound(std::wstring value) = 0; 
     178    //virtual std::wstring get_default_alias() = 0; 
     179 
     180  }; 
     181 
     182 
     183  //typedef enum {showLong, showShort, showProblems, showUnknown} showType; 
     184  template <class container_value_type, class impl_type> 
     185  class check_proxy_container : public check_proxy_interface<container_value_type> { 
     186    typedef check_proxy_container<container_value_type, impl_type> TThisType; 
     187    impl_type impl_; 
     188  public: 
     189    virtual typename impl_type::TPayloadValueType get_value(container_value_type &value) = 0; 
     190 
     191    void set_warn_bound(std::wstring value) { 
     192      impl_.warn = value; 
     193    } 
     194    void set_crit_bound(std::wstring value) { 
     195      impl_.crit = value; 
     196    } 
     197    void set_alias(std::wstring value) { 
     198      impl_.alias = value; 
     199    } 
     200 
     201 
     202    check_proxy_container() {} 
     203    /* 
     204    void setDefault(TThisType def) { 
     205      if (!warn.hasBounds()) 
     206        warn = def.warn; 
     207      if (!crit.hasBounds()) 
     208        crit = def.crit; 
     209      if (show == showUnknown) 
     210        show = def.show; 
     211    } 
     212    */ 
     213    bool showAll() { 
     214      return impl_.showAll(); 
     215    } 
     216    std::wstring gatherPerfData(container_value_type &value) { 
     217      typename impl_type::TPayloadValueType real_value = get_value(value); 
     218      return impl_.gatherPerfData(real_value); 
     219    } 
     220    bool hasBounds() { 
     221      return impl_.hasBounds(); 
     222    } 
     223    void runCheck(container_value_type &value, NSCAPI::nagiosReturn &returnCode, std::wstring &message, std::wstring &perf) { 
     224      typename impl_type::TPayloadValueType real_value = get_value(value); 
     225      return impl_.runCheck(real_value, returnCode, message, perf); 
     226    } 
     227  }; 
     228 
     229 
     230  template <class value_type> 
     231  struct check_multi_container { 
     232    typedef check_multi_container<value_type> TThisType; 
     233    typedef check_proxy_interface<value_type> check_type; 
     234    typedef std::list<check_type*> check_list_type; 
     235    check_list_type checks_; 
     236    std::wstring data; 
     237    std::wstring alias; 
     238 
     239    std::wstring cached_warn_; 
     240    std::wstring cached_crit_; 
     241 
     242    showType show; 
     243    bool perfData; 
     244 
     245    void set_warn_bound(std::wstring value) { 
     246//      if (checks_.empty()) 
     247        cached_warn_ = value; 
     248//      else 
     249//        (checks_.back())->set_warn_bound(value); 
     250    } 
     251    void set_crit_bound(std::wstring value) { 
     252//      if (checks_.empty()) 
     253        cached_crit_ = value; 
     254//      else 
     255//        (checks_.back())->set_crit_bound(value); 
     256    } 
     257 
     258    void add_check(check_type *check) { 
     259      if (check != NULL) { 
     260        if (!cached_warn_.empty()) 
     261          check->set_warn_bound(cached_warn_); 
     262        if (!cached_crit_.empty()) 
     263          check->set_crit_bound(cached_crit_); 
     264        checks_.push_back(check); 
     265      } 
     266      cached_warn_ = _T(""); 
     267      cached_crit_ = _T(""); 
     268    } 
     269 
     270    check_multi_container() : show(showUnknown), perfData(true) 
     271    {} 
     272  private: 
     273    check_multi_container(const TThisType &other)  
     274      : data(other.data), alias(other.alias), checks_(other.checks_), show(other.show)  
     275    {} 
     276  public: 
     277    ~check_multi_container() { 
     278      for (check_list_type::iterator it=checks_.begin(); it != checks_.end(); ++it) { 
     279        delete *it; 
     280      } 
     281      checks_.clear(); 
     282    } 
     283    std::wstring getAlias() { 
     284      if (alias.empty()) 
     285        return data; 
     286      return alias; 
     287    } 
     288    void setDefault(TThisType def) { 
     289      if (show == showUnknown) 
     290        show = def.show; 
     291    } 
     292    bool showAll() { 
     293      return show != showProblems; 
     294    } 
     295    std::wstring gatherPerfData(value_type &value) { 
     296      std::wstring ret; 
     297      for (check_list_type::const_iterator cit=checks_.begin(); cit != checks_.end(); ++cit) { 
     298        ret += (*cit)->gatherPerfData((*cit)->getAlias(), value); 
     299      } 
     300    } 
     301    bool hasBounds() { 
     302      for (check_list_type::const_iterator cit=checks_.begin(); cit != checks_.end(); ++cit) { 
     303        if ((*cit)->hasBounds()) 
     304          return true; 
     305      } 
     306      return false; 
     307    } 
     308    void runCheck(value_type &value, NSCAPI::nagiosReturn &returnCode, std::wstring &message, std::wstring &perf) { 
     309      for (check_list_type::const_iterator cit=checks_.begin(); cit != checks_.end(); ++cit) { 
     310        (*cit)->runCheck(value, returnCode, message, perf); 
     311      } 
     312      std::wcout << _T("result: ") << message << std::endl; 
    158313    } 
    159314  }; 
     
    8781033  }; 
    8791034  typedef ExactBounds<NumericBounds<unsigned long int, int_handler> > ExactBoundsULongInteger; 
     1035  typedef ExactBounds<NumericBounds<unsigned int, int_handler> > ExactBoundsUInteger; 
     1036  typedef ExactBounds<NumericBounds<unsigned long, int_handler> > ExactBoundsULong; 
     1037  typedef ExactBounds<NumericBounds<time_type, time_handler<__int64> > > ExactBoundsTime; 
    8801038 
    8811039  //typedef MaxMinBounds<NumericPercentageBounds<PercentageValueType<int ,int>, int_handler> > MaxMinPercentageBoundsInteger; 
  • branches/stable/include/file_helpers.hpp

    r179 r227  
    3030    } 
    3131  }; 
     32  class meta { 
     33  public: 
     34    static std::wstring get_path(std::wstring file) { 
     35      std::wstring::size_type pos = file.find_last_of('\\'); 
     36      if (pos == std::wstring::npos) { 
     37        return file; 
     38      } 
     39      return file.substr(0, pos); 
     40    } 
     41    static std::wstring get_filename(std::wstring file) { 
     42      std::wstring::size_type pos = file.find_last_of('\\'); 
     43      if (pos == std::wstring::npos || ++pos == std::wstring::npos) { 
     44        return _T(""); 
     45      } 
     46      return file.substr(pos); 
     47    } 
     48  }; 
    3249 
    3350  class patterns { 
  • branches/stable/include/utils.h

    r175 r227  
    6161      else if (p__.first == (_T("crit") postfix)) { obj.crit = p__.second; } \ 
    6262 
     63#define MAP_OPTIONS_EXACT_NUMERIC_ALL_MULTI(obj, postfix) \ 
     64      else if (p__.first == (_T("warn") postfix)) { obj.set_warn_bound(p__.second); } \ 
     65      else if (p__.first == (_T("crit") postfix)) { obj.set_crit_bound(p__.second); } \ 
     66 
    6367#define MAP_OPTIONS_PUSH_WTYPE(type, value, obj, list) \ 
    6468      else if (p__.first == value) { type o; o.obj = p__.second; list.push_back(o); } 
  • branches/stable/modules/CheckDisk/CheckDisk.cpp

    r225 r227  
    2626#include <error.hpp> 
    2727#include <file_helpers.hpp> 
     28#include <checkHelpers.hpp> 
    2829 
    2930CheckDisk gCheckDisk; 
     
    108109 
    109110template <class finder_function> 
    110 void recursive_scan(std::wstring dir, std::wstring pattern, int current_level, int max_level, finder_function & f, error_reporter * errors) { 
     111void recursive_scan(std::wstring dir, std::wstring pattern, int current_level, int max_level, finder_function & f, error_reporter * errors, bool debug) { 
    111112  if ((max_level != -1) && (current_level > max_level)) 
    112113    return; 
     
    114115 
    115116  DWORD fileAttr = GetFileAttributes(dir.c_str()); 
    116   NSC_DEBUG_MSG_STD(_T("Input is: ") + dir + _T(" / ") + strEx::ihextos(fileAttr)); 
     117  if (debug) NSC_DEBUG_MSG_STD(_T("Input is: ") + dir + _T(" / ") + strEx::ihextos(fileAttr)); 
    117118 
    118119  if (!is_directory(fileAttr)) { 
    119     NSC_DEBUG_MSG_STD(_T("Found a file dont do recursive scan: ") + dir); 
     120    if (debug) NSC_DEBUG_MSG_STD(_T("Found a file dont do recursive scan: ") + dir); 
    120121    // It is a file check it an return (dont check recursivly) 
    121122    pattern_type single_path = split_path_ex(dir); 
    122     NSC_DEBUG_MSG_STD(_T("Path is: ") + single_path.first); 
     123    if (debug) NSC_DEBUG_MSG_STD(_T("Path is: ") + single_path.first); 
    123124    HANDLE hFind = FindFirstFile(dir.c_str(), &wfd); 
    124125    if (hFind != INVALID_HANDLE_VALUE) { 
     
    129130  } 
    130131  std::wstring file_pattern = dir + _T("\\") + pattern; 
    131   NSC_DEBUG_MSG_STD(_T("File pattern: ") + file_pattern); 
     132  if (debug) NSC_DEBUG_MSG_STD(_T("File pattern: ") + file_pattern); 
    132133  HANDLE hFind = FindFirstFile(file_pattern.c_str(), &wfd); 
    133134  if (hFind != INVALID_HANDLE_VALUE) { 
     
    139140  } 
    140141  std::wstring dir_pattern = dir + _T("\\*.*"); 
    141   NSC_DEBUG_MSG_STD(_T("File pattern: ") + dir_pattern); 
     142  if (debug) NSC_DEBUG_MSG_STD(_T("File pattern: ") + dir_pattern); 
    142143  hFind = FindFirstFile(dir_pattern.c_str(), &wfd); 
    143144  if (hFind != INVALID_HANDLE_VALUE) { 
     
    145146      if (is_directory(wfd.dwFileAttributes)) { 
    146147        if ( (wcscmp(wfd.cFileName, _T(".")) != 0) && (wcscmp(wfd.cFileName, _T("..")) != 0) ) 
    147           recursive_scan<finder_function>(dir + _T("\\") + wfd.cFileName, pattern, current_level+1, max_level, f, errors); 
     148          recursive_scan<finder_function>(dir + _T("\\") + wfd.cFileName, pattern, current_level+1, max_level, f, errors, debug); 
    148149      } 
    149150    } while (FindNextFile(hFind, &wfd)); 
     
    355356  std::list<std::wstring> args = arrayBuffer::arrayBuffer2list(argLen, char_args); 
    356357  bool bPerfData = true; 
     358  bool debug = false; 
    357359  if (args.empty()) { 
    358360    message = _T("Missing argument(s)."); 
     
    369371    MAP_OPTIONS_STR(_T("MaxCrit"), tmpObject.crit.max) 
    370372    MAP_OPTIONS_STR(_T("MinCrit"), tmpObject.crit.min) 
     373    MAP_OPTIONS_BOOL_TRUE(_T("debug"), debug) 
    371374    MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) 
    372375    MAP_OPTIONS_SECONDARY_BEGIN(_T(":"), p2) 
     
    388391    NSC_error errors; 
    389392    pattern_type splitpath = split_pattern(path.data); 
    390     recursive_scan<get_size>(splitpath.first, splitpath.second, -1, -1, sizeFinder, &errors); 
     393    recursive_scan<get_size>(splitpath.first, splitpath.second, -1, -1, sizeFinder, &errors, debug); 
    391394    if (sizeFinder.hasError()) { 
    392395      message = _T("File not found check log for details"); 
     
    410413  file_info()  
    411414    : ullCreationTime(0) 
     415    , ullLastAccessTime(0) 
     416    , ullLastWriteTime(0) 
     417    , ullSize(0) 
    412418    , cached_version(false, _T(""))  
    413419    , cached_count(false, 0) 
     
    417423    , filename(filename_) 
    418424    , ullCreationTime(0) 
     425    , ullLastAccessTime(0) 
     426    , ullLastWriteTime(0) 
     427    , ullSize(0) 
    419428    , cached_version(false, _T(""))  
    420429    , cached_count(false, 0) 
     
    424433    ullLastAccessTime = ((info.ftLastAccessTime.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.ftLastAccessTime.dwLowDateTime); 
    425434    ullLastWriteTime = ((info.ftLastWriteTime.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.ftLastWriteTime.dwLowDateTime); 
     435  }; 
     436  file_info(std::wstring path_, std::wstring filename_)  
     437    : path(path_) 
     438    , filename(filename_) 
     439    , ullCreationTime(0) 
     440    , ullLastAccessTime(0) 
     441    , ullLastWriteTime(0) 
     442    , ullSize(0) 
     443    , cached_version(false, _T(""))  
     444    , cached_count(false, 0) 
     445  { 
    426446  }; 
    427447 
     
    489509    do { 
    490510      c = fgetc (pFile); 
    491       if (c == '\n') count++; 
     511      if (c == '\r') { 
     512        c = fgetc (pFile); 
     513        count++; 
     514      } else if (c == '\n') { 
     515        c = fgetc (pFile); 
     516        count++; 
     517      } 
    492518    } while (c != EOF); 
    493519    fclose (pFile); 
     
    662688  unsigned long long now; 
    663689  unsigned int hit_count; 
    664  
    665   file_filter_function_ex() : hit_count(0), error(false), debug_(false), bFilterIn(true), bFilterAll(true) {} 
     690  unsigned int file_count; 
     691  std::wstring last_error; 
     692  unsigned int error_count; 
     693 
     694  file_filter_function_ex() : hit_count(0), file_count(0), error(false), debug_(false), bFilterIn(true), bFilterAll(true), error_count(0) {} 
    666695  result_type operator()(argument_type ffd) { 
    667696    if (is_directory(ffd.wfd.dwFileAttributes)) 
     
    687716      if ((mode == filter_minus)&&(bTmpMatched)) { 
    688717        // a -<filter> hit so thrash item and bail out! 
    689         //if (debug_) 
     718        if (debug_) 
    690719          NSC_DEBUG_MSG_STD(_T("Matched: - ") + (*cit3).second.getValue() + _T(" for: ") + info.render(syntax)); 
    691720        bMatch = false; 
     
    693722      } else if ((mode == filter_plus)&&(!bTmpMatched)) { 
    694723        // a +<filter> missed hit so thrash item and bail out! 
    695         //if (debug_) 
     724        if (debug_) 
    696725          NSC_DEBUG_MSG_STD(_T("Matched (missed): + ") + (*cit3).second.getValue() + _T(" for: ") + info.render(syntax)); 
    697726        bMatch = false; 
     
    704733    } 
    705734 
    706     NSC_DEBUG_MSG_STD(_T("result: ") + strEx::itos(bFilterIn) + _T(" -- ") + strEx::itos(bMatch)); 
     735    //NSC_DEBUG_MSG_STD(_T("result: ") + strEx::itos(bFilterIn) + _T(" -- ") + strEx::itos(bMatch)); 
    707736    if ((bFilterIn&&bMatch)||(!bFilterIn&&!bMatch)) { 
    708737      strEx::append_list(message, info.render(syntax)); 
     
    715744      hit_count++; 
    716745    } 
     746    file_count++; 
    717747    return true; 
    718748  } 
     
    723753    if (errors != NULL) 
    724754      errors->report_error(msg); 
     755    last_error = msg; 
     756    error_count++; 
    725757    error = true; 
    726758  } 
     759 
     760  std::wstring render(std::wstring syntax) { 
     761    strEx::replace(syntax, _T("%list%"), message); 
     762    strEx::replace(syntax, _T("%matches%"), strEx::itos(hit_count)); 
     763    strEx::replace(syntax, _T("%files%"), strEx::itos(file_count)); 
     764    return syntax; 
     765  } 
     766 
    727767}; 
    728768 
     
    738778  std::wstring format = _T("%Y years %m mon %d days %H hours %M min %S sec"); 
    739779  std::wstring path; 
     780  bool debug = false; 
    740781  find_first_file_info finder; 
    741782  MAP_OPTIONS_BEGIN(stl_args) 
    742783    MAP_OPTIONS_STR(_T("path"), path) 
    743784    MAP_OPTIONS_STR(_T("date"), format) 
     785    MAP_OPTIONS_BOOL_TRUE(_T("debug"), debug) 
    744786    MAP_OPTIONS_FALLBACK(format) 
    745787  MAP_OPTIONS_END() 
     
    752794  NSC_error errors; 
    753795  pattern_type splitpath = split_pattern(path); 
    754   recursive_scan<find_first_file_info>(splitpath.first, splitpath.second, -1, -1, finder, &errors); 
     796  recursive_scan<find_first_file_info>(splitpath.first, splitpath.second, -1, -1, finder, &errors, debug); 
    755797  if (finder.hasError()) { 
    756798    message = _T("File not found (check log for details)"); 
     
    783825  bool bPerfData = true; 
    784826  unsigned int max_dir_depth = -1; 
     827  bool debug = false; 
    785828 
    786829  try { 
     
    794837      MAP_OPTIONS_STR2INT(_T("max-dir-depth"), max_dir_depth) 
    795838      MAP_OPTIONS_PUSH(_T("file"), paths) 
     839      MAP_OPTIONS_BOOL_TRUE(_T("debug"), debug) 
    796840      MAP_OPTIONS_BOOL_EX(_T("filter"), finder.bFilterIn, _T("in"), _T("out")) 
    797841      MAP_OPTIONS_BOOL_EX(_T("filter"), finder.bFilterAll, _T("all"), _T("any")) 
     
    816860  for (std::list<std::wstring>::const_iterator pit = paths.begin(); pit != paths.end(); ++pit) { 
    817861    pattern_type path = split_pattern(*pit); 
    818     recursive_scan<file_filter_function>(path.first, path.second, 0, max_dir_depth, finder, &errors); 
     862    recursive_scan<file_filter_function>(path.first, path.second, 0, max_dir_depth, finder, &errors, debug); 
    819863    if (finder.hasError()) { 
    820864      message = _T("File not found: ") + (*pit) + _T(" check log for details."); 
     
    863907  CheckFileContainer query; 
    864908  std::wstring syntax = _T("%filename%"); 
     909  std::wstring masterSyntax = _T("%list%"); 
    865910  std::wstring alias; 
    866911  std::wstring pattern = _T("*.*"); 
    867912  bool bPerfData = true; 
    868913  int max_dir_depth = -1; 
     914  bool debug = false; 
     915  bool ignoreError = false; 
    869916 
    870917  try { 
     
    874921      MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) 
    875922      MAP_OPTIONS_STR(_T("syntax"), syntax) 
     923      MAP_OPTIONS_STR(_T("master-syntax"), masterSyntax) 
    876924      MAP_OPTIONS_PUSH(_T("path"), paths) 
    877925      MAP_OPTIONS_STR(_T("pattern"), pattern) 
    878926      MAP_OPTIONS_STR(_T("alias"), alias) 
    879927      MAP_OPTIONS_PUSH(_T("file"), paths) 
     928      MAP_OPTIONS_BOOL_TRUE(_T("debug"), debug) 
     929      MAP_OPTIONS_BOOL_TRUE(_T("ignore-errors"), ignoreError) 
    880930      MAP_OPTIONS_STR2INT(_T("max-dir-depth"), max_dir_depth) 
    881931      MAP_OPTIONS_BOOL_EX(_T("filter"), finder.bFilterIn, _T("in"), _T("out")) 
     
    918968    FILETIME now; 
    919969    GetSystemTimeAsFileTime(&now); 
     970    finder.debug_ = debug; 
    920971    finder.now = ((now.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)now.dwLowDateTime); 
    921972    finder.syntax = syntax; 
    922973    NSC_error errors; 
    923974    for (std::list<std::wstring>::const_iterator pit = paths.begin(); pit != paths.end(); ++pit) { 
    924       recursive_scan<file_filter_function_ex>(*pit, pattern, 0, max_dir_depth, finder, &errors); 
    925       if (finder.hasError()) { 
    926         message = _T("Error when scanning: ") + (*pit) + _T(" check log for details."); 
     975      recursive_scan<file_filter_function_ex>(*pit, pattern, 0, max_dir_depth, finder, &errors, debug); 
     976      if (!ignoreError && finder.hasError()) { 
     977        message = _T("Error when scanning: ") + (*pit) + _T(" check log for details (") + strEx::itos(finder.error_count) + _T(": ") + finder.last_error + _T(")"); 
    927978        return NSCAPI::returnUNKNOWN; 
    928979      } 
    929980    } 
    930     message = finder.message; 
     981    message  = finder.render(masterSyntax); 
    931982    if (!alias.empty()) 
    932983      query.alias = alias; 
     
    936987    if ((truncate > 0) && (message.length() > (truncate-4))) { 
    937988      message = message.substr(0, truncate-4) + _T("..."); 
    938       perf = _T(""); 
     989      //perf = _T(""); 
    939990    } 
    940991    if (message.empty()) 
     
    942993    return returnCode; 
    943994} 
    944  
     995struct file_container : public file_info { 
     996  std::wstring error_; 
     997   
     998 
     999  file_container(const BY_HANDLE_FILE_INFORMATION info, std::wstring file) : file_info(info, file_helpers::meta::get_path(file), file_helpers::meta::get_filename(file)) {} 
     1000  file_container(std::wstring file, std::wstring error) : error_(error), file_info(file_helpers::meta::get_path(file), file_helpers::meta::get_filename(file)) {} 
     1001 
     1002  bool has_errors() { 
     1003    return !error_.empty(); 
     1004  } 
     1005  std::wstring get_error() { 
     1006    return error_; 
     1007  } 
     1008 
     1009}; 
     1010 
     1011typedef checkHolders::ExactBounds<checkHolders::NumericBounds<checkHolders::disk_size_type, checkHolders::disk_size_handler<checkHolders::disk_size_type> > > ExactBoundsDiscSize; 
     1012 
     1013 
     1014typedef checkHolders::CheckContainer<checkHolders::ExactBoundsULong> ExactULongContainer; 
     1015typedef checkHolders::CheckContainer<ExactBoundsDiscSize> DiscSizeContainer; 
     1016typedef checkHolders::CheckContainer<checkHolders::ExactBoundsTime> DateTimeContainer; 
     1017 
     1018struct check_file_size : public checkHolders::check_proxy_container<file_container, DiscSizeContainer> { 
     1019  check_file_size() { set_alias(_T("size")); } 
     1020  checkHolders::disk_size_type get_value(file_container &value) { 
     1021    return value.ullSize; 
     1022  } 
     1023}; 
     1024struct check_file_line_count : public checkHolders::check_proxy_container<file_container, ExactULongContainer> { 
     1025  check_file_line_count() { set_alias(_T("line-count")); } 
     1026  unsigned long get_value(file_container &value) { 
     1027    return value.get_line_count(); 
     1028  } 
     1029}; 
     1030struct check_file_dates : public checkHolders::check_proxy_container<file_container, DateTimeContainer> { 
     1031  enum type_type { 
     1032    date_access, date_creation, date_written 
     1033  } ; 
     1034  type_type type_; 
     1035  check_file_dates(type_type type) : type_(type)  
     1036  {  
     1037    if (type_ == date_creation) 
     1038      set_alias(_T("creation"));  
     1039    else if (type_ == date_access) 
     1040      set_alias(_T("access"));  
     1041    else if (type_ == date_written) 
     1042      set_alias(_T("written"));  
     1043    else 
     1044      set_alias(_T("unknown date type"));  
     1045  } 
     1046  unsigned long long get_value(file_container &value) { 
     1047    if (type_ == date_creation) 
     1048      return value.ullCreationTime; 
     1049    if (type_ == date_access) 
     1050      return value.ullLastAccessTime; 
     1051    if (type_ == date_written) 
     1052      return value.ullLastWriteTime; 
     1053    return -1; 
     1054  } 
     1055}; 
     1056 
     1057typedef checkHolders::check_multi_container<file_container> check_file_multi; 
     1058struct check_file_factories { 
     1059  static checkHolders::check_proxy_interface<file_container>* size() { 
     1060    return new check_file_size(); 
     1061  } 
     1062  static checkHolders::check_proxy_interface<file_container>* line_count() { 
     1063    return new check_file_line_count(); 
     1064  } 
     1065  static checkHolders::check_proxy_interface<file_container>* access() { 
     1066    return new check_file_dates(check_file_dates::date_access); 
     1067  } 
     1068  static checkHolders::check_proxy_interface<file_container>* creation() { 
     1069    return new check_file_dates(check_file_dates::date_creation); 
     1070  } 
     1071  static checkHolders::check_proxy_interface<file_container>* written() { 
     1072    return new check_file_dates(check_file_dates::date_written); 
     1073  } 
     1074}; 
     1075 
     1076file_container get_file_info(std::wstring file, unsigned long long now) { 
     1077 
     1078  BY_HANDLE_FILE_INFORMATION _info; 
     1079 
     1080  HANDLE hFile = CreateFile(file.c_str(), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, 
     1081    0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); 
     1082  if (hFile == INVALID_HANDLE_VALUE) { 
     1083    return file_container(file, _T("Could not open file: ") + file); 
     1084  } 
     1085  GetFileInformationByHandle(hFile, &_info); 
     1086  CloseHandle(hFile); 
     1087  file_container info(_info, file); 
     1088  info.ullNow = now; 
     1089  return info; 
     1090} 
     1091 
     1092#define MAP_FACTORY_PB(value, obj) \ 
     1093    else if ((p__.first == _T("check")) && (p__.second == ##value)) { checker.add_check(check_file_factories::obj()); } 
     1094 
     1095 
     1096NSCAPI::nagiosReturn CheckDisk::CheckSingleFile(const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf) { 
     1097  NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; 
     1098  std::list<std::wstring> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args); 
     1099  check_file_multi checker; 
     1100  typedef std::pair<int,file_filter> filteritem_type; 
     1101  typedef std::list<filteritem_type > filterlist_type; 
     1102  if (stl_args.empty()) { 
     1103    message = _T("Missing argument(s)."); 
     1104    return NSCAPI::returnUNKNOWN; 
     1105  } 
     1106  std::list<std::wstring> files; 
     1107  unsigned int truncate = 0; 
     1108  std::wstring syntax = _T("%filename%"); 
     1109  std::wstring alias; 
     1110  bool bPerfData = true; 
     1111 
     1112  try { 
     1113    MAP_OPTIONS_BEGIN(stl_args) 
     1114      //MAP_OPTIONS_NUMERIC_ALL(query, _T("")) 
     1115      MAP_OPTIONS_STR2INT(_T("truncate"), truncate) 
     1116      MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) 
     1117      MAP_OPTIONS_STR(_T("syntax"), syntax) 
     1118      MAP_OPTIONS_STR(_T("alias"), alias) 
     1119      MAP_OPTIONS_PUSH(_T("file"), files) 
     1120      MAP_OPTIONS_EXACT_NUMERIC_ALL_MULTI(checker, _T("")) 
     1121      MAP_FACTORY_PB(_T("size"), size) 
     1122      MAP_FACTORY_PB(_T("line-count"), line_count) 
     1123      MAP_FACTORY_PB(_T("creation"), creation) 
     1124      MAP_FACTORY_PB(_T("access"), access) 
     1125      MAP_FACTORY_PB(_T("written"), written) 
     1126      /* 
     1127      MAP_FILTER(_T("creation"), creation) 
     1128      MAP_FILTER(_T("written"), written) 
     1129      MAP_FILTER(_T("accessed"), accessed) 
     1130      MAP_FILTER(_T("version"), version) 
     1131      MAP_FILTER(_T("line-count"), line_count) 
     1132      */ 
     1133      MAP_OPTIONS_MISSING(message, _T("Unknown argument: ")) 
     1134      MAP_OPTIONS_END() 
     1135  } catch (filters::parse_exception e) { 
     1136    message = e.getMessage(); 
     1137    return NSCAPI::returnUNKNOWN; 
     1138  } catch (filters::filter_exception e) { 
     1139    message = e.getMessage(); 
     1140    return NSCAPI::returnUNKNOWN; 
     1141  } 
     1142  FILETIME now; 
     1143  GetSystemTimeAsFileTime(&now); 
     1144  unsigned __int64 nowi64 = ((now.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)now.dwLowDateTime); 
     1145  //finder.syntax = syntax; 
     1146  for (std::list<std::wstring>::const_iterator pit = files.begin(); pit != files.end(); ++pit) { 
     1147    file_container info = get_file_info(*pit, nowi64); 
     1148    checker.alias = info.render(syntax); 
     1149    checker.runCheck(info, returnCode, message, perf); 
     1150  } 
     1151  if ((truncate > 0) && (message.length() > (truncate-4))) { 
     1152    message = message.substr(0, truncate-4) + _T("..."); 
     1153    perf = _T(""); 
     1154  } 
     1155  if (message.empty()) 
     1156    message = _T("CheckSingleFile ok"); 
     1157  return returnCode; 
     1158} 
    9451159NSCAPI::nagiosReturn CheckDisk::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &msg, std::wstring &perf) { 
    9461160  if (command == _T("CheckFileSize")) { 
     
    9521166  } else if (command == _T("CheckFile2")) { 
    9531167    return CheckFile2(argLen, char_args, msg, perf); 
     1168  } else if (command == _T("CheckSingleFile")) { 
     1169    return CheckSingleFile(argLen, char_args, msg, perf); 
    9541170  } else if (command == _T("getFileAge")) { 
    9551171    return getFileAge(argLen, char_args, msg, perf); 
  • branches/stable/modules/CheckDisk/CheckDisk.h

    r165 r227  
    5757  NSCAPI::nagiosReturn CheckFile2(const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf); 
    5858  NSCAPI::nagiosReturn getFileAge(const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf); 
     59  NSCAPI::nagiosReturn CheckSingleFile(const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf); 
    5960 
    6061private: 
Note: See TracChangeset for help on using the changeset viewer.