Changeset f7663c9 in nscp
- Timestamp:
- 10/13/10 22:03:15 (3 years ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2
- Children:
- 1ecd26f
- Parents:
- c015acc
- Files:
-
- 1 added
- 12 edited
-
include/nscapi/settings.hpp (modified) (16 diffs)
-
modules/CheckDisk/CMakeLists.txt (modified) (1 diff)
-
modules/CheckDisk/CheckDisk.cpp (modified) (3 diffs)
-
modules/CheckDisk/CheckDisk.def (modified) (2 diffs)
-
modules/CheckDisk/CheckDisk.h (modified) (4 diffs)
-
modules/CheckDisk/module.cmake (added)
-
modules/CheckDisk/stdafx.h (modified) (2 diffs)
-
modules/FileLogger/FileLogger.cpp (modified) (1 diff)
-
modules/NRPEServer/NRPEServer.cpp (modified) (2 diffs)
-
modules/NSCAAgent/NSCAAgent.cpp (modified) (1 diff)
-
service/NSCPlugin.cpp (modified) (3 diffs)
-
service/NSCPlugin.h (modified) (2 diffs)
-
service/NSClient++.cpp (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
include/nscapi/settings.hpp
rc015acc rf7663c9 18 18 virtual std::wstring get_default_as_string() const = 0; 19 19 virtual void notify(nscapi::core_wrapper* core_, std::wstring path, std::wstring key) const = 0; 20 virtual void notify(nscapi::core_wrapper* core_, std::wstring parent, std::wstring path, std::wstring key) const = 0; 20 21 }; 21 22 template<class T> … … 50 51 update_target(&value); 51 52 } 53 virtual void notify(nscapi::core_wrapper* core_, std::wstring parent, std::wstring path, std::wstring key) const { 54 std::wstring default_value = core_->getSettingsString(parent, key, typed_key<T>::default_value_as_text_); 55 T value = boost::lexical_cast<T>(core_->getSettingsString(path, key, default_value)); 56 update_target(&value); 57 } 52 58 }; 53 59 template<class T> … … 60 66 virtual void notify(nscapi::core_wrapper* core_, std::wstring path, std::wstring key) const { 61 67 std::wstring val = core_->getSettingsString(path, key, typed_key<T>::default_value_as_text_); 68 T value = boost::lexical_cast<T>(core_->expand_path(val)); 69 update_target(&value); 70 } 71 virtual void notify(nscapi::core_wrapper* core_, std::wstring parent, std::wstring path, std::wstring key) const { 72 std::wstring def_val = core_->getSettingsString(parent, key, typed_key<T>::default_value_as_text_); 73 std::wstring val = core_->getSettingsString(path, key, def_val); 62 74 T value = boost::lexical_cast<T>(core_->expand_path(val)); 63 75 update_target(&value); … … 80 92 update_target(&value); 81 93 } 94 virtual void notify(nscapi::core_wrapper* core_, std::wstring parent, std::wstring path, std::wstring key) const { 95 T default_value = static_cast<T>(core_->getSettingsInt(parent, key, default_value_as_int_)); 96 T value = static_cast<T>(core_->getSettingsInt(path, key, default_value)); 97 update_target(&value); 98 } 82 99 protected: 83 100 int default_value_as_int_; … … 92 109 virtual void notify(nscapi::core_wrapper* core_, std::wstring path, std::wstring key) const { 93 110 T value = static_cast<T>(core_->getSettingsBool(path, key, typed_int_value<T>::default_value_as_int_==1)); 111 update_target(&value); 112 } 113 virtual void notify(nscapi::core_wrapper* core_, std::wstring parent, std::wstring path, std::wstring key) const { 114 T default_value = static_cast<T>(core_->getSettingsBool(parent, key, typed_int_value<T>::default_value_as_int_==1)); 115 T value = static_cast<T>(core_->getSettingsBool(path, key, default_value)); 94 116 update_target(&value); 95 117 } … … 265 287 std::wstring path; 266 288 std::wstring key_name; 289 std::wstring parent; 267 290 268 291 boost::shared_ptr<key_interface> key; … … 275 298 , description(description_) 276 299 {} 277 key_info(const key_info& obj) : path(obj.path), key_name(obj.key_name), key(obj.key), description(obj.description) {}300 key_info(const key_info& obj) : path(obj.path), key_name(obj.key_name), key(obj.key), description(obj.description), parent(obj.parent) {} 278 301 virtual key_info& operator=(const key_info& obj) { 279 302 path = obj.path; … … 281 304 key = obj.key; 282 305 description = obj.description; 283 return *this; 306 parent = obj.parent; 307 return *this; 308 } 309 void set_parent(std::wstring parent_) { 310 parent = parent_; 311 } 312 bool has_parent() const { 313 return !parent.empty(); 314 } 315 std::wstring get_parent() const { 316 return parent; 284 317 } 285 318 }; … … 344 377 settings_keys_easy_init(settings_registry* owner_) : owner(owner_) {} 345 378 settings_keys_easy_init(std::wstring path, settings_registry* owner_) : owner(owner_), path_(path) {} 379 settings_keys_easy_init(std::wstring path, std::wstring parent, settings_registry* owner_) : owner(owner_), path_(path), parent_(parent) {} 346 380 347 381 settings_keys_easy_init& operator()(std::wstring path, std::wstring key_name, key_interface *value, std::wstring title, std::wstring description) { 348 382 boost::shared_ptr<key_info> d(new key_info(path, key_name, value, description_container(title, description))); 383 if (!parent_.empty()) 384 d->set_parent(parent_); 349 385 add(d); 350 386 return *this; … … 353 389 settings_keys_easy_init& operator()(std::wstring key_name, key_interface* value, std::wstring title, std::wstring description) { 354 390 boost::shared_ptr<key_info> d(new key_info(path_, key_name, value, description_container(title, description))); 391 if (!parent_.empty()) 392 d->set_parent(parent_); 355 393 add(d); 356 394 return *this; … … 362 400 settings_registry* owner; 363 401 std::wstring path_; 402 std::wstring parent_; 364 403 }; 365 404 … … 391 430 public: 392 431 alias_extension(settings_registry * owner, std::wstring alias) : owner_(owner), alias_(alias) {} 432 alias_extension(const alias_extension &other) : owner_(other.owner_), alias_(other.alias_), parent_(other.parent_) {} 433 alias_extension& operator = (const alias_extension& other) { 434 owner_ = other.owner_; 435 alias_ = other.alias_; 436 parent_ = other.parent_; 437 return *this; 438 } 393 439 394 440 settings_keys_easy_init add_key_to_path(std::wstring path) { 395 return settings_keys_easy_init(get_path(path), owner_);441 return settings_keys_easy_init(get_path(path), parent_, owner_); 396 442 } 397 443 settings_paths_easy_init add_path(std::wstring path) { … … 406 452 407 453 settings_keys_easy_init add_key_to_settings(std::wstring path = _T("")) { 408 return settings_keys_easy_init(get_settings_path(path), owner_);454 return settings_keys_easy_init(get_settings_path(path), parent_, owner_); 409 455 } 410 456 settings_paths_easy_init add_path_to_settings(std::wstring path = _T("")) { … … 416 462 return _T("/settings/") + alias_ + _T("/") + path; 417 463 } 464 465 alias_extension add_parent(std::wstring parent_path) { 466 set_parent_path(parent_path); 467 return *this; 468 } 469 418 470 419 471 static std::wstring get_alias(std::wstring cur, std::wstring def) { … … 437 489 alias_ = get_alias(prefix, cur, def); 438 490 } 491 void set_parent_path(std::wstring parent) { 492 parent_ = parent; 493 } 439 494 440 495 private: 441 496 std::wstring alias_; 442 497 settings_registry * owner_; 498 std::wstring parent_; 443 499 }; 444 500 … … 495 551 void register_all() { 496 552 BOOST_FOREACH(key_list::value_type v, keys_) { 497 if (v->key) 498 core_->settings_register_key(v->path, v->key_name, v->key->get_type(), v->description.title, v->description.description, v->key->get_default_as_string(), v->description.advanced); 553 if (v->key) { 554 //std::wcout << _T("Setting: ") << v->key_name << _T(" ===> ") << v->parent << std::endl; 555 std::wstring desc = v->description.description; 556 if (v->has_parent()) 557 desc += _T(" Parent element can be found under: ") + v->parent; 558 core_->settings_register_key(v->path, v->key_name, v->key->get_type(), v->description.title, desc, v->key->get_default_as_string(), v->description.advanced); 559 } 499 560 } 500 561 BOOST_FOREACH(path_list::value_type v, paths_) { -
modules/CheckDisk/CMakeLists.txt
r5cd6bcf rf7663c9 28 28 ${Boost_FILESYSTEM_LIBRARY} 29 29 ${NSCP_DEF_PLUGIN_LIB} 30 version.lib 30 31 ) -
modules/CheckDisk/CheckDisk.cpp
rfe9cc46 rf7663c9 28 28 #include <checkHelpers.hpp> 29 29 30 namespace sh = nscapi::settings_helper; 31 30 32 CheckDisk gCheckDisk; 31 33 … … 39 41 } 40 42 41 bool CheckDisk::loadModule(NSCAPI::moduleLoadMode mode) { 42 43 bool CheckDisk::loadModule() { 44 return false; 45 } 46 47 bool CheckDisk::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { 43 48 try { 44 GET_CORE()->registerCommand(_T("check_drive"), _T("Check the disk space usage of a drive or volume.")); 45 GET_CORE()->registerCommand(_T("check_file"), _T("Check a single file and/or folder.")); 46 GET_CORE()->registerCommand(_T("check_files"), _T("Check a set of files and/or directories")); 47 48 show_errors_ = SETTINGS_GET_BOOL(check_disk::SHOW_ERRORS); 49 get_core()->registerCommand(_T("CheckFileSize"), _T("Check or directory a file and verify its size.")); 50 get_core()->registerCommand(_T("CheckDriveSize"), _T("Check the size (free-space) of a drive or volume.")); 51 get_core()->registerCommand(_T("CheckFile2"), _T("Check various aspects of a file and/or folder.")); 52 53 sh::settings_registry settings(nscapi::plugin_singleton->get_core()); 54 settings.set_alias(_T("NRPE"), alias, _T("server")); 55 56 settings.alias().add_path_to_settings() 57 (_T("NRPE SERVER SECTION"), _T("Section for NRPE (NRPEListener.dll) (check_nrpe) protocol options.")) 58 ; 59 60 settings.alias().add_key_to_settings() 61 (_T("show errors"), sh::bool_key(&show_errors_, false), 62 _T("SHOW ERRORS"), _T("")) 63 ; 64 } catch (std::exception &e) { 65 NSC_LOG_ERROR_STD(_T("Exception caught: ") + to_wstring(e.what())); 66 return false; 49 67 } catch (nscapi::nscapi_exception &e) { 50 68 NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_); 69 return false; 51 70 } catch (...) { 52 71 NSC_LOG_ERROR_STD(_T("Failed to register command.")); 72 return false; 53 73 } 54 74 return true; … … 65 85 } 66 86 67 using namespace boost::filesystem; 68 69 struct find_options { 70 bool recursive; 71 int max_depth; 72 int current_depth; 73 bool single_file; 74 75 find_options() : current_depth(0), single_file(false) {} 76 77 inline find_options go_down() { 78 find_options op = *this; 79 op.current_depth++; 80 return op; 81 } 82 inline bool has_all(std::list<file_item> & files) { 83 return single_file; 84 } 87 class error_reporter { 88 public: 89 virtual void report_error(std::wstring error) = 0; 90 virtual void report_warning(std::wstring error) = 0; 91 virtual bool has_error() = 0; 92 virtual std::wstring get_error() = 0; 85 93 }; 86 struct file_item { 87 bool is_directory() { 94 95 96 97 struct file_finder_data { 98 file_finder_data(const WIN32_FIND_DATA wfd_, const std::wstring path_, error_reporter *errors_) : wfd(wfd_), path(path_), errors(errors_) {} 99 const WIN32_FIND_DATA wfd; 100 const std::wstring path; 101 error_reporter *errors; 102 }; 103 typedef std::unary_function<const file_finder_data&, bool> baseFinderFunction; 104 105 struct get_size : public baseFinderFunction 106 { 107 get_size() : size(0) { } 108 result_type operator()(argument_type ffd) { 109 if (!is_directory(ffd.wfd.dwFileAttributes)) { 110 size += (ffd.wfd.nFileSizeHigh * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)ffd.wfd.nFileSizeLow; 111 } 88 112 return true; 89 113 } 90 bool is_file() { 114 inline unsigned long long getSize() { 115 return size; 116 } 117 inline void setError(error_reporter *errors, std::wstring msg) { 118 if (errors != NULL) 119 errors->report_error(msg); 120 } 121 private: 122 unsigned long long size; 123 }; 124 125 template <class finder_function> 126 void recursive_scan(std::wstring dir, std::wstring pattern, int current_level, int max_level, finder_function & f, error_reporter * errors, bool debug, bool recursive = false) { 127 if ((max_level != -1) && (current_level > max_level)) 128 return; 129 WIN32_FIND_DATA wfd; 130 131 DWORD fileAttr = GetFileAttributes(dir.c_str()); 132 if ((fileAttr == INVALID_FILE_ATTRIBUTES)&&(!recursive)) { 133 errors->report_error(_T("Invalid file specified: ") + dir); 134 } else if (fileAttr == INVALID_FILE_ATTRIBUTES) { 135 errors->report_warning(_T("Invalid file specified: ") + dir); 136 } 137 if (debug) NSC_DEBUG_MSG_STD(_T("Input is: ") + dir + _T(" / ") + strEx::ihextos(fileAttr)); 138 139 if (!is_directory(fileAttr)) { 140 if (debug) NSC_DEBUG_MSG_STD(_T("Found a file dont do recursive scan: ") + dir); 141 // It is a file check it an return (dont check recursivly) 142 pattern_type single_path = split_path_ex(dir); 143 if (debug) NSC_DEBUG_MSG_STD(_T("Path is: ") + single_path.first); 144 HANDLE hFind = FindFirstFile(dir.c_str(), &wfd); 145 if (hFind != INVALID_HANDLE_VALUE) { 146 f(file_finder_data(wfd, single_path.first, errors)); 147 FindClose(hFind); 148 } else { 149 NSC_DEBUG_MSG_STD(_T("File was NOT found!")); 150 } 151 return; 152 } 153 std::wstring file_pattern = dir + _T("\\") + pattern; 154 if (debug) NSC_DEBUG_MSG_STD(_T("File pattern: ") + file_pattern); 155 HANDLE hFind = FindFirstFile(file_pattern.c_str(), &wfd); 156 if (hFind != INVALID_HANDLE_VALUE) { 157 do { 158 if (!f(file_finder_data(wfd, dir, errors))) 159 break; 160 } while (FindNextFile(hFind, &wfd)); 161 FindClose(hFind); 162 } 163 std::wstring dir_pattern = dir + _T("\\*.*"); 164 if (debug) NSC_DEBUG_MSG_STD(_T("File pattern: ") + dir_pattern); 165 hFind = FindFirstFile(dir_pattern.c_str(), &wfd); 166 if (hFind != INVALID_HANDLE_VALUE) { 167 do { 168 if (is_directory(wfd.dwFileAttributes)) { 169 if ( (wcscmp(wfd.cFileName, _T(".")) != 0) && (wcscmp(wfd.cFileName, _T("..")) != 0) ) 170 recursive_scan<finder_function>(dir + _T("\\") + wfd.cFileName, pattern, current_level+1, max_level, f, errors, debug, true); 171 } 172 } while (FindNextFile(hFind, &wfd)); 173 FindClose(hFind); 174 } 175 } 176 177 178 179 NSCAPI::nagiosReturn CheckDisk::CheckDriveSize(std::list<std::wstring> args, std::wstring &message, std::wstring &perf) { 180 NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; 181 if (args.empty()) { 182 message = _T("Missing argument(s)."); 183 return NSCAPI::returnCRIT; 184 } 185 186 DriveContainer tmpObject; 187 bool bFilter = false; 188 bool bFilterRemote = false; 189 bool bFilterRemovable = false; 190 bool bFilterFixed = false; 191 bool bFilterCDROM = false; 192 bool bFilterNoRootDir = false; 193 bool bCheckAllDrives = false; 194 bool bCheckAllOthers = false; 195 bool bNSClient = false; 196 bool bPerfData = true; 197 std::list<DriveContainer> drives; 198 std::wstring strCheckAll; 199 200 MAP_OPTIONS_BEGIN(args) 201 MAP_OPTIONS_STR_AND(_T("Drive"), tmpObject.data, drives.push_back(tmpObject)) 202 MAP_OPTIONS_DISK_ALL(tmpObject, _T(""), _T("Free"), _T("Used")) 203 MAP_OPTIONS_SHOWALL(tmpObject) 204 MAP_OPTIONS_BOOL_VALUE(_T("FilterType"), bFilterFixed, _T("FIXED")) 205 MAP_OPTIONS_BOOL_VALUE(_T("FilterType"), bFilterCDROM, _T("CDROM")) 206 MAP_OPTIONS_BOOL_VALUE(_T("FilterType"), bFilterRemovable, _T("REMOVABLE")) 207 MAP_OPTIONS_BOOL_VALUE(_T("FilterType"), bFilterRemote, _T("REMOTE")) 208 MAP_OPTIONS_BOOL_VALUE(_T("FilterType"), bFilterNoRootDir, _T("NO_ROOT_DIR")) 209 MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) 210 MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient) 211 //MAP_OPTIONS_BOOL_TRUE(CHECK_ALL, bCheckAll) 212 MAP_OPTIONS_STR(CHECK_ALL, strCheckAll) 213 MAP_OPTIONS_BOOL_TRUE(CHECK_ALL_OTHERS, bCheckAllOthers) 214 MAP_OPTIONS_SECONDARY_BEGIN(_T(":"), p2) 215 else if (p2.first == _T("Drive")) { 216 tmpObject.data = p__.second; 217 tmpObject.alias = p2.second; 218 drives.push_back(tmpObject); 219 } 220 MAP_OPTIONS_MISSING_EX(p2, message, _T("Unknown argument: ")) 221 MAP_OPTIONS_SECONDARY_END() 222 MAP_OPTIONS_FALLBACK_AND(tmpObject.data, drives.push_back(tmpObject)) 223 MAP_OPTIONS_END() 224 bFilter = bFilterFixed || bFilterCDROM || bFilterRemote || bFilterRemovable; 225 226 if ((drives.size() == 0) && strCheckAll.empty()) 227 bCheckAllDrives = true; 228 229 if (strCheckAll == _T("volumes")) { 230 231 DWORD bufSize = GetLogicalDriveStrings(0, NULL)+5; 232 TCHAR *buffer = new TCHAR[bufSize+10]; 233 if (GetLogicalDriveStrings(bufSize, buffer)>0) { 234 while (buffer[0] != 0) { 235 std::wstring drv = buffer; 236 237 UINT drvType = GetDriveType(drv.c_str()); 238 if ( 239 ((!bFilter)&&(drvType == DRIVE_FIXED)) || 240 ((bFilter)&&(bFilterFixed)&&(drvType==DRIVE_FIXED)) || 241 ((bFilter)&&(bFilterCDROM)&&(drvType==DRIVE_CDROM)) || 242 ((bFilter)&&(bFilterRemote)&&(drvType==DRIVE_REMOTE)) || 243 ((bFilter)&&(bFilterRemovable)&&(drvType==DRIVE_REMOVABLE)) || 244 ((bFilter)&&(bFilterNoRootDir)&&(drvType==DRIVE_NO_ROOT_DIR)) 245 ) 246 drives.push_back(DriveContainer(drv, tmpObject.warn, tmpObject.crit)); 247 else 248 NSC_DEBUG_MSG_STD(_T("Ignoring drive: ") + drv); 249 250 buffer = &buffer[drv.size()]; 251 buffer++; 252 } 253 } else { 254 NSC_LOG_ERROR_STD(_T("Failed to get buffer size: ") + error::lookup::last_error()); 255 } 256 } 257 258 if (bCheckAllDrives) { 259 DWORD dwDrives = GetLogicalDrives(); 260 int idx = 0; 261 while (dwDrives != 0) { 262 if (dwDrives & 0x1) { 263 std::wstring drv; 264 drv += static_cast<TCHAR>('A' + idx); drv += _T(":\\"); 265 UINT drvType = GetDriveType(drv.c_str()); 266 if ( ((!bFilter)&&(drvType == DRIVE_FIXED)) || 267 ((bFilter)&&(bFilterFixed)&&(drvType==DRIVE_FIXED)) || 268 ((bFilter)&&(bFilterCDROM)&&(drvType==DRIVE_CDROM)) || 269 ((bFilter)&&(bFilterRemote)&&(drvType==DRIVE_REMOTE)) || 270 ((bFilter)&&(bFilterRemovable)&&(drvType==DRIVE_REMOVABLE)) || 271 ((bFilter)&&(bFilterNoRootDir)&&(drvType==DRIVE_NO_ROOT_DIR)) 272 ) 273 drives.push_back(DriveContainer(drv, tmpObject.warn, tmpObject.crit)); 274 } 275 idx++; 276 dwDrives >>= 1; 277 } 278 } 279 if (bCheckAllOthers) { 280 std::list<DriveContainer> checkdrives; 281 DWORD dwDrives = GetLogicalDrives(); 282 int idx = 0; 283 while (dwDrives != 0) { 284 if (dwDrives & 0x1) { 285 std::wstring drv; 286 drv += static_cast<TCHAR>('A' + idx); drv += _T(":\\"); 287 UINT drvType = GetDriveType(drv.c_str()); 288 if ( ((!bFilter)&&(drvType == DRIVE_FIXED)) || 289 ((bFilter)&&(bFilterFixed)&&(drvType==DRIVE_FIXED)) || 290 ((bFilter)&&(bFilterCDROM)&&(drvType==DRIVE_CDROM)) || 291 ((bFilter)&&(bFilterRemote)&&(drvType==DRIVE_REMOTE)) || 292 ((bFilter)&&(bFilterRemovable)&&(drvType==DRIVE_REMOVABLE)) || 293 ((bFilter)&&(bFilterNoRootDir)&&(drvType==DRIVE_NO_ROOT_DIR)) 294 ) 295 { 296 bool bFound = false; 297 for (std::list<DriveContainer>::const_iterator pit = drives.begin();pit!=drives.end();++pit) { 298 DriveContainer drive = (*pit); 299 if (_wcsicmp(drive.data.substr(0,1).c_str(), drv.substr(0,1).c_str())==0) 300 bFound = true; 301 } 302 if (!bFound) 303 checkdrives.push_back(DriveContainer(drv, tmpObject.warn, tmpObject.crit)); 304 } 305 } 306 idx++; 307 dwDrives >>= 1; 308 } 309 drives = checkdrives; 310 } 311 312 313 for (std::list<DriveContainer>::const_iterator pit = drives.begin();pit!=drives.end();++pit) { 314 DriveContainer drive = (*pit); 315 if (drive.data.length() == 1) 316 drive.data += _T(":"); 317 drive.perfData = bPerfData; 318 UINT drvType = GetDriveType(drive.data.c_str()); 319 320 if ((!bFilter)&&!((drvType == DRIVE_FIXED)||(drvType == DRIVE_NO_ROOT_DIR))) { 321 message = _T("UNKNOWN: Drive is not a fixed drive: ") + drive.getAlias() + _T(" (it is a ") + get_filter(drvType) + _T(" drive)"); 322 return NSCAPI::returnUNKNOWN; 323 } else if ( (bFilter)&& 324 ( 325 ((!bFilterFixed)&&((drvType==DRIVE_FIXED)||(drvType==DRIVE_NO_ROOT_DIR))) || 326 ((!bFilterCDROM)&&(drvType==DRIVE_CDROM)) || 327 ((!bFilterRemote)&&(drvType==DRIVE_REMOTE)) || 328 ((!bFilterRemovable)&&(drvType==DRIVE_REMOVABLE)) 329 )) { 330 message = _T("UNKNOWN: Drive does not match the current filter: ") + drive.getAlias() + _T(" (add FilterType=") + get_filter(drvType) + _T(" to check this drive)"); 331 return NSCAPI::returnUNKNOWN; 332 } 333 334 ULARGE_INTEGER freeBytesAvailableToCaller; 335 ULARGE_INTEGER totalNumberOfBytes; 336 ULARGE_INTEGER totalNumberOfFreeBytes; 337 if (!GetDiskFreeSpaceEx(drive.data.c_str(), &freeBytesAvailableToCaller, &totalNumberOfBytes, &totalNumberOfFreeBytes)) { 338 message = _T("CRITICAL: Could not get free space for: ") + drive.getAlias() + _T(" ") + drive.data + _T(" reason: ") + error::lookup::last_error(); 339 return NSCAPI::returnCRIT; 340 } 341 342 if (bNSClient) { 343 if (!message.empty()) 344 message += _T("&"); 345 message += strEx::itos(totalNumberOfFreeBytes.QuadPart); 346 message += _T("&"); 347 message += strEx::itos(totalNumberOfBytes.QuadPart); 348 } else { 349 checkHolders::PercentageValueType<checkHolders::disk_size_type, checkHolders::disk_size_type> value; 350 std::wstring tstr; 351 value.value = totalNumberOfBytes.QuadPart-totalNumberOfFreeBytes.QuadPart; 352 value.total = totalNumberOfBytes.QuadPart; 353 drive.setDefault(tmpObject); 354 drive.runCheck(value, returnCode, message, perf); 355 } 356 } 357 if (message.empty()) 358 message = _T("OK: All drives within bounds."); 359 else if (!bNSClient) 360 message = nscapi::plugin_helper::translateReturn(returnCode) + _T(": ") + message; 361 return returnCode; 362 } 363 364 std::wstring CheckDisk::get_filter(unsigned int drvType) { 365 if (drvType==DRIVE_FIXED) 366 return _T("FIXED"); 367 if (drvType==DRIVE_NO_ROOT_DIR) 368 return _T("NO_ROOT_DIR"); 369 if (drvType==DRIVE_CDROM) 370 return _T("CDROM"); 371 if (drvType==DRIVE_REMOTE) 372 return _T("REMOTE"); 373 if (drvType==DRIVE_REMOVABLE) 374 return _T("REMOVABLE"); 375 return _T("unknown: ") + strEx::itos(drvType); 376 } 377 378 class NSC_error : public error_reporter { 379 int error_count_; 380 int warning_count_; 381 std::wstring first_error_; 382 std::wstring last_error_; 383 public: 384 NSC_error() : error_count_(0), warning_count_(0) {} 385 void report_error(std::wstring error) { 386 if (error_count_==0) 387 first_error_ = error; 388 else 389 last_error_ = error; 390 error_count_++; 391 NSC_LOG_ERROR(error); 392 } 393 void report_warning(std::wstring error) { 394 NSC_LOG_MESSAGE(error); 395 } 396 std::wstring get_error() { 397 return strEx::itos(error_count_) + _T(" errors and ") + strEx::itos(warning_count_) + _T(" warnings where returned: ") + first_error_; 398 } 399 bool has_error() { 400 return error_count_ > 0; 401 } 402 }; 403 typedef std::pair<std::wstring,std::wstring> pattern_type; 404 pattern_type split_path_ex(std::wstring path) { 405 std::wstring baseDir; 406 if (file_helpers::checks::is_directory(path)) { 407 return pattern_type(path, _T("")); 408 } 409 std::wstring::size_type pos = path.find_last_of('\\'); 410 if (pos == std::wstring::npos) { 411 pattern_type(path, _T("*.*")); 412 } 413 NSC_DEBUG_MSG_STD(_T("Looking for: path: ") + path.substr(0, pos) + _T(", pattern: ") + path.substr(pos+1)); 414 return pattern_type(path.substr(0, pos), path.substr(pos+1)); 415 } 416 417 typedef std::pair<std::wstring,std::wstring> pattern_type; 418 pattern_type split_pattern(std::wstring path) { 419 std::wstring baseDir; 420 if (file_helpers::checks::exists(path)) { 421 return pattern_type(path, _T("")); 422 } 423 std::wstring::size_type pos = path.find_last_of('\\'); 424 if (pos == std::wstring::npos) { 425 pattern_type(path, _T("*.*")); 426 } 427 NSC_DEBUG_MSG_STD(_T("Looking for: pattern: ") + path.substr(0, pos) + _T(", pattern: ") + path.substr(pos+1)); 428 return pattern_type(path.substr(0, pos), path.substr(pos+1)); 429 } 430 431 432 NSCAPI::nagiosReturn CheckDisk::CheckFileSize(std::list<std::wstring> args, std::wstring &message, std::wstring &perf) { 433 NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; 434 bool bPerfData = true; 435 bool debug = false; 436 if (args.empty()) { 437 message = _T("Missing argument(s)."); 438 return NSCAPI::returnUNKNOWN; 439 } 440 PathContainer tmpObject; 441 std::list<PathContainer> paths; 442 443 MAP_OPTIONS_BEGIN(args) 444 MAP_OPTIONS_STR_AND(_T("File"), tmpObject.data, paths.push_back(tmpObject)) 445 MAP_OPTIONS_SHOWALL(tmpObject) 446 MAP_OPTIONS_STR(_T("MaxWarn"), tmpObject.warn.max) 447 MAP_OPTIONS_STR(_T("MinWarn"), tmpObject.warn.min) 448 MAP_OPTIONS_STR(_T("MaxCrit"), tmpObject.crit.max) 449 MAP_OPTIONS_STR(_T("MinCrit"), tmpObject.crit.min) 450 MAP_OPTIONS_BOOL_TRUE(_T("debug"), debug) 451 MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) 452 MAP_OPTIONS_SECONDARY_BEGIN(_T(":"), p2) 453 else if (p2.first == _T("File")) { 454 tmpObject.data = p__.second; 455 tmpObject.alias = p2.second; 456 paths.push_back(tmpObject); 457 } 458 MAP_OPTIONS_MISSING_EX(p2, message, _T("Unknown argument: ")) 459 MAP_OPTIONS_SECONDARY_END() 460 MAP_OPTIONS_MISSING(message, _T("Unknown argument: ")) 461 MAP_OPTIONS_END() 462 463 for (std::list<PathContainer>::const_iterator pit = paths.begin(); pit != paths.end(); ++pit) { 464 PathContainer path = (*pit); 465 std::wstring tstr; 466 std::wstring sName = path.getAlias(); 467 get_size sizeFinder; 468 NSC_error errors; 469 pattern_type splitpath = split_pattern(path.data); 470 recursive_scan<get_size>(splitpath.first, splitpath.second, -1, -1, sizeFinder, &errors, debug); 471 if (errors.has_error()) { 472 if (show_errors_) 473 message = errors.get_error(); 474 else 475 message = _T("Check contains error. Check log for details (or enable show_errors in nsc.ini)"); 476 return NSCAPI::returnUNKNOWN; 477 } 478 path.setDefault(tmpObject); 479 path.perfData = bPerfData; 480 481 checkHolders::disk_size_type size = sizeFinder.getSize(); 482 path.runCheck(size, returnCode, message, perf); 483 } 484 if (message.empty()) 485 message = _T("OK all file sizes are within bounds."); 486 else 487 message = nscapi::plugin_helper::translateReturn(returnCode) + _T(": ") + message; 488 return returnCode; 489 } 490 491 492 struct file_info { 493 494 std::wstring error; 495 //bool has_error; 496 497 static file_info get(__int64 now, std::wstring path, std::wstring file) { 498 return get_2(now, path, file); 499 } 500 static file_info get(__int64 now, file_finder_data data) { 501 return file_info(now, data.wfd, data.path, data.wfd.cFileName); 502 } 503 504 static file_info get_2(__int64 now, std::wstring path, std::wstring file) { 505 WIN32_FILE_ATTRIBUTE_DATA data; 506 if (!GetFileAttributesEx((path + _T("\\") + file).c_str(), GetFileExInfoStandard, reinterpret_cast<LPVOID>(&data))) { 507 file_info ret; 508 ret.error = _T("Could not open file (2) ") + path + _T("\\") + file + _T(": ") + error::lookup::last_error(); 509 return ret; 510 } 511 return file_info(now, data, path, file); 512 } 513 static file_info get_1(__int64 now, std::wstring path, std::wstring file) { 514 HANDLE hFile = CreateFile((path + _T("\\") + file).c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); 515 if (hFile == INVALID_HANDLE_VALUE) { 516 file_info ret; 517 ret.error = _T("Could not open file (1) ") + path + _T("\\") + file + _T(": ") + error::lookup::last_error(); 518 return ret; 519 } 520 BY_HANDLE_FILE_INFORMATION _info; 521 GetFileInformationByHandle(hFile, &_info); 522 CloseHandle(hFile); 523 return file_info(now, _info, path, file); 524 } 525 526 file_info() 527 : ullCreationTime(0) 528 , ullLastAccessTime(0) 529 , ullLastWriteTime(0) 530 , ullSize(0) 531 , ullNow(0) 532 , cached_version(false, _T("")) 533 , cached_count(false, 0) 534 {} 535 file_info(__int64 now, const WIN32_FILE_ATTRIBUTE_DATA info, std::wstring path_, std::wstring filename_) 536 : path(path_) 537 , filename(filename_) 538 , ullCreationTime(0) 539 , ullLastAccessTime(0) 540 , ullLastWriteTime(0) 541 , ullSize(0) 542 , ullNow(now) 543 , cached_version(false, _T("")) 544 , cached_count(false, 0) 545 { 546 ullSize = ((info.nFileSizeHigh * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.nFileSizeLow); 547 ullCreationTime = ((info.ftCreationTime.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.ftCreationTime.dwLowDateTime); 548 ullLastAccessTime = ((info.ftLastAccessTime.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.ftLastAccessTime.dwLowDateTime); 549 ullLastWriteTime = ((info.ftLastWriteTime.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.ftLastWriteTime.dwLowDateTime); 550 }; 551 file_info(__int64 now, const BY_HANDLE_FILE_INFORMATION info, std::wstring path_, std::wstring filename_) 552 : path(path_) 553 , filename(filename_) 554 , ullCreationTime(0) 555 , ullLastAccessTime(0) 556 , ullLastWriteTime(0) 557 , ullSize(0) 558 , ullNow(now) 559 , cached_version(false, _T("")) 560 , cached_count(false, 0) 561 { 562 ullSize = ((info.nFileSizeHigh * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.nFileSizeLow); 563 ullCreationTime = ((info.ftCreationTime.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.ftCreationTime.dwLowDateTime); 564 ullLastAccessTime = ((info.ftLastAccessTime.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.ftLastAccessTime.dwLowDateTime); 565 ullLastWriteTime = ((info.ftLastWriteTime.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.ftLastWriteTime.dwLowDateTime); 566 }; 567 file_info(__int64 now, const WIN32_FIND_DATA info, std::wstring path_, std::wstring filename_) 568 : path(path_) 569 , filename(filename_) 570 , ullCreationTime(0) 571 , ullLastAccessTime(0) 572 , ullLastWriteTime(0) 573 , ullSize(0) 574 , ullNow(now) 575 , cached_version(false, _T("")) 576 , cached_count(false, 0) 577 { 578 ullSize = ((info.nFileSizeHigh * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.nFileSizeLow); 579 ullCreationTime = ((info.ftCreationTime.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.ftCreationTime.dwLowDateTime); 580 ullLastAccessTime = ((info.ftLastAccessTime.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.ftLastAccessTime.dwLowDateTime); 581 ullLastWriteTime = ((info.ftLastWriteTime.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.ftLastWriteTime.dwLowDateTime); 582 }; 583 file_info(__int64 now, std::wstring path_, std::wstring filename_) 584 : path(path_) 585 , filename(filename_) 586 , ullCreationTime(0) 587 , ullLastAccessTime(0) 588 , ullLastWriteTime(0) 589 , ullSize(0) 590 , ullNow(now) 591 , cached_version(false, _T("")) 592 , cached_count(false, 0) 593 { 594 }; 595 596 unsigned long long ullSize; 597 __int64 ullCreationTime; 598 __int64 ullLastAccessTime; 599 __int64 ullLastWriteTime; 600 __int64 ullNow; 601 std::wstring filename; 602 std::wstring path; 603 std::pair<bool,std::wstring> cached_version; 604 std::pair<bool,unsigned long> cached_count; 605 606 static const __int64 MSECS_TO_100NS = 10000; 607 608 __int64 get_creation() { 609 return (ullNow-ullCreationTime)/MSECS_TO_100NS; 610 } 611 __int64 get_access() { 612 return (ullNow-ullLastAccessTime)/MSECS_TO_100NS; 613 } 614 __int64 get_write() { 615 return (ullNow-ullLastWriteTime)/MSECS_TO_100NS; 616 } 617 std::wstring render(std::wstring syntax) { 618 strEx::replace(syntax, _T("%path%"), path); 619 strEx::replace(syntax, _T("%filename%"), filename); 620 strEx::replace(syntax, _T("%creation%"), strEx::format_filetime(ullCreationTime, DATE_FORMAT)); 621 strEx::replace(syntax, _T("%access%"), strEx::format_filetime(ullLastAccessTime, DATE_FORMAT)); 622 strEx::replace(syntax, _T("%write%"), strEx::format_filetime(ullLastWriteTime, DATE_FORMAT)); 623 strEx::replace(syntax, _T("%creation-raw%"), strEx::itos(ullCreationTime)); 624 strEx::replace(syntax, _T("%access-raw%"), strEx::itos(ullLastAccessTime)); 625 strEx::replace(syntax, _T("%write-raw%"), strEx::itos(ullLastWriteTime)); 626 strEx::replace(syntax, _T("%now-raw%"), strEx::itos(ullNow)); 627 /* 628 strEx::replace(syntax, _T("%creation-d%"), strEx::format_filetime(ullCreationTime, DATE_FORMAT)); 629 strEx::replace(syntax, _T("%access-d%"), strEx::format_filetime(ullLastAccessTime, DATE_FORMAT)); 630 strEx::replace(syntax, _T("%write-d%"), strEx::format_filetime(ullLastWriteTime, DATE_FORMAT)); 631 */ 632 strEx::replace(syntax, _T("%size%"), strEx::itos_as_BKMG(ullSize)); 633 if (cached_version.first) 634 strEx::replace(syntax, _T("%version%"), cached_version.second); 635 if (cached_count.first) 636 strEx::replace(syntax, _T("%line-count%"), strEx::itos(cached_count.second)); 637 return syntax; 638 } 639 640 std::wstring get_version() { 641 if (cached_version.first) 642 return cached_version.second; 643 std::wstring fullpath = path+_T("\\")+filename; 644 645 DWORD dwDummy; 646 DWORD dwFVISize = GetFileVersionInfoSize(fullpath.c_str(),&dwDummy); 647 LPBYTE lpVersionInfo = new BYTE[dwFVISize+1]; 648 GetFileVersionInfo(fullpath.c_str(),0,dwFVISize,lpVersionInfo); 649 UINT uLen; 650 VS_FIXEDFILEINFO *lpFfi; 651 VerQueryValue( lpVersionInfo , _T("\\") , (LPVOID *)&lpFfi , &uLen ); 652 DWORD dwFileVersionMS = lpFfi->dwFileVersionMS; 653 DWORD dwFileVersionLS = lpFfi->dwFileVersionLS; 654 delete [] lpVersionInfo; 655 DWORD dwLeftMost = HIWORD(dwFileVersionMS); 656 DWORD dwSecondLeft = LOWORD(dwFileVersionMS); 657 DWORD dwSecondRight = HIWORD(dwFileVersionLS); 658 DWORD dwRightMost = LOWORD(dwFileVersionLS); 659 cached_version.second = strEx::itos(dwLeftMost) + _T(".") + 660 strEx::itos(dwSecondLeft) + _T(".") + 661 strEx::itos(dwSecondRight) + _T(".") + 662 strEx::itos(dwRightMost); 663 cached_version.first = true; 664 return cached_version.second; 665 } 666 667 unsigned long get_line_count() { 668 if (cached_count.first) 669 return cached_count.second; 670 671 unsigned long count = 0; 672 std::wstring fullpath = path+_T("\\")+filename; 673 FILE * pFile = fopen(strEx::wstring_to_string(fullpath).c_str(),"r");; 674 if (pFile==NULL) 675 return 0; 676 char c; 677 do { 678 c = fgetc (pFile); 679 if (c == '\r') { 680 c = fgetc (pFile); 681 count++; 682 } else if (c == '\n') { 683 c = fgetc (pFile); 684 count++; 685 } 686 } while (c != EOF); 687 fclose (pFile); 688 cached_count.second = count; 689 cached_count.first = true; 690 return cached_count.second; 691 } 692 }; 693 694 struct file_container : public file_info { 695 std::wstring error_; 696 697 698 static file_container get(std::wstring file) { 699 FILETIME now; 700 GetSystemTimeAsFileTime(&now); 701 unsigned __int64 nowi64 = ((now.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)now.dwLowDateTime); 702 return get(file, nowi64); 703 } 704 705 static file_container get(std::wstring file, unsigned long long now) { 706 707 BY_HANDLE_FILE_INFORMATION _info; 708 709 HANDLE hFile = CreateFile(file.c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, 710 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); 711 if (hFile == INVALID_HANDLE_VALUE) { 712 return file_container(now, file, _T("Could not open file: ") + file); 713 } 714 GetFileInformationByHandle(hFile, &_info); 715 CloseHandle(hFile); 716 file_container info(now, _info, file); 717 //info.ullNow = now; 718 return info; 719 } 720 721 722 file_container(__int64 now, const BY_HANDLE_FILE_INFORMATION info, std::wstring file) : file_info(now, info, file_helpers::meta::get_path(file), file_helpers::meta::get_filename(file)) {} 723 file_container(__int64 now, std::wstring file, std::wstring error) : error_(error), file_info(now, file_helpers::meta::get_path(file), file_helpers::meta::get_filename(file)) {} 724 725 bool has_errors() { 726 return !error_.empty(); 727 } 728 std::wstring get_error() { 729 return error_; 730 } 731 732 }; 733 struct file_filter { 734 filters::filter_all_numeric<unsigned long long, checkHolders::disk_size_handler<checkHolders::disk_size_type> > size; 735 filters::filter_all_times creation; 736 filters::filter_all_times accessed; 737 filters::filter_all_times written; 738 filters::filter_all_strings version; 739 filters::filter_all_num_ul line_count; 740 741 inline bool hasFilter() { 742 return size.hasFilter() || creation.hasFilter() || 743 accessed.hasFilter() || written.hasFilter(); 744 } 745 bool matchFilter(file_info &value) const { 746 if ((size.hasFilter())&&(size.matchFilter(value.ullSize))) 747 return true; 748 else if (creation.hasFilter()&&creation.matchFilter(value.get_creation())) 749 return true; 750 else if (accessed.hasFilter()&&accessed.matchFilter(value.get_access())) 751 return true; 752 else if (written.hasFilter()&&written.matchFilter(value.get_write())) 753 return true; 754 else if ((version.hasFilter())&&(version.matchFilter(value.get_version()))) 755 return true; 756 else if ((line_count.hasFilter())&&(line_count.matchFilter(value.get_line_count()))) 757 return true; 91 758 return false; 92 759 } 93 std::wstring get_name() { 94 return _T("hello"); 95 } 760 761 std::wstring getValue() const { 762 if (size.hasFilter()) 763 return _T("size: ") + size.getValue(); 764 if (creation.hasFilter()) 765 return _T("creation: ") + creation.getValue(); 766 if (accessed.hasFilter()) 767 return _T("accessed: ") + accessed.getValue(); 768 if (written.hasFilter()) 769 return _T("written: ") + written.getValue(); 770 if (version.hasFilter()) 771 return _T("written: ") + version.getValue(); 772 if (line_count.hasFilter()) 773 return _T("written: ") + line_count.getValue(); 774 return _T("UNknown..."); 775 } 776 96 777 }; 97 778 98 struct file_filter { 99 virtual bool matches(file_item &) = 0; 779 780 struct find_first_file_info : public baseFinderFunction 781 { 782 file_info info; 783 __int64 now_; 784 // std::wstring message; 785 find_first_file_info() : now_(0) { 786 FILETIME now; 787 GetSystemTimeAsFileTime(&now); 788 now_ = ((now.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)now.dwLowDateTime); 789 } 790 result_type operator()(argument_type ffd) { 791 if (is_directory(ffd.wfd.dwFileAttributes)) 792 return true; 793 794 file_info info = file_info::get(now_, ffd); 795 if (!info.error.empty()) { 796 setError(ffd.errors, info.error); 797 return false; 798 } 799 return false; 800 /* 801 BY_HANDLE_FILE_INFORMATION _info; 802 803 HANDLE hFile = CreateFile((ffd.path + _T("\\") + ffd.wfd.cFileName).c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, 804 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); 805 if (hFile == INVALID_HANDLE_VALUE) { 806 setError(ffd.errors, _T("Could not open file: ") + ffd.path + _T("\\") + ffd.wfd.cFileName + _T(": ") + error::lookup::last_error()); 807 return false; 808 } 809 GetFileInformationByHandle(hFile, &_info); 810 CloseHandle(hFile); 811 info = file_info(_info, ffd.path, ffd.wfd.cFileName); 812 return false; 813 */ 814 } 815 inline void setError(error_reporter *errors, std::wstring msg) { 816 if (errors != NULL) 817 errors->report_error(msg); 818 } 100 819 }; 101 820 102 103 bool find_files(const boost::filesystem::wpath & path, const find_options options, file_filter filter, std::list<file_item> & files) 821 struct file_filter_function : public baseFinderFunction 104 822 { 105 if ( !exists( path ) ) return false; 106 directory_iterator end_itr; // default construction yields past-the-end 107 for ( directory_iterator itr(path); itr != end_itr; ++itr ) { 108 if ( is_directory(itr->status()) ) { 109 if (find_files(itr->path(), options.go_down(), filter, files) && options.has_all(files)) 823 std::list<file_filter> filter_chain; 824 bool bFilterAll; 825 bool bFilterIn; 826 std::wstring message; 827 std::wstring syntax; 828 std::wstring alias; 829 unsigned long long now; 830 unsigned int hit_count; 831 __int64 now_; 832 833 file_filter_function() : now_(0), hit_count(0), bFilterIn(true), bFilterAll(true) { 834 FILETIME now; 835 GetSystemTimeAsFileTime(&now); 836 now_ = ((now.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)now.dwLowDateTime); 837 } 838 result_type operator()(argument_type ffd) { 839 if (is_directory(ffd.wfd.dwFileAttributes)) 840 return true; 841 842 file_info info = file_info::get(now, ffd); 843 if (!info.error.empty()) { 844 setError(ffd.errors, info.error); 845 return true; 846 } 847 /* 848 BY_HANDLE_FILE_INFORMATION _info; 849 850 HANDLE hFile = CreateFile((ffd.path + _T("\\") + ffd.wfd.cFileName).c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, 851 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); 852 if (hFile == INVALID_HANDLE_VALUE) { 853 setError(ffd.errors, _T("Could not open file: ") + ffd.path + _T("\\") + ffd.wfd.cFileName + _T(": ") + error::lookup::last_error()); 854 return true; 855 } 856 GetFileInformationByHandle(hFile, &_info); 857 CloseHandle(hFile); 858 file_info info(_info, ffd.path, ffd.wfd.cFileName); 859 info.ullNow = now; 860 */ 861 862 for (std::list<file_filter>::const_iterator cit3 = filter_chain.begin(); cit3 != filter_chain.end(); ++cit3 ) { 863 bool bMatch = bFilterAll; 864 bool bTmpMatched = (*cit3).matchFilter(info); 865 if (bFilterAll) { 866 if (!bTmpMatched) { 867 bMatch = false; 868 break; 869 } 870 } else { 871 if (bTmpMatched) { 872 bMatch = true; 873 break; 874 } 875 } 876 if ((bFilterIn&&bMatch)||(!bFilterIn&&!bMatch)) { 877 strEx::append_list(message, info.render(syntax)); 878 if (alias.length() < 16) 879 strEx::append_list(alias, info.filename); 880 else 881 strEx::append_list(alias, std::wstring(_T("..."))); 882 hit_count++; 883 } 884 } 885 return true; 886 } 887 inline void setError(error_reporter *errors, std::wstring msg) { 888 if (errors != NULL) 889 errors->report_error(msg); 890 } 891 }; 892 893 894 895 struct file_filter_function_ex : public baseFinderFunction 896 { 897 static const int filter_plus = 1; 898 static const int filter_minus = 2; 899 static const int filter_normal = 3; 900 901 typedef std::pair<int,file_filter> filteritem_type; 902 typedef std::list<filteritem_type > filterlist_type; 903 filterlist_type filter_chain; 904 bool bFilterAll; 905 bool bFilterIn; 906 bool debug_; 907 std::wstring message; 908 std::wstring syntax; 909 //std::wstring alias; 910 unsigned long long now_; 911 unsigned int hit_count; 912 unsigned int file_count; 913 914 file_filter_function_ex() : hit_count(0), file_count(0), debug_(false), bFilterIn(true), bFilterAll(true), now_(0) { 915 FILETIME now; 916 GetSystemTimeAsFileTime(&now); 917 now_ = ((now.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)now.dwLowDateTime); 918 } 919 result_type operator()(argument_type ffd) { 920 if (is_directory(ffd.wfd.dwFileAttributes)) 921 return true; 922 923 file_info info = file_info::get(now_, ffd); 924 if (!info.error.empty()) { 925 setError(ffd.errors, info.error); 926 return true; 927 } 928 /* 929 BY_HANDLE_FILE_INFORMATION _info; 930 931 HANDLE hFile = CreateFile((ffd.path + _T("\\") + ffd.wfd.cFileName).c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, 932 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); 933 if (hFile == INVALID_HANDLE_VALUE) { 934 setError(ffd.errors, _T("Could not open file: ") + ffd.path + _T("\\") + ffd.wfd.cFileName + _T(": ") + error::lookup::last_error()); 110 935 return true; 111 } else { 112 file_item file(itr->leaf()); 113 if (filter.matches(file)) { 114 files.push_back(file); 115 if (options.has_all(files)) 936 } 937 GetFileInformationByHandle(hFile, &_info); 938 CloseHandle(hFile); 939 file_info info(_info, ffd.path, ffd.wfd.cFileName); 940 info.ullNow = now; 941 */ 942 943 bool bMatch = !bFilterIn; 944 for (filterlist_type::const_iterator cit3 = filter_chain.begin(); cit3 != filter_chain.end(); ++cit3 ) { 945 bool bTmpMatched = (*cit3).second.matchFilter(info); 946 int mode = (*cit3).first; 947 948 if ((mode == filter_minus)&&(bTmpMatched)) { 949 // a -<filter> hit so thrash item and bail out! 950 if (debug_) 951 NSC_DEBUG_MSG_STD(_T("Matched: - ") + (*cit3).second.getValue() + _T(" for: ") + info.render(syntax)); 952 bMatch = false; 953 break; 954 } else if ((mode == filter_plus)&&(!bTmpMatched)) { 955 // a +<filter> missed hit so thrash item and bail out! 956 if (debug_) 957 NSC_DEBUG_MSG_STD(_T("Matched (missed): + ") + (*cit3).second.getValue() + _T(" for: ") + info.render(syntax)); 958 bMatch = false; 959 break; 960 } else if (bTmpMatched) { 961 if (debug_) 962 NSC_DEBUG_MSG_STD(_T("Matched: . (contiunue): ") + (*cit3).second.getValue() + _T(" for: ") + info.render(syntax)); 963 bMatch = true; 964 } 965 } 966 967 //NSC_DEBUG_MSG_STD(_T("result: ") + strEx::itos(bFilterIn) + _T(" -- ") + strEx::itos(bMatch)); 968 if ((bFilterIn&&bMatch)||(!bFilterIn&&!bMatch)) { 969 strEx::append_list(message, info.render(syntax)); 970 /* 971 if (alias.length() < 16) 972 strEx::append_list(alias, info.filename); 973 else 974 strEx::append_list(alias, std::wstring(_T("..."))); 975 */ 976 hit_count++; 977 } 978 file_count++; 116 979 return true; 117 980 } 118 } 119 } 120 return false; 121 } 122 123 void CheckDisk::check_file(PluginCommand::Request *request, PluginCommand::Response *response) { 124 const boost::filesystem::wpath & path; 125 const find_options options(...); 126 file_filter filter(...); 127 std::list<file_item> files; 128 129 130 131 find_files(path, options, filter, files); 132 } 133 134 135 void CheckDisk::handleCommand(std::wstring command, PluginCommand::Request *request, PluginCommand::Response *response) { 136 if (command == _T("check_file")) 137 check_file(request, response); 138 139 } 981 inline void setError(error_reporter *errors, std::wstring msg) { 982 if (errors != NULL) 983 errors->report_error(msg); 984 } 985 986 std::wstring render(std::wstring syntax) { 987 strEx::replace(syntax, _T("%list%"), message); 988 strEx::replace(syntax, _T("%matches%"), strEx::itos(hit_count)); 989 strEx::replace(syntax, _T("%files%"), strEx::itos(file_count)); 990 return syntax; 991 } 992 993 bool has_filter() { 994 return !filter_chain.empty(); 995 } 996 997 }; 998 999 1000 NSCAPI::nagiosReturn CheckDisk::getFileAge(std::list<std::wstring> args, std::wstring &message, std::wstring &perf) { 1001 NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; 1002 typedef checkHolders::CheckContainer<checkHolders::MaxMinBoundsUInteger> CheckFileContainer; 1003 if (args.empty()) { 1004 message = _T("Missing argument(s)."); 1005 return NSCAPI::returnUNKNOWN; 1006 } 1007 std::wstring format = _T("%Y years %m mon %d days %H hours %M min %S sec"); 1008 std::wstring path; 1009 bool debug = false; 1010 MAP_OPTIONS_BEGIN(args) 1011 MAP_OPTIONS_STR(_T("path"), path) 1012 MAP_OPTIONS_STR(_T("date"), format) 1013 MAP_OPTIONS_BOOL_TRUE(_T("debug"), debug) 1014 MAP_OPTIONS_FALLBACK(format) 1015 MAP_OPTIONS_END() 1016 1017 if (path.empty()) { 1018 message = _T("ERROR: no file specified."); 1019 return NSCAPI::returnUNKNOWN; 1020 } 1021 1022 file_container info = file_container::get(path); 1023 1024 if (!info.error_.empty()) { 1025 if (show_errors_) 1026 message = _T("0&") + info.error_; 1027 else 1028 message = _T("0&Check contains error. Check log for details (or enable show_errors in nsc.ini)"); 1029 return NSCAPI::returnUNKNOWN; 1030 } 1031 static const __int64 SECS_TO_100NS = 10000000; 1032 time_t value = (info.ullNow-info.ullLastWriteTime)/SECS_TO_100NS; 1033 message = strEx::itos(value/60) + _T("&") + strEx::format_time_delta(value, format); 1034 return NSCAPI::returnOK; 1035 } 1036 1037 1038 NSCAPI::nagiosReturn CheckDisk::CheckFile(std::list<std::wstring> arguments, std::wstring &message, std::wstring &perf) { 1039 typedef checkHolders::CheckContainer<checkHolders::MaxMinBoundsUInteger> CheckFileContainer; 1040 NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; 1041 if (arguments.empty()) { 1042 message = _T("Missing argument(s)."); 1043 return NSCAPI::returnUNKNOWN; 1044 } 1045 file_filter_function finder; 1046 PathContainer tmpObject; 1047 std::list<std::wstring> paths; 1048 unsigned int truncate = 0; 1049 CheckFileContainer query; 1050 std::wstring syntax = _T("%filename%"); 1051 std::wstring alias; 1052 bool bPerfData = true; 1053 unsigned int max_dir_depth = -1; 1054 bool debug = false; 1055 1056 try { 1057 MAP_OPTIONS_BEGIN(arguments) 1058 MAP_OPTIONS_NUMERIC_ALL(query, _T("")) 1059 MAP_OPTIONS_STR2INT(_T("truncate"), truncate) 1060 MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) 1061 MAP_OPTIONS_STR(_T("syntax"), syntax) 1062 MAP_OPTIONS_PUSH(_T("path"), paths) 1063 MAP_OPTIONS_STR(_T("alias"), alias) 1064 MAP_OPTIONS_STR2INT(_T("max-dir-depth"), max_dir_depth) 1065 MAP_OPTIONS_PUSH(_T("file"), paths) 1066 MAP_OPTIONS_BOOL_TRUE(_T("debug"), debug) 1067 MAP_OPTIONS_BOOL_EX(_T("filter"), finder.bFilterIn, _T("in"), _T("out")) 1068 MAP_OPTIONS_BOOL_EX(_T("filter"), finder.bFilterAll, _T("all"), _T("any")) 1069 MAP_OPTIONS_PUSH_WTYPE(file_filter, _T("filter-size"), size, finder.filter_chain) 1070 MAP_OPTIONS_PUSH_WTYPE(file_filter, _T("filter-creation"), creation, finder.filter_chain) 1071 MAP_OPTIONS_PUSH_WTYPE(file_filter, _T("filter-written"), written, finder.filter_chain) 1072 MAP_OPTIONS_PUSH_WTYPE(file_filter, _T("filter-accessed"), accessed, finder.filter_chain) 1073 MAP_OPTIONS_MISSING(message, _T("Unknown argument: ")) 1074 MAP_OPTIONS_END() 1075 } catch (filters::parse_exception e) { 1076 message = e.getMessage(); 1077 return NSCAPI::returnUNKNOWN; 1078 } catch (filters::filter_exception e) { 1079 message = e.getMessage(); 1080 return NSCAPI::returnUNKNOWN; 1081 } 1082 finder.syntax = syntax; 1083 NSC_error errors; 1084 for (std::list<std::wstring>::const_iterator pit = paths.begin(); pit != paths.end(); ++pit) { 1085 pattern_type path = split_pattern(*pit); 1086 recursive_scan<file_filter_function>(path.first, path.second, 0, max_dir_depth, finder, &errors, debug); 1087 if (errors.has_error()) { 1088 if (show_errors_) 1089 message = errors.get_error(); 1090 else 1091 message = _T("Check contains error. Check log for details (or enable show_errors in nsc.ini)"); 1092 return NSCAPI::returnUNKNOWN; 1093 } 1094 } 1095 message = finder.message; 1096 if (!alias.empty()) 1097 query.alias = alias; 1098 else 1099 query.alias = finder.alias; 1100 if (query.alias.empty()) 1101 query.alias = _T("no files found"); 1102 query.runCheck(finder.hit_count, returnCode, message, perf); 1103 if ((truncate > 0) && (message.length() > (truncate-4))) 1104 message = message.substr(0, truncate-4) + _T("..."); 1105 if (message.empty()) 1106 message = _T("CheckFile ok"); 1107 return returnCode; 1108 } 1109 1110 #define MAP_FILTER(value, obj) \ 1111 else if (p__.first == _T("filter+"##value)) { file_filter filter; filter.obj = p__.second; \ 1112 finder.filter_chain.push_back(filteritem_type(file_filter_function_ex::filter_plus, filter)); } \ 1113 else if (p__.first == _T("filter-"##value)) { file_filter filter; filter.obj = p__.second; \ 1114 finder.filter_chain.push_back(filteritem_type(file_filter_function_ex::filter_minus, filter)); } \ 1115 else if (p__.first == _T("filter."##value)) { file_filter filter; filter.obj = p__.second; \ 1116 finder.filter_chain.push_back(filteritem_type(file_filter_function_ex::filter_normal, filter)); } 1117 1118 NSCAPI::nagiosReturn CheckDisk::CheckFile2(std::list<std::wstring> args, std::wstring &message, std::wstring &perf) { 1119 NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; 1120 typedef checkHolders::CheckContainer<checkHolders::MaxMinBoundsUInteger> CheckFileContainer; 1121 typedef std::pair<int,file_filter> filteritem_type; 1122 typedef std::list<filteritem_type > filterlist_type; 1123 if (args.empty()) { 1124 message = _T("Missing argument(s)."); 1125 return NSCAPI::returnUNKNOWN; 1126 } 1127 file_filter_function_ex finder; 1128 PathContainer tmpObject; 1129 std::list<std::wstring> paths; 1130 unsigned int truncate = 0; 1131 CheckFileContainer query; 1132 std::wstring syntax = _T("%filename%"); 1133 std::wstring masterSyntax = _T("%list%"); 1134 std::wstring alias; 1135 std::wstring pattern = _T("*.*"); 1136 bool bPerfData = true; 1137 int max_dir_depth = -1; 1138 bool debug = false; 1139 bool ignoreError = false; 1140 1141 try { 1142 MAP_OPTIONS_BEGIN(args) 1143 MAP_OPTIONS_NUMERIC_ALL(query, _T("")) 1144 MAP_OPTIONS_STR2INT(_T("truncate"), truncate) 1145 MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) 1146 MAP_OPTIONS_STR(_T("syntax"), syntax) 1147 MAP_OPTIONS_STR(_T("master-syntax"), masterSyntax) 1148 MAP_OPTIONS_PUSH(_T("path"), paths) 1149 MAP_OPTIONS_STR(_T("pattern"), pattern) 1150 MAP_OPTIONS_STR(_T("alias"), alias) 1151 MAP_OPTIONS_PUSH(_T("file"), paths) 1152 MAP_OPTIONS_BOOL_TRUE(_T("debug"), debug) 1153 MAP_OPTIONS_BOOL_TRUE(_T("ignore-errors"), ignoreError) 1154 MAP_OPTIONS_STR2INT(_T("max-dir-depth"), max_dir_depth) 1155 MAP_OPTIONS_BOOL_EX(_T("filter"), finder.bFilterIn, _T("in"), _T("out")) 1156 MAP_OPTIONS_BOOL_EX(_T("filter"), finder.bFilterAll, _T("all"), _T("any")) 1157 /* 1158 MAP_OPTIONS_PUSH_WTYPE(file_filter, _T("filter-size"), fileSize, finder.filter_chain) 1159 MAP_OPTIONS_PUSH_WTYPE(file_filter, _T("filter-creation"), fileCreation, finder.filter_chain) 1160 MAP_OPTIONS_PUSH_WTYPE(file_filter, _T("filter-written"), fileWritten, finder.filter_chain) 1161 MAP_OPTIONS_PUSH_WTYPE(file_filter, _T("filter-accessed"), fileAccessed, finder.filter_chain) 1162 */ 1163 1164 MAP_FILTER(_T("size"), size) 1165 MAP_FILTER(_T("creation"), creation) 1166 MAP_FILTER(_T("written"), written) 1167 MAP_FILTER(_T("accessed"), accessed) 1168 MAP_FILTER(_T("version"), version) 1169 MAP_FILTER(_T("line-count"), line_count) 140 1170 /* 141 NSCAPI::nagiosReturn CheckDisk::handleCommand(const std::wstring command, std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) { 142 if (command == _T("check_drive")) { 143 //return CheckFileSize(argLen, char_args, msg, perf); 1171 MAP_FILTER(_T("filter.size"), size, filter_normal) 1172 MAP_FILTER(_T("filter.creation"), creation, filter_normal) 1173 MAP_FILTER(_T("filter.written"), written, filter_normal) 1174 MAP_FILTER(_T("filter.accessed"), accessed, filter_normal) 1175 MAP_FILTER(_T("filter.version"), version, filter_normal) 1176 1177 MAP_FILTER(_T("filter-size"), size, filter_minus) 1178 MAP_FILTER(_T("filter-creation"), creation, filter_minus) 1179 MAP_FILTER(_T("filter-written"), written, filter_minus) 1180 MAP_FILTER(_T("filter-accessed"), accessed, filter_minus) 1181 MAP_FILTER(_T("filter-version"), version, filter_minus) 1182 */ 1183 MAP_OPTIONS_MISSING(message, _T("Unknown argument: ")) 1184 MAP_OPTIONS_END() 1185 } catch (filters::parse_exception e) { 1186 message = e.getMessage(); 1187 return NSCAPI::returnUNKNOWN; 1188 } catch (filters::filter_exception e) { 1189 message = e.getMessage(); 1190 return NSCAPI::returnUNKNOWN; 1191 } 1192 if (paths.empty()) { 1193 message = _T("Missing path argument"); 1194 return NSCAPI::returnUNKNOWN; 1195 } 1196 if (!finder.has_filter()) { 1197 message = _T("Missing filter argument"); 1198 return NSCAPI::returnUNKNOWN; 1199 } 1200 finder.debug_ = debug; 1201 if (debug) 1202 NSC_DEBUG_MSG_STD(_T("NOW: ") + strEx::format_filetime(finder.now_)); 1203 finder.syntax = syntax; 1204 NSC_error errors; 1205 for (std::list<std::wstring>::const_iterator pit = paths.begin(); pit != paths.end(); ++pit) { 1206 recursive_scan<file_filter_function_ex>(*pit, pattern, 0, max_dir_depth, finder, &errors, debug); 1207 if (!ignoreError && errors.has_error()) { 1208 if (show_errors_) 1209 message = errors.get_error(); 1210 else 1211 message = _T("Check contains error. Check log for details (or enable show_errors in nsc.ini)"); 1212 return NSCAPI::returnUNKNOWN; 1213 } 1214 } 1215 message = finder.render(masterSyntax); 1216 if (!alias.empty()) 1217 query.alias = alias; 1218 else 1219 query.alias = _T("found files"); 1220 query.runCheck(finder.hit_count, returnCode, message, perf); 1221 if ((truncate > 0) && (message.length() > (truncate-4))) { 1222 message = message.substr(0, truncate-4) + _T("..."); 1223 //perf = _T(""); 1224 } 1225 if (message.empty()) 1226 message = _T("CheckFile ok"); 1227 return returnCode; 1228 } 1229 1230 1231 typedef checkHolders::ExactBounds<checkHolders::NumericBounds<checkHolders::disk_size_type, checkHolders::disk_size_handler<checkHolders::disk_size_type> > > ExactBoundsDiscSize; 1232 1233 1234 typedef checkHolders::CheckContainer<checkHolders::ExactBoundsULong> ExactULongContainer; 1235 typedef checkHolders::CheckContainer<ExactBoundsDiscSize> DiscSizeContainer; 1236 typedef checkHolders::CheckContainer<checkHolders::ExactBoundsTime> DateTimeContainer; 1237 1238 struct check_file_size : public checkHolders::check_proxy_container<file_container, DiscSizeContainer> { 1239 check_file_size() { set_alias(_T("size")); } 1240 checkHolders::disk_size_type get_value(file_container &value) { 1241 return value.ullSize; 1242 } 1243 }; 1244 struct check_file_line_count : public checkHolders::check_proxy_container<file_container, ExactULongContainer> { 1245 check_file_line_count() { set_alias(_T("line-count")); } 1246 unsigned long get_value(file_container &value) { 1247 return value.get_line_count(); 1248 } 1249 }; 1250 struct check_file_dates : public checkHolders::check_proxy_container<file_container, DateTimeContainer> { 1251 enum type_type { 1252 date_access, date_creation, date_written 1253 } ; 1254 type_type type_; 1255 check_file_dates(type_type type) : type_(type) 1256 { 1257 if (type_ == date_creation) 1258 set_alias(_T("creation")); 1259 else if (type_ == date_access) 1260 set_alias(_T("access")); 1261 else if (type_ == date_written) 1262 set_alias(_T("written")); 1263 else 1264 set_alias(_T("unknown date type")); 1265 } 1266 unsigned long long get_value(file_container &value) { 1267 if (type_ == date_creation) 1268 return value.ullCreationTime; 1269 if (type_ == date_access) 1270 return value.ullLastAccessTime; 1271 if (type_ == date_written) 1272 return value.ullLastWriteTime; 1273 return -1; 1274 } 1275 }; 1276 1277 typedef checkHolders::check_multi_container<file_container> check_file_multi; 1278 struct check_file_factories { 1279 static checkHolders::check_proxy_interface<file_container>* size() { 1280 return new check_file_size(); 1281 } 1282 static checkHolders::check_proxy_interface<file_container>* line_count() { 1283 return new check_file_line_count(); 1284 } 1285 static checkHolders::check_proxy_interface<file_container>* access() { 1286 return new check_file_dates(check_file_dates::date_access); 1287 } 1288 static checkHolders::check_proxy_interface<file_container>* creation() { 1289 return new check_file_dates(check_file_dates::date_creation); 1290 } 1291 static checkHolders::check_proxy_interface<file_container>* written() { 1292 return new check_file_dates(check_file_dates::date_written); 1293 } 1294 }; 1295 1296 #define MAP_FACTORY_PB(value, obj) \ 1297 else if ((p__.first == _T("check")) && (p__.second == ##value)) { checker.add_check(check_file_factories::obj()); } 1298 1299 1300 NSCAPI::nagiosReturn CheckDisk::CheckSingleFile(std::list<std::wstring> args, std::wstring &message, std::wstring &perf) { 1301 NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; 1302 check_file_multi checker; 1303 typedef std::pair<int,file_filter> filteritem_type; 1304 typedef std::list<filteritem_type > filterlist_type; 1305 if (args.empty()) { 1306 message = _T("Missing argument(s)."); 1307 return NSCAPI::returnUNKNOWN; 1308 } 1309 std::list<std::wstring> files; 1310 unsigned int truncate = 0; 1311 std::wstring syntax = _T("%filename%"); 1312 std::wstring alias; 1313 bool bPerfData = true; 1314 1315 try { 1316 MAP_OPTIONS_BEGIN(args) 1317 //MAP_OPTIONS_NUMERIC_ALL(query, _T("")) 1318 MAP_OPTIONS_STR2INT(_T("truncate"), truncate) 1319 MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) 1320 MAP_OPTIONS_STR(_T("syntax"), syntax) 1321 MAP_OPTIONS_STR(_T("alias"), alias) 1322 MAP_OPTIONS_PUSH(_T("file"), files) 1323 MAP_OPTIONS_EXACT_NUMERIC_ALL_MULTI(checker, _T("")) 1324 MAP_FACTORY_PB(_T("size"), size) 1325 MAP_FACTORY_PB(_T("line-count"), line_count) 1326 MAP_FACTORY_PB(_T("creation"), creation) 1327 MAP_FACTORY_PB(_T("access"), access) 1328 MAP_FACTORY_PB(_T("written"), written) 1329 /* 1330 MAP_FILTER(_T("creation"), creation) 1331 MAP_FILTER(_T("written"), written) 1332 MAP_FILTER(_T("accessed"), accessed) 1333 MAP_FILTER(_T("version"), version) 1334 MAP_FILTER(_T("line-count"), line_count) 1335 */ 1336 MAP_OPTIONS_MISSING(message, _T("Unknown argument: ")) 1337 MAP_OPTIONS_END() 1338 } catch (filters::parse_exception e) { 1339 message = e.getMessage(); 1340 return NSCAPI::returnUNKNOWN; 1341 } catch (filters::filter_exception e) { 1342 message = e.getMessage(); 1343 return NSCAPI::returnUNKNOWN; 1344 } 1345 FILETIME now; 1346 GetSystemTimeAsFileTime(&now); 1347 unsigned __int64 nowi64 = ((now.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)now.dwLowDateTime); 1348 //finder.syntax = syntax; 1349 for (std::list<std::wstring>::const_iterator pit = files.begin(); pit != files.end(); ++pit) { 1350 file_container info = file_container::get(*pit, nowi64); 1351 checker.alias = info.render(syntax); 1352 checker.runCheck(info, returnCode, message, perf); 1353 } 1354 if ((truncate > 0) && (message.length() > (truncate-4))) { 1355 message = message.substr(0, truncate-4) + _T("..."); 1356 perf = _T(""); 1357 } 1358 if (message.empty()) 1359 message = _T("CheckSingleFile ok"); 1360 return returnCode; 1361 } 1362 NSCAPI::nagiosReturn CheckDisk::handleCommand(const strEx::wci_string command, std::list<std::wstring> arguments, std::wstring &message, std::wstring &perf) { 1363 if (command == _T("CheckFileSize")) { 1364 return CheckFileSize(arguments, message, perf); 1365 } else if (command == _T("CheckDriveSize")) { 1366 return CheckDriveSize(arguments, message, perf); 1367 } else if (command == _T("CheckFile")) { 1368 return CheckFile(arguments, message, perf); 1369 } else if (command == _T("CheckFile2")) { 1370 return CheckFile2(arguments, message, perf); 1371 } else if (command == _T("CheckSingleFile")) { 1372 return CheckSingleFile(arguments, message, perf); 1373 } else if (command == _T("getFileAge")) { 1374 return getFileAge(arguments, message, perf); 144 1375 } 145 1376 return NSCAPI::returnIgnored; 146 1377 } 147 */ 1378 148 1379 149 1380 NSC_WRAP_DLL(); -
modules/CheckDisk/CheckDisk.def
re26cfe0 rf7663c9 4 4 NSModuleHelperInit 5 5 NSLoadModule 6 NSLoadModuleEx 6 7 NSGetModuleName 7 8 NSGetModuleVersion … … 12 13 NSUnloadModule 13 14 NSGetModuleDescription 15 NSDeleteBuffer 14 16 -
modules/CheckDisk/CheckDisk.h
rfe9cc46 rf7663c9 25 25 #include <checkHelpers.hpp> 26 26 27 class CheckDisk : public nscapi::impl::CommandImpl{27 class CheckDisk : public nscapi::impl::SimpleCommand, nscapi::impl::simple_plugin { 28 28 private: 29 29 bool show_errors_; … … 33 33 virtual ~CheckDisk(); 34 34 // Module calls 35 bool loadModule(NSCAPI::moduleLoadMode mode); 35 bool loadModule(); 36 bool loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode); 36 37 bool unloadModule(); 37 38 … … 43 44 } 44 45 nscapi::plugin_wrapper::module_version getModuleVersion() { 45 nscapi::plugin_wrapper::module_version version = {0, 3, 0};46 nscapi::plugin_wrapper::module_version version = {0, 0, 1 }; 46 47 return version; 47 48 } … … 50 51 bool hasMessageHandler(); 51 52 std::wstring get_filter(unsigned int drvType); 52 void handleCommand(PluginCommand::Request *request, PluginCommand::Response *response);53 NSCAPI::nagiosReturn handleCommand(const strEx::wci_string command, std::list<std::wstring> arguments, std::wstring &message, std::wstring &perf); 53 54 54 55 // Check commands 55 // NSCAPI::nagiosReturn CheckFileSize(const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf);56 // NSCAPI::nagiosReturn CheckDriveSize(const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf);57 // NSCAPI::nagiosReturn CheckFile(const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf);58 // NSCAPI::nagiosReturn CheckFile2(const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf);59 // NSCAPI::nagiosReturn getFileAge(const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf);60 // NSCAPI::nagiosReturn CheckSingleFile(const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf);56 NSCAPI::nagiosReturn CheckFileSize(std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf); 57 NSCAPI::nagiosReturn CheckDriveSize(std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf); 58 NSCAPI::nagiosReturn CheckFile2(std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf); 59 NSCAPI::nagiosReturn getFileAge(std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf); 60 NSCAPI::nagiosReturn CheckSingleFile(std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf); 61 NSCAPI::nagiosReturn CheckFile(std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf); 61 62 62 63 private: -
modules/CheckDisk/stdafx.h
rfe9cc46 rf7663c9 21 21 #pragma once 22 22 23 23 24 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers 24 25 // Windows Header Files: … … 28 29 #include <functional> 29 30 30 #include <NSCAPI.h> 31 #include <boost/lexical_cast.hpp> 32 33 #include <config.h> 34 #include <unicode_char.hpp> 35 #include <types.hpp> 31 36 #include <NSCAPI.h> 32 37 #include <nscapi/plugin.hpp> 33 //#include <NSCHelper.h>34 //#include <nsc_module_wrapper.hpp>35 38 36 39 #ifdef MEMCHECK -
modules/FileLogger/FileLogger.cpp
rc015acc rf7663c9 104 104 //_T("DEBUG LOGGING"), _T("Enable debug logging can help track down errors and find problems but will impact overall performance negativly.")) 105 105 106 (_T("log mask"), sh::wstring_key(&log_mask, false),106 (_T("log mask"), sh::wstring_key(&log_mask, _T("false")), 107 107 _T("LOG MASK"), _T("The log mask information, error, warning, critical, debug")) 108 108 -
modules/NRPEServer/NRPEServer.cpp
rc015acc rf7663c9 70 70 _T("PORT NUMBER"), _T("Port to use for NRPE.")) 71 71 72 (_T("bind to"), sh::string_key(&info_.address),73 _T("BIND TO ADDRESS"), _T("Allows you to bind server to a specific local address. This has to be a dotted ip address not a host name. Leaving this blank will bind to all available IP addresses."))74 75 (_T("socket queue size"), sh::int_key(&info_.back_log, 0),76 _T("LISTEN QUEUE"), _T("Number of sockets to queue before starting to refuse new incoming connections. This can be used to tweak the amount of simultaneous sockets that the server accepts."))77 78 72 (_T("thread pool"), sh::uint_key(&info_.thread_pool_size, 10), 79 73 _T("THREAD POOL"), _T("")) … … 100 94 _T("SSL CERTIFICATE"), _T("")) 101 95 ; 96 97 settings.alias().add_parent(_T("/settings/default")).add_key_to_settings() 98 99 (_T("bind to"), sh::string_key(&info_.address), 100 _T("BIND TO ADDRESS"), _T("Allows you to bind server to a specific local address. This has to be a dotted ip address not a host name. Leaving this blank will bind to all available IP addresses.")) 101 102 (_T("socket queue size"), sh::int_key(&info_.back_log, 0), 103 _T("LISTEN QUEUE"), _T("Number of sockets to queue before starting to refuse new incoming connections. This can be used to tweak the amount of simultaneous sockets that the server accepts.")) 104 105 ; 106 107 102 108 103 109 settings.register_all(); -
modules/NSCAAgent/NSCAAgent.cpp
rc015acc rf7663c9 73 73 _T("CACHE HOSTNAME"), _T("")) 74 74 75 (_T("delay"), sh::string_fun_key<std::wstring>(boost::bind(&NSCAAgent::set_delay, this, _1), 0),75 (_T("delay"), sh::string_fun_key<std::wstring>(boost::bind(&NSCAAgent::set_delay, this, _1), _T("0")), 76 76 _T("DELAY"), _T("")) 77 77 -
service/NSCPlugin.cpp
rc015acc rf7663c9 46 46 ,fHasNotificationHandler(NULL) 47 47 ,fHandleNotification(NULL) 48 , bLoaded_(false)48 ,loaded_(false) 49 49 ,lastIsMsgPlugin_(false) 50 50 ,broken_(false) … … 132 132 } 133 133 loadRemoteProcs_(); 134 bLoaded_ = true;135 134 } 136 135 137 136 bool NSCPlugin::load_plugin(NSCAPI::moduleLoadMode mode) { 137 if (loaded_) 138 return true; 138 139 if (!fLoadModule) 139 140 throw NSPluginException(module_, _T("Critical error (fLoadModule)")); 140 return fLoadModule(alias_.c_str(), mode); 141 if (fLoadModule(alias_.c_str(), mode)) { 142 loaded_ = true; 143 return true; 144 } 145 return false; 141 146 } 142 147 … … 300 305 if (!isLoaded()) 301 306 return; 302 bLoaded_ = false;307 loaded_ = false; 303 308 if (!fUnLoadModule) 304 309 throw NSPluginException(module_, _T("Critical error (fUnLoadModule)")); -
service/NSCPlugin.h
rc015acc rf7663c9 99 99 class NSCPlugin : boost::noncopyable { 100 100 private: 101 bool bLoaded_; // Status of plug in101 //bool bLoaded_; // Status of plug in 102 102 dll::dll module_; 103 103 bool broken_; 104 104 unsigned int plugin_id_; 105 105 std::wstring alias_; 106 bool loaded_; 106 107 107 108 nscapi::plugin_api::lpModuleHelperInit fModuleHelperInit; … … 173 174 } 174 175 bool isLoaded() const { 175 return bLoaded_;176 return module_.is_loaded(); 176 177 } 177 178 unsigned int get_id() const { return plugin_id_; } -
service/NSClient++.cpp
rc015acc rf7663c9 518 518 void NSClientT::load_all_plugins(int mode) { 519 519 boost::filesystem::wpath pluginPath; 520 { 520 521 try { 521 522 pluginPath = expand_path(_T("${module-path}")); … … 535 536 LOG_CRITICAL_STD(_T("Failed to register plugin key: ") + v.second); 536 537 } 538 } 539 540 } 541 542 try { 543 loadPlugins(mode); 544 } catch (...) { 545 LOG_ERROR_CORE_STD(_T("Unknown exception loading plugins")); 546 } 547 537 548 // std::wstring desc; 538 549 // std::wstring name = v.second; … … 559 570 // } 560 571 } 561 }562 572 563 573 void NSClientT::session_error(std::wstring file, unsigned int line, std::wstring msg) {
Note: See TracChangeset
for help on using the changeset viewer.








