| 1 | // NSClientCompat.cpp : Defines the entry point for the DLL application. |
|---|
| 2 | // |
|---|
| 3 | |
|---|
| 4 | #include "stdafx.h" |
|---|
| 5 | #include "CheckSystem.h" |
|---|
| 6 | #include <utils.h> |
|---|
| 7 | #include <tlhelp32.h> |
|---|
| 8 | #include <EnumNtSrv.h> |
|---|
| 9 | #include <EnumProcess.h> |
|---|
| 10 | |
|---|
| 11 | CheckSystem gNSClientCompat; |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * DLL Entry point |
|---|
| 15 | * @param hModule |
|---|
| 16 | * @param ul_reason_for_call |
|---|
| 17 | * @param lpReserved |
|---|
| 18 | * @return |
|---|
| 19 | */ |
|---|
| 20 | BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) |
|---|
| 21 | { |
|---|
| 22 | NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call); |
|---|
| 23 | return TRUE; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * Default c-tor |
|---|
| 28 | * @return |
|---|
| 29 | */ |
|---|
| 30 | CheckSystem::CheckSystem() : processMethod_(0) {} |
|---|
| 31 | /** |
|---|
| 32 | * Default d-tor |
|---|
| 33 | * @return |
|---|
| 34 | */ |
|---|
| 35 | CheckSystem::~CheckSystem() {} |
|---|
| 36 | /** |
|---|
| 37 | * Load (initiate) module. |
|---|
| 38 | * Start the background collector thread and let it run until unloadModule() is called. |
|---|
| 39 | * @return true |
|---|
| 40 | */ |
|---|
| 41 | bool CheckSystem::loadModule() { |
|---|
| 42 | pdhThread.createThread(); |
|---|
| 43 | |
|---|
| 44 | std::string wantedMethod = NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_ENUMPROC_METHOD, C_SYSTEM_ENUMPROC_METHOD_DEFAULT); |
|---|
| 45 | |
|---|
| 46 | CEnumProcess tmp; |
|---|
| 47 | int method = tmp.GetAvailableMethods(); |
|---|
| 48 | |
|---|
| 49 | if (wantedMethod == C_SYSTEM_ENUMPROC_METHOD_PSAPI) { |
|---|
| 50 | NSC_DEBUG_MSG_STD("Using PSAPI method."); |
|---|
| 51 | if (method == (method|ENUM_METHOD::PSAPI)) { |
|---|
| 52 | processMethod_ = ENUM_METHOD::PSAPI; |
|---|
| 53 | } else { |
|---|
| 54 | NSC_LOG_ERROR_STD("PSAPI method not available, check " C_SYSTEM_ENUMPROC_METHOD " option."); |
|---|
| 55 | } |
|---|
| 56 | } else { |
|---|
| 57 | NSC_DEBUG_MSG_STD("Using TOOLHELP method."); |
|---|
| 58 | if (method == (method|ENUM_METHOD::TOOLHELP)) { |
|---|
| 59 | processMethod_ = ENUM_METHOD::TOOLHELP; |
|---|
| 60 | } else { |
|---|
| 61 | NSC_LOG_ERROR_STD("TOOLHELP method not avalible, check " C_SYSTEM_ENUMPROC_METHOD " option."); |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | return true; |
|---|
| 65 | } |
|---|
| 66 | /** |
|---|
| 67 | * Unload (terminate) module. |
|---|
| 68 | * Attempt to stop the background processing thread. |
|---|
| 69 | * @return true if successfully, false if not (if not things might be bad) |
|---|
| 70 | */ |
|---|
| 71 | bool CheckSystem::unloadModule() { |
|---|
| 72 | if (!pdhThread.exitThread(20000)) { |
|---|
| 73 | std::cout << "MAJOR ERROR: Could not unload thread..." << std::endl; |
|---|
| 74 | NSC_LOG_ERROR("Could not exit the thread, memory leak and potential corruption may be the result..."); |
|---|
| 75 | } |
|---|
| 76 | return true; |
|---|
| 77 | } |
|---|
| 78 | /** |
|---|
| 79 | * Return the module name. |
|---|
| 80 | * @return The module name |
|---|
| 81 | */ |
|---|
| 82 | std::string CheckSystem::getModuleName() { |
|---|
| 83 | return "System Checks Module."; |
|---|
| 84 | } |
|---|
| 85 | /** |
|---|
| 86 | * Module version |
|---|
| 87 | * @return module version |
|---|
| 88 | */ |
|---|
| 89 | NSCModuleWrapper::module_version CheckSystem::getModuleVersion() { |
|---|
| 90 | NSCModuleWrapper::module_version version = {0, 3, 0 }; |
|---|
| 91 | return version; |
|---|
| 92 | } |
|---|
| 93 | /** |
|---|
| 94 | * Check if we have a command handler. |
|---|
| 95 | * @return true (as we have a command handler) |
|---|
| 96 | */ |
|---|
| 97 | bool CheckSystem::hasCommandHandler() { |
|---|
| 98 | return true; |
|---|
| 99 | } |
|---|
| 100 | /** |
|---|
| 101 | * Check if we have a message handler. |
|---|
| 102 | * @return false as we have no message handler |
|---|
| 103 | */ |
|---|
| 104 | bool CheckSystem::hasMessageHandler() { |
|---|
| 105 | return false; |
|---|
| 106 | } |
|---|
| 107 | /* |
|---|
| 108 | */ |
|---|
| 109 | /** |
|---|
| 110 | * Main command parser and delegator. |
|---|
| 111 | * This also handles a lot of the simpler responses (though some are deferred to other helper functions) |
|---|
| 112 | * |
|---|
| 113 | * |
|---|
| 114 | * @param command |
|---|
| 115 | * @param argLen |
|---|
| 116 | * @param **args |
|---|
| 117 | * @return |
|---|
| 118 | */ |
|---|
| 119 | NSCAPI::nagiosReturn CheckSystem::handleCommand(const strEx::blindstr command, const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) { |
|---|
| 120 | std::list<std::string> stl_args; |
|---|
| 121 | CheckSystem::returnBundle rb; |
|---|
| 122 | if (command == "checkCPU") { |
|---|
| 123 | return checkCPU(argLen, char_args, msg, perf); |
|---|
| 124 | } else if (command == "checkUpTime") { |
|---|
| 125 | return checkUpTime(argLen, char_args, msg, perf); |
|---|
| 126 | } else if (command == "checkServiceState") { |
|---|
| 127 | return checkServiceState(argLen, char_args, msg, perf); |
|---|
| 128 | } else if (command == "checkProcState") { |
|---|
| 129 | return checkProcState(argLen, char_args, msg, perf); |
|---|
| 130 | } else if (command == "checkMem") { |
|---|
| 131 | return checkMem(argLen, char_args, msg, perf); |
|---|
| 132 | } else if (command == "checkCounter") { |
|---|
| 133 | return checkCounter(argLen, char_args, msg, perf); |
|---|
| 134 | } |
|---|
| 135 | return NSCAPI::returnIgnored; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | // checkCPU warn=80 crit=90 time=20m time=10s time=4 |
|---|
| 139 | // checkCPU warn=80 crit=90 time=20m time=10s time=4 showAll |
|---|
| 140 | // checkCPU 20 10 4 nsclient |
|---|
| 141 | NSCAPI::nagiosReturn CheckSystem::checkCPU(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) |
|---|
| 142 | { |
|---|
| 143 | std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 144 | if (stl_args.empty()) { |
|---|
| 145 | msg = "ERROR: Missing argument exception."; |
|---|
| 146 | return NSCAPI::returnUNKNOWN; |
|---|
| 147 | } |
|---|
| 148 | int warn; |
|---|
| 149 | int crit; |
|---|
| 150 | std::list<std::string> times; |
|---|
| 151 | bool bNSCLientCompatible = false; |
|---|
| 152 | bool bShowAll = false; |
|---|
| 153 | NSCAPI::nagiosReturn ret = NSCAPI::returnOK; |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | for (arrayBuffer::arrayList::const_iterator it = stl_args.begin(); it != stl_args.end(); ++it) { |
|---|
| 157 | strEx::token t = strEx::getToken((*it), '='); |
|---|
| 158 | if (t.first == "crit") |
|---|
| 159 | crit = strEx::stoi(t.second); |
|---|
| 160 | else if (t.first == "warn") |
|---|
| 161 | warn = strEx::stoi(t.second); |
|---|
| 162 | else if (t.first == "time") |
|---|
| 163 | times.push_back(t.second); |
|---|
| 164 | else if (t.first == NSCLIENT) |
|---|
| 165 | bNSCLientCompatible = true; |
|---|
| 166 | else if (t.first == "showAll") |
|---|
| 167 | bShowAll = true; |
|---|
| 168 | else |
|---|
| 169 | times.push_back(t.first); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | for (std::list<std::string>::iterator it = times.begin(); it != times.end(); ++it) { |
|---|
| 173 | PDHCollector *pObject = pdhThread.getThread(); |
|---|
| 174 | if (!pObject) { |
|---|
| 175 | msg = "ERROR: PDH Collection thread not running."; |
|---|
| 176 | return NSCAPI::returnUNKNOWN; |
|---|
| 177 | } |
|---|
| 178 | if (bNSCLientCompatible) { |
|---|
| 179 | int v = pObject->getCPUAvrage((*it) + "m"); |
|---|
| 180 | if (v == -1) { |
|---|
| 181 | msg = "ERROR: We don't collect data this far back: " + (*it); |
|---|
| 182 | return NSCAPI::returnUNKNOWN; |
|---|
| 183 | } |
|---|
| 184 | if (!msg.empty()) msg += "&"; |
|---|
| 185 | msg += strEx::itos(v); |
|---|
| 186 | } else { |
|---|
| 187 | int v = pObject->getCPUAvrage((*it)); |
|---|
| 188 | if (v == -1) { |
|---|
| 189 | msg = "ERROR: We don't collect data this far back: " + (*it); |
|---|
| 190 | return NSCAPI::returnUNKNOWN; |
|---|
| 191 | } else { |
|---|
| 192 | if (v > warn) { |
|---|
| 193 | NSCHelper::escalteReturnCodeToWARN(ret); |
|---|
| 194 | msg += strEx::itos(v) + "% > " + strEx::itos(warn) + " " + (*it); |
|---|
| 195 | } if (v > crit) { |
|---|
| 196 | NSCHelper::escalteReturnCodeToCRIT(ret); |
|---|
| 197 | msg += strEx::itos(v) + "% > " + strEx::itos(crit) + " " + (*it); |
|---|
| 198 | } else if (bShowAll) { |
|---|
| 199 | msg += strEx::itos(v) + "% "; |
|---|
| 200 | } |
|---|
| 201 | perf += "'" + (*it) + " average'=" + strEx::itos(v) + "%;" + strEx::itos(warn) + ";" + strEx::itos(crit) + "; "; |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | if (bNSCLientCompatible) { |
|---|
| 206 | // Don't prefix/postfix the output for NSClient |
|---|
| 207 | } else if (msg.empty()) { |
|---|
| 208 | msg = "CPU Load ok."; |
|---|
| 209 | } else { |
|---|
| 210 | msg = "CPU Load: " + msg; |
|---|
| 211 | } |
|---|
| 212 | return ret; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | // checkUpTime crit=1d warn=6h |
|---|
| 216 | // checkUpTime nsclient |
|---|
| 217 | NSCAPI::nagiosReturn CheckSystem::checkUpTime(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) |
|---|
| 218 | { |
|---|
| 219 | std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 220 | if (stl_args.empty()) { |
|---|
| 221 | msg = "ERROR: Missing argument exception."; |
|---|
| 222 | return NSCAPI::returnUNKNOWN; |
|---|
| 223 | } |
|---|
| 224 | unsigned long long warn; |
|---|
| 225 | unsigned long long crit; |
|---|
| 226 | bool bNSCLientCompatible = false; |
|---|
| 227 | |
|---|
| 228 | for (arrayBuffer::arrayList::const_iterator it = stl_args.begin(); it != stl_args.end(); ++it) { |
|---|
| 229 | strEx::token t = strEx::getToken((*it), '='); |
|---|
| 230 | if (t.first == "crit") |
|---|
| 231 | crit = strEx::stoi64_as_time(t.second); |
|---|
| 232 | else if (t.first == "warn") |
|---|
| 233 | warn = strEx::stoi64_as_time(t.second); |
|---|
| 234 | else if (t.first == NSCLIENT) |
|---|
| 235 | bNSCLientCompatible = true; |
|---|
| 236 | else { |
|---|
| 237 | msg = "Invalid argument: " + t.first; |
|---|
| 238 | return NSCAPI::returnUNKNOWN; |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | PDHCollector *pObject = pdhThread.getThread(); |
|---|
| 242 | if (!pObject) { |
|---|
| 243 | msg = "ERROR: PDH Collection thread not running."; |
|---|
| 244 | return NSCAPI::returnUNKNOWN; |
|---|
| 245 | } |
|---|
| 246 | unsigned long long uptime = pObject->getUptime(); |
|---|
| 247 | if (bNSCLientCompatible) { |
|---|
| 248 | msg = strEx::itos(uptime); |
|---|
| 249 | return NSCAPI::returnOK; |
|---|
| 250 | } else { |
|---|
| 251 | uptime *= 1000; |
|---|
| 252 | if (uptime < crit) { |
|---|
| 253 | msg = "Client has uptime (" + strEx::itos_as_time(uptime) + ") < critical (" + strEx::itos_as_time(crit) + ")"; |
|---|
| 254 | return NSCAPI::returnCRIT; |
|---|
| 255 | } |
|---|
| 256 | if (uptime < warn) { |
|---|
| 257 | msg = "Client has uptime (" + strEx::itos_as_time(uptime) + ") < warning (" + strEx::itos_as_time(warn) + ")"; |
|---|
| 258 | return NSCAPI::returnWARN; |
|---|
| 259 | } |
|---|
| 260 | } |
|---|
| 261 | return NSCAPI::returnOK; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | /** |
|---|
| 268 | * Retrieve the service state of one or more services (by name). |
|---|
| 269 | * Parse a list with a service names and verify that all named services are running. |
|---|
| 270 | * <pre> |
|---|
| 271 | * Syntax: |
|---|
| 272 | * request: checkServiceState <option> [<option> [...]] |
|---|
| 273 | * Return: <return state>&<service1> : <state1> - <service2> : <state2> - ... |
|---|
| 274 | * Available options: |
|---|
| 275 | * <name>=<state> Check if a service has a specific state |
|---|
| 276 | * State can be wither started or stopped |
|---|
| 277 | * ShowAll Show the state of all listed service. If not set only critical services are listed. |
|---|
| 278 | * Examples: |
|---|
| 279 | * checkServiceState showAll myService MyService |
|---|
| 280 | *</pre> |
|---|
| 281 | * |
|---|
| 282 | * @param command Command to execute |
|---|
| 283 | * @param argLen The length of the argument buffer |
|---|
| 284 | * @param **char_args The argument buffer |
|---|
| 285 | * @param &msg String to put message in |
|---|
| 286 | * @param &perf String to put performance data in |
|---|
| 287 | * @return The status of the command |
|---|
| 288 | */ |
|---|
| 289 | NSCAPI::nagiosReturn CheckSystem::checkServiceState(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) |
|---|
| 290 | { |
|---|
| 291 | std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 292 | if (stl_args.empty()) { |
|---|
| 293 | msg = "ERROR: Missing argument exception."; |
|---|
| 294 | return NSCAPI::returnUNKNOWN; |
|---|
| 295 | } |
|---|
| 296 | std::list<std::pair<std::string,states> > services; |
|---|
| 297 | NSCAPI::nagiosReturn ret = NSCAPI::returnOK; |
|---|
| 298 | bool bShowAll = false; |
|---|
| 299 | |
|---|
| 300 | for (arrayBuffer::arrayList::const_iterator it = stl_args.begin(); it != stl_args.end(); ++it) { |
|---|
| 301 | strEx::token t = strEx::getToken((*it), '='); |
|---|
| 302 | if (t.first == SHOW_ALL) |
|---|
| 303 | bShowAll = true; |
|---|
| 304 | else if (t.first == SHOW_FAIL) { |
|---|
| 305 | bShowAll = false; |
|---|
| 306 | } else { |
|---|
| 307 | if (t.second.empty()) |
|---|
| 308 | services.push_back(std::pair<std::string,states>(t.first, started)); |
|---|
| 309 | else { |
|---|
| 310 | if (t.second == "started") |
|---|
| 311 | services.push_back(std::pair<std::string,states>(t.first, started)); |
|---|
| 312 | else |
|---|
| 313 | services.push_back(std::pair<std::string,states>(t.first, stopped)); |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | } |
|---|
| 317 | for (std::list<std::pair<std::string,states> >::iterator it = services.begin(); it != services.end(); ++it) { |
|---|
| 318 | TNtServiceInfo info = TNtServiceInfo::GetService((*it).first.c_str()); |
|---|
| 319 | std::string tmp; |
|---|
| 320 | if ( (info.m_dwCurrentState == SERVICE_RUNNING) && ((*it).second == started) ) { |
|---|
| 321 | if (bShowAll) |
|---|
| 322 | tmp = info.m_strServiceName + " : Started"; |
|---|
| 323 | } else if ( (info.m_dwCurrentState == SERVICE_STOPPED) && ((*it).second == stopped) ) { |
|---|
| 324 | if (bShowAll) |
|---|
| 325 | tmp = info.m_strServiceName + " : Stopped"; |
|---|
| 326 | } else if ((info.m_dwCurrentState == SERVICE_STOPPED) && ((*it).second == started) ) { |
|---|
| 327 | NSCHelper::escalteReturnCodeToCRIT(ret); |
|---|
| 328 | tmp = info.m_strServiceName + " : Stopped"; |
|---|
| 329 | } else if ((info.m_dwCurrentState == SERVICE_RUNNING) && ((*it).second == stopped) ) { |
|---|
| 330 | NSCHelper::escalteReturnCodeToCRIT(ret); |
|---|
| 331 | tmp = info.m_strServiceName + " : Started"; |
|---|
| 332 | } else { |
|---|
| 333 | NSCHelper::escalteReturnCodeToWARN(ret); |
|---|
| 334 | tmp = info.m_strServiceName + " : Unknown"; |
|---|
| 335 | } |
|---|
| 336 | if (!msg.empty()&&!tmp.empty()) |
|---|
| 337 | msg += " - "; |
|---|
| 338 | msg += tmp; |
|---|
| 339 | } |
|---|
| 340 | if (msg.empty()) |
|---|
| 341 | msg ="All services ok."; |
|---|
| 342 | return ret; |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | /** |
|---|
| 347 | * Check availible memory and return various check results |
|---|
| 348 | * Example: checkMem showAll maxWarn=50 maxCrit=75 |
|---|
| 349 | * |
|---|
| 350 | * @param command Command to execute |
|---|
| 351 | * @param argLen The length of the argument buffer |
|---|
| 352 | * @param **char_args The argument buffer |
|---|
| 353 | * @param &msg String to put message in |
|---|
| 354 | * @param &perf String to put performance data in |
|---|
| 355 | * @return The status of the command |
|---|
| 356 | */ |
|---|
| 357 | NSCAPI::nagiosReturn CheckSystem::checkMem(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) |
|---|
| 358 | { |
|---|
| 359 | std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 360 | if (stl_args.empty()) { |
|---|
| 361 | msg = "ERROR: Missing argument exception."; |
|---|
| 362 | return NSCAPI::returnUNKNOWN; |
|---|
| 363 | } |
|---|
| 364 | std::list<std::pair<std::string,states> > services; |
|---|
| 365 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 366 | bool bShowAll = false; |
|---|
| 367 | bool bNSCLientCompatible = false; |
|---|
| 368 | |
|---|
| 369 | checkHolders::SizeMaxMinPercentage<> warn; |
|---|
| 370 | checkHolders::SizeMaxMinPercentage<> crit; |
|---|
| 371 | |
|---|
| 372 | for (arrayBuffer::arrayList::const_iterator it = stl_args.begin(); it != stl_args.end(); ++it) { |
|---|
| 373 | strEx::token t = strEx::getToken((*it), '='); |
|---|
| 374 | if (t.first == SHOW_ALL) |
|---|
| 375 | bShowAll = true; |
|---|
| 376 | else if (t.first == "MaxWarn") { |
|---|
| 377 | warn.max.set(t.second); |
|---|
| 378 | } else if (t.first == "MinWarn") { |
|---|
| 379 | warn.min.set(t.second); |
|---|
| 380 | } else if (t.first == "MaxCrit") { |
|---|
| 381 | crit.max.set(t.second); |
|---|
| 382 | } else if (t.first == "MinCrit") { |
|---|
| 383 | crit.min.set(t.second); |
|---|
| 384 | } else if (t.first == NSCLIENT) |
|---|
| 385 | bNSCLientCompatible = true; |
|---|
| 386 | else { |
|---|
| 387 | msg = "Invalid argument: " + t.first; |
|---|
| 388 | return NSCAPI::returnUNKNOWN; |
|---|
| 389 | } |
|---|
| 390 | } |
|---|
| 391 | |
|---|
| 392 | PDHCollector *pObject = pdhThread.getThread(); |
|---|
| 393 | if (!pObject) { |
|---|
| 394 | msg = "ERROR: PDH Collection thread not running."; |
|---|
| 395 | return NSCAPI::returnUNKNOWN; |
|---|
| 396 | } |
|---|
| 397 | long long pageCommit = pObject->getMemCommit(); |
|---|
| 398 | long long pageCommitLimit = pObject->getMemCommitLimit(); |
|---|
| 399 | if (bNSCLientCompatible) { |
|---|
| 400 | msg = strEx::itos(pageCommitLimit) + "&" + strEx::itos(pageCommit); |
|---|
| 401 | return NSCAPI::returnOK; |
|---|
| 402 | } else { |
|---|
| 403 | std::string tStr; |
|---|
| 404 | if (crit.max.hasBounds() && crit.max.checkMAX(pageCommit, pageCommitLimit)) { |
|---|
| 405 | tStr = crit.max.prettyPrint("page", pageCommit, pageCommitLimit) + " > critical"; |
|---|
| 406 | NSCHelper::escalteReturnCodeToCRIT(returnCode); |
|---|
| 407 | } else if (crit.min.hasBounds() && crit.min.checkMIN(pageCommit, pageCommitLimit)) { |
|---|
| 408 | tStr = crit.min.prettyPrint("page", pageCommit, pageCommitLimit) + " < critical"; |
|---|
| 409 | NSCHelper::escalteReturnCodeToCRIT(returnCode); |
|---|
| 410 | } else if (warn.max.hasBounds() && warn.max.checkMAX(pageCommit, pageCommitLimit)) { |
|---|
| 411 | tStr = warn.max.prettyPrint("page", pageCommit, pageCommitLimit) + " > warning"; |
|---|
| 412 | NSCHelper::escalteReturnCodeToWARN(returnCode); |
|---|
| 413 | } else if (warn.min.hasBounds() && warn.min.checkMIN(pageCommit, pageCommitLimit)) { |
|---|
| 414 | tStr = warn.min.prettyPrint("page", pageCommit, pageCommitLimit) + " < warning"; |
|---|
| 415 | NSCHelper::escalteReturnCodeToWARN(returnCode); |
|---|
| 416 | } else if (bShowAll) { |
|---|
| 417 | tStr = "page: " + strEx::itos_as_BKMG(pageCommit); |
|---|
| 418 | } |
|---|
| 419 | perf += checkHolders::SizeMaxMinPercentage<>::printPerf("page", pageCommit, pageCommitLimit, warn, crit); |
|---|
| 420 | msg += tStr; |
|---|
| 421 | } |
|---|
| 422 | if (msg.empty()) |
|---|
| 423 | msg = "OK memory within bounds."; |
|---|
| 424 | else |
|---|
| 425 | msg = NSCHelper::translateReturn(returnCode) + ": " + msg; |
|---|
| 426 | return returnCode; |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | typedef std::hash_map<std::string,DWORD> NSPROCLST; |
|---|
| 430 | /** |
|---|
| 431 | * Get a hash_map with all running processes. |
|---|
| 432 | * @return a hash_map with all running processes |
|---|
| 433 | */ |
|---|
| 434 | NSPROCLST GetProcessList(int processMethod) |
|---|
| 435 | { |
|---|
| 436 | NSPROCLST ret; |
|---|
| 437 | if (processMethod == 0) { |
|---|
| 438 | NSC_LOG_ERROR_STD("ProcessMethod not defined or not available."); |
|---|
| 439 | return ret; |
|---|
| 440 | } |
|---|
| 441 | CEnumProcess enumeration; |
|---|
| 442 | enumeration.SetMethod(processMethod); |
|---|
| 443 | CEnumProcess::CProcessEntry entry; |
|---|
| 444 | for (BOOL OK = enumeration.GetProcessFirst(&entry); OK; OK = enumeration.GetProcessNext(&entry) ) { |
|---|
| 445 | ret[entry.lpFilename] = entry.dwPID; |
|---|
| 446 | } |
|---|
| 447 | return ret; |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | /** |
|---|
| 451 | * Check process state and return result |
|---|
| 452 | * |
|---|
| 453 | * @param command Command to execute |
|---|
| 454 | * @param argLen The length of the argument buffer |
|---|
| 455 | * @param **char_args The argument buffer |
|---|
| 456 | * @param &msg String to put message in |
|---|
| 457 | * @param &perf String to put performance data in |
|---|
| 458 | * @return The status of the command |
|---|
| 459 | */ |
|---|
| 460 | NSCAPI::nagiosReturn CheckSystem::checkProcState(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) |
|---|
| 461 | { |
|---|
| 462 | std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 463 | if (stl_args.empty()) { |
|---|
| 464 | msg = "ERROR: Missing argument exception."; |
|---|
| 465 | return NSCAPI::returnUNKNOWN; |
|---|
| 466 | } |
|---|
| 467 | std::list<std::pair<std::string,states> > procs; |
|---|
| 468 | NSCAPI::nagiosReturn ret = NSCAPI::returnOK; |
|---|
| 469 | bool bShowAll = false; |
|---|
| 470 | |
|---|
| 471 | for (arrayBuffer::arrayList::const_iterator it = stl_args.begin(); it != stl_args.end(); ++it) { |
|---|
| 472 | strEx::token t = strEx::getToken((*it), '='); |
|---|
| 473 | if (t.first == SHOW_ALL) |
|---|
| 474 | bShowAll = true; |
|---|
| 475 | else if (t.first == SHOW_FAIL) { |
|---|
| 476 | bShowAll = false; |
|---|
| 477 | } else { |
|---|
| 478 | if (t.second.empty()) |
|---|
| 479 | procs.push_back(std::pair<std::string,states>(t.first, started)); |
|---|
| 480 | else { |
|---|
| 481 | if (t.second == "started") |
|---|
| 482 | procs.push_back(std::pair<std::string,states>(t.first, started)); |
|---|
| 483 | else |
|---|
| 484 | procs.push_back(std::pair<std::string,states>(t.first, stopped)); |
|---|
| 485 | } |
|---|
| 486 | } |
|---|
| 487 | } |
|---|
| 488 | NSPROCLST runningProcs; |
|---|
| 489 | try { |
|---|
| 490 | runningProcs = GetProcessList(processMethod_); |
|---|
| 491 | } catch (char *c) { |
|---|
| 492 | NSC_LOG_ERROR_STD("ERROR: " + c); |
|---|
| 493 | msg = static_cast<std::string>("ERROR: ") + c; |
|---|
| 494 | return NSCAPI::returnCRIT; |
|---|
| 495 | } |
|---|
| 496 | |
|---|
| 497 | for (std::list<std::pair<std::string,states> >::iterator it = procs.begin(); it != procs.end(); ++it) { |
|---|
| 498 | NSPROCLST::iterator proc = runningProcs.find((*it).first); |
|---|
| 499 | bool bFound = proc != runningProcs.end(); |
|---|
| 500 | std::string tmp; |
|---|
| 501 | if ( (bFound) && ((*it).second == started) ) { |
|---|
| 502 | if (bShowAll) |
|---|
| 503 | tmp = (*it).first + " : Running"; |
|---|
| 504 | } else if ( (!bFound) && ((*it).second == stopped) ) { |
|---|
| 505 | if (bShowAll) |
|---|
| 506 | tmp = (*it).first + " : Stopped"; |
|---|
| 507 | } else if ( (!bFound) && ((*it).second == started) ) { |
|---|
| 508 | NSCHelper::escalteReturnCodeToCRIT(ret); |
|---|
| 509 | tmp = (*it).first + " : Stopped"; |
|---|
| 510 | } else if ( (bFound) && ((*it).second == stopped) ) { |
|---|
| 511 | NSCHelper::escalteReturnCodeToCRIT(ret); |
|---|
| 512 | tmp = (*it).first + " : Running"; |
|---|
| 513 | } |
|---|
| 514 | if (!msg.empty()&&!tmp.empty()) |
|---|
| 515 | msg += " - "; |
|---|
| 516 | msg += tmp; |
|---|
| 517 | } |
|---|
| 518 | if (msg.empty()) |
|---|
| 519 | msg ="All processes ok."; |
|---|
| 520 | return ret; |
|---|
| 521 | } |
|---|
| 522 | |
|---|
| 523 | /** |
|---|
| 524 | * Check a counter and return the value |
|---|
| 525 | * |
|---|
| 526 | * @param command Command to execute |
|---|
| 527 | * @param argLen The length of the argument buffer |
|---|
| 528 | * @param **char_args The argument buffer |
|---|
| 529 | * @param &msg String to put message in |
|---|
| 530 | * @param &perf String to put performance data in |
|---|
| 531 | * @return The status of the command |
|---|
| 532 | * |
|---|
| 533 | * @todo add parsing support for NRPE |
|---|
| 534 | */ |
|---|
| 535 | NSCAPI::nagiosReturn CheckSystem::checkCounter(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) |
|---|
| 536 | { |
|---|
| 537 | std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 538 | if (stl_args.empty()) { |
|---|
| 539 | msg = "ERROR: Missing argument exception."; |
|---|
| 540 | return NSCAPI::returnUNKNOWN; |
|---|
| 541 | } |
|---|
| 542 | std::list<std::pair<std::string,std::string> > counters; |
|---|
| 543 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 544 | bool bShowAll = false; |
|---|
| 545 | bool bNSCLientCompatible = false; |
|---|
| 546 | |
|---|
| 547 | checkHolders::SizeMaxMin<__int64, checkHolders::int64_handler<> > warn; |
|---|
| 548 | checkHolders::SizeMaxMin<__int64, checkHolders::int64_handler<> > crit; |
|---|
| 549 | |
|---|
| 550 | for (arrayBuffer::arrayList::const_iterator it = stl_args.begin(); it != stl_args.end(); ++it) { |
|---|
| 551 | strEx::token t = strEx::getToken((*it), '='); |
|---|
| 552 | if (t.first == SHOW_ALL) |
|---|
| 553 | bShowAll = true; |
|---|
| 554 | else if (t.first == SHOW_FAIL) { |
|---|
| 555 | bShowAll = false; |
|---|
| 556 | } else if (t.first == "MaxWarn") { |
|---|
| 557 | warn.max.set(t.second); |
|---|
| 558 | } else if (t.first == "MinWarn") { |
|---|
| 559 | warn.min.set(t.second); |
|---|
| 560 | } else if (t.first == "MaxCrit") { |
|---|
| 561 | crit.max.set(t.second); |
|---|
| 562 | } else if (t.first == "MinCrit") { |
|---|
| 563 | crit.min.set(t.second); |
|---|
| 564 | } else if (t.first == NSCLIENT) { |
|---|
| 565 | bNSCLientCompatible = true; |
|---|
| 566 | } else if (t.first == "Counter") { |
|---|
| 567 | counters.push_back(std::pair<std::string,std::string>("",t.second)); |
|---|
| 568 | } else if (t.first.find(":") != std::string::npos) { |
|---|
| 569 | std::pair<std::string,std::string> t2 = strEx::split(t.first,":"); |
|---|
| 570 | if (t2.first == "Counter") { |
|---|
| 571 | counters.push_back(std::pair<std::string,std::string>(t2.second,t.second)); |
|---|
| 572 | } else { |
|---|
| 573 | msg = "Unknown command: " + t.first; |
|---|
| 574 | return NSCAPI::returnUNKNOWN; |
|---|
| 575 | } |
|---|
| 576 | } else { |
|---|
| 577 | counters.push_back(std::pair<std::string,std::string>("",t.first)); |
|---|
| 578 | } |
|---|
| 579 | } |
|---|
| 580 | |
|---|
| 581 | for (std::list<std::pair<std::string,std::string> >::iterator it = counters.begin(); it != counters.end(); ++it) { |
|---|
| 582 | std::string name; |
|---|
| 583 | try { |
|---|
| 584 | PDH::PDHQuery pdh; |
|---|
| 585 | PDHCollectors::StaticPDHCounterListener counter; |
|---|
| 586 | std::string name = (*it).first; |
|---|
| 587 | if (name.empty()) |
|---|
| 588 | name = (*it).second; |
|---|
| 589 | pdh.addCounter((*it).second, &counter); |
|---|
| 590 | pdh.open(); |
|---|
| 591 | pdh.collect(); |
|---|
| 592 | if (bNSCLientCompatible) { |
|---|
| 593 | msg += strEx::itos(counter.getValue()); |
|---|
| 594 | } else { |
|---|
| 595 | std::string tStr; |
|---|
| 596 | if (crit.max.hasBounds() && crit.max.checkMAX(counter.getValue())) { |
|---|
| 597 | tStr = crit.max.prettyPrint(name, counter.getValue()) + " > critical"; |
|---|
| 598 | NSCHelper::escalteReturnCodeToCRIT(returnCode); |
|---|
| 599 | } else if (crit.min.hasBounds() && crit.min.checkMIN(counter.getValue())) { |
|---|
| 600 | tStr = crit.min.prettyPrint(name, counter.getValue()) + " < critical"; |
|---|
| 601 | NSCHelper::escalteReturnCodeToCRIT(returnCode); |
|---|
| 602 | } else if (warn.max.hasBounds() && warn.max.checkMAX(counter.getValue())) { |
|---|
| 603 | tStr = warn.max.prettyPrint(name, counter.getValue()) + " > warning"; |
|---|
| 604 | NSCHelper::escalteReturnCodeToWARN(returnCode); |
|---|
| 605 | } else if (warn.min.hasBounds() && warn.min.checkMIN(counter.getValue())) { |
|---|
| 606 | tStr = warn.min.prettyPrint(name, counter.getValue()) + " < warning"; |
|---|
| 607 | NSCHelper::escalteReturnCodeToWARN(returnCode); |
|---|
| 608 | } else if (bShowAll) { |
|---|
| 609 | tStr = name + ": " + strEx::itos(counter.getValue()); |
|---|
| 610 | } |
|---|
| 611 | perf += checkHolders::SizeMaxMin<__int64, checkHolders::int64_handler<> >::printPerf(name, counter.getValue(), warn, crit); |
|---|
| 612 | msg += tStr; |
|---|
| 613 | } |
|---|
| 614 | pdh.close(); |
|---|
| 615 | } catch (const PDH::PDHException &e) { |
|---|
| 616 | NSC_LOG_ERROR_STD("ERROR: " + e.str_ + " (" + name + ")"); |
|---|
| 617 | msg = static_cast<std::string>("ERROR: ") + e.str_; |
|---|
| 618 | return 0; |
|---|
| 619 | } |
|---|
| 620 | } |
|---|
| 621 | if (msg.empty()) |
|---|
| 622 | msg = "OK all counters within bounds."; |
|---|
| 623 | else |
|---|
| 624 | msg = NSCHelper::translateReturn(returnCode) + ": " + msg; |
|---|
| 625 | return returnCode; |
|---|
| 626 | } |
|---|
| 627 | NSC_WRAPPERS_MAIN_DEF(gNSClientCompat); |
|---|
| 628 | NSC_WRAPPERS_IGNORE_MSG_DEF(); |
|---|
| 629 | NSC_WRAPPERS_HANDLE_CMD_DEF(gNSClientCompat); |
|---|