| 1 | // CheckEventLog.cpp : Defines the entry point for the DLL application. |
|---|
| 2 | // |
|---|
| 3 | |
|---|
| 4 | #include "stdafx.h" |
|---|
| 5 | #include "CheckDisk.h" |
|---|
| 6 | #include <strEx.h> |
|---|
| 7 | #include <time.h> |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | CheckDisk gCheckDisk; |
|---|
| 13 | |
|---|
| 14 | BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) |
|---|
| 15 | { |
|---|
| 16 | NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call); |
|---|
| 17 | return TRUE; |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | CheckDisk::CheckDisk() { |
|---|
| 21 | } |
|---|
| 22 | CheckDisk::~CheckDisk() { |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | bool CheckDisk::loadModule() { |
|---|
| 27 | return true; |
|---|
| 28 | } |
|---|
| 29 | bool CheckDisk::unloadModule() { |
|---|
| 30 | return true; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | bool CheckDisk::hasCommandHandler() { |
|---|
| 34 | return true; |
|---|
| 35 | } |
|---|
| 36 | bool CheckDisk::hasMessageHandler() { |
|---|
| 37 | return false; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | typedef std::unary_function<const WIN32_FIND_DATA&, bool> baseClass; |
|---|
| 41 | struct GetSize : public baseClass |
|---|
| 42 | { |
|---|
| 43 | GetSize() : size(0) { } |
|---|
| 44 | result_type operator()(argument_type wfd) { |
|---|
| 45 | if (!(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) { |
|---|
| 46 | size += (wfd.nFileSizeHigh * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)wfd.nFileSizeLow; |
|---|
| 47 | } |
|---|
| 48 | return true; |
|---|
| 49 | } |
|---|
| 50 | inline unsigned long long getSize() { |
|---|
| 51 | return size; |
|---|
| 52 | } |
|---|
| 53 | private: |
|---|
| 54 | unsigned long long size; |
|---|
| 55 | }; |
|---|
| 56 | |
|---|
| 57 | void RecursiveScanDirectory(std::string dir, GetSize & f) { |
|---|
| 58 | std::string baseDir; |
|---|
| 59 | std::string::size_type pos = dir.find_last_of('\\'); |
|---|
| 60 | if (pos == std::string::npos) |
|---|
| 61 | return; |
|---|
| 62 | baseDir = dir.substr(0, pos); |
|---|
| 63 | |
|---|
| 64 | WIN32_FIND_DATA wfd; |
|---|
| 65 | HANDLE hFind = FindFirstFile(dir.c_str(), &wfd); |
|---|
| 66 | if (hFind != INVALID_HANDLE_VALUE) { |
|---|
| 67 | do { |
|---|
| 68 | if (!f(wfd)) |
|---|
| 69 | break; |
|---|
| 70 | if ((wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) { |
|---|
| 71 | if ( (strcmp(wfd.cFileName, ".") != 0) && (strcmp(wfd.cFileName, "..") != 0) ) |
|---|
| 72 | RecursiveScanDirectory(baseDir + "\\" + wfd.cFileName + "\\*.*", f); |
|---|
| 73 | } |
|---|
| 74 | } while (FindNextFile(hFind, &wfd)); |
|---|
| 75 | } |
|---|
| 76 | FindClose(hFind); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | NSCAPI::nagiosReturn CheckDisk::CheckDriveSize(const unsigned int argLen, char **char_args, std::string &message, std::string &perf) { |
|---|
| 81 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 82 | std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 83 | if (args.empty()) { |
|---|
| 84 | message = "Missing argument(s)."; |
|---|
| 85 | return NSCAPI::returnCRIT; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | DriveConatiner tmpObject; |
|---|
| 89 | bool bFilter = false; |
|---|
| 90 | bool bFilterRemote = false; |
|---|
| 91 | bool bFilterRemovable = false; |
|---|
| 92 | bool bFilterFixed = false; |
|---|
| 93 | bool bFilterCDROM = false; |
|---|
| 94 | bool bCheckAll = false; |
|---|
| 95 | bool bNSClient = false; |
|---|
| 96 | std::list<DriveConatiner> drives; |
|---|
| 97 | |
|---|
| 98 | MAP_OPTIONS_BEGIN(args) |
|---|
| 99 | MAP_OPTIONS_STR_AND("Drive", tmpObject.data, drives.push_back(tmpObject)) |
|---|
| 100 | MAP_OPTIONS_DISK_ALL(tmpObject, "", "Free", "Used") |
|---|
| 101 | MAP_OPTIONS_SHOWALL(tmpObject) |
|---|
| 102 | MAP_OPTIONS_BOOL_VALUE("FilterType", bFilterFixed, "FIXED") |
|---|
| 103 | MAP_OPTIONS_BOOL_VALUE("FilterType", bFilterCDROM, "CDROM") |
|---|
| 104 | MAP_OPTIONS_BOOL_VALUE("FilterType", bFilterRemovable, "REMOVABLE") |
|---|
| 105 | MAP_OPTIONS_BOOL_VALUE("FilterType", bFilterRemote, "REMOTE") |
|---|
| 106 | MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient) |
|---|
| 107 | MAP_OPTIONS_BOOL_TRUE(CHECK_ALL, bCheckAll) |
|---|
| 108 | MAP_OPTIONS_SECONDARY_BEGIN(":", p2) |
|---|
| 109 | else if (p2.first == "Drive") { |
|---|
| 110 | tmpObject.data = p__.second; |
|---|
| 111 | tmpObject.alias = p2.second; |
|---|
| 112 | drives.push_back(tmpObject); |
|---|
| 113 | } |
|---|
| 114 | MAP_OPTIONS_MISSING_EX(p2, message, "Unknown argument: ") |
|---|
| 115 | MAP_OPTIONS_SECONDARY_END() |
|---|
| 116 | MAP_OPTIONS_FALLBACK_AND(tmpObject.data, drives.push_back(tmpObject)) |
|---|
| 117 | MAP_OPTIONS_END() |
|---|
| 118 | bFilter = bFilterFixed || bFilterCDROM || bFilterRemote || bFilterRemovable; |
|---|
| 119 | |
|---|
| 120 | if (bCheckAll) { |
|---|
| 121 | DWORD dwDrives = GetLogicalDrives(); |
|---|
| 122 | int idx = 0; |
|---|
| 123 | while (dwDrives != 0) { |
|---|
| 124 | if (dwDrives & 0x1) { |
|---|
| 125 | std::string drv; |
|---|
| 126 | drv += static_cast<char>('A' + idx); drv += ":\\"; |
|---|
| 127 | UINT drvType = GetDriveType(drv.c_str()); |
|---|
| 128 | if ( ((!bFilter)&&(drvType == DRIVE_FIXED)) || |
|---|
| 129 | ((bFilter)&&(bFilterFixed)&&(drvType==DRIVE_FIXED)) || |
|---|
| 130 | ((bFilter)&&(bFilterCDROM)&&(drvType==DRIVE_CDROM)) || |
|---|
| 131 | ((bFilter)&&(bFilterRemote)&&(drvType==DRIVE_REMOTE)) || |
|---|
| 132 | ((bFilter)&&(bFilterRemovable)&&(drvType==DRIVE_REMOVABLE)) ) |
|---|
| 133 | drives.push_back(DriveConatiner(drv, tmpObject.warn, tmpObject.crit)); |
|---|
| 134 | } |
|---|
| 135 | idx++; |
|---|
| 136 | dwDrives >>= 1; |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | for (std::list<DriveConatiner>::const_iterator pit = drives.begin();pit!=drives.end();++pit) { |
|---|
| 142 | DriveConatiner drive = (*pit); |
|---|
| 143 | if (drive.data.length() == 1) |
|---|
| 144 | drive.data += ":"; |
|---|
| 145 | UINT drvType = GetDriveType(drive.data.c_str()); |
|---|
| 146 | |
|---|
| 147 | if ((!bFilter)&&!((drvType == DRIVE_FIXED)||(drvType == DRIVE_NO_ROOT_DIR))) { |
|---|
| 148 | message = "UNKNOWN: Drive is not a fixed drive: " + drive.getAlias() + " (it is a: " + strEx::itos(drvType) + ")"; |
|---|
| 149 | return NSCAPI::returnUNKNOWN; |
|---|
| 150 | } else if ( (bFilter)&&( (!bFilterFixed)&&((drvType==DRIVE_FIXED)||(drvType==DRIVE_NO_ROOT_DIR))) || |
|---|
| 151 | ((!bFilterCDROM)&&(drvType==DRIVE_CDROM)) || |
|---|
| 152 | ((!bFilterRemote)&&(drvType==DRIVE_REMOTE)) || |
|---|
| 153 | ((!bFilterRemovable)&&(drvType==DRIVE_REMOVABLE)) ) { |
|---|
| 154 | message = "UNKNOWN: Drive does not match the current filter: " + drive.getAlias() + " (it is a: " + strEx::itos(drvType) + ")"; |
|---|
| 155 | return NSCAPI::returnUNKNOWN; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | ULARGE_INTEGER freeBytesAvailableToCaller; |
|---|
| 159 | ULARGE_INTEGER totalNumberOfBytes; |
|---|
| 160 | ULARGE_INTEGER totalNumberOfFreeBytes; |
|---|
| 161 | if (!GetDiskFreeSpaceEx(drive.data.c_str(), &freeBytesAvailableToCaller, &totalNumberOfBytes, &totalNumberOfFreeBytes)) { |
|---|
| 162 | message = "UNKNOWN: Could not get free space for: " + drive.getAlias() + + " \"" + drive.data + "\" reason: " + strEx::itos(GetLastError()); |
|---|
| 163 | return NSCAPI::returnUNKNOWN; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | if (bNSClient) { |
|---|
| 167 | if (!message.empty()) |
|---|
| 168 | message += "&"; |
|---|
| 169 | message += strEx::itos(totalNumberOfFreeBytes.QuadPart); |
|---|
| 170 | message += "&"; |
|---|
| 171 | message += strEx::itos(totalNumberOfBytes.QuadPart); |
|---|
| 172 | } else { |
|---|
| 173 | checkHolders::PercentageValueType<checkHolders::disk_size_type, checkHolders::disk_size_type> value; |
|---|
| 174 | std::string tstr; |
|---|
| 175 | value.value = totalNumberOfBytes.QuadPart-totalNumberOfFreeBytes.QuadPart; |
|---|
| 176 | value.total = totalNumberOfBytes.QuadPart; |
|---|
| 177 | drive.setDefault(tmpObject); |
|---|
| 178 | drive.runCheck(value, returnCode, message, perf); |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | if (message.empty()) |
|---|
| 182 | message = "OK: All drives within bounds."; |
|---|
| 183 | else if (!bNSClient) |
|---|
| 184 | message = NSCHelper::translateReturn(returnCode) + ": " + message; |
|---|
| 185 | return returnCode; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | NSCAPI::nagiosReturn CheckDisk::CheckFileSize(const unsigned int argLen, char **char_args, std::string &message, std::string &perf) { |
|---|
| 191 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 192 | std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 193 | if (args.empty()) { |
|---|
| 194 | message = "Missing argument(s)."; |
|---|
| 195 | return NSCAPI::returnUNKNOWN; |
|---|
| 196 | } |
|---|
| 197 | PathConatiner tmpObject; |
|---|
| 198 | std::list<PathConatiner> paths; |
|---|
| 199 | |
|---|
| 200 | MAP_OPTIONS_BEGIN(args) |
|---|
| 201 | MAP_OPTIONS_STR_AND("File", tmpObject.data, paths.push_back(tmpObject)) |
|---|
| 202 | MAP_OPTIONS_SHOWALL(tmpObject) |
|---|
| 203 | MAP_OPTIONS_STR("MaxWarn", tmpObject.warn.max) |
|---|
| 204 | MAP_OPTIONS_STR("MinWarn", tmpObject.warn.min) |
|---|
| 205 | MAP_OPTIONS_STR("MaxCrit", tmpObject.crit.max) |
|---|
| 206 | MAP_OPTIONS_STR("MinCrit", tmpObject.crit.min) |
|---|
| 207 | MAP_OPTIONS_SECONDARY_BEGIN(":", p2) |
|---|
| 208 | else if (p2.first == "File") { |
|---|
| 209 | tmpObject.data = p__.second; |
|---|
| 210 | tmpObject.alias = p2.second; |
|---|
| 211 | paths.push_back(tmpObject); |
|---|
| 212 | } |
|---|
| 213 | MAP_OPTIONS_MISSING_EX(p2, message, "Unknown argument: ") |
|---|
| 214 | MAP_OPTIONS_SECONDARY_END() |
|---|
| 215 | MAP_OPTIONS_MISSING(message, "Unknown argument: ") |
|---|
| 216 | MAP_OPTIONS_END() |
|---|
| 217 | |
|---|
| 218 | for (std::list<PathConatiner>::const_iterator pit = paths.begin(); pit != paths.end(); ++pit) { |
|---|
| 219 | PathConatiner path = (*pit); |
|---|
| 220 | std::string tstr; |
|---|
| 221 | std::string sName = path.getAlias(); |
|---|
| 222 | GetSize sizeFinder; |
|---|
| 223 | RecursiveScanDirectory(path.data, sizeFinder); |
|---|
| 224 | path.setDefault(tmpObject); |
|---|
| 225 | |
|---|
| 226 | checkHolders::disk_size_type size = sizeFinder.getSize(); |
|---|
| 227 | path.runCheck(size, returnCode, message, perf); |
|---|
| 228 | } |
|---|
| 229 | if (message.empty()) |
|---|
| 230 | message = "OK all file sizes are within bounds."; |
|---|
| 231 | else |
|---|
| 232 | message = NSCHelper::translateReturn(returnCode) + ": " + message; |
|---|
| 233 | return returnCode; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | NSCAPI::nagiosReturn CheckDisk::handleCommand(const strEx::blindstr command, const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) { |
|---|
| 237 | if (command == "CheckFileSize") { |
|---|
| 238 | return CheckFileSize(argLen, char_args, msg, perf); |
|---|
| 239 | } else if (command == "CheckDriveSize") { |
|---|
| 240 | return CheckDriveSize(argLen, char_args, msg, perf); |
|---|
| 241 | } |
|---|
| 242 | return NSCAPI::returnIgnored; |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | NSC_WRAPPERS_MAIN_DEF(gCheckDisk); |
|---|
| 247 | NSC_WRAPPERS_IGNORE_MSG_DEF(); |
|---|
| 248 | NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckDisk); |
|---|