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