| 1 | /************************************************************************** |
|---|
| 2 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> * |
|---|
| 3 | * * |
|---|
| 4 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp * |
|---|
| 5 | * * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify * |
|---|
| 7 | * it under the terms of the GNU General Public License as published by * |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
|---|
| 9 | * (at your option) any later version. * |
|---|
| 10 | * * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, * |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 14 | * GNU General Public License for more details. * |
|---|
| 15 | * * |
|---|
| 16 | * You should have received a copy of the GNU General Public License * |
|---|
| 17 | * along with this program; if not, write to the * |
|---|
| 18 | * Free Software Foundation, Inc., * |
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
|---|
| 20 | ***************************************************************************/ |
|---|
| 21 | |
|---|
| 22 | #include "stdafx.h" |
|---|
| 23 | |
|---|
| 24 | #include <boost/regex.hpp> |
|---|
| 25 | #include <boost/lexical_cast.hpp> |
|---|
| 26 | |
|---|
| 27 | #include "CheckSystem.h" |
|---|
| 28 | #include <utils.h> |
|---|
| 29 | #include <tlhelp32.h> |
|---|
| 30 | #include <EnumNtSrv.h> |
|---|
| 31 | #include <EnumProcess.h> |
|---|
| 32 | #include <checkHelpers.hpp> |
|---|
| 33 | #include <map> |
|---|
| 34 | #include <set> |
|---|
| 35 | #include <sysinfo.h> |
|---|
| 36 | #include <filter_framework.hpp> |
|---|
| 37 | #include <simple_registry.hpp> |
|---|
| 38 | #include <settings/client/settings_client.hpp> |
|---|
| 39 | #include <arrayBuffer.h> |
|---|
| 40 | |
|---|
| 41 | #include <config.h> |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * Default c-tor |
|---|
| 45 | * @return |
|---|
| 46 | */ |
|---|
| 47 | CheckSystem::CheckSystem() : pdhThread(_T("pdhThread")) {} |
|---|
| 48 | /** |
|---|
| 49 | * Default d-tor |
|---|
| 50 | * @return |
|---|
| 51 | */ |
|---|
| 52 | CheckSystem::~CheckSystem() {} |
|---|
| 53 | |
|---|
| 54 | namespace sh = nscapi::settings_helper; |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * Load (initiate) module. |
|---|
| 58 | * Start the background collector thread and let it run until unloadModule() is called. |
|---|
| 59 | * @return true |
|---|
| 60 | */ |
|---|
| 61 | bool CheckSystem::loadModule() { |
|---|
| 62 | return loadModuleEx(_T(""), NSCAPI::normalStart); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | * New version of the load call. |
|---|
| 67 | * Start the background collector thread and let it run until unloadModule() is called. |
|---|
| 68 | * @return true |
|---|
| 69 | */ |
|---|
| 70 | |
|---|
| 71 | bool CheckSystem::loadModuleEx(std::wstring alias, NSCAPI::moduleLoadMode mode) { |
|---|
| 72 | PDHCollector::system_counter_data *data = new PDHCollector::system_counter_data; |
|---|
| 73 | data->check_intervall = 100; |
|---|
| 74 | try { |
|---|
| 75 | typedef std::map<std::wstring,std::wstring> counter_map_type; |
|---|
| 76 | std::map<std::wstring,std::wstring> counters; |
|---|
| 77 | bool default_counters = true; |
|---|
| 78 | |
|---|
| 79 | sh::settings_registry settings(get_settings_proxy()); |
|---|
| 80 | settings.set_alias(_T("check"), alias, _T("system/windows")); |
|---|
| 81 | |
|---|
| 82 | settings.alias().add_path_to_settings() |
|---|
| 83 | (_T("WINDOWS CHECK SYSTEM"), _T("Section for system checks and system settings")) |
|---|
| 84 | |
|---|
| 85 | (_T("service mapping"), _T("SERVICE MAPPING SECTION"), _T("Confiure which services has to be in which state")) |
|---|
| 86 | |
|---|
| 87 | (_T("pdh"), _T("PDH COUNTER INFORMATION"), _T("")) |
|---|
| 88 | |
|---|
| 89 | (_T("pdh/counters"), sh::wstring_map_path(&counters) |
|---|
| 90 | , _T("PDH COUNTERS"), _T("")) |
|---|
| 91 | |
|---|
| 92 | ; |
|---|
| 93 | |
|---|
| 94 | settings.alias().add_key_to_settings() |
|---|
| 95 | (_T("default"), sh::bool_key(&default_counters, true), |
|---|
| 96 | _T("DEFAULT COUNTERS"), _T("Load the default counters: ") PDH_SYSTEM_KEY_CPU _T(", ") PDH_SYSTEM_KEY_MCB _T(", ") PDH_SYSTEM_KEY_MCL _T(" and ") PDH_SYSTEM_KEY_UPT _T(" If not you need to specify these manually. ") ) |
|---|
| 97 | |
|---|
| 98 | (_T("default buffer length"), sh::wstring_key(&data->buffer_length, _T("1h")), |
|---|
| 99 | _T("DEFAULT INTERVALL"), _T("Used to define the default intervall for range buffer checks (ie. CPU).")) |
|---|
| 100 | |
|---|
| 101 | ; |
|---|
| 102 | |
|---|
| 103 | settings.alias().add_key_to_settings(_T("service mapping")) |
|---|
| 104 | |
|---|
| 105 | (_T("BOOT_START"), sh::wstring_vector_key(&lookups_, SERVICE_BOOT_START, _T("ignored")), |
|---|
| 106 | _T("SERVICE_BOOT_START"), _T("TODO"), true) |
|---|
| 107 | |
|---|
| 108 | (_T("SYSTEM_START"), sh::wstring_vector_key(&lookups_, SERVICE_SYSTEM_START, _T("ignored")), |
|---|
| 109 | _T("SERVICE_SYSTEM_START"), _T("TODO"), true) |
|---|
| 110 | |
|---|
| 111 | (_T("AUTO_START"), sh::wstring_vector_key(&lookups_, SERVICE_AUTO_START, _T("started")), |
|---|
| 112 | _T("SERVICE_AUTO_START"), _T("TODO"), true) |
|---|
| 113 | |
|---|
| 114 | (_T("DEMAND_START"), sh::wstring_vector_key(&lookups_, SERVICE_DEMAND_START, _T("ignored")), |
|---|
| 115 | _T("SERVICE_DEMAND_START"), _T("TODO"), true) |
|---|
| 116 | |
|---|
| 117 | (_T("DISABLED"), sh::wstring_vector_key(&lookups_, SERVICE_DISABLED, _T("stopped")), |
|---|
| 118 | _T("SERVICE_DISABLED"), _T("TODO"), true) |
|---|
| 119 | |
|---|
| 120 | (_T("DELAYED"), sh::wstring_vector_key(&lookups_, NSCP_SERVICE_DELAYED, _T("ignored")), |
|---|
| 121 | _T("SERVICE_DELAYED"), _T("TODO"), true) |
|---|
| 122 | |
|---|
| 123 | ; |
|---|
| 124 | |
|---|
| 125 | settings.register_all(); |
|---|
| 126 | settings.notify(); |
|---|
| 127 | |
|---|
| 128 | typedef PDHCollector::system_counter_data::counter cnt; |
|---|
| 129 | if (default_counters) { |
|---|
| 130 | data->counters.push_back(cnt(PDH_SYSTEM_KEY_CPU, _T("\\238(_total)\\6"), cnt::type_int64, cnt::format_large, cnt::rrd)); |
|---|
| 131 | data->counters.push_back(cnt(PDH_SYSTEM_KEY_MCB, _T("\\4\\26"), cnt::type_int64, cnt::format_large, cnt::value)); |
|---|
| 132 | data->counters.push_back(cnt(PDH_SYSTEM_KEY_MCL, _T("\\4\\30"), cnt::type_int64, cnt::format_large, cnt::value)); |
|---|
| 133 | data->counters.push_back(cnt(PDH_SYSTEM_KEY_UPT, _T("\\2\\674"), cnt::type_int64, cnt::format_large, cnt::value)); |
|---|
| 134 | } |
|---|
| 135 | BOOST_FOREACH(counter_map_type::value_type c, counters) { |
|---|
| 136 | data->counters.push_back(cnt(c.first, c.second, cnt::type_int64, cnt::format_large, cnt::value)); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | register_command(_T("checkCPU"), _T("Check the CPU load of the computer.")); |
|---|
| 140 | register_command(_T("checkUpTime"), _T("Check the up-time of the computer.")); |
|---|
| 141 | register_command(_T("checkServiceState"), _T("Check the state of one or more of the computer services.")); |
|---|
| 142 | register_command(_T("checkProcState"), _T("Check the state of one or more of the processes running on the computer.")); |
|---|
| 143 | register_command(_T("checkMem"), _T("Check free/used memory on the system.")); |
|---|
| 144 | register_command(_T("checkCounter"), _T("Check a PDH counter.")); |
|---|
| 145 | register_command(_T("listCounterInstances"), _T("List all instances for a counter.")); |
|---|
| 146 | register_command(_T("checkSingleRegEntry"), _T("Check registry key")); |
|---|
| 147 | } catch (nscapi::nscapi_exception &e) { |
|---|
| 148 | NSC_LOG_ERROR_STD(_T("Failed to register command: ") + utf8::cvt<std::wstring>(e.what())); |
|---|
| 149 | return false; |
|---|
| 150 | } catch (std::exception &e) { |
|---|
| 151 | NSC_LOG_ERROR_STD(_T("Exception: ") + utf8::cvt<std::wstring>(e.what())); |
|---|
| 152 | return false; |
|---|
| 153 | } catch (...) { |
|---|
| 154 | NSC_LOG_ERROR_STD(_T("Failed to register command.")); |
|---|
| 155 | return false; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | if (mode == NSCAPI::normalStart) { |
|---|
| 159 | pdhThread.createThread(data); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | return true; |
|---|
| 163 | } |
|---|
| 164 | /** |
|---|
| 165 | * Unload (terminate) module. |
|---|
| 166 | * Attempt to stop the background processing thread. |
|---|
| 167 | * @return true if successfully, false if not (if not things might be bad) |
|---|
| 168 | */ |
|---|
| 169 | bool CheckSystem::unloadModule() { |
|---|
| 170 | if (!pdhThread.exitThread(20000)) { |
|---|
| 171 | std::wcout << _T("MAJOR ERROR: Could not unload thread...") << std::endl; |
|---|
| 172 | NSC_LOG_ERROR(_T("Could not exit the thread, memory leak and potential corruption may be the result...")); |
|---|
| 173 | } |
|---|
| 174 | return true; |
|---|
| 175 | } |
|---|
| 176 | /** |
|---|
| 177 | * Check if we have a command handler. |
|---|
| 178 | * @return true (as we have a command handler) |
|---|
| 179 | */ |
|---|
| 180 | bool CheckSystem::hasCommandHandler() { |
|---|
| 181 | return true; |
|---|
| 182 | } |
|---|
| 183 | /** |
|---|
| 184 | * Check if we have a message handler. |
|---|
| 185 | * @return false as we have no message handler |
|---|
| 186 | */ |
|---|
| 187 | bool CheckSystem::hasMessageHandler() { |
|---|
| 188 | return false; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | int CheckSystem::commandLineExec(const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &result) { |
|---|
| 192 | if (command == _T("help")) { |
|---|
| 193 | std::wcerr << _T("Usage: ... CheckSystem <command>") << std::endl; |
|---|
| 194 | std::wcerr << _T("Commands: debugpdh, listpdh, pdhlookup, pdhmatch, pdhobject") << std::endl; |
|---|
| 195 | return -1; |
|---|
| 196 | } |
|---|
| 197 | if (command == _T("debugpdh")) { |
|---|
| 198 | PDH::Enumerations::Objects lst; |
|---|
| 199 | try { |
|---|
| 200 | lst = PDH::Enumerations::EnumObjects(); |
|---|
| 201 | } catch (const PDH::PDHException e) { |
|---|
| 202 | std::wcout << _T("Service enumeration failed: ") << e.getError(); |
|---|
| 203 | return 0; |
|---|
| 204 | } |
|---|
| 205 | for (PDH::Enumerations::Objects::iterator it = lst.begin();it!=lst.end();++it) { |
|---|
| 206 | if ((*it).instances.size() > 0) { |
|---|
| 207 | for (PDH::Enumerations::Instances::const_iterator it2 = (*it).instances.begin();it2!=(*it).instances.end();++it2) { |
|---|
| 208 | for (PDH::Enumerations::Counters::const_iterator it3 = (*it).counters.begin();it3!=(*it).counters.end();++it3) { |
|---|
| 209 | std::wstring counter = _T("\\") + (*it).name + _T("(") + (*it2).name + _T(")\\") + (*it3).name; |
|---|
| 210 | std::wcout << _T("testing: ") << counter << _T(": "); |
|---|
| 211 | std::list<std::wstring> errors; |
|---|
| 212 | std::list<std::wstring> status; |
|---|
| 213 | std::wstring error; |
|---|
| 214 | bool bStatus = true; |
|---|
| 215 | if (PDH::PDHResolver::validate(counter, error, false)) { |
|---|
| 216 | status.push_back(_T("open")); |
|---|
| 217 | } else { |
|---|
| 218 | errors.push_back(_T("NOT found: ") + error); |
|---|
| 219 | bStatus = false; |
|---|
| 220 | } |
|---|
| 221 | if (bStatus) { |
|---|
| 222 | |
|---|
| 223 | typedef boost::shared_ptr<PDH::PDHCounter> counter_ptr; |
|---|
| 224 | counter_ptr pCounter; |
|---|
| 225 | PDH::PDHQuery pdh; |
|---|
| 226 | try { |
|---|
| 227 | pdh.addCounter(counter); |
|---|
| 228 | pdh.open(); |
|---|
| 229 | |
|---|
| 230 | if (pCounter != NULL) { |
|---|
| 231 | try { |
|---|
| 232 | PDH::PDHCounterInfo info = pCounter->getCounterInfo(); |
|---|
| 233 | errors.push_back(_T("CounterName: ") + info.szCounterName); |
|---|
| 234 | errors.push_back(_T("ExplainText: ") + info.szExplainText); |
|---|
| 235 | errors.push_back(_T("FullPath: ") + info.szFullPath); |
|---|
| 236 | errors.push_back(_T("InstanceName: ") + info.szInstanceName); |
|---|
| 237 | errors.push_back(_T("MachineName: ") + info.szMachineName); |
|---|
| 238 | errors.push_back(_T("ObjectName: ") + info.szObjectName); |
|---|
| 239 | errors.push_back(_T("ParentInstance: ") + info.szParentInstance); |
|---|
| 240 | errors.push_back(_T("Type: ") + strEx::itos(info.dwType)); |
|---|
| 241 | errors.push_back(_T("Scale: ") + strEx::itos(info.lScale)); |
|---|
| 242 | errors.push_back(_T("Default Scale: ") + strEx::itos(info.lDefaultScale)); |
|---|
| 243 | errors.push_back(_T("Status: ") + strEx::itos(info.CStatus)); |
|---|
| 244 | status.push_back(_T("described")); |
|---|
| 245 | } catch (const PDH::PDHException e) { |
|---|
| 246 | errors.push_back(_T("Describe failed: ") + e.getError()); |
|---|
| 247 | bStatus = false; |
|---|
| 248 | } |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | pdh.gatherData(); |
|---|
| 252 | pdh.close(); |
|---|
| 253 | status.push_back(_T("queried")); |
|---|
| 254 | } catch (const PDH::PDHException e) { |
|---|
| 255 | errors.push_back(_T("Query failed: ") + e.getError()); |
|---|
| 256 | bStatus = false; |
|---|
| 257 | try { |
|---|
| 258 | pdh.gatherData(); |
|---|
| 259 | pdh.close(); |
|---|
| 260 | bStatus = true; |
|---|
| 261 | } catch (const PDH::PDHException e) { |
|---|
| 262 | errors.push_back(_T("Query failed (again!): ") + e.getError()); |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | } |
|---|
| 267 | if (!bStatus) { |
|---|
| 268 | std::list<std::wstring>::const_iterator cit = status.begin(); |
|---|
| 269 | for (;cit != status.end(); ++cit) { |
|---|
| 270 | std::wcout << *cit << _T(", "); |
|---|
| 271 | } |
|---|
| 272 | std::wcout << std::endl; |
|---|
| 273 | std::wcout << _T(" | Log") << std::endl; |
|---|
| 274 | std::wcout << _T("--+------ -- -") << std::endl; |
|---|
| 275 | cit = errors.begin(); |
|---|
| 276 | for (;cit != errors.end(); ++cit) { |
|---|
| 277 | std::wcout << _T(" | ") << *cit << std::endl; |
|---|
| 278 | } |
|---|
| 279 | } else { |
|---|
| 280 | std::list<std::wstring>::const_iterator cit = status.begin(); |
|---|
| 281 | for (;cit != status.end(); ++cit) { |
|---|
| 282 | std::wcout << *cit << _T(", ");; |
|---|
| 283 | } |
|---|
| 284 | std::wcout << std::endl; |
|---|
| 285 | } |
|---|
| 286 | } |
|---|
| 287 | } |
|---|
| 288 | } else { |
|---|
| 289 | if ((*it).counters.size() == 0) { |
|---|
| 290 | std::wcout << _T("empty counter: ") << (*it).name << std::endl; |
|---|
| 291 | } |
|---|
| 292 | for (PDH::Enumerations::Counters::const_iterator it2 = (*it).counters.begin();it2!=(*it).counters.end();++it2) { |
|---|
| 293 | std::wstring counter = _T("\\") + (*it).name + _T("\\") + (*it2).name; |
|---|
| 294 | std::wcout << _T("testing: ") << counter << _T(": "); |
|---|
| 295 | std::wstring error; |
|---|
| 296 | if (PDH::PDHResolver::validate(counter, error, false)) { |
|---|
| 297 | std::wcout << _T(" found "); |
|---|
| 298 | } else { |
|---|
| 299 | std::wcout << _T(" *NOT* found (") << error << _T(") ") << std::endl; |
|---|
| 300 | break; |
|---|
| 301 | } |
|---|
| 302 | bool bOpend = false; |
|---|
| 303 | try { |
|---|
| 304 | PDH::PDHQuery pdh; |
|---|
| 305 | //PDHCollectors::StaticPDHCounterListener<double, PDH_FMT_DOUBLE> cDouble; |
|---|
| 306 | pdh.addCounter(counter); |
|---|
| 307 | pdh.open(); |
|---|
| 308 | pdh.gatherData(); |
|---|
| 309 | pdh.close(); |
|---|
| 310 | bOpend = true; |
|---|
| 311 | } catch (const PDH::PDHException e) { |
|---|
| 312 | std::wcout << _T(" could *not* be open (") << e.getError() << _T(") ") << std::endl; |
|---|
| 313 | break; |
|---|
| 314 | } |
|---|
| 315 | std::wcout << _T(" open "); |
|---|
| 316 | std::wcout << std::endl; |
|---|
| 317 | } |
|---|
| 318 | } |
|---|
| 319 | } |
|---|
| 320 | } else if (command == _T("listpdh")) { |
|---|
| 321 | bool porcelain = arguments.size() > 0 && arguments.front() == _T("--porcelain"); |
|---|
| 322 | PDH::Enumerations::Objects lst; |
|---|
| 323 | try { |
|---|
| 324 | lst = PDH::Enumerations::EnumObjects(); |
|---|
| 325 | } catch (const PDH::PDHException e) { |
|---|
| 326 | result = _T("ERROR: Service enumeration failed: ") + e.getError(); |
|---|
| 327 | return NSCAPI::returnUNKNOWN; |
|---|
| 328 | } |
|---|
| 329 | std::wstringstream ss; |
|---|
| 330 | BOOST_FOREACH(PDH::Enumerations::Object &obj, lst) { |
|---|
| 331 | if (!obj.error.empty()) { |
|---|
| 332 | ss << "error," << obj.name << "," << utf8::to_unicode(obj.error) << _T("\n"); |
|---|
| 333 | } else if (obj.instances.size() > 0) { |
|---|
| 334 | BOOST_FOREACH(const PDH::Enumerations::Instance &inst, obj.instances) { |
|---|
| 335 | BOOST_FOREACH(const PDH::Enumerations::Counter &count, obj.counters) { |
|---|
| 336 | if (porcelain) |
|---|
| 337 | ss << "counter," << obj.name << _T(",") << inst.name << _T(",") << count.name << _T("\n"); |
|---|
| 338 | else |
|---|
| 339 | ss << _T("\\") << obj.name << _T("(") << inst.name << _T(")\\") << count.name << _T("\n"); |
|---|
| 340 | } |
|---|
| 341 | } |
|---|
| 342 | } else { |
|---|
| 343 | BOOST_FOREACH(const PDH::Enumerations::Counter &count, obj.counters) { |
|---|
| 344 | if (porcelain) |
|---|
| 345 | ss << obj.name << _T(",") << count.name << _T("\n"); |
|---|
| 346 | else |
|---|
| 347 | ss << _T("\\") << obj.name << _T("\\") << count.name << _T("\n"); |
|---|
| 348 | } |
|---|
| 349 | } |
|---|
| 350 | } |
|---|
| 351 | result = ss.str(); |
|---|
| 352 | return NSCAPI::returnOK; |
|---|
| 353 | } else if (command == _T("pdhlookup")) { |
|---|
| 354 | try { |
|---|
| 355 | if (arguments.size() == 0) { |
|---|
| 356 | NSC_LOG_ERROR_STD(_T("Need to specify counter index name!")); |
|---|
| 357 | return 0; |
|---|
| 358 | } |
|---|
| 359 | std::wstring name = arguments.front(); |
|---|
| 360 | DWORD dw = PDH::PDHResolver::lookupIndex(name); |
|---|
| 361 | NSC_LOG_MESSAGE_STD(_T("--+--[ Lookup Result ]----------------------------------------")); |
|---|
| 362 | NSC_LOG_MESSAGE_STD(_T(" | Index for '") + name + _T("' is ") + strEx::itos(dw)); |
|---|
| 363 | NSC_LOG_MESSAGE_STD(_T("--+-----------------------------------------------------------")); |
|---|
| 364 | } catch (const PDH::PDHException e) { |
|---|
| 365 | NSC_LOG_ERROR_STD(_T("Failed to lookup index: ") + e.getError()); |
|---|
| 366 | return 0; |
|---|
| 367 | } |
|---|
| 368 | } else if (command == _T("pdhmatch")) { |
|---|
| 369 | try { |
|---|
| 370 | if (arguments.size() == 0) { |
|---|
| 371 | NSC_LOG_ERROR_STD(_T("Need to specify counter index name!")); |
|---|
| 372 | return 0; |
|---|
| 373 | } |
|---|
| 374 | std::wstring name = arguments.front(); |
|---|
| 375 | std::list<std::wstring> list = PDH::PDHResolver::PdhExpandCounterPath(name.c_str()); |
|---|
| 376 | NSC_LOG_MESSAGE_STD(_T("--+--[ Lookup Result ]----------------------------------------")); |
|---|
| 377 | for (std::list<std::wstring>::const_iterator cit = list.begin(); cit != list.end(); ++cit) { |
|---|
| 378 | NSC_LOG_MESSAGE_STD(_T(" | Found '") + *cit); |
|---|
| 379 | } |
|---|
| 380 | NSC_LOG_MESSAGE_STD(_T("--+-----------------------------------------------------------")); |
|---|
| 381 | } catch (const PDH::PDHException e) { |
|---|
| 382 | NSC_LOG_ERROR_STD(_T("Failed to lookup index: ") + e.getError()); |
|---|
| 383 | return 0; |
|---|
| 384 | } |
|---|
| 385 | } else if (command == _T("pdhobject")) { |
|---|
| 386 | try { |
|---|
| 387 | if (arguments.size() == 0) { |
|---|
| 388 | NSC_LOG_ERROR_STD(_T("Need to specify counter index name!")); |
|---|
| 389 | return 0; |
|---|
| 390 | } |
|---|
| 391 | std::wstring name = arguments.front(); |
|---|
| 392 | PDH::Enumerations::pdh_object_details list = PDH::Enumerations::EnumObjectInstances(name.c_str()); |
|---|
| 393 | NSC_LOG_MESSAGE_STD(_T("--+--[ Lookup Result ]----------------------------------------")); |
|---|
| 394 | for (std::list<std::wstring>::const_iterator cit = list.counters.begin(); cit != list.counters.end(); ++cit) { |
|---|
| 395 | NSC_LOG_MESSAGE_STD(_T(" | Found Counter: ") + *cit); |
|---|
| 396 | } |
|---|
| 397 | for (std::list<std::wstring>::const_iterator cit = list.instances.begin(); cit != list.instances.end(); ++cit) { |
|---|
| 398 | NSC_LOG_MESSAGE_STD(_T(" | Found Instance: ") + *cit); |
|---|
| 399 | } |
|---|
| 400 | NSC_LOG_MESSAGE_STD(_T("--+-----------------------------------------------------------")); |
|---|
| 401 | } catch (const PDH::PDHException e) { |
|---|
| 402 | NSC_LOG_ERROR_STD(_T("Failed to lookup index: ") + e.getError()); |
|---|
| 403 | return 0; |
|---|
| 404 | } |
|---|
| 405 | } |
|---|
| 406 | return 0; |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | /** |
|---|
| 411 | * Main command parser and delegator. |
|---|
| 412 | * This also handles a lot of the simpler responses (though some are deferred to other helper functions) |
|---|
| 413 | * |
|---|
| 414 | * |
|---|
| 415 | * @param command |
|---|
| 416 | * @param argLen |
|---|
| 417 | * @param **args |
|---|
| 418 | * @return |
|---|
| 419 | */ |
|---|
| 420 | NSCAPI::nagiosReturn CheckSystem::handleCommand(const std::wstring &target, const std::wstring &command, std::list<std::wstring> &arguments, std::wstring &message, std::wstring &perf) { |
|---|
| 421 | CheckSystem::returnBundle rb; |
|---|
| 422 | if (command == _T("checkcpu")) { |
|---|
| 423 | return checkCPU(arguments, message, perf); |
|---|
| 424 | } else if (command == _T("checkuptime")) { |
|---|
| 425 | return checkUpTime(arguments, message, perf); |
|---|
| 426 | } else if (command == _T("checkservicestate")) { |
|---|
| 427 | return checkServiceState(arguments, message, perf); |
|---|
| 428 | } else if (command == _T("checkprocstate")) { |
|---|
| 429 | return checkProcState(arguments, message, perf); |
|---|
| 430 | } else if (command == _T("checkmem")) { |
|---|
| 431 | return checkMem(arguments, message, perf); |
|---|
| 432 | } else if (command == _T("checkcounter")) { |
|---|
| 433 | return checkCounter(arguments, message, perf); |
|---|
| 434 | } else if (command == _T("listcounterinstances")) { |
|---|
| 435 | return listCounterInstances(arguments, message, perf); |
|---|
| 436 | } else if (command == _T("checksingleregentry")) { |
|---|
| 437 | return checkSingleRegEntry(arguments, message, perf); |
|---|
| 438 | } |
|---|
| 439 | return NSCAPI::returnIgnored; |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | class cpuload_handler { |
|---|
| 444 | public: |
|---|
| 445 | static int parse(std::wstring s) { |
|---|
| 446 | return strEx::stoi(s); |
|---|
| 447 | } |
|---|
| 448 | static int parse_percent(std::wstring s) { |
|---|
| 449 | return strEx::stoi(s); |
|---|
| 450 | } |
|---|
| 451 | static std::wstring print(int value) { |
|---|
| 452 | return strEx::itos(value) + _T("%"); |
|---|
| 453 | } |
|---|
| 454 | static std::wstring print_unformated(int value) { |
|---|
| 455 | return strEx::itos(value); |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | static std::wstring get_perf_unit(__int64 value) { |
|---|
| 459 | return _T("%"); |
|---|
| 460 | } |
|---|
| 461 | static std::wstring print_perf(__int64 value, std::wstring unit) { |
|---|
| 462 | return boost::lexical_cast<std::wstring>(value); |
|---|
| 463 | } |
|---|
| 464 | static std::wstring print_percent(int value) { |
|---|
| 465 | return boost::lexical_cast<std::wstring>(value) + _T("%"); |
|---|
| 466 | } |
|---|
| 467 | static std::wstring key_prefix() { |
|---|
| 468 | return _T("average load "); |
|---|
| 469 | } |
|---|
| 470 | static std::wstring key_postfix() { |
|---|
| 471 | return _T(""); |
|---|
| 472 | } |
|---|
| 473 | }; |
|---|
| 474 | NSCAPI::nagiosReturn CheckSystem::checkCPU(std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) { |
|---|
| 475 | typedef checkHolders::CheckContainer<checkHolders::MaxMinBounds<checkHolders::NumericBounds<int, cpuload_handler> > > CPULoadContainer; |
|---|
| 476 | |
|---|
| 477 | if (arguments.empty()) { |
|---|
| 478 | msg = _T("ERROR: Missing argument exception."); |
|---|
| 479 | return NSCAPI::returnUNKNOWN; |
|---|
| 480 | } |
|---|
| 481 | std::list<CPULoadContainer> list; |
|---|
| 482 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 483 | bool bNSClient = false; |
|---|
| 484 | bool bPerfData = true; |
|---|
| 485 | CPULoadContainer tmpObject; |
|---|
| 486 | |
|---|
| 487 | tmpObject.data = _T("cpuload"); |
|---|
| 488 | |
|---|
| 489 | MAP_OPTIONS_BEGIN(arguments) |
|---|
| 490 | MAP_OPTIONS_NUMERIC_ALL(tmpObject, _T("")) |
|---|
| 491 | MAP_OPTIONS_STR(_T("warn"), tmpObject.warn.max_) |
|---|
| 492 | MAP_OPTIONS_STR(_T("crit"), tmpObject.crit.max_) |
|---|
| 493 | MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) |
|---|
| 494 | MAP_OPTIONS_STR_AND(_T("time"), tmpObject.data, list.push_back(tmpObject)) |
|---|
| 495 | MAP_OPTIONS_STR_AND(_T("Time"), tmpObject.data, list.push_back(tmpObject)) |
|---|
| 496 | MAP_OPTIONS_SHOWALL(tmpObject) |
|---|
| 497 | MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient) |
|---|
| 498 | MAP_OPTIONS_SECONDARY_BEGIN(_T(":"), p2) |
|---|
| 499 | else if (p2.first == _T("Time")) { |
|---|
| 500 | tmpObject.data = p__.second; |
|---|
| 501 | tmpObject.alias = p2.second; |
|---|
| 502 | list.push_back(tmpObject); |
|---|
| 503 | } |
|---|
| 504 | MAP_OPTIONS_MISSING_EX(p2, msg, _T("Unknown argument: ")) |
|---|
| 505 | MAP_OPTIONS_SECONDARY_END() |
|---|
| 506 | MAP_OPTIONS_FALLBACK_AND(tmpObject.data, list.push_back(tmpObject)) |
|---|
| 507 | MAP_OPTIONS_END() |
|---|
| 508 | |
|---|
| 509 | for (std::list<CPULoadContainer>::const_iterator it = list.begin(); it != list.end(); ++it) { |
|---|
| 510 | CPULoadContainer load = (*it); |
|---|
| 511 | PDHCollector *pObject = pdhThread.getThread(); |
|---|
| 512 | if (!pObject) { |
|---|
| 513 | msg = _T("ERROR: PDH Collection thread not running."); |
|---|
| 514 | return NSCAPI::returnUNKNOWN; |
|---|
| 515 | } |
|---|
| 516 | int value = pObject->getCPUAvrage(load.data + _T("m")); |
|---|
| 517 | if (value == -1) { |
|---|
| 518 | msg = _T("ERROR: Could not get data for ") + load.getAlias() + _T(" perhaps we don't collect data this far back?"); |
|---|
| 519 | return NSCAPI::returnUNKNOWN; |
|---|
| 520 | } |
|---|
| 521 | if (bNSClient) { |
|---|
| 522 | if (!msg.empty()) msg += _T("&"); |
|---|
| 523 | msg += strEx::itos(value); |
|---|
| 524 | } else { |
|---|
| 525 | load.setDefault(tmpObject); |
|---|
| 526 | load.perfData = bPerfData; |
|---|
| 527 | load.runCheck(value, returnCode, msg, perf); |
|---|
| 528 | } |
|---|
| 529 | } |
|---|
| 530 | |
|---|
| 531 | if (msg.empty()) |
|---|
| 532 | msg = _T("OK CPU Load ok."); |
|---|
| 533 | else if (!bNSClient) |
|---|
| 534 | msg = nscapi::plugin_helper::translateReturn(returnCode) + _T(": ") + msg; |
|---|
| 535 | return returnCode; |
|---|
| 536 | } |
|---|
| 537 | |
|---|
| 538 | NSCAPI::nagiosReturn CheckSystem::checkUpTime(std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) |
|---|
| 539 | { |
|---|
| 540 | typedef checkHolders::CheckContainer<checkHolders::MaxMinBoundsTime> UpTimeContainer; |
|---|
| 541 | |
|---|
| 542 | if (arguments.empty()) { |
|---|
| 543 | msg = _T("ERROR: Missing argument exception."); |
|---|
| 544 | return NSCAPI::returnUNKNOWN; |
|---|
| 545 | } |
|---|
| 546 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 547 | bool bNSClient = false; |
|---|
| 548 | bool bPerfData = true; |
|---|
| 549 | UpTimeContainer bounds; |
|---|
| 550 | |
|---|
| 551 | bounds.data = _T("uptime"); |
|---|
| 552 | |
|---|
| 553 | MAP_OPTIONS_BEGIN(arguments) |
|---|
| 554 | MAP_OPTIONS_NUMERIC_ALL(bounds, _T("")) |
|---|
| 555 | MAP_OPTIONS_STR(_T("warn"), bounds.warn.min_) |
|---|
| 556 | MAP_OPTIONS_STR(_T("crit"), bounds.crit.min_) |
|---|
| 557 | MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) |
|---|
| 558 | MAP_OPTIONS_STR(_T("Alias"), bounds.data) |
|---|
| 559 | MAP_OPTIONS_SHOWALL(bounds) |
|---|
| 560 | MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient) |
|---|
| 561 | MAP_OPTIONS_MISSING(msg, _T("Unknown argument: ")) |
|---|
| 562 | MAP_OPTIONS_END() |
|---|
| 563 | |
|---|
| 564 | |
|---|
| 565 | PDHCollector *pObject = pdhThread.getThread(); |
|---|
| 566 | if (!pObject) { |
|---|
| 567 | msg = _T("ERROR: PDH Collection thread not running."); |
|---|
| 568 | return NSCAPI::returnUNKNOWN; |
|---|
| 569 | } |
|---|
| 570 | unsigned long long value = pObject->getUptime(); |
|---|
| 571 | if (value == -1) { |
|---|
| 572 | msg = _T("ERROR: Could not get value"); |
|---|
| 573 | return NSCAPI::returnUNKNOWN; |
|---|
| 574 | } |
|---|
| 575 | if (bNSClient) { |
|---|
| 576 | msg = strEx::itos(value); |
|---|
| 577 | } else { |
|---|
| 578 | value *= 1000; |
|---|
| 579 | bounds.perfData = bPerfData; |
|---|
| 580 | bounds.runCheck(value, returnCode, msg, perf); |
|---|
| 581 | } |
|---|
| 582 | |
|---|
| 583 | if (msg.empty()) |
|---|
| 584 | msg = _T("OK all counters within bounds."); |
|---|
| 585 | else if (!bNSClient) |
|---|
| 586 | msg = nscapi::plugin_helper::translateReturn(returnCode) + _T(": ") + msg; |
|---|
| 587 | return returnCode; |
|---|
| 588 | } |
|---|
| 589 | |
|---|
| 590 | // @todo state_handler |
|---|
| 591 | |
|---|
| 592 | |
|---|
| 593 | inline int get_state(DWORD state) { |
|---|
| 594 | if (state == SERVICE_RUNNING) |
|---|
| 595 | return checkHolders::state_started; |
|---|
| 596 | else if (state == SERVICE_STOPPED) |
|---|
| 597 | return checkHolders::state_stopped; |
|---|
| 598 | else if (state == MY_SERVICE_NOT_FOUND) |
|---|
| 599 | return checkHolders::state_not_found; |
|---|
| 600 | return checkHolders::state_none; |
|---|
| 601 | } |
|---|
| 602 | |
|---|
| 603 | /** |
|---|
| 604 | * Retrieve the service state of one or more services (by name). |
|---|
| 605 | * Parse a list with a service names and verify that all named services are running. |
|---|
| 606 | * <pre> |
|---|
| 607 | * Syntax: |
|---|
| 608 | * request: checkServiceState <option> [<option> [...]] |
|---|
| 609 | * Return: <return state>&<service1> : <state1> - <service2> : <state2> - ... |
|---|
| 610 | * Available options: |
|---|
| 611 | * <name>=<state> Check if a service has a specific state |
|---|
| 612 | * State can be wither started or stopped |
|---|
| 613 | * ShowAll Show the state of all listed service. If not set only critical services are listed. |
|---|
| 614 | * Examples: |
|---|
| 615 | * checkServiceState showAll myService MyService |
|---|
| 616 | *</pre> |
|---|
| 617 | * |
|---|
| 618 | * @param command Command to execute |
|---|
| 619 | * @param argLen The length of the argument buffer |
|---|
| 620 | * @param **char_args The argument buffer |
|---|
| 621 | * @param &msg String to put message in |
|---|
| 622 | * @param &perf String to put performance data in |
|---|
| 623 | * @return The status of the command |
|---|
| 624 | */ |
|---|
| 625 | NSCAPI::nagiosReturn CheckSystem::checkServiceState(std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) |
|---|
| 626 | { |
|---|
| 627 | typedef checkHolders::CheckContainer<checkHolders::SimpleBoundsStateBoundsInteger> StateContainer; |
|---|
| 628 | if (arguments.empty()) { |
|---|
| 629 | msg = _T("ERROR: Missing argument exception."); |
|---|
| 630 | return NSCAPI::returnUNKNOWN; |
|---|
| 631 | } |
|---|
| 632 | std::list<StateContainer> list; |
|---|
| 633 | std::set<std::wstring> excludeList; |
|---|
| 634 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 635 | bool bNSClient = false; |
|---|
| 636 | StateContainer tmpObject; |
|---|
| 637 | bool bPerfData = true; |
|---|
| 638 | bool bAutoStart = false; |
|---|
| 639 | unsigned int truncate = 0; |
|---|
| 640 | |
|---|
| 641 | tmpObject.data = _T("service"); |
|---|
| 642 | tmpObject.crit.state = _T("started"); |
|---|
| 643 | //{{ |
|---|
| 644 | MAP_OPTIONS_BEGIN(arguments) |
|---|
| 645 | MAP_OPTIONS_SHOWALL(tmpObject) |
|---|
| 646 | MAP_OPTIONS_STR(_T("Alias"), tmpObject.data) |
|---|
| 647 | MAP_OPTIONS_STR2INT(_T("truncate"), truncate) |
|---|
| 648 | MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) |
|---|
| 649 | MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient) |
|---|
| 650 | MAP_OPTIONS_BOOL_TRUE(_T("CheckAll"), bAutoStart) |
|---|
| 651 | MAP_OPTIONS_INSERT(_T("exclude"), excludeList) |
|---|
| 652 | //MAP_OPTIONS_SECONDARY_BEGIN(_T(":"), p2) |
|---|
| 653 | //MAP_OPTIONS_MISSING_EX(p2, msg, _T("Unknown argument: ")) |
|---|
| 654 | //MAP_OPTIONS_SECONDARY_END() |
|---|
| 655 | else { |
|---|
| 656 | tmpObject.data = p__.first; |
|---|
| 657 | if (p__.second.empty()) |
|---|
| 658 | tmpObject.crit.state = _T("started"); |
|---|
| 659 | else |
|---|
| 660 | tmpObject.crit.state = p__.second; |
|---|
| 661 | list.push_back(tmpObject); |
|---|
| 662 | } |
|---|
| 663 | MAP_OPTIONS_END() |
|---|
| 664 | //}} |
|---|
| 665 | if (bAutoStart) { |
|---|
| 666 | // get a list of all service with startup type Automatic |
|---|
| 667 | /* |
|---|
| 668 | ;check_all_services[SERVICE_BOOT_START]=ignored |
|---|
| 669 | ;check_all_services[SERVICE_SYSTEM_START]=ignored |
|---|
| 670 | ;check_all_services[SERVICE_AUTO_START]=started |
|---|
| 671 | ;check_all_services[SERVICE_DEMAND_START]=ignored |
|---|
| 672 | ;check_all_services[SERVICE_DISABLED]=stopped |
|---|
| 673 | std::wstring wantedMethod = NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_ENUMPROC_METHOD, C_SYSTEM_ENUMPROC_METHOD_DEFAULT); |
|---|
| 674 | */ |
|---|
| 675 | |
|---|
| 676 | bool vista = systemInfo::isAboveVista(systemInfo::getOSVersion()); |
|---|
| 677 | std::list<TNtServiceInfo> service_list_automatic = TNtServiceInfo::EnumServices(SERVICE_WIN32,SERVICE_INACTIVE|SERVICE_ACTIVE, vista); |
|---|
| 678 | for (std::list<TNtServiceInfo>::const_iterator service =service_list_automatic.begin();service!=service_list_automatic.end();++service) { |
|---|
| 679 | if (excludeList.find((*service).m_strServiceName) == excludeList.end()) { |
|---|
| 680 | tmpObject.data = (*service).m_strServiceName; |
|---|
| 681 | std::wstring x = lookups_[(*service).m_dwStartType]; |
|---|
| 682 | if (x != _T("ignored")) { |
|---|
| 683 | tmpObject.crit.state = x; |
|---|
| 684 | list.push_back(tmpObject); |
|---|
| 685 | } |
|---|
| 686 | } |
|---|
| 687 | } |
|---|
| 688 | tmpObject.crit.state = _T("ignored"); |
|---|
| 689 | } |
|---|
| 690 | for (std::list<StateContainer>::iterator it = list.begin(); it != list.end(); ++it) { |
|---|
| 691 | TNtServiceInfo info; |
|---|
| 692 | if (bNSClient) { |
|---|
| 693 | try { |
|---|
| 694 | info = TNtServiceInfo::GetService((*it).data.c_str()); |
|---|
| 695 | } catch (NTServiceException e) { |
|---|
| 696 | if (!msg.empty()) msg += _T(" - "); |
|---|
| 697 | msg += (*it).data + _T(": Error"); |
|---|
| 698 | nscapi::plugin_helper::escalteReturnCodeToWARN(returnCode); |
|---|
| 699 | continue; |
|---|
| 700 | } |
|---|
| 701 | if ((*it).crit.state.hasBounds()) { |
|---|
| 702 | bool ok = (*it).crit.state.check(get_state(info.m_dwCurrentState)); |
|---|
| 703 | if (!ok || (*it).showAll()) { |
|---|
| 704 | if (info.m_dwCurrentState == SERVICE_RUNNING) { |
|---|
| 705 | if (!msg.empty()) msg += _T(" - "); |
|---|
| 706 | msg += (*it).data + _T(": Started"); |
|---|
| 707 | } else if (info.m_dwCurrentState == SERVICE_STOPPED) { |
|---|
| 708 | if (!msg.empty()) msg += _T(" - "); |
|---|
| 709 | msg += (*it).data + _T(": Stopped"); |
|---|
| 710 | } else if (info.m_dwCurrentState == MY_SERVICE_NOT_FOUND) { |
|---|
| 711 | if (!msg.empty()) msg += _T(" - "); |
|---|
| 712 | msg += (*it).data + _T(": Not found"); |
|---|
| 713 | } else { |
|---|
| 714 | if (!msg.empty()) msg += _T(" - "); |
|---|
| 715 | msg += (*it).data + _T(": Unknown"); |
|---|
| 716 | } |
|---|
| 717 | if (!ok) |
|---|
| 718 | nscapi::plugin_helper::escalteReturnCodeToCRIT(returnCode); |
|---|
| 719 | } |
|---|
| 720 | } |
|---|
| 721 | } else { |
|---|
| 722 | try { |
|---|
| 723 | info = TNtServiceInfo::GetService((*it).data.c_str()); |
|---|
| 724 | } catch (NTServiceException e) { |
|---|
| 725 | NSC_LOG_ERROR_STD(e.getError()); |
|---|
| 726 | msg = e.getError(); |
|---|
| 727 | return NSCAPI::returnUNKNOWN; |
|---|
| 728 | } |
|---|
| 729 | checkHolders::state_type value; |
|---|
| 730 | if (info.m_dwCurrentState == SERVICE_RUNNING) |
|---|
| 731 | value = checkHolders::state_started; |
|---|
| 732 | else if (info.m_dwCurrentState == SERVICE_STOPPED) |
|---|
| 733 | value = checkHolders::state_stopped; |
|---|
| 734 | else if (info.m_dwCurrentState == MY_SERVICE_NOT_FOUND) |
|---|
| 735 | value = checkHolders::state_not_found; |
|---|
| 736 | else { |
|---|
| 737 | NSC_LOG_MESSAGE(_T("Service had no (valid) state: ") + (*it).data + _T(" (") + strEx::itos(info.m_dwCurrentState) + _T(")")); |
|---|
| 738 | value = checkHolders::state_none; |
|---|
| 739 | } |
|---|
| 740 | unsigned int x = returnCode; |
|---|
| 741 | (*it).perfData = bPerfData; |
|---|
| 742 | (*it).setDefault(tmpObject); |
|---|
| 743 | (*it).runCheck(value, returnCode, msg, perf); |
|---|
| 744 | // NSC_LOG_MESSAGE(_T("Service: ") + (*it).data + _T(" (") + strEx::itos(info.m_dwCurrentState) + _T(":") + strEx::itos((*it).warn.state.value_) + _T(":") + strEx::itos((*it).crit.state.value_) + _T(") -- (") + strEx::itos(returnCode) + _T(":") + strEx::itos(x) + _T(")")); |
|---|
| 745 | } |
|---|
| 746 | } |
|---|
| 747 | if ((truncate > 0) && (msg.length() > (truncate-4))) |
|---|
| 748 | msg = msg.substr(0, truncate-4) + _T("..."); |
|---|
| 749 | if (msg.empty() && returnCode == NSCAPI::returnOK) |
|---|
| 750 | msg = _T("OK: All services are in their appropriate state."); |
|---|
| 751 | else if (msg.empty()) |
|---|
| 752 | msg = nscapi::plugin_helper::translateReturn(returnCode) + _T(": Whooha this is odd."); |
|---|
| 753 | else if (!bNSClient) |
|---|
| 754 | msg = nscapi::plugin_helper::translateReturn(returnCode) + _T(": ") + msg; |
|---|
| 755 | return returnCode; |
|---|
| 756 | } |
|---|
| 757 | |
|---|
| 758 | /** |
|---|
| 759 | * Check available memory and return various check results |
|---|
| 760 | * Example: checkMem showAll maxWarn=50 maxCrit=75 |
|---|
| 761 | * |
|---|
| 762 | * @param command Command to execute |
|---|
| 763 | * @param argLen The length of the argument buffer |
|---|
| 764 | * @param **char_args The argument buffer |
|---|
| 765 | * @param &msg String to put message in |
|---|
| 766 | * @param &perf String to put performance data in |
|---|
| 767 | * @return The status of the command |
|---|
| 768 | */ |
|---|
| 769 | NSCAPI::nagiosReturn CheckSystem::checkMem(std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) |
|---|
| 770 | { |
|---|
| 771 | typedef checkHolders::CheckContainer<checkHolders::MaxMinBounds<checkHolders::NumericPercentageBounds<checkHolders::PercentageValueType<unsigned __int64, unsigned __int64>, checkHolders::disk_size_handler<unsigned __int64> > > > MemoryContainer; |
|---|
| 772 | if (arguments.empty()) { |
|---|
| 773 | msg = _T("ERROR: Missing argument exception."); |
|---|
| 774 | return NSCAPI::returnUNKNOWN; |
|---|
| 775 | } |
|---|
| 776 | std::list<MemoryContainer> list; |
|---|
| 777 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 778 | bool bShowAll = false; |
|---|
| 779 | bool bPerfData = true; |
|---|
| 780 | bool bNSClient = false; |
|---|
| 781 | MemoryContainer tmpObject; |
|---|
| 782 | |
|---|
| 783 | MAP_OPTIONS_BEGIN(arguments) |
|---|
| 784 | MAP_OPTIONS_SHOWALL(tmpObject) |
|---|
| 785 | MAP_OPTIONS_STR_AND(_T("type"), tmpObject.data, list.push_back(tmpObject)) |
|---|
| 786 | MAP_OPTIONS_STR_AND(_T("Type"), tmpObject.data, list.push_back(tmpObject)) |
|---|
| 787 | MAP_OPTIONS_SECONDARY_BEGIN(_T(":"), p2) |
|---|
| 788 | MAP_OPTIONS_SECONDARY_STR_AND(p2,_T("type"), tmpObject.data, tmpObject.alias, list.push_back(tmpObject)) |
|---|
| 789 | MAP_OPTIONS_MISSING_EX(p2, msg, _T("Unknown argument: ")) |
|---|
| 790 | MAP_OPTIONS_SECONDARY_END() |
|---|
| 791 | MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) |
|---|
| 792 | MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient) |
|---|
| 793 | MAP_OPTIONS_DISK_ALL(tmpObject, _T(""), _T("Free"), _T("Used")) |
|---|
| 794 | MAP_OPTIONS_STR(_T("Alias"), tmpObject.data) |
|---|
| 795 | MAP_OPTIONS_SHOWALL(tmpObject) |
|---|
| 796 | MAP_OPTIONS_MISSING(msg, _T("Unknown argument: ")) |
|---|
| 797 | MAP_OPTIONS_END() |
|---|
| 798 | |
|---|
| 799 | if (bNSClient) { |
|---|
| 800 | tmpObject.data = _T("paged"); |
|---|
| 801 | list.push_back(tmpObject); |
|---|
| 802 | } |
|---|
| 803 | |
|---|
| 804 | checkHolders::PercentageValueType<unsigned long long, unsigned long long> dataPaged; |
|---|
| 805 | CheckMemory::memData data; |
|---|
| 806 | bool firstPaged = true; |
|---|
| 807 | bool firstMem = true; |
|---|
| 808 | for (std::list<MemoryContainer>::const_iterator pit = list.begin(); pit != list.end(); ++pit) { |
|---|
| 809 | MemoryContainer check = (*pit); |
|---|
| 810 | check.setDefault(tmpObject); |
|---|
| 811 | checkHolders::PercentageValueType<unsigned long long, unsigned long long> value; |
|---|
| 812 | if (firstPaged && (check.data == _T("paged"))) { |
|---|
| 813 | firstPaged = false; |
|---|
| 814 | PDHCollector *pObject = pdhThread.getThread(); |
|---|
| 815 | if (!pObject) { |
|---|
| 816 | msg = _T("ERROR: PDH Collection thread not running."); |
|---|
| 817 | return NSCAPI::returnUNKNOWN; |
|---|
| 818 | } |
|---|
| 819 | dataPaged.value = pObject->getMemCommit(); |
|---|
| 820 | if (dataPaged.value == -1) { |
|---|
| 821 | msg = _T("ERROR: Failed to get PDH value."); |
|---|
| 822 | return NSCAPI::returnUNKNOWN; |
|---|
| 823 | } |
|---|
| 824 | dataPaged.total = pObject->getMemCommitLimit(); |
|---|
| 825 | if (dataPaged.total == -1) { |
|---|
| 826 | msg = _T("ERROR: Failed to get PDH value."); |
|---|
| 827 | return NSCAPI::returnUNKNOWN; |
|---|
| 828 | } |
|---|
| 829 | } else if (firstMem) { |
|---|
| 830 | try { |
|---|
| 831 | data = memoryChecker.getMemoryStatus(); |
|---|
| 832 | } catch (CheckMemoryException e) { |
|---|
| 833 | msg = e.getError(); |
|---|
| 834 | return NSCAPI::returnCRIT; |
|---|
| 835 | } |
|---|
| 836 | } |
|---|
| 837 | |
|---|
| 838 | if (check.data == _T("page")) { |
|---|
| 839 | value.value = data.pageFile.total-data.pageFile.avail; // mem.dwTotalPageFile-mem.dwAvailPageFile; |
|---|
| 840 | value.total = data.pageFile.total; //mem.dwTotalPageFile; |
|---|
| 841 | if (check.alias.empty()) |
|---|
| 842 | check.alias = _T("page file"); |
|---|
| 843 | } else if (check.data == _T("physical")) { |
|---|
| 844 | value.value = data.phys.total-data.phys.avail; //mem.dwTotalPhys-mem.dwAvailPhys; |
|---|
| 845 | value.total = data.phys.total; //mem.dwTotalPhys; |
|---|
| 846 | if (check.alias.empty()) |
|---|
| 847 | check.alias = _T("physical memory"); |
|---|
| 848 | } else if (check.data == _T("virtual")) { |
|---|
| 849 | value.value = data.virtualMem.total-data.virtualMem.avail;//mem.dwTotalVirtual-mem.dwAvailVirtual; |
|---|
| 850 | value.total = data.virtualMem.total;//mem.dwTotalVirtual; |
|---|
| 851 | if (check.alias.empty()) |
|---|
| 852 | check.alias = _T("virtual memory"); |
|---|
| 853 | } else if (check.data == _T("paged")) { |
|---|
| 854 | value.value = dataPaged.value; |
|---|
| 855 | value.total = dataPaged.total; |
|---|
| 856 | if (check.alias.empty()) |
|---|
| 857 | check.alias = _T("paged bytes"); |
|---|
| 858 | } else { |
|---|
| 859 | msg = check.data + _T(" is not a known check..."); |
|---|
| 860 | return NSCAPI::returnCRIT; |
|---|
| 861 | } |
|---|
| 862 | if (bNSClient) { |
|---|
| 863 | msg = strEx::itos(value.total) + _T("&") + strEx::itos(value.value); |
|---|
| 864 | return NSCAPI::returnOK; |
|---|
| 865 | } else { |
|---|
| 866 | check.perfData = bPerfData; |
|---|
| 867 | check.runCheck(value, returnCode, msg, perf); |
|---|
| 868 | } |
|---|
| 869 | } |
|---|
| 870 | |
|---|
| 871 | if (msg.empty()) |
|---|
| 872 | msg = _T("OK memory within bounds."); |
|---|
| 873 | else |
|---|
| 874 | msg = nscapi::plugin_helper::translateReturn(returnCode) + _T(": ") + msg; |
|---|
| 875 | return returnCode; |
|---|
| 876 | } |
|---|
| 877 | typedef struct NSPROCDATA__ { |
|---|
| 878 | unsigned int count; |
|---|
| 879 | unsigned int hung_count; |
|---|
| 880 | CEnumProcess::CProcessEntry entry; |
|---|
| 881 | std::wstring key; |
|---|
| 882 | |
|---|
| 883 | NSPROCDATA__() : count(0), hung_count(0) {} |
|---|
| 884 | NSPROCDATA__(const NSPROCDATA__ &other) : count(other.count), hung_count(other.hung_count), entry(other.entry), key(other.key) {} |
|---|
| 885 | } NSPROCDATA; |
|---|
| 886 | typedef std::map<std::wstring,NSPROCDATA> NSPROCLST; |
|---|
| 887 | |
|---|
| 888 | class NSC_error : public CEnumProcess::error_reporter { |
|---|
| 889 | void report_error(std::wstring error) { |
|---|
| 890 | NSC_LOG_ERROR(error); |
|---|
| 891 | } |
|---|
| 892 | void report_warning(std::wstring error) { |
|---|
| 893 | NSC_LOG_MESSAGE(error); |
|---|
| 894 | } |
|---|
| 895 | void report_debug(std::wstring error) { |
|---|
| 896 | NSC_DEBUG_MSG_STD(_T("PROC::: ") + error); |
|---|
| 897 | } |
|---|
| 898 | void report_debug_enter(std::wstring error) { |
|---|
| 899 | NSC_DEBUG_MSG_STD(_T("PROC>>> ") + error); |
|---|
| 900 | } |
|---|
| 901 | void report_debug_exit(std::wstring error) { |
|---|
| 902 | NSC_DEBUG_MSG_STD(_T("PROC<<<") + error); |
|---|
| 903 | } |
|---|
| 904 | }; |
|---|
| 905 | |
|---|
| 906 | /** |
|---|
| 907 | * Get a hash_map with all running processes. |
|---|
| 908 | * @return a hash_map with all running processes |
|---|
| 909 | */ |
|---|
| 910 | NSPROCLST GetProcessList(bool getCmdLines, bool use16Bit) |
|---|
| 911 | { |
|---|
| 912 | NSPROCLST ret; |
|---|
| 913 | CEnumProcess enumeration; |
|---|
| 914 | if (!enumeration.has_PSAPI()) { |
|---|
| 915 | NSC_LOG_ERROR_STD(_T("Failed to enumerat processes")); |
|---|
| 916 | NSC_LOG_ERROR_STD(_T("PSAPI method not availabletry installing \"Platform SDK Redistributable: PSAPI for Windows NT\" from Microsoft.")); |
|---|
| 917 | NSC_LOG_ERROR_STD(_T("Try this URL: http://www.microsoft.com/downloads/details.aspx?FamilyID=3d1fbaed-d122-45cf-9d46-1cae384097ac")); |
|---|
| 918 | throw CEnumProcess::process_enumeration_exception(_T("PSAPI not avalible, please see eror log for details.")); |
|---|
| 919 | } |
|---|
| 920 | NSC_error err; |
|---|
| 921 | CEnumProcess::process_list list = enumeration.enumerate_processes(getCmdLines, use16Bit, &err); |
|---|
| 922 | for (CEnumProcess::process_list::const_iterator entry = list.begin(); entry != list.end(); ++entry) { |
|---|
| 923 | std::wstring key; |
|---|
| 924 | if (getCmdLines && !(*entry).command_line.empty()) |
|---|
| 925 | key = (*entry).command_line; |
|---|
| 926 | else |
|---|
| 927 | key = boost::to_lower_copy((*entry).filename); |
|---|
| 928 | NSPROCLST::iterator it = ret.find(key); |
|---|
| 929 | if (it == ret.end()) { |
|---|
| 930 | ret[key].entry = (*entry); |
|---|
| 931 | ret[key].count = 1; |
|---|
| 932 | ret[key].hung_count = (*entry).hung?1:0; |
|---|
| 933 | ret[key].key = key; |
|---|
| 934 | } else { |
|---|
| 935 | if ((*entry).hung) |
|---|
| 936 | (*it).second.hung_count++; |
|---|
| 937 | (*it).second.count++; |
|---|
| 938 | } |
|---|
| 939 | } |
|---|
| 940 | return ret; |
|---|
| 941 | } |
|---|
| 942 | |
|---|
| 943 | |
|---|
| 944 | |
|---|
| 945 | struct process_count_result { |
|---|
| 946 | unsigned long running; |
|---|
| 947 | unsigned long hung; |
|---|
| 948 | process_count_result() : running(0), hung(0) {} |
|---|
| 949 | |
|---|
| 950 | std::wstring format_value(std::wstring tag, unsigned long count) { |
|---|
| 951 | if (count > 1) |
|---|
| 952 | return tag + _T("(") + strEx::itos(count) +_T(")"); |
|---|
| 953 | if (count == 1) |
|---|
| 954 | return tag; |
|---|
| 955 | return _T(""); |
|---|
| 956 | } |
|---|
| 957 | std::wstring to_wstring() { |
|---|
| 958 | if (running > 0 && hung > 0) |
|---|
| 959 | return format_value(_T("running"), running) + _T(", ") + format_value(_T("hung"), hung); |
|---|
| 960 | if (running > 0) |
|---|
| 961 | return format_value(_T("running"), running); |
|---|
| 962 | if (hung > 0) |
|---|
| 963 | return format_value(_T("hung"), hung); |
|---|
| 964 | return _T("stopped"); |
|---|
| 965 | } |
|---|
| 966 | std::wstring to_wstring_short() { |
|---|
| 967 | if (running > 0 && hung > 0) |
|---|
| 968 | return _T("running, hung"); |
|---|
| 969 | if (running > 0) |
|---|
| 970 | return _T("running"); |
|---|
| 971 | if (hung > 0) |
|---|
| 972 | return _T("hung"); |
|---|
| 973 | return _T("stopped"); |
|---|
| 974 | } |
|---|
| 975 | |
|---|
| 976 | }; |
|---|
| 977 | |
|---|
| 978 | class ProcessBound { |
|---|
| 979 | public: |
|---|
| 980 | checkHolders::ExactBounds<checkHolders::NumericBounds<unsigned long, checkHolders::int_handler> > running; |
|---|
| 981 | checkHolders::ExactBounds<checkHolders::NumericBounds<unsigned long, checkHolders::int_handler> > hung; |
|---|
| 982 | typedef checkHolders::NumericBounds<unsigned long, checkHolders::int_handler> THolder; |
|---|
| 983 | |
|---|
| 984 | typedef ProcessBound TMyType; |
|---|
| 985 | typedef process_count_result TValueType; |
|---|
| 986 | |
|---|
| 987 | ProcessBound() {} |
|---|
| 988 | ProcessBound(const ProcessBound &other) { |
|---|
| 989 | running = other.running; |
|---|
| 990 | hung = other.hung; |
|---|
| 991 | } |
|---|
| 992 | |
|---|
| 993 | void reset() { |
|---|
| 994 | running.reset(); |
|---|
| 995 | hung.reset(); |
|---|
| 996 | } |
|---|
| 997 | bool hasBounds() { |
|---|
| 998 | return running.hasBounds() || hung.hasBounds(); |
|---|
| 999 | } |
|---|
| 1000 | static std::wstring toStringLong(TValueType &value) { |
|---|
| 1001 | return value.to_wstring(); |
|---|
| 1002 | } |
|---|
| 1003 | static std::wstring toStringShort(TValueType &value) { |
|---|
| 1004 | return value.to_wstring_short(); |
|---|
| 1005 | } |
|---|
| 1006 | std::wstring gatherPerfData(std::wstring alias, TValueType &value, TMyType &warn, TMyType &crit) { |
|---|
| 1007 | if (hung.hasBounds()) |
|---|
| 1008 | return hung.gatherPerfData(alias, value.hung, warn.hung, crit.hung); |
|---|
| 1009 | return running.gatherPerfData(alias, value.running, warn.running, crit.running); |
|---|
| 1010 | } |
|---|
| 1011 | std::wstring gatherPerfData(std::wstring alias, TValueType &value) { |
|---|
| 1012 | THolder tmp; |
|---|
| 1013 | if (hung.hasBounds()) |
|---|
| 1014 | return tmp.gatherPerfData(alias, value.hung); |
|---|
| 1015 | return tmp.gatherPerfData(alias, value.running); |
|---|
| 1016 | } |
|---|
| 1017 | bool check(TValueType &value, std::wstring lable, std::wstring &message, checkHolders::ResultType type) { |
|---|
| 1018 | if (hung.hasBounds()) { |
|---|
| 1019 | return hung.check_preformatted(value.hung, value.to_wstring(), lable, message, type); |
|---|
| 1020 | } else { |
|---|
| 1021 | return running.check_preformatted(value.running, value.to_wstring(), lable, message, type); |
|---|
| 1022 | } |
|---|
| 1023 | return false; |
|---|
| 1024 | } |
|---|
| 1025 | |
|---|
| 1026 | }; |
|---|
| 1027 | /** |
|---|
| 1028 | * Check process state and return result |
|---|
| 1029 | * |
|---|
| 1030 | * @param command Command to execute |
|---|
| 1031 | * @param argLen The length of the argument buffer |
|---|
| 1032 | * @param **char_args The argument buffer |
|---|
| 1033 | * @param &msg String to put message in |
|---|
| 1034 | * @param &perf String to put performance data in |
|---|
| 1035 | * @return The status of the command |
|---|
| 1036 | */ |
|---|
| 1037 | NSCAPI::nagiosReturn CheckSystem::checkProcState(std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) |
|---|
| 1038 | { |
|---|
| 1039 | typedef checkHolders::CheckContainer<ProcessBound> StateContainer; |
|---|
| 1040 | |
|---|
| 1041 | // typedef checkHolders::CheckContainer<checkHolders::ExactBoundsState> StateContainer2; |
|---|
| 1042 | |
|---|
| 1043 | if (arguments.empty()) { |
|---|
| 1044 | msg = _T("ERROR: Missing argument exception."); |
|---|
| 1045 | return NSCAPI::returnUNKNOWN; |
|---|
| 1046 | } |
|---|
| 1047 | std::list<StateContainer> list; |
|---|
| 1048 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 1049 | bool bNSClient = false; |
|---|
| 1050 | StateContainer tmpObject; |
|---|
| 1051 | bool bPerfData = true; |
|---|
| 1052 | bool use16bit = false; |
|---|
| 1053 | bool useCmdLine = false; |
|---|
| 1054 | bool ignoreState = false; |
|---|
| 1055 | typedef enum { |
|---|
| 1056 | match_string, match_substring, match_regexp |
|---|
| 1057 | } match_type; |
|---|
| 1058 | match_type match = match_string; |
|---|
| 1059 | |
|---|
| 1060 | |
|---|
| 1061 | |
|---|
| 1062 | //tmpObject.data = _T("uptime"); |
|---|
| 1063 | //tmpObject.crit.min = 1; |
|---|
| 1064 | |
|---|
| 1065 | MAP_OPTIONS_BEGIN(arguments) |
|---|
| 1066 | //MAP_OPTIONS_NUMERIC_ALL(tmpObject, _T("Count")) |
|---|
| 1067 | MAP_OPTIONS_EXACT_NUMERIC_ALL_EX(tmpObject, _T("Count"), running) |
|---|
| 1068 | MAP_OPTIONS_EXACT_NUMERIC_LEGACY_EX(tmpObject, _T("Count"), running) |
|---|
| 1069 | MAP_OPTIONS_EXACT_NUMERIC_ALL_EX(tmpObject, _T("HungCount"), hung) |
|---|
| 1070 | MAP_OPTIONS_EXACT_NUMERIC_LEGACY_EX(tmpObject, _T("HungCount"), hung) |
|---|
| 1071 | MAP_OPTIONS_STR(_T("Alias"), tmpObject.alias) |
|---|
| 1072 | MAP_OPTIONS_SHOWALL(tmpObject) |
|---|
| 1073 | MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) |
|---|
| 1074 | MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient) |
|---|
| 1075 | MAP_OPTIONS_BOOL_TRUE(_T("ignore-state"), ignoreState) |
|---|
| 1076 | MAP_OPTIONS_BOOL_TRUE(_T("cmdLine"), useCmdLine) |
|---|
| 1077 | MAP_OPTIONS_BOOL_TRUE(_T("16bit"), use16bit) |
|---|
| 1078 | MAP_OPTIONS_MODE(_T("match"), _T("string"), match, match_string) |
|---|
| 1079 | MAP_OPTIONS_MODE(_T("match"), _T("regexp"), match, match_regexp) |
|---|
| 1080 | MAP_OPTIONS_MODE(_T("match"), _T("substr"), match, match_substring) |
|---|
| 1081 | MAP_OPTIONS_MODE(_T("match"), _T("substring"), match, match_substring) |
|---|
| 1082 | MAP_OPTIONS_SECONDARY_BEGIN(_T(":"), p2) |
|---|
| 1083 | else if (p2.first == _T("Proc")) { |
|---|
| 1084 | tmpObject.data = p__.second; |
|---|
| 1085 | tmpObject.alias = p2.second; |
|---|
| 1086 | list.push_back(tmpObject); |
|---|
| 1087 | } |
|---|
| 1088 | MAP_OPTIONS_MISSING_EX(p2, msg, _T("Unknown argument: ")) |
|---|
| 1089 | MAP_OPTIONS_SECONDARY_END() |
|---|
| 1090 | else { |
|---|
| 1091 | tmpObject.data = p__.first; |
|---|
| 1092 | if (p__.second.empty()) { |
|---|
| 1093 | if (!tmpObject.crit.running.hasBounds() && !tmpObject.warn.running.hasBounds()) |
|---|
| 1094 | tmpObject.crit.running.min = _T("1"); |
|---|
| 1095 | } else if (p__.second == _T("started")) { |
|---|
| 1096 | if (!tmpObject.crit.running.hasBounds() && !tmpObject.warn.running.hasBounds()) |
|---|
| 1097 | tmpObject.crit.running.min = _T("1"); |
|---|
| 1098 | } else if (p__.second == _T("stopped")) { |
|---|
| 1099 | if (!tmpObject.crit.running.hasBounds() && !tmpObject.warn.running.hasBounds()) |
|---|
| 1100 | tmpObject.crit.running.max = _T("0"); |
|---|
| 1101 | } else if (p__.second == _T("hung")) { |
|---|
| 1102 | if (!tmpObject.crit.hung.hasBounds() && !tmpObject.warn.hung.hasBounds()) |
|---|
| 1103 | tmpObject.crit.hung.max = _T("1"); |
|---|
| 1104 | } |
|---|
| 1105 | list.push_back(tmpObject); |
|---|
| 1106 | tmpObject.reset(); |
|---|
| 1107 | } |
|---|
| 1108 | MAP_OPTIONS_END() |
|---|
| 1109 | |
|---|
| 1110 | NSPROCLST runningProcs; |
|---|
| 1111 | try { |
|---|
| 1112 | runningProcs = GetProcessList(useCmdLine, use16bit); |
|---|
| 1113 | } catch (CEnumProcess::process_enumeration_exception &e) { |
|---|
| 1114 | NSC_LOG_ERROR_STD(_T("ERROR: ") + e.what()); |
|---|
| 1115 | msg = static_cast<std::wstring>(_T("ERROR: ")) + e.what(); |
|---|
| 1116 | return NSCAPI::returnUNKNOWN; |
|---|
| 1117 | } catch (...) { |
|---|
| 1118 | NSC_LOG_ERROR_STD(_T("Unhandled error when processing command")); |
|---|
| 1119 | msg = _T("Unhandled error when processing command"); |
|---|
| 1120 | return NSCAPI::returnUNKNOWN; |
|---|
| 1121 | } |
|---|
| 1122 | |
|---|
| 1123 | for (std::list<StateContainer>::iterator it = list.begin(); it != list.end(); ++it) { |
|---|
| 1124 | NSPROCLST::iterator proc; |
|---|
| 1125 | if (match == match_string) { |
|---|
| 1126 | proc = runningProcs.find((*it).data); |
|---|
| 1127 | } else if (match == match_substring) { |
|---|
| 1128 | for (proc=runningProcs.begin();proc!=runningProcs.end();++proc) { |
|---|
| 1129 | if ((*proc).first.find((*it).data) != std::wstring::npos) |
|---|
| 1130 | break; |
|---|
| 1131 | } |
|---|
| 1132 | } else if (match == match_regexp) { |
|---|
| 1133 | try { |
|---|
| 1134 | boost::wregex filter((*it).data,boost::regex::icase); |
|---|
| 1135 | for (proc=runningProcs.begin();proc!=runningProcs.end();++proc) { |
|---|
| 1136 | std::wstring value = (*proc).first; |
|---|
| 1137 | if (boost::regex_match(value, filter)) |
|---|
| 1138 | break; |
|---|
| 1139 | } |
|---|
| 1140 | } catch (const boost::bad_expression e) { |
|---|
| 1141 | NSC_LOG_ERROR_STD(_T("Failed to compile regular expression: ") + (*proc).first); |
|---|
| 1142 | msg = _T("Failed to compile regular expression: ") + (*proc).first; |
|---|
| 1143 | return NSCAPI::returnUNKNOWN; |
|---|
| 1144 | } catch (...) { |
|---|
| 1145 | NSC_LOG_ERROR_STD(_T("Failed to compile regular expression: ") + (*proc).first); |
|---|
| 1146 | msg = _T("Failed to compile regular expression: ") + (*proc).first; |
|---|
| 1147 | return NSCAPI::returnUNKNOWN; |
|---|
| 1148 | } |
|---|
| 1149 | } else { |
|---|
| 1150 | NSC_LOG_ERROR_STD(_T("Unsupported mode for: ") + (*proc).first); |
|---|
| 1151 | msg = _T("Unsupported mode for: ") + (*proc).first; |
|---|
| 1152 | return NSCAPI::returnUNKNOWN; |
|---|
| 1153 | } |
|---|
| 1154 | bool bFound = (proc != runningProcs.end()); |
|---|
| 1155 | if (bNSClient) { |
|---|
| 1156 | if (bFound && (*it).showAll()) { |
|---|
| 1157 | if (!msg.empty()) msg += _T(" - "); |
|---|
| 1158 | msg += (*proc).first + _T(": Running"); |
|---|
| 1159 | } else if (bFound) { |
|---|
| 1160 | } else { |
|---|
| 1161 | if (!msg.empty()) msg += _T(" - "); |
|---|
| 1162 | msg += (*it).data + _T(": not running"); |
|---|
| 1163 | nscapi::plugin_helper::escalteReturnCodeToCRIT(returnCode); |
|---|
| 1164 | } |
|---|
| 1165 | } else { |
|---|
| 1166 | process_count_result value; |
|---|
| 1167 | if (bFound) { |
|---|
| 1168 | value.hung = (*proc).second.hung_count; |
|---|
| 1169 | value.running = (*proc).second.count; |
|---|
| 1170 | } else { |
|---|
| 1171 | value.hung = 0; |
|---|
| 1172 | value.running = 0; |
|---|
| 1173 | // if (ignoreState) |
|---|
| 1174 | // value.state = checkHolders::state_stopped | checkHolders::state_started | checkHolders::state_hung; |
|---|
| 1175 | // else |
|---|
| 1176 | // value.state = checkHolders::state_stopped; |
|---|
| 1177 | } |
|---|
| 1178 | if (bFound && (*it).alias.empty()) { |
|---|
| 1179 | (*it).alias = (*proc).first; |
|---|
| 1180 | } |
|---|
| 1181 | (*it).perfData = bPerfData; |
|---|
| 1182 | (*it).runCheck(value, returnCode, msg, perf); |
|---|
| 1183 | } |
|---|
| 1184 | |
|---|
| 1185 | } |
|---|
| 1186 | if (msg.empty()) |
|---|
| 1187 | msg = _T("OK: All processes are running."); |
|---|
| 1188 | else if (!bNSClient) |
|---|
| 1189 | msg = nscapi::plugin_helper::translateReturn(returnCode) + _T(": ") + msg; |
|---|
| 1190 | return returnCode; |
|---|
| 1191 | } |
|---|
| 1192 | |
|---|
| 1193 | template<class T> |
|---|
| 1194 | class PerfDataContainer : public checkHolders::CheckContainer<T> { |
|---|
| 1195 | private: |
|---|
| 1196 | typedef PDHCollectors::StaticPDHCounterListener<double, PDH_FMT_DOUBLE> counter_type; |
|---|
| 1197 | typedef boost::shared_ptr<counter_type> ptr_lsnr_type; |
|---|
| 1198 | ptr_lsnr_type cDouble; |
|---|
| 1199 | public: |
|---|
| 1200 | |
|---|
| 1201 | PerfDataContainer() : CheckContainer<T>() {} |
|---|
| 1202 | |
|---|
| 1203 | PerfDataContainer(const PerfDataContainer &other) : CheckContainer<T>(other), cDouble(other.cDouble) {} |
|---|
| 1204 | const PerfDataContainer& operator =(const PerfDataContainer &other) { |
|---|
| 1205 | *((CheckContainer<T>*)this) = other; |
|---|
| 1206 | cDouble = other.cDouble; |
|---|
| 1207 | return *this; |
|---|
| 1208 | } |
|---|
| 1209 | ptr_lsnr_type get_listener() { |
|---|
| 1210 | if (!cDouble) |
|---|
| 1211 | cDouble = ptr_lsnr_type(new counter_type()); |
|---|
| 1212 | return cDouble; |
|---|
| 1213 | } |
|---|
| 1214 | }; |
|---|
| 1215 | |
|---|
| 1216 | /** |
|---|
| 1217 | * Check a counter and return the value |
|---|
| 1218 | * |
|---|
| 1219 | * @param command Command to execute |
|---|
| 1220 | * @param argLen The length of the argument buffer |
|---|
| 1221 | * @param **char_args The argument buffer |
|---|
| 1222 | * @param &msg String to put message in |
|---|
| 1223 | * @param &perf String to put performance data in |
|---|
| 1224 | * @return The status of the command |
|---|
| 1225 | * |
|---|
| 1226 | * @todo add parsing support for NRPE |
|---|
| 1227 | */ |
|---|
| 1228 | NSCAPI::nagiosReturn CheckSystem::checkCounter(std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) |
|---|
| 1229 | { |
|---|
| 1230 | typedef PerfDataContainer<checkHolders::MaxMinBoundsDouble> CounterContainer; |
|---|
| 1231 | |
|---|
| 1232 | if (arguments.empty()) { |
|---|
| 1233 | msg = _T("ERROR: Missing argument exception."); |
|---|
| 1234 | return NSCAPI::returnUNKNOWN; |
|---|
| 1235 | } |
|---|
| 1236 | std::list<CounterContainer> counters; |
|---|
| 1237 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 1238 | bool bNSClient = false; |
|---|
| 1239 | bool bPerfData = true; |
|---|
| 1240 | /* average maax */ |
|---|
| 1241 | bool bCheckAverages = true; |
|---|
| 1242 | std::wstring invalidStatus = _T("UNKNOWN"); |
|---|
| 1243 | unsigned int averageDelay = 1000; |
|---|
| 1244 | CounterContainer tmpObject; |
|---|
| 1245 | bool bExpandIndex = false; |
|---|
| 1246 | bool bForceReload = false; |
|---|
| 1247 | std::wstring extra_format; |
|---|
| 1248 | |
|---|
| 1249 | MAP_OPTIONS_BEGIN(arguments) |
|---|
| 1250 | MAP_OPTIONS_STR(_T("InvalidStatus"), invalidStatus) |
|---|
| 1251 | MAP_OPTIONS_STR_AND(_T("Counter"), tmpObject.data, counters.push_back(tmpObject)) |
|---|
| 1252 | MAP_OPTIONS_STR(_T("MaxWarn"), tmpObject.warn.max_) |
|---|
| 1253 | MAP_OPTIONS_STR(_T("MinWarn"), tmpObject.warn.min_) |
|---|
| 1254 | MAP_OPTIONS_STR(_T("MaxCrit"), tmpObject.crit.max_) |
|---|
| 1255 | MAP_OPTIONS_STR(_T("MinCrit"), tmpObject.crit.min_) |
|---|
| 1256 | MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) |
|---|
| 1257 | MAP_OPTIONS_STR(_T("Alias"), tmpObject.data) |
|---|
| 1258 | MAP_OPTIONS_STR(_T("format"), extra_format) |
|---|
| 1259 | MAP_OPTIONS_SHOWALL(tmpObject) |
|---|
| 1260 | MAP_OPTIONS_BOOL_EX(_T("Averages"), bCheckAverages, _T("true"), _T("false")) |
|---|
| 1261 | MAP_OPTIONS_BOOL_TRUE(NSCLIENT, bNSClient) |
|---|
| 1262 | MAP_OPTIONS_BOOL_TRUE(_T("index"), bExpandIndex) |
|---|
| 1263 | MAP_OPTIONS_BOOL_TRUE(_T("reload"), bForceReload) |
|---|
| 1264 | MAP_OPTIONS_FIRST_CHAR('\\', tmpObject.data, counters.push_back(tmpObject)) |
|---|
| 1265 | MAP_OPTIONS_SECONDARY_BEGIN(_T(":"), p2) |
|---|
| 1266 | else if (p2.first == _T("Counter")) { |
|---|
| 1267 | tmpObject.data = p__.second; |
|---|
| 1268 | tmpObject.alias = p2.second; |
|---|
| 1269 | counters.push_back(tmpObject); |
|---|
| 1270 | } |
|---|
| 1271 | MAP_OPTIONS_MISSING_EX(p2, msg, _T("Unknown argument: ")) |
|---|
| 1272 | MAP_OPTIONS_SECONDARY_END() |
|---|
| 1273 | MAP_OPTIONS_FALLBACK_AND(tmpObject.data, counters.push_back(tmpObject)) |
|---|
| 1274 | MAP_OPTIONS_END() |
|---|
| 1275 | |
|---|
| 1276 | if (counters.empty()) { |
|---|
| 1277 | msg = _T("No counters specified"); |
|---|
| 1278 | return NSCAPI::returnUNKNOWN; |
|---|
| 1279 | } |
|---|
| 1280 | PDH::PDHQuery pdh; |
|---|
| 1281 | |
|---|
| 1282 | bool has_counter = false; |
|---|
| 1283 | BOOST_FOREACH(CounterContainer &counter, counters) { |
|---|
| 1284 | try { |
|---|
| 1285 | if (counter.data.find('\\') == std::wstring::npos) { |
|---|
| 1286 | /* |
|---|
| 1287 | value = pObject->get_double(counter.data); |
|---|
| 1288 | if (value == -1) { |
|---|
| 1289 | msg = _T("ERROR: Failed to get counter value: ") + counter.data; |
|---|
| 1290 | return NSCAPI::returnUNKNOWN; |
|---|
| 1291 | } |
|---|
| 1292 | */ |
|---|
| 1293 | } else { |
|---|
| 1294 | std::wstring tstr; |
|---|
| 1295 | if (bExpandIndex) { |
|---|
| 1296 | PDH::PDHResolver::expand_index(counter.data); |
|---|
| 1297 | } |
|---|
| 1298 | if (!PDH::PDHResolver::validate(counter.data, tstr, bForceReload)) { |
|---|
| 1299 | NSC_LOG_ERROR_STD(_T("ERROR: Counter not found: ") + counter.data + _T(": ") + tstr); |
|---|
| 1300 | if (bNSClient) { |
|---|
| 1301 | NSC_LOG_ERROR_STD(_T("ERROR: Counter not found: ") + counter.data + _T(": ") + tstr); |
|---|
| 1302 | //msg = _T("0"); |
|---|
| 1303 | } else { |
|---|
| 1304 | msg = _T("CRIT: Counter not found: ") + counter.data + _T(": ") + tstr; |
|---|
| 1305 | return NSCAPI::returnCRIT; |
|---|
| 1306 | } |
|---|
| 1307 | } |
|---|
| 1308 | if (!extra_format.empty()) { |
|---|
| 1309 | boost::char_separator<wchar_t> sep(_T(",")); |
|---|
| 1310 | |
|---|
| 1311 | boost::tokenizer< boost::char_separator<wchar_t>, std::wstring::const_iterator, std::wstring > tokens(extra_format, sep); |
|---|
| 1312 | DWORD flags = 0; |
|---|
| 1313 | BOOST_FOREACH(std::wstring t, tokens) { |
|---|
| 1314 | if (extra_format == _T("nocap100")) |
|---|
| 1315 | flags |= PDH_FMT_NOCAP100; |
|---|
| 1316 | else if (extra_format == _T("1000")) |
|---|
| 1317 | flags |= PDH_FMT_1000; |
|---|
| 1318 | else if (extra_format == _T("noscale")) |
|---|
| 1319 | flags |= PDH_FMT_NOSCALE; |
|---|
| 1320 | else { |
|---|
| 1321 | NSC_LOG_ERROR_STD(_T("Unsupported extrta format: ") + extra_format); |
|---|
| 1322 | } |
|---|
| 1323 | } |
|---|
| 1324 | counter.get_listener()->set_extra_format(flags); |
|---|
| 1325 | } |
|---|
| 1326 | pdh.addCounter(counter.data, counter.get_listener()); |
|---|
| 1327 | has_counter = true; |
|---|
| 1328 | } |
|---|
| 1329 | } catch (const PDH::PDHException e) { |
|---|
| 1330 | NSC_LOG_ERROR_STD(_T("ERROR: ") + e.getError() + _T(" (") + counter.getAlias() + _T("|") + counter.data + _T(")")); |
|---|
| 1331 | if (bNSClient) |
|---|
| 1332 | msg = _T("0"); |
|---|
| 1333 | else |
|---|
| 1334 | msg = static_cast<std::wstring>(_T("ERROR: ")) + e.getError()+ _T(" (") + counter.getAlias() + _T("|") + counter.data + _T(")"); |
|---|
| 1335 | return NSCAPI::returnUNKNOWN; |
|---|
| 1336 | } |
|---|
| 1337 | } |
|---|
| 1338 | if (has_counter) { |
|---|
| 1339 | try { |
|---|
| 1340 | pdh.open(); |
|---|
| 1341 | if (bCheckAverages) { |
|---|
| 1342 | pdh.collect(); |
|---|
| 1343 | Sleep(1000); |
|---|
| 1344 | } |
|---|
| 1345 | pdh.collect(); |
|---|
| 1346 | pdh.gatherData(); |
|---|
| 1347 | pdh.close(); |
|---|
| 1348 | } catch (const PDH::PDHException e) { |
|---|
| 1349 | NSC_LOG_ERROR_STD(_T("ERROR: ") + e.getError()); |
|---|
| 1350 | if (bNSClient) |
|---|
| 1351 | msg = _T("0"); |
|---|
| 1352 | else |
|---|
| 1353 | msg = _T("ERROR: ") + e.getError(); |
|---|
| 1354 | return NSCAPI::returnUNKNOWN; |
|---|
| 1355 | } |
|---|
| 1356 | } |
|---|
| 1357 | BOOST_FOREACH(CounterContainer &counter, counters) { |
|---|
| 1358 | try { |
|---|
| 1359 | double value = 0; |
|---|
| 1360 | if (counter.data.find('\\') == std::wstring::npos) { |
|---|
| 1361 | PDHCollector *pObject = pdhThread.getThread(); |
|---|
| 1362 | if (!pObject) { |
|---|
| 1363 | msg = _T("ERROR: PDH Collection thread not running."); |
|---|
| 1364 | return NSCAPI::returnUNKNOWN; |
|---|
| 1365 | } |
|---|
| 1366 | value = pObject->get_double(counter.data); |
|---|
| 1367 | if (value == -1) { |
|---|
| 1368 | msg = _T("ERROR: Failed to get counter value: ") + counter.data; |
|---|
| 1369 | return NSCAPI::returnUNKNOWN; |
|---|
| 1370 | } |
|---|
| 1371 | } else { |
|---|
| 1372 | value = counter.get_listener()->getValue(); |
|---|
| 1373 | } |
|---|
| 1374 | |
|---|
| 1375 | if (bNSClient) { |
|---|
| 1376 | if (!msg.empty()) |
|---|
| 1377 | msg += _T(","); |
|---|
| 1378 | msg += strEx::itos(static_cast<float>(value)); |
|---|
| 1379 | } else { |
|---|
| 1380 | counter.perfData = bPerfData; |
|---|
| 1381 | counter.setDefault(tmpObject); |
|---|
| 1382 | counter.runCheck(value, returnCode, msg, perf); |
|---|
| 1383 | } |
|---|
| 1384 | } catch (const PDH::PDHException e) { |
|---|
| 1385 | NSC_LOG_ERROR_STD(_T("ERROR: ") + e.getError() + _T(" (") + counter.getAlias() + _T("|") + counter.data + _T(")")); |
|---|
| 1386 | if (bNSClient) |
|---|
| 1387 | msg = _T("0"); |
|---|
| 1388 | else |
|---|
| 1389 | msg = static_cast<std::wstring>(_T("ERROR: ")) + e.getError()+ _T(" (") + counter.getAlias() + _T("|") + counter.data + _T(")"); |
|---|
| 1390 | return NSCAPI::returnUNKNOWN; |
|---|
| 1391 | } |
|---|
| 1392 | } |
|---|
| 1393 | |
|---|
| 1394 | if (msg.empty() && !bNSClient) |
|---|
| 1395 | msg = _T("OK all counters within bounds."); |
|---|
| 1396 | else if (msg.empty()) { |
|---|
| 1397 | NSC_LOG_ERROR_STD(_T("No value found returning 0?")); |
|---|
| 1398 | msg = _T("0"); |
|---|
| 1399 | }else if (!bNSClient) |
|---|
| 1400 | msg = nscapi::plugin_helper::translateReturn(returnCode) + _T(": ") + msg; |
|---|
| 1401 | return returnCode; |
|---|
| 1402 | } |
|---|
| 1403 | |
|---|
| 1404 | |
|---|
| 1405 | |
|---|
| 1406 | /** |
|---|
| 1407 | * List all instances for a given counter. |
|---|
| 1408 | * |
|---|
| 1409 | * @param command Command to execute |
|---|
| 1410 | * @param argLen The length of the argument buffer |
|---|
| 1411 | * @param **char_args The argument buffer |
|---|
| 1412 | * @param &msg String to put message in |
|---|
| 1413 | * @param &perf String to put performance data in |
|---|
| 1414 | * @return The status of the command |
|---|
| 1415 | * |
|---|
| 1416 | * @todo add parsing support for NRPE |
|---|
| 1417 | */ |
|---|
| 1418 | NSCAPI::nagiosReturn CheckSystem::listCounterInstances(std::list<std::wstring> arguments, std::wstring &msg, std::wstring &perf) |
|---|
| 1419 | { |
|---|
| 1420 | typedef checkHolders::CheckContainer<checkHolders::MaxMinBoundsDouble> CounterContainer; |
|---|
| 1421 | |
|---|
| 1422 | if (arguments.empty()) { |
|---|
| 1423 | msg = _T("ERROR: Missing argument exception."); |
|---|
| 1424 | return NSCAPI::returnUNKNOWN; |
|---|
| 1425 | } |
|---|
| 1426 | |
|---|
| 1427 | std::wstring counter; |
|---|
| 1428 | BOOST_FOREACH(std::wstring s, arguments) { counter+= s + _T(" "); } |
|---|
| 1429 | try { |
|---|
| 1430 | PDH::Enumerations::pdh_object_details obj = PDH::Enumerations::EnumObjectInstances(counter); |
|---|
| 1431 | for (PDH::Enumerations::pdh_object_details::list::const_iterator it = obj.instances.begin(); it!=obj.instances.end();++it) { |
|---|
| 1432 | if (!msg.empty()) |
|---|
| 1433 | msg += _T(", "); |
|---|
| 1434 | msg += (*it); |
|---|
| 1435 | } |
|---|
| 1436 | if (msg.empty()) { |
|---|
| 1437 | msg = _T("ERROR: No instances found"); |
|---|
| 1438 | return NSCAPI::returnUNKNOWN; |
|---|
| 1439 | } |
|---|
| 1440 | } catch (const PDH::PDHException e) { |
|---|
| 1441 | msg = _T("ERROR: Failed to enumerate counter instances: " + e.getError()); |
|---|
| 1442 | return NSCAPI::returnUNKNOWN; |
|---|
| 1443 | } catch (...) { |
|---|
| 1444 | msg = _T("ERROR: Failed to enumerate counter instances: <UNKNOWN EXCEPTION>"); |
|---|
| 1445 | return NSCAPI::returnUNKNOWN; |
|---|
| 1446 | } |
|---|
| 1447 | return NSCAPI::returnOK; |
|---|
| 1448 | } |
|---|
| 1449 | |
|---|
| 1450 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 1451 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 1452 | |
|---|
| 1453 | struct regkey_info { |
|---|
| 1454 | |
|---|
| 1455 | std::wstring error; |
|---|
| 1456 | |
|---|
| 1457 | static regkey_info get(__int64 now, std::wstring path) { |
|---|
| 1458 | return regkey_info(now, path); |
|---|
| 1459 | } |
|---|
| 1460 | |
|---|
| 1461 | regkey_info() |
|---|
| 1462 | : ullLastWriteTime(0) |
|---|
| 1463 | , iType(0) |
|---|
| 1464 | , ullNow(0) |
|---|
| 1465 | , uiExists(0) |
|---|
| 1466 | , ullChildCount(0) |
|---|
| 1467 | {} |
|---|
| 1468 | regkey_info(__int64 now, std::wstring path) |
|---|
| 1469 | : path(path) |
|---|
| 1470 | , ullLastWriteTime(0) |
|---|
| 1471 | , iType(0) |
|---|
| 1472 | , ullNow(now) |
|---|
| 1473 | , uiExists(0) |
|---|
| 1474 | , ullChildCount(0) |
|---|
| 1475 | { |
|---|
| 1476 | std::wstring key; |
|---|
| 1477 | try { |
|---|
| 1478 | std::wcout << _T("opening: ") << path << std::endl; |
|---|
| 1479 | std::wstring::size_type pos = path.find_first_of(L'\\'); |
|---|
| 1480 | if (pos != std::wstring::npos) { |
|---|
| 1481 | key = path.substr(0, pos); |
|---|
| 1482 | path = path.substr(pos+1); |
|---|
| 1483 | std::wcout << key << _T(":") << path << std::endl; |
|---|
| 1484 | simple_registry::registry_key rkey(simple_registry::parseHKEY(key), path); |
|---|
| 1485 | info = rkey.get_info(); |
|---|
| 1486 | uiExists = 1; |
|---|
| 1487 | } else { |
|---|
| 1488 | error = _T("Failed to parse key"); |
|---|
| 1489 | } |
|---|
| 1490 | } catch (simple_registry::registry_exception &e) { |
|---|
| 1491 | try { |
|---|
| 1492 | std::wstring::size_type pos = path.find_last_of(L'\\'); |
|---|
| 1493 | if (pos != std::wstring::npos) { |
|---|
| 1494 | std::wstring item = path.substr(pos+1); |
|---|
| 1495 | path = path.substr(0, pos); |
|---|
| 1496 | std::wcout << key << _T(":") << path << _T(".") << item << std::endl; |
|---|
| 1497 | simple_registry::registry_key rkey(simple_registry::parseHKEY(key), path); |
|---|
| 1498 | info = rkey.get_info(item); |
|---|
| 1499 | uiExists = 1; |
|---|
| 1500 | } else { |
|---|
| 1501 | error = _T("Failed to parse key"); |
|---|
| 1502 | } |
|---|
| 1503 | } catch (simple_registry::registry_exception &e) { |
|---|
| 1504 | //error = e.what(); |
|---|
| 1505 | } |
|---|
| 1506 | } catch (...) { |
|---|
| 1507 | error = _T("Unknown exception"); |
|---|
| 1508 | } |
|---|
| 1509 | |
|---|
| 1510 | //HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\eventlog\Application\MaxSize |
|---|
| 1511 | |
|---|
| 1512 | // TODO get key info here! |
|---|
| 1513 | //ullLastWriteTime = ((info.ftLastWriteTime.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)info.ftLastWriteTime.dwLowDateTime); |
|---|
| 1514 | }; |
|---|
| 1515 | |
|---|
| 1516 | unsigned long long ullSize; |
|---|
| 1517 | __int64 ullLastWriteTime; |
|---|
| 1518 | __int64 ullNow; |
|---|
| 1519 | std::wstring filename; |
|---|
| 1520 | std::wstring path; |
|---|
| 1521 | unsigned long ullChildCount; |
|---|
| 1522 | unsigned long uiExists; |
|---|
| 1523 | unsigned int iType; |
|---|
| 1524 | simple_registry::registry_key::reg_info info; |
|---|
| 1525 | |
|---|
| 1526 | static const __int64 MSECS_TO_100NS = 10000; |
|---|
| 1527 | |
|---|
| 1528 | __int64 get_written() { |
|---|
| 1529 | return (ullNow-ullLastWriteTime)/MSECS_TO_100NS; |
|---|
| 1530 | } |
|---|
| 1531 | std::wstring render(std::wstring syntax) { |
|---|
| 1532 | strEx::replace(syntax, _T("%path%"), path); |
|---|
| 1533 | strEx::replace(syntax, _T("%key%"), filename); |
|---|
| 1534 | strEx::replace(syntax, _T("%write%"), strEx::format_filetime(ullLastWriteTime, DATE_FORMAT)); |
|---|
| 1535 | strEx::replace(syntax, _T("%write-raw%"), strEx::itos(ullLastWriteTime)); |
|---|
| 1536 | strEx::replace(syntax, _T("%now-raw%"), strEx::itos(ullNow)); |
|---|
| 1537 | strEx::replace(syntax, _T("%type%"), strEx::itos_as_BKMG(iType)); |
|---|
| 1538 | strEx::replace(syntax, _T("%child-count%"), strEx::itos(ullChildCount)); |
|---|
| 1539 | strEx::replace(syntax, _T("%exists%"), strEx::itos(uiExists)); |
|---|
| 1540 | strEx::replace(syntax, _T("%int%"), strEx::itos(info.iValue)); |
|---|
| 1541 | strEx::replace(syntax, _T("%int-value%"), strEx::itos(info.iValue)); |
|---|
| 1542 | strEx::replace(syntax, _T("%string%"), info.sValue); |
|---|
| 1543 | strEx::replace(syntax, _T("%string-value%"), info.sValue); |
|---|
| 1544 | return syntax; |
|---|
| 1545 | } |
|---|
| 1546 | }; |
|---|
| 1547 | |
|---|
| 1548 | |
|---|
| 1549 | struct regkey_filter { |
|---|
| 1550 | filters::filter_all_times written; |
|---|
| 1551 | filters::filter_all_num_ul type; |
|---|
| 1552 | filters::filter_all_num_ul exists; |
|---|
| 1553 | filters::filter_all_num_ul child_count; |
|---|
| 1554 | filters::filter_all_num_ll value_int; |
|---|
| 1555 | filters::filter_all_strings value_string; |
|---|
| 1556 | |
|---|
| 1557 | inline bool hasFilter() { |
|---|
| 1558 | return type.hasFilter() || exists.hasFilter() || written.hasFilter() || child_count.hasFilter() || value_int.hasFilter() || value_string.hasFilter(); |
|---|
| 1559 | } |
|---|
| 1560 | bool matchFilter(regkey_info &value) const { |
|---|
| 1561 | if ((written.hasFilter())&&(written.matchFilter(value.get_written()))) |
|---|
| 1562 | return true; |
|---|
| 1563 | else if (type.hasFilter()&&type.matchFilter(value.iType)) |
|---|
| 1564 | return true; |
|---|
| 1565 | else if (exists.hasFilter()&&exists.matchFilter(value.uiExists)) |
|---|
| 1566 | return true; |
|---|
| 1567 | else if ((child_count.hasFilter())&&(child_count.matchFilter(value.ullChildCount))) |
|---|
| 1568 | return true; |
|---|
| 1569 | else if ((value_int.hasFilter())&&(value_int.matchFilter(value.info.iValue))) |
|---|
| 1570 | return true; |
|---|
| 1571 | else if ((value_string.hasFilter())&&(value_string.matchFilter(value.info.sValue))) |
|---|
| 1572 | return true; |
|---|
| 1573 | return false; |
|---|
| 1574 | } |
|---|
| 1575 | |
|---|
| 1576 | std::wstring getValue() const { |
|---|
| 1577 | if (written.hasFilter()) |
|---|
| 1578 | return _T("written: ") + written.getValue(); |
|---|
| 1579 | if (type.hasFilter()) |
|---|
| 1580 | return _T("type: ") + type.getValue(); |
|---|
| 1581 | if (exists.hasFilter()) |
|---|
| 1582 | return _T("exists: ") + exists.getValue(); |
|---|
| 1583 | if (child_count.hasFilter()) |
|---|
| 1584 | return _T("child_count: ") + child_count.getValue(); |
|---|
| 1585 | if (value_int.hasFilter()) |
|---|
| 1586 | return _T("value(i): ") + value_int.getValue(); |
|---|
| 1587 | if (value_string.hasFilter()) |
|---|
| 1588 | return _T("value(s): ") + value_string.getValue(); |
|---|
| 1589 | return _T("UNknown..."); |
|---|
| 1590 | } |
|---|
| 1591 | |
|---|
| 1592 | }; |
|---|
| 1593 | |
|---|
| 1594 | |
|---|
| 1595 | struct regkey_container : public regkey_info { |
|---|
| 1596 | |
|---|
| 1597 | static regkey_container get(std::wstring path, unsigned long long now) { |
|---|
| 1598 | return regkey_container(now, path); |
|---|
| 1599 | } |
|---|
| 1600 | |
|---|
| 1601 | |
|---|
| 1602 | regkey_container(__int64 now, std::wstring path) : regkey_info(now, path) {} |
|---|
| 1603 | |
|---|
| 1604 | bool has_errors() { |
|---|
| 1605 | return !error.empty(); |
|---|
| 1606 | } |
|---|
| 1607 | std::wstring get_error() { |
|---|
| 1608 | return error; |
|---|
| 1609 | } |
|---|
| 1610 | |
|---|
| 1611 | }; |
|---|
| 1612 | |
|---|
| 1613 | |
|---|
| 1614 | class regkey_type_handler { |
|---|
| 1615 | public: |
|---|
| 1616 | static int parse(std::wstring s) { |
|---|
| 1617 | return 1; |
|---|
| 1618 | } |
|---|
| 1619 | static std::wstring print(int value) { |
|---|
| 1620 | return _T("unknown"); |
|---|
| 1621 | } |
|---|
| 1622 | static std::wstring print_unformated(int value) { |
|---|
| 1623 | return strEx::itos(value); |
|---|
| 1624 | } |
|---|
| 1625 | static std::wstring key_prefix() { |
|---|
| 1626 | return _T(""); |
|---|
| 1627 | } |
|---|
| 1628 | static std::wstring key_postfix() { |
|---|
| 1629 | return _T(""); |
|---|
| 1630 | } |
|---|
| 1631 | static std::wstring get_perf_unit(int value) { |
|---|
| 1632 | return _T(""); |
|---|
| 1633 | } |
|---|
| 1634 | static std::wstring print_perf(int value, std::wstring unit) { |
|---|
| 1635 | return strEx::itos(value); |
|---|
| 1636 | } |
|---|
| 1637 | }; |
|---|
| 1638 | class regkey_exists_handler { |
|---|
| 1639 | public: |
|---|
| 1640 | static int parse(std::wstring s) { |
|---|
| 1641 | if (s == _T("true")) |
|---|
| 1642 | return 1; |
|---|
| 1643 | return 0; |
|---|
| 1644 | } |
|---|
| 1645 | static std::wstring print(int value) { |
|---|
| 1646 | return value==1?_T("true"):_T("false"); |
|---|
| 1647 | } |
|---|
| 1648 | static std::wstring print_unformated(int value) { |
|---|
| 1649 | return strEx::itos(value); |
|---|
| 1650 | } |
|---|
| 1651 | static std::wstring key_prefix() { |
|---|
| 1652 | return _T(""); |
|---|
| 1653 | } |
|---|
| 1654 | static std::wstring key_postfix() { |
|---|
| 1655 | return _T(""); |
|---|
| 1656 | } |
|---|
| 1657 | static std::wstring get_perf_unit(int value) { |
|---|
| 1658 | return _T(""); |
|---|
| 1659 | } |
|---|
| 1660 | static std::wstring print_perf(int value, std::wstring unit) { |
|---|
| 1661 | return strEx::itos(value); |
|---|
| 1662 | } |
|---|
| 1663 | }; |
|---|
| 1664 | |
|---|
| 1665 | typedef checkHolders::CheckContainer<checkHolders::ExactBounds<checkHolders::NumericBounds<int, regkey_type_handler> > > RegTypeContainer; |
|---|
| 1666 | typedef checkHolders::CheckContainer<checkHolders::ExactBounds<checkHolders::NumericBounds<int, regkey_exists_handler> > > RegExistsContainer; |
|---|
| 1667 | |
|---|
| 1668 | typedef checkHolders::CheckContainer<checkHolders::ExactBoundsULong> ExactULongContainer; |
|---|
| 1669 | typedef checkHolders::CheckContainer<checkHolders::ExactBoundsLongLong> ExactLongLongContainer; |
|---|
| 1670 | typedef checkHolders::CheckContainer<checkHolders::ExactBoundsTime> DateTimeContainer; |
|---|
| 1671 | typedef checkHolders::CheckContainer<checkHolders::FilterBounds<filters::filter_all_strings> > StringContainer; |
|---|
| 1672 | |
|---|
| 1673 | struct check_regkey_child_count : public checkHolders::check_proxy_container<regkey_container, ExactULongContainer> { |
|---|
| 1674 | check_regkey_child_count() { set_alias(_T("child-count")); } |
|---|
| 1675 | unsigned long get_value(regkey_container &value) { |
|---|
| 1676 | return value.ullChildCount; |
|---|
| 1677 | } |
|---|
| 1678 | }; |
|---|
| 1679 | struct check_regkey_int_value : public checkHolders::check_proxy_container<regkey_container, ExactLongLongContainer> { |
|---|
| 1680 | check_regkey_int_value() { set_alias(_T("value")); } |
|---|
| 1681 | long long get_value(regkey_container &value) { |
|---|
| 1682 | return value.info.iValue; |
|---|
| 1683 | } |
|---|
| 1684 | }; |
|---|
| 1685 | struct check_regkey_string_value : public checkHolders::check_proxy_container<regkey_container, StringContainer> { |
|---|
| 1686 | check_regkey_string_value() { set_alias(_T("value")); } |
|---|
| 1687 | std::wstring get_value(regkey_container &value) { |
|---|
| 1688 | return value.info.sValue; |
|---|
| 1689 | } |
|---|
| 1690 | }; |
|---|
| 1691 | struct check_regkey_written : public checkHolders::check_proxy_container<regkey_container, DateTimeContainer> { |
|---|
| 1692 | check_regkey_written() { set_alias(_T("written")); } |
|---|
| 1693 | unsigned long long get_value(regkey_container &value) { |
|---|
| 1694 | return value.ullLastWriteTime; |
|---|
| 1695 | } |
|---|
| 1696 | }; |
|---|
| 1697 | struct check_regkey_type : public checkHolders::check_proxy_container<regkey_container, RegTypeContainer> { |
|---|
| 1698 | check_regkey_type() { set_alias(_T("type")); } |
|---|
| 1699 | int get_value(regkey_container &value) { |
|---|
| 1700 | return value.iType; |
|---|
| 1701 | } |
|---|
| 1702 | }; |
|---|
| 1703 | struct check_regkey_exists : public checkHolders::check_proxy_container<regkey_container, RegExistsContainer> { |
|---|
| 1704 | check_regkey_exists() { set_alias(_T("exists")); } |
|---|
| 1705 | int get_value(regkey_container &value) { |
|---|
| 1706 | return value.uiExists; |
|---|
| 1707 | } |
|---|
| 1708 | }; |
|---|
| 1709 | |
|---|
| 1710 | |
|---|
| 1711 | typedef checkHolders::check_multi_container<regkey_container> check_file_multi; |
|---|
| 1712 | struct check_regkey_factories { |
|---|
| 1713 | static checkHolders::check_proxy_interface<regkey_container>* type() { |
|---|
| 1714 | return new check_regkey_type(); |
|---|
| 1715 | } |
|---|
| 1716 | static checkHolders::check_proxy_interface<regkey_container>* exists() { |
|---|
| 1717 | return new check_regkey_exists(); |
|---|
| 1718 | } |
|---|
| 1719 | static checkHolders::check_proxy_interface<regkey_container>* child_count() { |
|---|
| 1720 | return new check_regkey_child_count(); |
|---|
| 1721 | } |
|---|
| 1722 | static checkHolders::check_proxy_interface<regkey_container>* written() { |
|---|
| 1723 | return new check_regkey_written(); |
|---|
| 1724 | } |
|---|
| 1725 | static checkHolders::check_proxy_interface<regkey_container>* value_string() { |
|---|
| 1726 | return new check_regkey_string_value(); |
|---|
| 1727 | } |
|---|
| 1728 | static checkHolders::check_proxy_interface<regkey_container>* value_int() { |
|---|
| 1729 | return new check_regkey_int_value(); |
|---|
| 1730 | } |
|---|
| 1731 | }; |
|---|
| 1732 | |
|---|
| 1733 | #define MAP_FACTORY_PB(value, obj) \ |
|---|
| 1734 | else if ((p__.first == _T("check")) && (p__.second == ##value)) { checker.add_check(check_regkey_factories::obj()); } |
|---|
| 1735 | |
|---|
| 1736 | |
|---|
| 1737 | NSCAPI::nagiosReturn CheckSystem::checkSingleRegEntry(std::list<std::wstring> arguments, std::wstring &message, std::wstring &perf) { |
|---|
| 1738 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 1739 | check_file_multi checker; |
|---|
| 1740 | typedef std::pair<int,regkey_filter> filteritem_type; |
|---|
| 1741 | typedef std::list<filteritem_type > filterlist_type; |
|---|
| 1742 | if (arguments.empty()) { |
|---|
| 1743 | message = _T("Missing argument(s)."); |
|---|
| 1744 | return NSCAPI::returnUNKNOWN; |
|---|
| 1745 | } |
|---|
| 1746 | std::list<std::wstring> files; |
|---|
| 1747 | unsigned int truncate = 0; |
|---|
| 1748 | std::wstring syntax = _T("%filename%"); |
|---|
| 1749 | std::wstring alias; |
|---|
| 1750 | bool bPerfData = true; |
|---|
| 1751 | |
|---|
| 1752 | try { |
|---|
| 1753 | MAP_OPTIONS_BEGIN(arguments) |
|---|
| 1754 | MAP_OPTIONS_STR2INT(_T("truncate"), truncate) |
|---|
| 1755 | MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) |
|---|
| 1756 | MAP_OPTIONS_STR(_T("syntax"), syntax) |
|---|
| 1757 | MAP_OPTIONS_STR(_T("alias"), alias) |
|---|
| 1758 | MAP_OPTIONS_PUSH(_T("path"), files) |
|---|
| 1759 | MAP_OPTIONS_SHOWALL(checker) |
|---|
| 1760 | MAP_OPTIONS_EXACT_NUMERIC_ALL_MULTI(checker, _T("")) |
|---|
| 1761 | MAP_FACTORY_PB(_T("type"), type) |
|---|
| 1762 | MAP_FACTORY_PB(_T("child-count"), child_count) |
|---|
| 1763 | MAP_FACTORY_PB(_T("written"), written) |
|---|
| 1764 | MAP_FACTORY_PB(_T("int"), value_int) |
|---|
| 1765 | MAP_FACTORY_PB(_T("string"), value_string) |
|---|
| 1766 | MAP_OPTIONS_MISSING(message, _T("Unknown argument: ")) |
|---|
| 1767 | MAP_OPTIONS_END() |
|---|
| 1768 | } catch (filters::parse_exception e) { |
|---|
| 1769 | message = e.getMessage(); |
|---|
| 1770 | return NSCAPI::returnUNKNOWN; |
|---|
| 1771 | } catch (filters::filter_exception e) { |
|---|
| 1772 | message = e.getMessage(); |
|---|
| 1773 | return NSCAPI::returnUNKNOWN; |
|---|
| 1774 | } |
|---|
| 1775 | FILETIME now; |
|---|
| 1776 | GetSystemTimeAsFileTime(&now); |
|---|
| 1777 | unsigned __int64 nowi64 = ((now.dwHighDateTime * ((unsigned long long)MAXDWORD+1)) + (unsigned long long)now.dwLowDateTime); |
|---|
| 1778 | for (std::list<std::wstring>::const_iterator pit = files.begin(); pit != files.end(); ++pit) { |
|---|
| 1779 | regkey_container info = regkey_container::get(*pit, nowi64); |
|---|
| 1780 | if (info.has_errors()) { |
|---|
| 1781 | message = info.error; |
|---|
| 1782 | return NSCAPI::returnUNKNOWN; |
|---|
| 1783 | } |
|---|
| 1784 | checker.alias = info.render(syntax); |
|---|
| 1785 | checker.runCheck(info, returnCode, message, perf); |
|---|
| 1786 | } |
|---|
| 1787 | if ((truncate > 0) && (message.length() > (truncate-4))) { |
|---|
| 1788 | message = message.substr(0, truncate-4) + _T("..."); |
|---|
| 1789 | perf = _T(""); |
|---|
| 1790 | } |
|---|
| 1791 | if (message.empty()) |
|---|
| 1792 | message = _T("CheckSingleRegkey ok"); |
|---|
| 1793 | return returnCode; |
|---|
| 1794 | } |
|---|
| 1795 | |
|---|
| 1796 | NSC_WRAP_DLL(); |
|---|
| 1797 | NSC_WRAPPERS_MAIN_DEF(CheckSystem); |
|---|
| 1798 | NSC_WRAPPERS_IGNORE_MSG_DEF(); |
|---|
| 1799 | NSC_WRAPPERS_HANDLE_CMD_DEF(); |
|---|
| 1800 | NSC_WRAPPERS_CLI_DEF(); |
|---|