Changeset e26cfe0 in nscp for modules/CheckSystem/CheckSystem.cpp
- Timestamp:
- 07/11/05 21:55:28 (8 years ago)
- Branches:
- master, 0.4.0, 0.4.1, 0.4.2, stable
- Children:
- 75d5e70
- Parents:
- 6654022
- File:
-
- 1 edited
-
modules/CheckSystem/CheckSystem.cpp (modified) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
-
modules/CheckSystem/CheckSystem.cpp
r6654022 re26cfe0 8 8 #include <EnumNtSrv.h> 9 9 #include <EnumProcess.h> 10 #include <sysinfo.h> 11 #include <checkHelpers.hpp> 10 12 11 13 CheckSystem gNSClientCompat; … … 47 49 int method = tmp.GetAvailableMethods(); 48 50 49 if (wantedMethod == C_SYSTEM_ENUMPROC_METHOD_PSAPI) { 51 52 53 if (wantedMethod == C_SYSTEM_ENUMPROC_METHOD_AUTO) { 54 OSVERSIONINFO osVer = systemInfo::getOSVersion(); 55 if (systemInfo::isBelowNT4(osVer)) { 56 NSC_DEBUG_MSG_STD("Autodetected NT4<, using PSAPI process enumeration."); 57 processMethod_ = ENUM_METHOD::PSAPI; 58 } else if (systemInfo::isAboveW2K(osVer)) { 59 NSC_DEBUG_MSG_STD("Autodetected W2K>, using TOOLHELP process enumeration."); 60 processMethod_ = ENUM_METHOD::TOOLHELP; 61 } else { 62 NSC_DEBUG_MSG_STD("Autodetected failed, using PSAPI process enumeration."); 63 processMethod_ = ENUM_METHOD::PSAPI; 64 } 65 } else if (wantedMethod == C_SYSTEM_ENUMPROC_METHOD_PSAPI) { 50 66 NSC_DEBUG_MSG_STD("Using PSAPI method."); 51 67 if (method == (method|ENUM_METHOD::PSAPI)) { … … 75 91 } 76 92 return true; 77 }78 /**79 * Return the module name.80 * @return The module name81 */82 std::string CheckSystem::getModuleName() {83 return "System Checks Module.";84 }85 /**86 * Module version87 * @return module version88 */89 NSCModuleWrapper::module_version CheckSystem::getModuleVersion() {90 NSCModuleWrapper::module_version version = {0, 3, 0 };91 return version;92 93 } 93 94 /** … … 136 137 } 137 138 138 // checkCPU warn=80 crit=90 time=20m time=10s time=4139 // checkCPU warn=80 crit=90 time=20m time=10s time=4 showAll140 // checkCPU 20 10 4 nsclient141 139 NSCAPI::nagiosReturn CheckSystem::checkCPU(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) 142 140 { 141 typedef checkHolders::CheckConatiner<checkHolders::MaxMinBoundsInteger> CPULoadConatiner; 142 143 143 std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args); 144 144 if (stl_args.empty()) { … … 146 146 return NSCAPI::returnUNKNOWN; 147 147 } 148 int warn; 149 int crit; 150 std::list<std::string> times; 151 bool bNSCLientCompatible = false; 148 std::list<CPULoadConatiner> list; 149 NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; 152 150 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) { 151 bool bNSClient = false; 152 CPULoadConatiner tmpObject; 153 154 tmpObject.data = "cpuload"; 155 156 MAP_OPTIONS_BEGIN(stl_args) 157 MAP_OPTIONS_STR("MaxWarn", tmpObject.warn.max) 158 MAP_OPTIONS_STR("warn", tmpObject.warn.max) 159 MAP_OPTIONS_STR("MinWarn", tmpObject.warn.min) 160 MAP_OPTIONS_STR("MaxCrit", tmpObject.crit.max) 161 MAP_OPTIONS_STR("crit", tmpObject.crit.max) 162 MAP_OPTIONS_STR("MinCrit", tmpObject.crit.min) 163 MAP_OPTIONS_STR_AND("time", tmpObject.data, list.push_back(tmpObject)) 164 MAP_OPTIONS_STR_AND("Time", tmpObject.data, list.push_back(tmpObject)) 165 MAP_OPTIONS_BOOL_TRUE(SHOW_ALL, bShowAll) 166 MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient) 167 MAP_OPTIONS_SECONDARY_BEGIN(":", p2) 168 else if (p2.first == "Time") { 169 tmpObject.data = p__.second; 170 tmpObject.alias = p2.second; 171 list.push_back(tmpObject); 172 } 173 MAP_OPTIONS_MISSING_EX(p2, msg, "Unknown argument: ") 174 MAP_OPTIONS_SECONDARY_END() 175 else { tmpObject.data = p__.first, list.push_back(tmpObject); } 176 MAP_OPTIONS_END() 177 178 for (std::list<CPULoadConatiner>::const_iterator it = list.begin(); it != list.end(); ++it) { 179 CPULoadConatiner load = (*it); 173 180 PDHCollector *pObject = pdhThread.getThread(); 174 181 if (!pObject) { … … 176 183 return NSCAPI::returnUNKNOWN; 177 184 } 178 if (bNSC LientCompatible) {179 int v = pObject->getCPUAvrage((*it)+ "m");180 if (v == -1) {181 msg = "ERROR: We don't collect data this far back: " + (*it);185 if (bNSClient) { 186 int value = pObject->getCPUAvrage(load.data + "m"); 187 if (value == -1) { 188 msg = "ERROR: We don't collect data this far back: " + load.getAlias(); 182 189 return NSCAPI::returnUNKNOWN; 183 190 } 184 191 if (!msg.empty()) msg += "&"; 185 msg += strEx::itos(v );192 msg += strEx::itos(value); 186 193 } else { 187 int v = pObject->getCPUAvrage((*it));188 if (v == -1) {189 msg = "ERROR: We don't collect data this far back: " + (*it);194 int value = pObject->getCPUAvrage(load.data); 195 if (value == -1) { 196 msg = "ERROR: We don't collect data this far back: " + load.getAlias(); 190 197 return NSCAPI::returnUNKNOWN; 191 198 } 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 199 load.setDefault(tmpObject); 200 load.runCheck(value, returnCode, msg, perf, bShowAll); 201 } 202 } 203 } 204 205 if (msg.empty()) 206 msg = "OK CPU Load ok."; 207 else if (!bNSClient) 208 msg = NSCHelper::translateReturn(returnCode) + ": " + msg; 209 return returnCode; 210 } 211 217 212 NSCAPI::nagiosReturn CheckSystem::checkUpTime(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) 218 213 { 214 typedef checkHolders::CheckConatiner<checkHolders::MaxMinBoundsTime> UpTimeConatiner; 215 219 216 std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args); 220 217 if (stl_args.empty()) { … … 222 219 return NSCAPI::returnUNKNOWN; 223 220 } 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 } 221 NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; 222 bool bShowAll = false; 223 bool bNSClient = false; 224 UpTimeConatiner bounds; 225 226 bounds.data = "uptime"; 227 228 MAP_OPTIONS_BEGIN(stl_args) 229 MAP_OPTIONS_STR("MaxWarn", bounds.warn.max) 230 MAP_OPTIONS_STR("warn", bounds.warn.min) 231 MAP_OPTIONS_STR("MinWarn", bounds.warn.min) 232 MAP_OPTIONS_STR("MaxCrit", bounds.crit.max) 233 MAP_OPTIONS_STR("crit", bounds.crit.min) 234 MAP_OPTIONS_STR("MinCrit", bounds.crit.min) 235 MAP_OPTIONS_STR("Alias", bounds.data) 236 MAP_OPTIONS_BOOL_TRUE(SHOW_ALL, bShowAll) 237 MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient) 238 MAP_OPTIONS_MISSING(msg, "Unknown argument: ") 239 MAP_OPTIONS_END() 240 241 241 242 PDHCollector *pObject = pdhThread.getThread(); 242 243 if (!pObject) { … … 244 245 return NSCAPI::returnUNKNOWN; 245 246 } 246 unsigned long long uptime = pObject->getUptime(); 247 if (bNSCLientCompatible) { 248 msg = strEx::itos(uptime); 249 return NSCAPI::returnOK; 247 unsigned long long value = pObject->getUptime(); 248 if (bNSClient) { 249 msg = strEx::itos(value); 250 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 251 value *= 1000; 252 bounds.runCheck(value, returnCode, msg, perf, bShowAll); 253 } 254 255 if (msg.empty()) 256 msg = "OK all counters within bounds."; 257 else if (!bNSClient) 258 msg = NSCHelper::translateReturn(returnCode) + ": " + msg; 259 return returnCode; 260 } 261 262 // @todo state_handler 264 263 265 264 … … 289 288 NSCAPI::nagiosReturn CheckSystem::checkServiceState(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) 290 289 { 290 typedef checkHolders::CheckConatiner<checkHolders::SimpleBoundsStateBoundsInteger> StateConatiner; 291 291 std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args); 292 292 if (stl_args.empty()) { … … 294 294 return NSCAPI::returnUNKNOWN; 295 295 } 296 std::list< std::pair<std::string,states> > services;297 NSCAPI::nagiosReturn ret = NSCAPI::returnOK;296 std::list<StateConatiner> list; 297 NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; 298 298 bool bShowAll = false; 299 299 bool bNSClient = false; 300 301 for (arrayBuffer::arrayList::const_iterator it = stl_args.begin(); it != stl_args.end(); ++it) { 302 strEx::token t = strEx::getToken((*it), '='); 303 if (t.first == SHOW_ALL) 304 bShowAll = true; 305 else if (t.first == NSCLIENT) 306 bNSClient = true; 307 else if (t.first == SHOW_FAIL) { 308 bShowAll = false; 300 StateConatiner tmpObject; 301 302 tmpObject.data = "uptime"; 303 tmpObject.warn.state = "started"; 304 305 MAP_OPTIONS_BEGIN(stl_args) 306 /* 307 MAP_OPTIONS_STR("MaxWarn", tmpObject.warn.max) 308 MAP_OPTIONS_STR("warn", tmpObject.warn.min) 309 MAP_OPTIONS_STR("MinWarn", tmpObject.warn.min) 310 MAP_OPTIONS_STR("MaxCrit", tmpObject.crit.max) 311 MAP_OPTIONS_STR("crit", tmpObject.crit.min) 312 MAP_OPTIONS_STR("MinCrit", tmpObject.crit.min) 313 */ 314 MAP_OPTIONS_STR("Alias", tmpObject.data) 315 MAP_OPTIONS_BOOL_TRUE(SHOW_ALL, bShowAll) 316 MAP_OPTIONS_BOOL_FALSE(SHOW_FAIL, bShowAll) 317 MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient) 318 MAP_OPTIONS_SECONDARY_BEGIN(":", p2) 319 else if (p2.first == "Time") { 320 tmpObject.data = p__.second; 321 tmpObject.alias = p2.second; 322 list.push_back(tmpObject); 323 } 324 MAP_OPTIONS_MISSING_EX(p2, msg, "Unknown argument: ") 325 MAP_OPTIONS_SECONDARY_END() 326 else { 327 tmpObject.data = p__.first; 328 if (p__.second.empty()) 329 tmpObject.crit.state = "started"; 330 else 331 tmpObject.crit.state = p__.second; 332 list.push_back(tmpObject); 333 } 334 MAP_OPTIONS_END() 335 336 for (std::list<StateConatiner>::iterator it = list.begin(); it != list.end(); ++it) { 337 TNtServiceInfo info; 338 if (bNSClient) { 339 try { 340 info = TNtServiceInfo::GetService((*it).data.c_str()); 341 } catch (NTServiceException e) { 342 if (!msg.empty()) msg += " - "; 343 msg += (*it).data + ": Unknown"; 344 NSCHelper::escalteReturnCodeToWARN(returnCode); 345 continue; 346 } 347 if ((info.m_dwCurrentState == SERVICE_RUNNING) && (bShowAll)) { 348 if (!msg.empty()) msg += " - "; 349 msg += (*it).data + ": Started"; 350 } else if (info.m_dwCurrentState == SERVICE_RUNNING) { 351 } else if (info.m_dwCurrentState == SERVICE_STOPPED) { 352 if (!msg.empty()) msg += " - "; 353 msg += (*it).data + ": Stopped"; 354 NSCHelper::escalteReturnCodeToCRIT(returnCode); 355 } else { 356 if (!msg.empty()) msg += " - "; 357 msg += (*it).data + ": Unknown"; 358 NSCHelper::escalteReturnCodeToWARN(returnCode); 359 } 309 360 } else { 310 if (t.second.empty()) 311 services.push_back(std::pair<std::string,states>(t.first, started)); 312 else { 313 if (t.second == "started") 314 services.push_back(std::pair<std::string,states>(t.first, started)); 315 else 316 services.push_back(std::pair<std::string,states>(t.first, stopped)); 317 } 318 } 319 } 320 for (std::list<std::pair<std::string,states> >::iterator it = services.begin(); it != services.end(); ++it) { 321 TNtServiceInfo info = TNtServiceInfo::GetService((*it).first.c_str()); 322 std::string tmp; 323 if ( (info.m_dwCurrentState == SERVICE_RUNNING) && ((*it).second == started) ) { 324 if (bShowAll) 325 tmp = info.m_strServiceName + " : Started"; 326 } else if ( (info.m_dwCurrentState == SERVICE_STOPPED) && ((*it).second == stopped) ) { 327 if (bShowAll) 328 tmp = info.m_strServiceName + " : Stopped"; 329 } else if ((info.m_dwCurrentState == SERVICE_STOPPED) && ((*it).second == started) ) { 330 NSCHelper::escalteReturnCodeToCRIT(ret); 331 tmp = info.m_strServiceName + " : Stopped"; 332 } else if ((info.m_dwCurrentState == SERVICE_RUNNING) && ((*it).second == stopped) ) { 333 NSCHelper::escalteReturnCodeToCRIT(ret); 334 tmp = info.m_strServiceName + " : Started"; 335 } else { 336 NSCHelper::escalteReturnCodeToWARN(ret); 337 tmp = info.m_strServiceName + " : Unknown"; 338 } 339 if (!msg.empty()&&!tmp.empty()) 340 msg += " - "; 341 msg += tmp; 361 try { 362 info = TNtServiceInfo::GetService((*it).data.c_str()); 363 } catch (NTServiceException e) { 364 NSC_LOG_ERROR_STD(e.getError()); 365 msg = e.getError(); 366 return NSCAPI::returnUNKNOWN; 367 } 368 checkHolders::state_type value; 369 if (info.m_dwCurrentState == SERVICE_RUNNING) 370 value = checkHolders::state_started; 371 else if (info.m_dwCurrentState == SERVICE_STOPPED) 372 value = checkHolders::state_stopped; 373 else 374 value = checkHolders::state_none; 375 (*it).runCheck(value, returnCode, msg, perf, bShowAll); 376 } 377 342 378 } 343 379 if (msg.empty()) 344 msg = "All services are running.";380 msg = "OK: All services are running."; 345 381 else if (!bNSClient) 346 msg = NSCHelper::translateReturn(ret ) + ": " + msg;347 return ret ;382 msg = NSCHelper::translateReturn(returnCode) + ": " + msg; 383 return returnCode; 348 384 } 349 385 … … 362 398 NSCAPI::nagiosReturn CheckSystem::checkMem(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) 363 399 { 400 typedef checkHolders::CheckConatiner<checkHolders::MaxMinPercentageBoundsInt64 > MemoryConatiner; 364 401 std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args); 365 402 if (stl_args.empty()) { … … 367 404 return NSCAPI::returnUNKNOWN; 368 405 } 369 std::list<std::pair<std::string,states> > services;370 406 NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; 371 407 bool bShowAll = false; 372 bool bNSCLientCompatible = false; 373 374 checkHolders::SizeMaxMinPercentage<> warn; 375 checkHolders::SizeMaxMinPercentage<> crit; 376 377 for (arrayBuffer::arrayList::const_iterator it = stl_args.begin(); it != stl_args.end(); ++it) { 378 strEx::token t = strEx::getToken((*it), '='); 379 if (t.first == SHOW_ALL) 380 bShowAll = true; 381 else if (t.first == "MaxWarn") { 382 warn.max.set(t.second); 383 } else if (t.first == "MinWarn") { 384 warn.min.set(t.second); 385 } else if (t.first == "MaxCrit") { 386 crit.max.set(t.second); 387 } else if (t.first == "MinCrit") { 388 crit.min.set(t.second); 389 } else if (t.first == NSCLIENT) 390 bNSCLientCompatible = true; 391 else { 392 msg = "Invalid argument: " + t.first; 393 return NSCAPI::returnUNKNOWN; 394 } 395 } 408 bool bNSClient = false; 409 MemoryConatiner bounds; 410 411 bounds.data = "page"; 412 413 MAP_OPTIONS_BEGIN(stl_args) 414 MAP_OPTIONS_STR("MaxWarn", bounds.warn.max) 415 MAP_OPTIONS_STR("MinWarn", bounds.warn.min) 416 MAP_OPTIONS_STR("MaxCrit", bounds.crit.max) 417 MAP_OPTIONS_STR("MinCrit", bounds.crit.min) 418 MAP_OPTIONS_STR("Alias", bounds.data) 419 MAP_OPTIONS_BOOL_TRUE(SHOW_ALL, bShowAll) 420 MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient) 421 MAP_OPTIONS_MISSING(msg, "Unknown argument: ") 422 MAP_OPTIONS_END() 396 423 397 424 PDHCollector *pObject = pdhThread.getThread(); … … 402 429 long long pageCommit = pObject->getMemCommit(); 403 430 long long pageCommitLimit = pObject->getMemCommitLimit(); 404 if (bNSC LientCompatible) {431 if (bNSClient) { 405 432 msg = strEx::itos(pageCommitLimit) + "&" + strEx::itos(pageCommit); 406 433 return NSCAPI::returnOK; 407 434 } else { 408 std::string tStr; 409 if (crit.max.hasBounds() && crit.max.checkMAX(pageCommit, pageCommitLimit)) { 410 tStr = crit.max.prettyPrint("page", pageCommit, pageCommitLimit) + " > critical"; 411 NSCHelper::escalteReturnCodeToCRIT(returnCode); 412 } else if (crit.min.hasBounds() && crit.min.checkMIN(pageCommit, pageCommitLimit)) { 413 tStr = crit.min.prettyPrint("page", pageCommit, pageCommitLimit) + " < critical"; 414 NSCHelper::escalteReturnCodeToCRIT(returnCode); 415 } else if (warn.max.hasBounds() && warn.max.checkMAX(pageCommit, pageCommitLimit)) { 416 tStr = warn.max.prettyPrint("page", pageCommit, pageCommitLimit) + " > warning"; 417 NSCHelper::escalteReturnCodeToWARN(returnCode); 418 } else if (warn.min.hasBounds() && warn.min.checkMIN(pageCommit, pageCommitLimit)) { 419 tStr = warn.min.prettyPrint("page", pageCommit, pageCommitLimit) + " < warning"; 420 NSCHelper::escalteReturnCodeToWARN(returnCode); 421 } else if (bShowAll) { 422 tStr = "page: " + strEx::itos_as_BKMG(pageCommit); 423 } 424 perf += checkHolders::SizeMaxMinPercentage<>::printPerf("page", pageCommit, pageCommitLimit, warn, crit); 425 msg += tStr; 435 bounds.warn.max.setMax(pageCommitLimit); 436 bounds.warn.min.setMax(pageCommitLimit); 437 bounds.crit.max.setMax(pageCommitLimit); 438 bounds.crit.min.setMax(pageCommitLimit); 439 bounds.runCheck(pageCommit, returnCode, msg, perf, bShowAll); 426 440 } 427 441 if (msg.empty()) … … 431 445 return returnCode; 432 446 } 433 434 typedef std::hash_map<std::string,DWORD> NSPROCLST; 447 typedef struct NSPROCDATA__ { 448 NSPROCDATA__() : count(0) {} 449 NSPROCDATA__(const NSPROCDATA__ &other) { 450 count = other.count; 451 entry = other.entry; 452 } 453 454 unsigned int count; 455 CEnumProcess::CProcessEntry entry; 456 } NSPROCDATA; 457 typedef std::hash_map<std::string,NSPROCDATA> NSPROCLST; 435 458 /** 436 459 * Get a hash_map with all running processes. … … 448 471 CEnumProcess::CProcessEntry entry; 449 472 for (BOOL OK = enumeration.GetProcessFirst(&entry); OK; OK = enumeration.GetProcessNext(&entry) ) { 450 ret[entry.lpFilename] = entry.dwPID; 473 NSPROCLST::iterator it = ret.find(entry.sFilename); 474 if (it == ret.end()) 475 ret[entry.sFilename].entry = entry; 476 else 477 (*it).second.count++; 451 478 } 452 479 return ret; … … 465 492 NSCAPI::nagiosReturn CheckSystem::checkProcState(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) 466 493 { 494 typedef checkHolders::CheckConatiner<checkHolders::MaxMinStateBoundsStateBoundsInteger> StateConatiner; 467 495 std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args); 468 496 if (stl_args.empty()) { … … 470 498 return NSCAPI::returnUNKNOWN; 471 499 } 472 std::list< std::pair<std::string,states> > procs;473 NSCAPI::nagiosReturn ret = NSCAPI::returnOK;500 std::list<StateConatiner> list; 501 NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; 474 502 bool bShowAll = false; 475 503 bool bNSClient = false; 476 477 for (arrayBuffer::arrayList::const_iterator it = stl_args.begin(); it != stl_args.end(); ++it) { 478 strEx::token t = strEx::getToken((*it), '='); 479 if (t.first == SHOW_ALL) 480 bShowAll = true; 481 else if (t.first == NSCLIENT) 482 bNSClient = true; 483 else if (t.first == SHOW_FAIL) { 484 bShowAll = false; 485 } else { 486 if (t.second.empty()) 487 procs.push_back(std::pair<std::string,states>(t.first, started)); 488 else { 489 if (t.second == "started") 490 procs.push_back(std::pair<std::string,states>(t.first, started)); 491 else 492 procs.push_back(std::pair<std::string,states>(t.first, stopped)); 493 } 494 } 495 } 504 StateConatiner tmpObject; 505 506 tmpObject.data = "uptime"; 507 tmpObject.crit.state = "started"; 508 509 MAP_OPTIONS_BEGIN(stl_args) 510 MAP_OPTIONS_STR("MaxWarnCount", tmpObject.warn.max) 511 MAP_OPTIONS_STR("MinWarnCount", tmpObject.warn.min) 512 MAP_OPTIONS_STR("MaxCritCount", tmpObject.crit.max) 513 MAP_OPTIONS_STR("MinCritCount", tmpObject.crit.min) 514 MAP_OPTIONS_STR("Alias", tmpObject.alias) 515 MAP_OPTIONS_BOOL_TRUE(SHOW_ALL, bShowAll) 516 MAP_OPTIONS_BOOL_FALSE(SHOW_FAIL, bShowAll) 517 MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient) 518 MAP_OPTIONS_SECONDARY_BEGIN(":", p2) 519 else if (p2.first == "Proc") { 520 tmpObject.data = p__.second; 521 tmpObject.alias = p2.second; 522 list.push_back(tmpObject); 523 } 524 MAP_OPTIONS_MISSING_EX(p2, msg, "Unknown argument: ") 525 MAP_OPTIONS_SECONDARY_END() 526 else { 527 tmpObject.data = p__.first; 528 if (p__.second.empty()) 529 tmpObject.crit.state = "started"; 530 else 531 tmpObject.crit.state = p__.second; 532 list.push_back(tmpObject); 533 } 534 MAP_OPTIONS_END() 535 536 496 537 NSPROCLST runningProcs; 497 538 try { … … 500 541 NSC_LOG_ERROR_STD("ERROR: " + c); 501 542 msg = static_cast<std::string>("ERROR: ") + c; 502 return NSCAPI::return CRIT;503 } 504 505 for (std::list< std::pair<std::string,states> >::iterator it = procs.begin(); it != procs.end(); ++it) {506 NSPROCLST::iterator proc = runningProcs.find((*it). first);543 return NSCAPI::returnUNKNOWN; 544 } 545 546 for (std::list<StateConatiner>::iterator it = list.begin(); it != list.end(); ++it) { 547 NSPROCLST::iterator proc = runningProcs.find((*it).data); 507 548 bool bFound = proc != runningProcs.end(); 508 549 std::string tmp; 509 if ( (bFound) && ((*it).second == started) ) { 510 if (bShowAll) 511 tmp = (*it).first + " : Running"; 512 } else if ( (!bFound) && ((*it).second == stopped) ) { 513 if (bShowAll) 514 tmp = (*it).first + " : Stopped"; 515 } else if ( (!bFound) && ((*it).second == started) ) { 516 NSCHelper::escalteReturnCodeToCRIT(ret); 517 tmp = (*it).first + " : Stopped"; 518 } else if ( (bFound) && ((*it).second == stopped) ) { 519 NSCHelper::escalteReturnCodeToCRIT(ret); 520 tmp = (*it).first + " : Running"; 521 } 522 if (!msg.empty()&&!tmp.empty()) 523 msg += " - "; 524 msg += tmp; 550 TNtServiceInfo info; 551 if (bNSClient) { 552 if (bFound && bShowAll) { 553 if (!msg.empty()) msg += " - "; 554 msg += (*it).data + ": Started"; 555 } else if (bFound) { 556 } else { 557 if (!msg.empty()) msg += " - "; 558 msg += (*it).data + ": Stopped"; 559 NSCHelper::escalteReturnCodeToCRIT(returnCode); 560 } 561 } else { 562 checkHolders::MaxMinStateValueType<int, checkHolders::state_type> value; 563 if (bFound) { 564 value.count = (*proc).second.count; 565 value.state = checkHolders::state_started; 566 } else { 567 value.count = 0; 568 value.state = checkHolders::state_stopped; 569 } 570 (*it).runCheck(value, returnCode, msg, perf, bShowAll); 571 } 572 525 573 } 526 574 if (msg.empty()) 527 msg = "All processes ok.";575 msg = "OK: All processes are running."; 528 576 else if (!bNSClient) 529 msg = NSCHelper::translateReturn(ret) + ": " + msg; 530 return ret; 531 } 577 msg = NSCHelper::translateReturn(returnCode) + ": " + msg; 578 return returnCode; 579 } 580 532 581 533 582 /** … … 545 594 NSCAPI::nagiosReturn CheckSystem::checkCounter(const unsigned int argLen, char **char_args, std::string &msg, std::string &perf) 546 595 { 596 typedef checkHolders::CheckConatiner<checkHolders::MaxMinBoundsDouble> CounterConatiner; 597 547 598 std::list<std::string> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args); 548 599 if (stl_args.empty()) { … … 550 601 return NSCAPI::returnUNKNOWN; 551 602 } 552 std::list< std::pair<std::string,std::string>> counters;603 std::list<CounterConatiner> counters; 553 604 NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; 554 605 bool bShowAll = false; 555 bool bNSCLientCompatible = false; 556 557 checkHolders::SizeMaxMin<__int64, checkHolders::int64_handler<> > warn; 558 checkHolders::SizeMaxMin<__int64, checkHolders::int64_handler<> > crit; 559 560 for (arrayBuffer::arrayList::const_iterator it = stl_args.begin(); it != stl_args.end(); ++it) { 561 strEx::token t = strEx::getToken((*it), '='); 562 if (t.first == SHOW_ALL) 563 bShowAll = true; 564 else if (t.first == SHOW_FAIL) { 565 bShowAll = false; 566 } else if (t.first == "MaxWarn") { 567 warn.max.set(t.second); 568 } else if (t.first == "MinWarn") { 569 warn.min.set(t.second); 570 } else if (t.first == "MaxCrit") { 571 crit.max.set(t.second); 572 } else if (t.first == "MinCrit") { 573 crit.min.set(t.second); 574 } else if (t.first == NSCLIENT) { 575 bNSCLientCompatible = true; 576 } else if (t.first == "Counter") { 577 counters.push_back(std::pair<std::string,std::string>("",t.second)); 578 } else if (t.first.find(":") != std::string::npos) { 579 std::pair<std::string,std::string> t2 = strEx::split(t.first,":"); 580 if (t2.first == "Counter") { 581 counters.push_back(std::pair<std::string,std::string>(t2.second,t.second)); 582 } else { 583 msg = "Unknown command: " + t.first; 606 bool bNSClient = false; 607 /* average maax */ 608 bool bCheckAverages = true; 609 unsigned int averageDelay = 1000; 610 CounterConatiner tmpObject; 611 612 MAP_OPTIONS_BEGIN(stl_args) 613 MAP_OPTIONS_STR_AND("Counter", tmpObject.data, counters.push_back(tmpObject)) 614 MAP_OPTIONS_STR("MaxWarn", tmpObject.warn.max) 615 MAP_OPTIONS_STR("MinWarn", tmpObject.warn.min) 616 MAP_OPTIONS_STR("MaxCrit", tmpObject.crit.max) 617 MAP_OPTIONS_STR("MinCrit", tmpObject.crit.min) 618 MAP_OPTIONS_STR("Alias", tmpObject.data) 619 MAP_OPTIONS_BOOL_TRUE(SHOW_ALL, bShowAll) 620 MAP_OPTIONS_BOOL_FALSE(SHOW_FAIL, bShowAll) 621 MAP_OPTIONS_BOOL_EX("Averages", bCheckAverages, "true", "false") 622 MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient) 623 MAP_OPTIONS_SECONDARY_BEGIN(":", p2) 624 else if (p2.first == "Counter") { 625 tmpObject.data = p__.second; 626 tmpObject.alias = p2.second; 627 counters.push_back(tmpObject); 628 } 629 MAP_OPTIONS_MISSING_EX(p2, msg, "Unknown argument: ") 630 MAP_OPTIONS_SECONDARY_END() 631 else { 632 tmpObject.data = p__.first; 633 counters.push_back(tmpObject); 634 } 635 MAP_OPTIONS_END() 636 637 for (std::list<CounterConatiner>::const_iterator cit = counters.begin(); cit != counters.end(); ++cit) { 638 CounterConatiner counter = (*cit); 639 try { 640 std::string tstr; 641 if (!PDH::Enumerations::validate(counter.data, tstr)) { 642 msg = tstr; 584 643 return NSCAPI::returnUNKNOWN; 585 644 } 586 } else {587 counters.push_back(std::pair<std::string,std::string>("",t.first));588 }589 }590 591 for (std::list<std::pair<std::string,std::string> >::iterator it = counters.begin(); it != counters.end(); ++it) {592 std::string name;593 try {594 645 PDH::PDHQuery pdh; 595 PDHCollectors::StaticPDHCounterListener<double, PDH_FMT_DOUBLE> counter; 596 std::string name = (*it).first; 597 if (name.empty()) 598 name = (*it).second; 599 pdh.addCounter((*it).second, &counter); 646 PDHCollectors::StaticPDHCounterListener<double, PDH_FMT_DOUBLE> cDouble; 647 pdh.addCounter(counter.data, &cDouble); 600 648 pdh.open(); 601 649 pdh.collect(); 602 if (bNSCLientCompatible) { 603 msg += strEx::itos(counter.getValue()); 650 Sleep(1000); 651 pdh.gatherData(); 652 pdh.close(); 653 double value = cDouble.getValue(); 654 std::cout << "Collected double data: " << value << std::endl; 655 if (bNSClient) { 656 msg += strEx::itos(value); 604 657 } else { 605 std::string tStr; 606 if (crit.max.hasBounds() && crit.max.checkMAX(counter.getValue())) { 607 tStr = crit.max.prettyPrint(name, counter.getValue()) + " > critical"; 608 NSCHelper::escalteReturnCodeToCRIT(returnCode); 609 } else if (crit.min.hasBounds() && crit.min.checkMIN(counter.getValue())) { 610 tStr = crit.min.prettyPrint(name, counter.getValue()) + " < critical"; 611 NSCHelper::escalteReturnCodeToCRIT(returnCode); 612 } else if (warn.max.hasBounds() && warn.max.checkMAX(counter.getValue())) { 613 tStr = warn.max.prettyPrint(name, counter.getValue()) + " > warning"; 614 NSCHelper::escalteReturnCodeToWARN(returnCode); 615 } else if (warn.min.hasBounds() && warn.min.checkMIN(counter.getValue())) { 616 tStr = warn.min.prettyPrint(name, counter.getValue()) + " < warning"; 617 NSCHelper::escalteReturnCodeToWARN(returnCode); 618 } else if (bShowAll) { 619 tStr = name + ": " + strEx::itos(counter.getValue()); 620 } 621 perf += checkHolders::SizeMaxMin<__int64, checkHolders::int64_handler<> >::printPerf(name, counter.getValue(), warn, crit); 622 msg += tStr; 623 } 624 pdh.close(); 625 } catch (const PDH::PDHException &e) { 626 NSC_LOG_ERROR_STD("ERROR: " + e.str_ + " (" + name + ")"); 627 msg = static_cast<std::string>("ERROR: ") + e.str_; 628 return 0; 629 } 630 } 658 counter.setDefault(tmpObject); 659 counter.runCheck(value, returnCode, msg, perf, bShowAll); 660 } 661 } catch (const PDH::PDHException e) { 662 NSC_LOG_ERROR_STD("ERROR: " + e.getError() + " (" + counter.getAlias() + ")"); 663 msg = static_cast<std::string>("ERROR: ") + e.getError(); 664 return NSCAPI::returnUNKNOWN; 665 } 666 } 667 631 668 if (msg.empty()) 632 669 msg = "OK all counters within bounds."; 633 else if (!bNSC LientCompatible)670 else if (!bNSClient) 634 671 msg = NSCHelper::translateReturn(returnCode) + ": " + msg; 635 672 return returnCode; … … 638 675 NSC_WRAPPERS_IGNORE_MSG_DEF(); 639 676 NSC_WRAPPERS_HANDLE_CMD_DEF(gNSClientCompat); 677 NSC_WRAPPERS_HANDLE_CONFIGURATION(gNSClientCompat); 678 679 680 681 MODULE_SETTINGS_START(CheckSystem, "System check module", "...") 682 683 PAGE("Check options") 684 685 ITEM_EDIT_TEXT("Check resolution", "This is how often the PDH data is polled and stored in the CPU buffer. (this is enterd in 1/th: of a second)") 686 OPTION("unit", "1/10:th of a second") 687 ITEM_MAP_TO("basic_ini_text_mapper") 688 OPTION("section", "Check System") 689 OPTION("key", "CheckResolution") 690 OPTION("default", "10") 691 ITEM_END() 692 693 ITEM_EDIT_TEXT("CPU buffer size", "This is the size of the buffer that stores CPU history.") 694 ITEM_MAP_TO("basic_ini_text_mapper") 695 OPTION("section", "Check System") 696 OPTION("key", "CPUBufferSize") 697 OPTION("default", "1h") 698 ITEM_END() 699 700 PAGE_END() 701 ADVANCED_PAGE("Compatiblity settings") 702 703 ITEM_EDIT_TEXT("MemoryCommitByte", "The memory commited bytes used to calculate the avalible memory.") 704 OPTION("disableCaption", "Attempt to autodetect this.") 705 OPTION("disabled", "auto") 706 ITEM_MAP_TO("basic_ini_text_mapper") 707 OPTION("section", "Check System") 708 OPTION("key", "MemoryCommitByte") 709 OPTION("default", "auto") 710 ITEM_END() 711 712 ITEM_EDIT_TEXT("MemoryCommitLimit", "The memory commit limit used to calculate the avalible memory.") 713 OPTION("disableCaption", "Attempt to autodetect this.") 714 OPTION("disabled", "auto") 715 ITEM_MAP_TO("basic_ini_text_mapper") 716 OPTION("section", "Check System") 717 OPTION("key", "MemoryCommitLimit") 718 OPTION("default", "auto") 719 ITEM_END() 720 721 ITEM_EDIT_TEXT("SystemSystemUpTime", "The PDH counter for the System uptime.") 722 OPTION("disableCaption", "Attempt to autodetect this.") 723 OPTION("disabled", "auto") 724 ITEM_MAP_TO("basic_ini_text_mapper") 725 OPTION("section", "Check System") 726 OPTION("key", "SystemSystemUpTime") 727 OPTION("default", "auto") 728 ITEM_END() 729 730 ITEM_EDIT_TEXT("SystemTotalProcessorTime", "The PDH conter usaed to measure CPU load.") 731 OPTION("disableCaption", "Attempt to autodetect this.") 732 OPTION("disabled", "auto") 733 ITEM_MAP_TO("basic_ini_text_mapper") 734 OPTION("section", "Check System") 735 OPTION("key", "SystemTotalProcessorTime") 736 OPTION("default", "auto") 737 ITEM_END() 738 739 ITEM_EDIT_TEXT("ProcessEnumerationMethod", "The method to use when enumerating processes") 740 OPTION("count", "3") 741 OPTION("caption_1", "Autodetect (TOOLHELP for NT/4 and PSAPI for W2k)") 742 OPTION("value_1", "auto") 743 OPTION("caption_2", "TOOLHELP use this for NT/4 systems") 744 OPTION("value_2", "TOOLHELP") 745 OPTION("caption_3", "PSAPI use this for W2k (and abowe) systems") 746 OPTION("value_3", "PSAPI") 747 ITEM_MAP_TO("basic_ini_text_mapper") 748 OPTION("section", "Check System") 749 OPTION("key", "ProcessEnumerationMethod") 750 OPTION("default", "auto") 751 ITEM_END() 752 753 PAGE_END() 754 MODULE_SETTINGS_END()
Note: See TracChangeset
for help on using the changeset viewer.








