| 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 | #include <utils.h> |
|---|
| 9 | |
|---|
| 10 | CheckDisk gCheckDisk; |
|---|
| 11 | |
|---|
| 12 | BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) |
|---|
| 13 | { |
|---|
| 14 | NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call); |
|---|
| 15 | return TRUE; |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | CheckDisk::CheckDisk() { |
|---|
| 19 | } |
|---|
| 20 | CheckDisk::~CheckDisk() { |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | bool CheckDisk::loadModule() { |
|---|
| 25 | return true; |
|---|
| 26 | } |
|---|
| 27 | bool CheckDisk::unloadModule() { |
|---|
| 28 | return true; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | std::string CheckDisk::getModuleName() { |
|---|
| 32 | return "CheckDisk Various Disk related checks."; |
|---|
| 33 | } |
|---|
| 34 | NSCModuleWrapper::module_version CheckDisk::getModuleVersion() { |
|---|
| 35 | NSCModuleWrapper::module_version version = {0, 0, 1 }; |
|---|
| 36 | return version; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | bool CheckDisk::hasCommandHandler() { |
|---|
| 40 | return true; |
|---|
| 41 | } |
|---|
| 42 | bool CheckDisk::hasMessageHandler() { |
|---|
| 43 | return false; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | typedef std::unary_function<const WIN32_FIND_DATA&, bool> baseClass; |
|---|
| 47 | struct GetSize : public baseClass |
|---|
| 48 | { |
|---|
| 49 | GetSize() : size(0) { } |
|---|
| 50 | result_type operator()(argument_type wfd) { |
|---|
| 51 | if (!(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) { |
|---|
| 52 | size += (wfd.nFileSizeHigh * ((long long)MAXDWORD+1)) + (long long)wfd.nFileSizeLow; |
|---|
| 53 | } |
|---|
| 54 | return true; |
|---|
| 55 | } |
|---|
| 56 | inline long long getSize() { |
|---|
| 57 | return size; |
|---|
| 58 | } |
|---|
| 59 | private: |
|---|
| 60 | long long size; |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | void RecursiveScanDirectory(std::string dir, GetSize & f) { |
|---|
| 64 | std::string baseDir; |
|---|
| 65 | std::string::size_type pos = dir.find_last_of('\\'); |
|---|
| 66 | if (pos == std::string::npos) |
|---|
| 67 | return; |
|---|
| 68 | baseDir = dir.substr(0, pos); |
|---|
| 69 | |
|---|
| 70 | WIN32_FIND_DATA wfd; |
|---|
| 71 | HANDLE hFind = FindFirstFile(dir.c_str(), &wfd); |
|---|
| 72 | if (hFind != INVALID_HANDLE_VALUE) { |
|---|
| 73 | do { |
|---|
| 74 | if (!f(wfd)) |
|---|
| 75 | break; |
|---|
| 76 | if ((wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) { |
|---|
| 77 | if ( (strcmp(wfd.cFileName, ".") != 0) && (strcmp(wfd.cFileName, "..") != 0) ) |
|---|
| 78 | RecursiveScanDirectory(baseDir + "\\" + wfd.cFileName + "\\*.*", f); |
|---|
| 79 | } |
|---|
| 80 | } while (FindNextFile(hFind, &wfd)); |
|---|
| 81 | } |
|---|
| 82 | FindClose(hFind); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | NSCAPI::nagiosReturn CheckDisk::CheckDriveSize(const unsigned int argLen, char **char_args, std::string &message, std::string &perf) { |
|---|
| 86 | // CheckFileSize |
|---|
| 87 | // request: CheckFileSize <option> <option>... |
|---|
| 88 | // <option> MaxWarn=<size gmkb> |
|---|
| 89 | // MaxCrit=<size gmkb> |
|---|
| 90 | // MinWarn=<size gmkb> |
|---|
| 91 | // MinCrit=<size gmkb> |
|---|
| 92 | // ShowAll |
|---|
| 93 | // File=<path> |
|---|
| 94 | // File:<shortname>=<path> |
|---|
| 95 | // |
|---|
| 96 | // Return: <return state>&<return string>... |
|---|
| 97 | // <return state> 0 - No errors |
|---|
| 98 | // 1 - Unknown |
|---|
| 99 | // 2 - Errors |
|---|
| 100 | // <size gmkb> is a size with a possible modifier letter (such as G for gigabyte, M for Megabyte, K for kilobyte etc) |
|---|
| 101 | // Examples: |
|---|
| 102 | // <return string> <directory> <size gmkb> ... |<shortname>=<size>:<warn>:<crit> |
|---|
| 103 | // test: CheckFileSize ShowAll MaxWarn=1024M MaxCrit=4096M File:WIN=c:\WINDOWS\*.* |
|---|
| 104 | // CheckFileSize |
|---|
| 105 | // |
|---|
| 106 | // check_nscp -H <ip> -p <port> -s <passwd> -c <commandstring> |
|---|
| 107 | // |
|---|
| 108 | // ./check_nscp -H 192.168.0.167 -p 1234 -s pwd -c 'CheckFileSize&ShowAll&MaxWarn=1024M&MaxCrit=4096M&File:WIN=c:\WINDOWS\*.*' |
|---|
| 109 | // WIN: 1G (2110962363B)|WIN:2110962363:1073741824:4294967296 |
|---|
| 110 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 111 | std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 112 | if (args.empty()) { |
|---|
| 113 | message = "Missing argument(s)."; |
|---|
| 114 | return NSCAPI::returnCRIT; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | checkHolders::SizeMaxMin warn; |
|---|
| 118 | checkHolders::SizeMaxMin crit; |
|---|
| 119 | bool bShowAll = false; |
|---|
| 120 | bool bNSClient = false; |
|---|
| 121 | std::list<std::string> drives; |
|---|
| 122 | |
|---|
| 123 | std::list<std::string>::const_iterator cit; |
|---|
| 124 | for (cit=args.begin();cit!=args.end();++cit) { |
|---|
| 125 | std::string arg = *cit; |
|---|
| 126 | std::pair<std::string,std::string> p = strEx::split(arg,"="); |
|---|
| 127 | if (p.first == "Drive") { |
|---|
| 128 | drives.push_back(p.second); |
|---|
| 129 | } else if (p.first == "MaxWarn") { |
|---|
| 130 | warn.max.set(p.second); |
|---|
| 131 | } else if (p.first == "MinWarn") { |
|---|
| 132 | warn.min.set(p.second); |
|---|
| 133 | } else if (p.first == "MaxCrit") { |
|---|
| 134 | crit.max.set(p.second); |
|---|
| 135 | } else if (p.first == "MinCrit") { |
|---|
| 136 | crit.min.set(p.second); |
|---|
| 137 | } else if (p.first == "ShowAll") { |
|---|
| 138 | bShowAll = true; |
|---|
| 139 | } else if (p.first == "nsclient") { |
|---|
| 140 | bNSClient = true; |
|---|
| 141 | } else { |
|---|
| 142 | drives.push_back(p.first); |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | for (std::list<std::string>::iterator it = drives.begin();it!=drives.end();it++) { |
|---|
| 147 | std::string drive = (*it); |
|---|
| 148 | if (drive.length() == 1) |
|---|
| 149 | drive += ":"; |
|---|
| 150 | if (GetDriveType(drive.c_str()) != DRIVE_FIXED){ |
|---|
| 151 | message = "ERROR: Drive is not a fixed drive: " + drive; |
|---|
| 152 | return NSCAPI::returnUNKNOWN; |
|---|
| 153 | } |
|---|
| 154 | ULARGE_INTEGER freeBytesAvailableToCaller; |
|---|
| 155 | ULARGE_INTEGER totalNumberOfBytes; |
|---|
| 156 | ULARGE_INTEGER totalNumberOfFreeBytes; |
|---|
| 157 | if (!GetDiskFreeSpaceEx(drive.c_str(), &freeBytesAvailableToCaller, &totalNumberOfBytes, &totalNumberOfFreeBytes)) { |
|---|
| 158 | message = "ERROR: Could not get free space for" + drive; |
|---|
| 159 | return NSCAPI::returnUNKNOWN; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | if (bNSClient) { |
|---|
| 163 | message += strEx::itos(totalNumberOfFreeBytes.QuadPart) + "&"; |
|---|
| 164 | message += strEx::itos(totalNumberOfBytes.QuadPart) + "&"; |
|---|
| 165 | } else { |
|---|
| 166 | std::string tStr; |
|---|
| 167 | long long usedSpace = totalNumberOfBytes.QuadPart-totalNumberOfFreeBytes.QuadPart; |
|---|
| 168 | long long totalSpace = totalNumberOfBytes.QuadPart; |
|---|
| 169 | if (crit.max.hasBounds() && crit.max.checkMAX(usedSpace, totalSpace)) { |
|---|
| 170 | message += crit.max.prettyPrint(drive, usedSpace, totalSpace); |
|---|
| 171 | NSCHelper::escalteReturnCodeToCRIT(returnCode); |
|---|
| 172 | } else if (crit.min.hasBounds() && crit.min.checkMIN(usedSpace, totalSpace)) { |
|---|
| 173 | tStr = crit.min.prettyPrint(drive, usedSpace, totalSpace); |
|---|
| 174 | NSCHelper::escalteReturnCodeToCRIT(returnCode); |
|---|
| 175 | } else if (warn.max.hasBounds() && warn.max.checkMAX(usedSpace, totalSpace)) { |
|---|
| 176 | tStr = warn.max.prettyPrint(drive, usedSpace, totalSpace); |
|---|
| 177 | NSCHelper::escalteReturnCodeToWARN(returnCode); |
|---|
| 178 | } else if (warn.min.hasBounds() && warn.min.checkMIN(usedSpace, totalSpace)) { |
|---|
| 179 | tStr = warn.min.prettyPrint(drive, usedSpace, totalSpace); |
|---|
| 180 | NSCHelper::escalteReturnCodeToWARN(returnCode); |
|---|
| 181 | } else if (bShowAll) { |
|---|
| 182 | tStr = drive + ": " + strEx::itos_as_BKMG(usedSpace); |
|---|
| 183 | } |
|---|
| 184 | perf += checkHolders::SizeMaxMin::printPerf(drive, usedSpace, totalSpace, warn, crit); |
|---|
| 185 | if (!message.empty() && !tStr.empty()) |
|---|
| 186 | message += ", "; |
|---|
| 187 | if (!tStr.empty()) |
|---|
| 188 | message += tStr; |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | if (message.empty()) |
|---|
| 192 | message = "All drive sizes are within bounds."; |
|---|
| 193 | return returnCode; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | NSCAPI::nagiosReturn CheckDisk::CheckFileSize(const unsigned int argLen, char **char_args, std::string &message, std::string &perf) { |
|---|
| 197 | // CheckFileSize |
|---|
| 198 | // request: CheckFileSize <option> <option>... |
|---|
| 199 | // <option> MaxWarn=<size gmkb> |
|---|
| 200 | // MaxCrit=<size gmkb> |
|---|
| 201 | // MinWarn=<size gmkb> |
|---|
| 202 | // MinCrit=<size gmkb> |
|---|
| 203 | // ShowAll |
|---|
| 204 | // File=<path> |
|---|
| 205 | // File:<shortname>=<path> |
|---|
| 206 | // |
|---|
| 207 | // Return: <return state>&<return string>... |
|---|
| 208 | // <return state> 0 - No errors |
|---|
| 209 | // 1 - Unknown |
|---|
| 210 | // 2 - Errors |
|---|
| 211 | // <size gmkb> is a size with a possible modifier letter (such as G for gigabyte, M for Megabyte, K for kilobyte etc) |
|---|
| 212 | // Examples: |
|---|
| 213 | // <return string> <directory> <size gmkb> ... |<shortname>=<size>:<warn>:<crit> |
|---|
| 214 | // test: CheckFileSize ShowAll MaxWarn=1024M MaxCrit=4096M File:WIN=c:\WINDOWS\*.* |
|---|
| 215 | // CheckFileSize |
|---|
| 216 | // |
|---|
| 217 | // check_nscp -H <ip> -p <port> -s <passwd> -c <commandstring> |
|---|
| 218 | // |
|---|
| 219 | // ./check_nscp -H 192.168.0.167 -p 1234 -s pwd -c 'CheckFileSize&ShowAll&MaxWarn=1024M&MaxCrit=4096M&File:WIN=c:\WINDOWS\*.*' |
|---|
| 220 | // WIN: 1G (2110962363B)|WIN:2110962363:1073741824:4294967296 |
|---|
| 221 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 222 | std::list<std::string> args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 223 | if (args.empty()) { |
|---|
| 224 | message = "Missing argument(s)."; |
|---|
| 225 | return NSCAPI::returnCRIT; |
|---|
| 226 | } |
|---|
| 227 | long long maxWarn = 0; |
|---|
| 228 | long long maxCrit = 0; |
|---|
| 229 | long long minWarn = 0; |
|---|
| 230 | long long minCrit = 0; |
|---|
| 231 | bool bShowAll = false; |
|---|
| 232 | std::list<std::pair<std::string,std::string> > paths; |
|---|
| 233 | |
|---|
| 234 | std::list<std::string>::const_iterator cit; |
|---|
| 235 | for (cit=args.begin();cit!=args.end();++cit) { |
|---|
| 236 | std::string arg = *cit; |
|---|
| 237 | std::pair<std::string,std::string> p = strEx::split(arg,"="); |
|---|
| 238 | if (p.first == "File") { |
|---|
| 239 | paths.push_back(std::pair<std::string,std::string>("",p.second)); |
|---|
| 240 | } else if (p.first == "MaxWarn") { |
|---|
| 241 | maxWarn = strEx::stoi64_as_BKMG(p.second); |
|---|
| 242 | } else if (p.first == "MinWarn") { |
|---|
| 243 | minWarn = strEx::stoi64_as_BKMG(p.second); |
|---|
| 244 | } else if (p.first == "MaxCrit") { |
|---|
| 245 | maxCrit = strEx::stoi64_as_BKMG(p.second); |
|---|
| 246 | } else if (p.first == "MinCrit") { |
|---|
| 247 | minCrit = strEx::stoi64_as_BKMG(p.second); |
|---|
| 248 | } else if (p.first == "ShowAll") { |
|---|
| 249 | bShowAll = true; |
|---|
| 250 | } else if (p.first.find(":") != std::string::npos) { |
|---|
| 251 | std::pair<std::string,std::string> p2 = strEx::split(p.first,":"); |
|---|
| 252 | if (p2.first == "File") { |
|---|
| 253 | paths.push_back(std::pair<std::string,std::string>(p2.second,p.second)); |
|---|
| 254 | } else { |
|---|
| 255 | message = "Unknown command: " + p.first; |
|---|
| 256 | return NSCAPI::returnCRIT; |
|---|
| 257 | } |
|---|
| 258 | } else { |
|---|
| 259 | message = "Unknown command: " + p.first; |
|---|
| 260 | return NSCAPI::returnCRIT; |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | std::list<std::pair<std::string,std::string> >::const_iterator pit; |
|---|
| 265 | for (pit = paths.begin(); pit != paths.end(); ++pit) { |
|---|
| 266 | std::string tstr; |
|---|
| 267 | GetSize sizeFinder; |
|---|
| 268 | std::string sName = (*pit).first; |
|---|
| 269 | if (sName.empty()) |
|---|
| 270 | sName = (*pit).second; |
|---|
| 271 | RecursiveScanDirectory((*pit).second, sizeFinder); |
|---|
| 272 | |
|---|
| 273 | if ((maxCrit!=0) && (sizeFinder.getSize() > maxCrit)) { |
|---|
| 274 | tstr = sName + ": " + strEx::itos_as_BKMG(sizeFinder.getSize()); |
|---|
| 275 | returnCode = NSCAPI::returnCRIT; |
|---|
| 276 | } else if (sizeFinder.getSize() < minCrit) { |
|---|
| 277 | tstr = sName + ": " + strEx::itos_as_BKMG(sizeFinder.getSize()); |
|---|
| 278 | NSCHelper::escalteReturnCodeToCRIT(returnCode); |
|---|
| 279 | } else if ((maxWarn!=0)&&(sizeFinder.getSize() > maxWarn)) { |
|---|
| 280 | tstr = sName + ": " + strEx::itos_as_BKMG(sizeFinder.getSize()); |
|---|
| 281 | NSCHelper::escalteReturnCodeToWARN(returnCode); |
|---|
| 282 | } else if (sizeFinder.getSize() < minWarn) { |
|---|
| 283 | tstr = sName + ": " + strEx::itos_as_BKMG(sizeFinder.getSize()); |
|---|
| 284 | NSCHelper::escalteReturnCodeToWARN(returnCode); |
|---|
| 285 | } else if (bShowAll) { |
|---|
| 286 | tstr = sName + ": " + strEx::itos_as_BKMG(sizeFinder.getSize()); |
|---|
| 287 | } |
|---|
| 288 | if (!(*pit).first.empty()) |
|---|
| 289 | perf += (*pit).first + "=" + strEx::itos(sizeFinder.getSize()) + ";" + strEx::itos(maxWarn) + ";" + strEx::itos(maxCrit) + " "; |
|---|
| 290 | if (!message.empty() && !tstr.empty()) |
|---|
| 291 | message += ", "; |
|---|
| 292 | if (!tstr.empty()) |
|---|
| 293 | message += tstr; |
|---|
| 294 | } |
|---|
| 295 | if (message.empty()) |
|---|
| 296 | message = "OK all file sizes are within bounds."; |
|---|
| 297 | return returnCode; |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | #define BUFFER_SIZE 1024*64 |
|---|
| 302 | |
|---|
| 303 | NSCAPI::nagiosReturn CheckDisk::handleCommand(const std::string command, const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) { |
|---|
| 304 | if (command == "CheckFileSize") { |
|---|
| 305 | return CheckFileSize(argLen, char_args, msg, perf); |
|---|
| 306 | } else if (command == "CheckDriveSize") { |
|---|
| 307 | return CheckDriveSize(argLen, char_args, msg, perf); |
|---|
| 308 | |
|---|
| 309 | // } else if (command == "CheckFileDate") { |
|---|
| 310 | } |
|---|
| 311 | return NSCAPI::returnIgnored; |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | NSC_WRAPPERS_MAIN_DEF(gCheckDisk); |
|---|
| 316 | NSC_WRAPPERS_IGNORE_MSG_DEF(); |
|---|
| 317 | NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckDisk); |
|---|