| 1 | /************************************************************************** |
|---|
| 2 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> * |
|---|
| 3 | * * |
|---|
| 4 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp * |
|---|
| 5 | * * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify * |
|---|
| 7 | * it under the terms of the GNU General Public License as published by * |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
|---|
| 9 | * (at your option) any later version. * |
|---|
| 10 | * * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, * |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 14 | * GNU General Public License for more details. * |
|---|
| 15 | * * |
|---|
| 16 | * You should have received a copy of the GNU General Public License * |
|---|
| 17 | * along with this program; if not, write to the * |
|---|
| 18 | * Free Software Foundation, Inc., * |
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
|---|
| 20 | ***************************************************************************/ |
|---|
| 21 | #include "stdafx.h" |
|---|
| 22 | #include "CheckDisk.h" |
|---|
| 23 | #include <strEx.h> |
|---|
| 24 | #include <time.h> |
|---|
| 25 | #include <filter_framework.hpp> |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | CheckDisk gCheckDisk; |
|---|
| 30 | |
|---|
| 31 | BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) |
|---|
| 32 | { |
|---|
| 33 | NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call); |
|---|
| 34 | return TRUE; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | CheckDisk::CheckDisk() { |
|---|
| 38 | } |
|---|
| 39 | CheckDisk::~CheckDisk() { |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | bool CheckDisk::loadModule() { |
|---|
| 44 | return true; |
|---|
| 45 | } |
|---|
| 46 | bool CheckDisk::unloadModule() { |
|---|
| 47 | return true; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | bool CheckDisk::hasCommandHandler() { |
|---|
| 51 | return true; |
|---|
| 52 | } |
|---|
| 53 | bool CheckDisk::hasMessageHandler() { |
|---|
| 54 | return false; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | struct file_finder_data { |
|---|
| 58 | file_finder_data(const WIN32_FIND_DATA wfd_, const std::string path_) : wfd(wfd_), path(path_) {} |
|---|
| 59 | const WIN32_FIND_DATA wfd; |
|---|
| 60 | const std::string path; |
|---|
| 61 | }; |
|---|
| 62 | typedef std::unary_function<const file_finder_data&, bool> baseFinderFunction; |
|---|
| 63 | |
|---|
| 64 | struct get_size : public baseFinderFunction |
|---|
| 65 | { |
|---|
| 66 | bool error; |
|---|
| 67 | get_size() : size(0), error(false) { } |
|---|
| 68 | result_type operator()(argument_type ffd) { |
|---|
| 69 | if (!(ffd.wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) { |
|---|
| 70 | size += (ffd.wfd.nFileSizeHigh * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)ffd.wfd.nFileSizeLow; |
|---|
| 71 | } |
|---|
| 72 | return true; |
|---|
| 73 | } |
|---|
| 74 | inline unsigned long long getSize() { |
|---|
| 75 | return size; |
|---|
| 76 | } |
|---|
| 77 | inline const bool hasError() const { |
|---|
| 78 | return error; |
|---|
| 79 | } |
|---|
| 80 | inline void setError(std::string) { |
|---|
| 81 | error = true; |
|---|
| 82 | } |
|---|
| 83 | private: |
|---|
| 84 | unsigned long long size; |
|---|
| 85 | }; |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | template <class finder_function> |
|---|
| 89 | void recursive_scan(std::string dir, finder_function & f) { |
|---|
| 90 | std::string baseDir; |
|---|
| 91 | std::string::size_type pos = dir.find_last_of('\\'); |
|---|
| 92 | if (pos == std::string::npos) |
|---|
| 93 | return; |
|---|
| 94 | baseDir = dir.substr(0, pos); |
|---|
| 95 | |
|---|
| 96 | WIN32_FIND_DATA wfd; |
|---|
| 97 | HANDLE hFind = FindFirstFile(dir.c_str(), &wfd); |
|---|
| 98 | if (hFind != INVALID_HANDLE_VALUE) { |
|---|
| 99 | do { |
|---|
| 100 | if (!f(file_finder_data(wfd, baseDir))) |
|---|
| 101 | break; |
|---|
| 102 | if ((wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) { |
|---|
| 103 | if ( (strcmp(wfd.cFileName, ".") != 0) && (strcmp(wfd.cFileName, "..") != 0) ) |
|---|
| 104 | recursive_scan<finder_function>(baseDir + "\\" + wfd.cFileName + "\\*.*", f); |
|---|
| 105 | } |
|---|
| 106 | } while (FindNextFile(hFind, &wfd)); |
|---|
| 107 | } else { |
|---|
| 108 | f.setError("File not found"); |
|---|
| 109 | } |
|---|
| 110 | FindClose(hFind); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | NSCAPI::nagiosReturn CheckDisk::CheckDriveSize(const unsigned int argLen, char **char_args, std::string &message, std::string &perf) { |
|---|
| 116 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 117 | std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 118 | if (args.empty()) { |
|---|
| 119 | message = "Missing argument(s)."; |
|---|
| 120 | return NSCAPI::returnCRIT; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | DriveConatiner tmpObject; |
|---|
| 124 | bool bFilter = false; |
|---|
| 125 | bool bFilterRemote = false; |
|---|
| 126 | bool bFilterRemovable = false; |
|---|
| 127 | bool bFilterFixed = false; |
|---|
| 128 | bool bFilterCDROM = false; |
|---|
| 129 | bool bCheckAll = false; |
|---|
| 130 | bool bCheckAllOthers = false; |
|---|
| 131 | bool bNSClient = false; |
|---|
| 132 | bool bPerfData = true; |
|---|
| 133 | std::list<DriveConatiner> drives; |
|---|
| 134 | |
|---|
| 135 | MAP_OPTIONS_BEGIN(args) |
|---|
| 136 | MAP_OPTIONS_STR_AND("Drive", tmpObject.data, drives.push_back(tmpObject)) |
|---|
| 137 | MAP_OPTIONS_DISK_ALL(tmpObject, "", "Free", "Used") |
|---|
| 138 | MAP_OPTIONS_SHOWALL(tmpObject) |
|---|
| 139 | MAP_OPTIONS_BOOL_VALUE("FilterType", bFilterFixed, "FIXED") |
|---|
| 140 | MAP_OPTIONS_BOOL_VALUE("FilterType", bFilterCDROM, "CDROM") |
|---|
| 141 | MAP_OPTIONS_BOOL_VALUE("FilterType", bFilterRemovable, "REMOVABLE") |
|---|
| 142 | MAP_OPTIONS_BOOL_VALUE("FilterType", bFilterRemote, "REMOTE") |
|---|
| 143 | MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) |
|---|
| 144 | MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient) |
|---|
| 145 | MAP_OPTIONS_BOOL_TRUE(CHECK_ALL, bCheckAll) |
|---|
| 146 | MAP_OPTIONS_BOOL_TRUE(CHECK_ALL_OTHERS, bCheckAllOthers) |
|---|
| 147 | MAP_OPTIONS_SECONDARY_BEGIN(":", p2) |
|---|
| 148 | else if (p2.first == "Drive") { |
|---|
| 149 | tmpObject.data = p__.second; |
|---|
| 150 | tmpObject.alias = p2.second; |
|---|
| 151 | drives.push_back(tmpObject); |
|---|
| 152 | } |
|---|
| 153 | MAP_OPTIONS_MISSING_EX(p2, message, "Unknown argument: ") |
|---|
| 154 | MAP_OPTIONS_SECONDARY_END() |
|---|
| 155 | MAP_OPTIONS_FALLBACK_AND(tmpObject.data, drives.push_back(tmpObject)) |
|---|
| 156 | MAP_OPTIONS_END() |
|---|
| 157 | bFilter = bFilterFixed || bFilterCDROM || bFilterRemote || bFilterRemovable; |
|---|
| 158 | |
|---|
| 159 | if (bCheckAll) { |
|---|
| 160 | DWORD dwDrives = GetLogicalDrives(); |
|---|
| 161 | int idx = 0; |
|---|
| 162 | while (dwDrives != 0) { |
|---|
| 163 | if (dwDrives & 0x1) { |
|---|
| 164 | std::string drv; |
|---|
| 165 | drv += static_cast<char>('A' + idx); drv += ":\\"; |
|---|
| 166 | UINT drvType = GetDriveType(drv.c_str()); |
|---|
| 167 | if ( ((!bFilter)&&(drvType == DRIVE_FIXED)) || |
|---|
| 168 | ((bFilter)&&(bFilterFixed)&&(drvType==DRIVE_FIXED)) || |
|---|
| 169 | ((bFilter)&&(bFilterCDROM)&&(drvType==DRIVE_CDROM)) || |
|---|
| 170 | ((bFilter)&&(bFilterRemote)&&(drvType==DRIVE_REMOTE)) || |
|---|
| 171 | ((bFilter)&&(bFilterRemovable)&&(drvType==DRIVE_REMOVABLE)) ) |
|---|
| 172 | drives.push_back(DriveConatiner(drv, tmpObject.warn, tmpObject.crit)); |
|---|
| 173 | } |
|---|
| 174 | idx++; |
|---|
| 175 | dwDrives >>= 1; |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | if (bCheckAllOthers) { |
|---|
| 179 | std::list<DriveConatiner> checkdrives; |
|---|
| 180 | DWORD dwDrives = GetLogicalDrives(); |
|---|
| 181 | int idx = 0; |
|---|
| 182 | while (dwDrives != 0) { |
|---|
| 183 | if (dwDrives & 0x1) { |
|---|
| 184 | std::string drv; |
|---|
| 185 | drv += static_cast<char>('A' + idx); drv += ":\\"; |
|---|
| 186 | UINT drvType = GetDriveType(drv.c_str()); |
|---|
| 187 | if ( ((!bFilter)&&(drvType == DRIVE_FIXED)) || |
|---|
| 188 | ((bFilter)&&(bFilterFixed)&&(drvType==DRIVE_FIXED)) || |
|---|
| 189 | ((bFilter)&&(bFilterCDROM)&&(drvType==DRIVE_CDROM)) || |
|---|
| 190 | ((bFilter)&&(bFilterRemote)&&(drvType==DRIVE_REMOTE)) || |
|---|
| 191 | ((bFilter)&&(bFilterRemovable)&&(drvType==DRIVE_REMOVABLE)) ) |
|---|
| 192 | { |
|---|
| 193 | bool bFound = false; |
|---|
| 194 | for (std::list<DriveConatiner>::const_iterator pit = drives.begin();pit!=drives.end();++pit) { |
|---|
| 195 | DriveConatiner drive = (*pit); |
|---|
| 196 | if (drive.data == drv) |
|---|
| 197 | bFound = true; |
|---|
| 198 | } |
|---|
| 199 | if (!bFound) |
|---|
| 200 | checkdrives.push_back(DriveConatiner(drv, tmpObject.warn, tmpObject.crit)); |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | idx++; |
|---|
| 204 | dwDrives >>= 1; |
|---|
| 205 | } |
|---|
| 206 | drives = checkdrives; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | for (std::list<DriveConatiner>::const_iterator pit = drives.begin();pit!=drives.end();++pit) { |
|---|
| 211 | DriveConatiner drive = (*pit); |
|---|
| 212 | if (drive.data.length() == 1) |
|---|
| 213 | drive.data += ":"; |
|---|
| 214 | drive.perfData = bPerfData; |
|---|
| 215 | UINT drvType = GetDriveType(drive.data.c_str()); |
|---|
| 216 | |
|---|
| 217 | if ((!bFilter)&&!((drvType == DRIVE_FIXED)||(drvType == DRIVE_NO_ROOT_DIR))) { |
|---|
| 218 | message = "UNKNOWN: Drive is not a fixed drive: " + drive.getAlias() + " (it is a: " + strEx::itos(drvType) + ")"; |
|---|
| 219 | return NSCAPI::returnUNKNOWN; |
|---|
| 220 | } else if ( (bFilter)&&( (!bFilterFixed)&&((drvType==DRIVE_FIXED)||(drvType==DRIVE_NO_ROOT_DIR))) || |
|---|
| 221 | ((!bFilterCDROM)&&(drvType==DRIVE_CDROM)) || |
|---|
| 222 | ((!bFilterRemote)&&(drvType==DRIVE_REMOTE)) || |
|---|
| 223 | ((!bFilterRemovable)&&(drvType==DRIVE_REMOVABLE)) ) { |
|---|
| 224 | message = "UNKNOWN: Drive does not match the current filter: " + drive.getAlias() + " (it is a: " + strEx::itos(drvType) + ")"; |
|---|
| 225 | return NSCAPI::returnUNKNOWN; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | ULARGE_INTEGER freeBytesAvailableToCaller; |
|---|
| 229 | ULARGE_INTEGER totalNumberOfBytes; |
|---|
| 230 | ULARGE_INTEGER totalNumberOfFreeBytes; |
|---|
| 231 | if (!GetDiskFreeSpaceEx(drive.data.c_str(), &freeBytesAvailableToCaller, &totalNumberOfBytes, &totalNumberOfFreeBytes)) { |
|---|
| 232 | message = "UNKNOWN: Could not get free space for: " + drive.getAlias() + + " \"" + drive.data + "\" reason: " + strEx::itos(GetLastError()); |
|---|
| 233 | return NSCAPI::returnUNKNOWN; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | if (bNSClient) { |
|---|
| 237 | if (!message.empty()) |
|---|
| 238 | message += "&"; |
|---|
| 239 | message += strEx::itos(totalNumberOfFreeBytes.QuadPart); |
|---|
| 240 | message += "&"; |
|---|
| 241 | message += strEx::itos(totalNumberOfBytes.QuadPart); |
|---|
| 242 | } else { |
|---|
| 243 | checkHolders::PercentageValueType<checkHolders::disk_size_type, checkHolders::disk_size_type> value; |
|---|
| 244 | std::string tstr; |
|---|
| 245 | value.value = totalNumberOfBytes.QuadPart-totalNumberOfFreeBytes.QuadPart; |
|---|
| 246 | value.total = totalNumberOfBytes.QuadPart; |
|---|
| 247 | drive.setDefault(tmpObject); |
|---|
| 248 | drive.runCheck(value, returnCode, message, perf); |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | if (message.empty()) |
|---|
| 252 | message = "OK: All drives within bounds."; |
|---|
| 253 | else if (!bNSClient) |
|---|
| 254 | message = NSCHelper::translateReturn(returnCode) + ": " + message; |
|---|
| 255 | return returnCode; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | NSCAPI::nagiosReturn CheckDisk::CheckFileSize(const unsigned int argLen, char **char_args, std::string &message, std::string &perf) { |
|---|
| 261 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 262 | std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 263 | bool bPerfData = true; |
|---|
| 264 | if (args.empty()) { |
|---|
| 265 | message = "Missing argument(s)."; |
|---|
| 266 | return NSCAPI::returnUNKNOWN; |
|---|
| 267 | } |
|---|
| 268 | PathConatiner tmpObject; |
|---|
| 269 | std::list<PathConatiner> paths; |
|---|
| 270 | |
|---|
| 271 | MAP_OPTIONS_BEGIN(args) |
|---|
| 272 | MAP_OPTIONS_STR_AND("File", tmpObject.data, paths.push_back(tmpObject)) |
|---|
| 273 | MAP_OPTIONS_SHOWALL(tmpObject) |
|---|
| 274 | MAP_OPTIONS_STR("MaxWarn", tmpObject.warn.max) |
|---|
| 275 | MAP_OPTIONS_STR("MinWarn", tmpObject.warn.min) |
|---|
| 276 | MAP_OPTIONS_STR("MaxCrit", tmpObject.crit.max) |
|---|
| 277 | MAP_OPTIONS_STR("MinCrit", tmpObject.crit.min) |
|---|
| 278 | MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) |
|---|
| 279 | MAP_OPTIONS_SECONDARY_BEGIN(":", p2) |
|---|
| 280 | else if (p2.first == "File") { |
|---|
| 281 | tmpObject.data = p__.second; |
|---|
| 282 | tmpObject.alias = p2.second; |
|---|
| 283 | paths.push_back(tmpObject); |
|---|
| 284 | } |
|---|
| 285 | MAP_OPTIONS_MISSING_EX(p2, message, "Unknown argument: ") |
|---|
| 286 | MAP_OPTIONS_SECONDARY_END() |
|---|
| 287 | MAP_OPTIONS_MISSING(message, "Unknown argument: ") |
|---|
| 288 | MAP_OPTIONS_END() |
|---|
| 289 | |
|---|
| 290 | for (std::list<PathConatiner>::const_iterator pit = paths.begin(); pit != paths.end(); ++pit) { |
|---|
| 291 | PathConatiner path = (*pit); |
|---|
| 292 | std::string tstr; |
|---|
| 293 | std::string sName = path.getAlias(); |
|---|
| 294 | get_size sizeFinder; |
|---|
| 295 | recursive_scan<get_size>(path.data, sizeFinder); |
|---|
| 296 | if (sizeFinder.hasError()) { |
|---|
| 297 | message = "File not found"; |
|---|
| 298 | return NSCAPI::returnUNKNOWN; |
|---|
| 299 | } |
|---|
| 300 | path.setDefault(tmpObject); |
|---|
| 301 | path.perfData = bPerfData; |
|---|
| 302 | |
|---|
| 303 | checkHolders::disk_size_type size = sizeFinder.getSize(); |
|---|
| 304 | path.runCheck(size, returnCode, message, perf); |
|---|
| 305 | } |
|---|
| 306 | if (message.empty()) |
|---|
| 307 | message = "OK all file sizes are within bounds."; |
|---|
| 308 | else |
|---|
| 309 | message = NSCHelper::translateReturn(returnCode) + ": " + message; |
|---|
| 310 | return returnCode; |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | struct file_info { |
|---|
| 315 | file_info() : ullCreationTime(0) {} |
|---|
| 316 | file_info(const BY_HANDLE_FILE_INFORMATION info, std::string filename_) : filename(filename_), ullCreationTime(0) { |
|---|
| 317 | ullSize = ((info.nFileSizeHigh * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.nFileSizeLow); |
|---|
| 318 | ullCreationTime = ((info.ftCreationTime.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.ftCreationTime.dwLowDateTime); |
|---|
| 319 | ullLastAccessTime = ((info.ftLastAccessTime.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.ftLastAccessTime.dwLowDateTime); |
|---|
| 320 | ullLastWriteTime = ((info.ftLastWriteTime.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.ftLastWriteTime.dwLowDateTime); |
|---|
| 321 | }; |
|---|
| 322 | |
|---|
| 323 | unsigned long long ullSize; |
|---|
| 324 | unsigned long long ullCreationTime; |
|---|
| 325 | unsigned long long ullLastAccessTime; |
|---|
| 326 | unsigned long long ullLastWriteTime; |
|---|
| 327 | unsigned long long ullNow; |
|---|
| 328 | std::string filename; |
|---|
| 329 | |
|---|
| 330 | std::string render(std::string syntax) { |
|---|
| 331 | strEx::replace(syntax, "%filename%", filename); |
|---|
| 332 | strEx::replace(syntax, "%creation%", strEx::format_filetime(ullCreationTime, DATE_FORMAT)); |
|---|
| 333 | strEx::replace(syntax, "%access%", strEx::format_filetime(ullLastAccessTime, DATE_FORMAT)); |
|---|
| 334 | strEx::replace(syntax, "%write%", strEx::format_filetime(ullLastWriteTime, DATE_FORMAT)); |
|---|
| 335 | strEx::replace(syntax, "%size%", strEx::itos_as_BKMG(ullSize)); |
|---|
| 336 | return syntax; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | }; |
|---|
| 340 | |
|---|
| 341 | struct file_filter { |
|---|
| 342 | filters::filter_all_numeric<unsigned long long, checkHolders::disk_size_handler<checkHolders::disk_size_type> > fileSize; |
|---|
| 343 | filters::filter_all_times fileCreation; |
|---|
| 344 | filters::filter_all_times fileAccessed; |
|---|
| 345 | filters::filter_all_times fileWritten; |
|---|
| 346 | static const __int64 MSECS_TO_100NS = 10000; |
|---|
| 347 | |
|---|
| 348 | inline bool hasFilter() { |
|---|
| 349 | return fileSize.hasFilter() || fileCreation.hasFilter() || |
|---|
| 350 | fileAccessed.hasFilter() || fileWritten.hasFilter(); |
|---|
| 351 | } |
|---|
| 352 | bool matchFilter(const file_info &value) const { |
|---|
| 353 | if ((fileSize.hasFilter())&&(fileSize.matchFilter(value.ullSize))) |
|---|
| 354 | return true; |
|---|
| 355 | else if ((fileCreation.hasFilter())&&(fileCreation.matchFilter((value.ullNow-value.ullCreationTime)/MSECS_TO_100NS))) |
|---|
| 356 | return true; |
|---|
| 357 | else if ((fileAccessed.hasFilter())&&(fileAccessed.matchFilter((value.ullNow-value.ullLastAccessTime)/MSECS_TO_100NS))) |
|---|
| 358 | return true; |
|---|
| 359 | else if ((fileWritten.hasFilter())&&(fileWritten.matchFilter((value.ullNow-value.ullLastWriteTime)/MSECS_TO_100NS))) |
|---|
| 360 | return true; |
|---|
| 361 | return false; |
|---|
| 362 | } |
|---|
| 363 | }; |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | struct find_first_file_info : public baseFinderFunction |
|---|
| 367 | { |
|---|
| 368 | file_info info; |
|---|
| 369 | bool error; |
|---|
| 370 | // std::string message; |
|---|
| 371 | find_first_file_info() : error(false) {} |
|---|
| 372 | result_type operator()(argument_type ffd) { |
|---|
| 373 | if ((ffd.wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) |
|---|
| 374 | return true; |
|---|
| 375 | BY_HANDLE_FILE_INFORMATION _info; |
|---|
| 376 | |
|---|
| 377 | HANDLE hFile = CreateFile((ffd.path + "\\" + ffd.wfd.cFileName).c_str(), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, |
|---|
| 378 | 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); |
|---|
| 379 | if (hFile == INVALID_HANDLE_VALUE) { |
|---|
| 380 | setError("Could not open file: " + ffd.path + "\\" + ffd.wfd.cFileName + ": " + strEx::itos(GetLastError())); |
|---|
| 381 | return false; |
|---|
| 382 | } |
|---|
| 383 | GetFileInformationByHandle(hFile, &_info); |
|---|
| 384 | CloseHandle(hFile); |
|---|
| 385 | info = file_info(_info, ffd.wfd.cFileName); |
|---|
| 386 | return false; |
|---|
| 387 | } |
|---|
| 388 | inline const bool hasError() const { |
|---|
| 389 | return error; |
|---|
| 390 | } |
|---|
| 391 | inline void setError(std::string) { |
|---|
| 392 | error = true; |
|---|
| 393 | } |
|---|
| 394 | }; |
|---|
| 395 | |
|---|
| 396 | struct file_filter_function : public baseFinderFunction |
|---|
| 397 | { |
|---|
| 398 | std::list<file_filter> filter_chain; |
|---|
| 399 | bool bFilterAll; |
|---|
| 400 | bool bFilterIn; |
|---|
| 401 | bool error; |
|---|
| 402 | std::string message; |
|---|
| 403 | std::string syntax; |
|---|
| 404 | unsigned long long now; |
|---|
| 405 | unsigned int hit_count; |
|---|
| 406 | |
|---|
| 407 | file_filter_function() : hit_count(0), error(false), bFilterIn(true), bFilterAll(true) {} |
|---|
| 408 | result_type operator()(argument_type ffd) { |
|---|
| 409 | if ((ffd.wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) |
|---|
| 410 | return true; |
|---|
| 411 | BY_HANDLE_FILE_INFORMATION _info; |
|---|
| 412 | |
|---|
| 413 | HANDLE hFile = CreateFile((ffd.path + "\\" + ffd.wfd.cFileName).c_str(), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, |
|---|
| 414 | 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); |
|---|
| 415 | if (hFile == INVALID_HANDLE_VALUE) { |
|---|
| 416 | setError("Could not open file: " + ffd.path + "\\" + ffd.wfd.cFileName + ": " + strEx::itos(GetLastError())); |
|---|
| 417 | } |
|---|
| 418 | GetFileInformationByHandle(hFile, &_info); |
|---|
| 419 | CloseHandle(hFile); |
|---|
| 420 | file_info info(_info, ffd.wfd.cFileName); |
|---|
| 421 | info.ullNow = now; |
|---|
| 422 | |
|---|
| 423 | for (std::list<file_filter>::const_iterator cit3 = filter_chain.begin(); cit3 != filter_chain.end(); ++cit3 ) { |
|---|
| 424 | bool bMatch = bFilterAll; |
|---|
| 425 | bool bTmpMatched = (*cit3).matchFilter(info); |
|---|
| 426 | if (bFilterAll) { |
|---|
| 427 | if (!bTmpMatched) { |
|---|
| 428 | bMatch = false; |
|---|
| 429 | break; |
|---|
| 430 | } |
|---|
| 431 | } else { |
|---|
| 432 | if (bTmpMatched) { |
|---|
| 433 | bMatch = true; |
|---|
| 434 | break; |
|---|
| 435 | } |
|---|
| 436 | } |
|---|
| 437 | if ((bFilterIn&&bMatch)||(!bFilterIn&&!bMatch)) { |
|---|
| 438 | strEx::append_list(message, info.render(syntax)); |
|---|
| 439 | hit_count++; |
|---|
| 440 | } |
|---|
| 441 | } |
|---|
| 442 | return true; |
|---|
| 443 | } |
|---|
| 444 | inline const bool hasError() const { |
|---|
| 445 | return error; |
|---|
| 446 | } |
|---|
| 447 | inline void setError(std::string) { |
|---|
| 448 | error = true; |
|---|
| 449 | } |
|---|
| 450 | }; |
|---|
| 451 | |
|---|
| 452 | NSCAPI::nagiosReturn CheckDisk::getFileAge(const unsigned int argLen, char **char_args, std::string &message, std::string &perf) { |
|---|
| 453 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 454 | std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 455 | typedef checkHolders::CheckConatiner<checkHolders::MaxMinBoundsUInteger> CheckFileConatiner; |
|---|
| 456 | if (stl_args.empty()) { |
|---|
| 457 | message = "Missing argument(s)."; |
|---|
| 458 | return NSCAPI::returnUNKNOWN; |
|---|
| 459 | } |
|---|
| 460 | std::string dstr = "%#c"; |
|---|
| 461 | std::string path; |
|---|
| 462 | find_first_file_info finder; |
|---|
| 463 | MAP_OPTIONS_BEGIN(stl_args) |
|---|
| 464 | MAP_OPTIONS_STR("path", path) |
|---|
| 465 | MAP_OPTIONS_FALLBACK(dstr) |
|---|
| 466 | MAP_OPTIONS_END() |
|---|
| 467 | |
|---|
| 468 | if (path.empty()) { |
|---|
| 469 | message = "ERROR: no file specified."; |
|---|
| 470 | return NSCAPI::returnUNKNOWN; |
|---|
| 471 | } |
|---|
| 472 | |
|---|
| 473 | recursive_scan<find_first_file_info>(path, finder); |
|---|
| 474 | if (finder.hasError()) { |
|---|
| 475 | message = "File not found"; |
|---|
| 476 | return NSCAPI::returnUNKNOWN; |
|---|
| 477 | } |
|---|
| 478 | FILETIME now_; |
|---|
| 479 | GetSystemTimeAsFileTime(&now_); |
|---|
| 480 | unsigned long long now = ((now_.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)now_.dwLowDateTime); |
|---|
| 481 | time_t value = (now-finder.info.ullLastWriteTime)/10000000; |
|---|
| 482 | |
|---|
| 483 | char buf[51]; |
|---|
| 484 | size_t l = strftime(buf, 50, dstr.c_str(), gmtime(&value)); |
|---|
| 485 | if (l <= 0 || l >= 50) { |
|---|
| 486 | message = "ERROR: could format time."; |
|---|
| 487 | return NSCAPI::returnUNKNOWN; |
|---|
| 488 | } |
|---|
| 489 | buf[l] = 0; |
|---|
| 490 | message = strEx::itos(value/60) + "&" + buf; |
|---|
| 491 | return NSCAPI::returnOK; |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | NSCAPI::nagiosReturn CheckDisk::CheckFile(const unsigned int argLen, char **char_args, std::string &message, std::string &perf) { |
|---|
| 496 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 497 | std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 498 | typedef checkHolders::CheckConatiner<checkHolders::MaxMinBoundsUInteger> CheckFileConatiner; |
|---|
| 499 | if (stl_args.empty()) { |
|---|
| 500 | message = "Missing argument(s)."; |
|---|
| 501 | return NSCAPI::returnUNKNOWN; |
|---|
| 502 | } |
|---|
| 503 | file_filter_function finder; |
|---|
| 504 | PathConatiner tmpObject; |
|---|
| 505 | std::list<std::string> paths; |
|---|
| 506 | unsigned int truncate = 0; |
|---|
| 507 | CheckFileConatiner query; |
|---|
| 508 | std::string syntax = "%filename%"; |
|---|
| 509 | bool bPerfData = true; |
|---|
| 510 | |
|---|
| 511 | try { |
|---|
| 512 | MAP_OPTIONS_BEGIN(stl_args) |
|---|
| 513 | MAP_OPTIONS_NUMERIC_ALL(query, "") |
|---|
| 514 | MAP_OPTIONS_STR2INT("truncate", truncate) |
|---|
| 515 | MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) |
|---|
| 516 | MAP_OPTIONS_STR("syntax", syntax) |
|---|
| 517 | MAP_OPTIONS_PUSH("path", paths) |
|---|
| 518 | MAP_OPTIONS_PUSH("file", paths) |
|---|
| 519 | MAP_OPTIONS_BOOL_EX("filter", finder.bFilterIn, "in", "out") |
|---|
| 520 | MAP_OPTIONS_BOOL_EX("filter", finder.bFilterAll, "all", "any") |
|---|
| 521 | MAP_OPTIONS_PUSH_WTYPE(file_filter, "filter-size", fileSize, finder.filter_chain) |
|---|
| 522 | MAP_OPTIONS_PUSH_WTYPE(file_filter, "filter-creation", fileCreation, finder.filter_chain) |
|---|
| 523 | MAP_OPTIONS_PUSH_WTYPE(file_filter, "filter-written", fileWritten, finder.filter_chain) |
|---|
| 524 | MAP_OPTIONS_PUSH_WTYPE(file_filter, "filter-accessed", fileAccessed, finder.filter_chain) |
|---|
| 525 | MAP_OPTIONS_MISSING(message, "Unknown argument: ") |
|---|
| 526 | MAP_OPTIONS_END() |
|---|
| 527 | } catch (filters::parse_exception e) { |
|---|
| 528 | message = e.getMessage(); |
|---|
| 529 | return NSCAPI::returnUNKNOWN; |
|---|
| 530 | } catch (filters::filter_exception e) { |
|---|
| 531 | message = e.getMessage(); |
|---|
| 532 | return NSCAPI::returnUNKNOWN; |
|---|
| 533 | } |
|---|
| 534 | FILETIME now; |
|---|
| 535 | GetSystemTimeAsFileTime(&now); |
|---|
| 536 | finder.now = ((now.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)now.dwLowDateTime); |
|---|
| 537 | finder.syntax = syntax; |
|---|
| 538 | for (std::list<std::string>::const_iterator pit = paths.begin(); pit != paths.end(); ++pit) { |
|---|
| 539 | recursive_scan<file_filter_function>((*pit), finder); |
|---|
| 540 | if (finder.hasError()) { |
|---|
| 541 | message = "File not found: " + (*pit); |
|---|
| 542 | return NSCAPI::returnUNKNOWN; |
|---|
| 543 | } |
|---|
| 544 | } |
|---|
| 545 | message = finder.message; |
|---|
| 546 | if (finder.error) |
|---|
| 547 | return NSCAPI::returnUNKNOWN; |
|---|
| 548 | query.runCheck(finder.hit_count, returnCode, message, perf); |
|---|
| 549 | if ((truncate > 0) && (message.length() > (truncate-4))) |
|---|
| 550 | message = message.substr(0, truncate-4) + "..."; |
|---|
| 551 | if (message.empty()) |
|---|
| 552 | message = "CheckFile ok"; |
|---|
| 553 | return returnCode; |
|---|
| 554 | } |
|---|
| 555 | |
|---|
| 556 | NSCAPI::nagiosReturn CheckDisk::handleCommand(const strEx::blindstr command, const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) { |
|---|
| 557 | if (command == "CheckFileSize") { |
|---|
| 558 | return CheckFileSize(argLen, char_args, msg, perf); |
|---|
| 559 | } else if (command == "CheckDriveSize") { |
|---|
| 560 | return CheckDriveSize(argLen, char_args, msg, perf); |
|---|
| 561 | } else if (command == "CheckFile") { |
|---|
| 562 | return CheckFile(argLen, char_args, msg, perf); |
|---|
| 563 | } else if (command == "getFileAge") { |
|---|
| 564 | return getFileAge(argLen, char_args, msg, perf); |
|---|
| 565 | } |
|---|
| 566 | return NSCAPI::returnIgnored; |
|---|
| 567 | } |
|---|
| 568 | |
|---|
| 569 | |
|---|
| 570 | NSC_WRAPPERS_MAIN_DEF(gCheckDisk); |
|---|
| 571 | NSC_WRAPPERS_IGNORE_MSG_DEF(); |
|---|
| 572 | NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckDisk); |
|---|