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