| 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 | #include "CheckEventLog.h" |
|---|
| 24 | #include <filter_framework.hpp> |
|---|
| 25 | #include <boost/foreach.hpp> |
|---|
| 26 | |
|---|
| 27 | #include <strEx.h> |
|---|
| 28 | #include <time.h> |
|---|
| 29 | #include <utils.h> |
|---|
| 30 | #include <error.hpp> |
|---|
| 31 | #include <map> |
|---|
| 32 | #include <vector> |
|---|
| 33 | |
|---|
| 34 | CheckEventLog gCheckEventLog; |
|---|
| 35 | |
|---|
| 36 | BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) |
|---|
| 37 | { |
|---|
| 38 | NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call); |
|---|
| 39 | return TRUE; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | CheckEventLog::CheckEventLog() { |
|---|
| 43 | } |
|---|
| 44 | CheckEventLog::~CheckEventLog() { |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | bool CheckEventLog::loadModule() { |
|---|
| 49 | try { |
|---|
| 50 | NSCModuleHelper::registerCommand(_T("CheckEventLog"), _T("Check for errors in the event logger!")); |
|---|
| 51 | debug_ = NSCModuleHelper::getSettingsInt(EVENTLOG_SECTION_TITLE, EVENTLOG_DEBUG, EVENTLOG_DEBUG_DEFAULT)==1; |
|---|
| 52 | lookup_names_ = NSCModuleHelper::getSettingsInt(EVENTLOG_SECTION_TITLE, EVENTLOG_LOOKUP_NAMES, EVENTLOG_LOOKUP_NAMES_DEFAULT)==1; |
|---|
| 53 | syntax_ = NSCModuleHelper::getSettingsString(EVENTLOG_SECTION_TITLE, EVENTLOG_SYNTAX, EVENTLOG_SYNTAX_DEFAULT); |
|---|
| 54 | buffer_length_ = NSCModuleHelper::getSettingsInt(EVENTLOG_SECTION_TITLE, EVENTLOG_BUFFER, EVENTLOG_BUFFER_DEFAULT); |
|---|
| 55 | } catch (NSCModuleHelper::NSCMHExcpetion &e) { |
|---|
| 56 | NSC_LOG_ERROR_STD(_T("Failed to register command: ") + e.msg_); |
|---|
| 57 | } catch (...) { |
|---|
| 58 | NSC_LOG_ERROR_STD(_T("Failed to register command.")); |
|---|
| 59 | } |
|---|
| 60 | return true; |
|---|
| 61 | } |
|---|
| 62 | bool CheckEventLog::unloadModule() { |
|---|
| 63 | return true; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | bool CheckEventLog::hasCommandHandler() { |
|---|
| 67 | return true; |
|---|
| 68 | } |
|---|
| 69 | bool CheckEventLog::hasMessageHandler() { |
|---|
| 70 | return false; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | namespace simple_registry { |
|---|
| 74 | class registry_exception { |
|---|
| 75 | std::wstring what_; |
|---|
| 76 | public: |
|---|
| 77 | registry_exception(std::wstring what) : what_(what) {} |
|---|
| 78 | registry_exception(std::wstring path, std::wstring what) : what_(path + _T(" -- ") + what) {} |
|---|
| 79 | registry_exception(std::wstring path, std::wstring key, std::wstring what) : what_(path + _T(".") + key + _T(" -- ") + what) {} |
|---|
| 80 | std::wstring what() { |
|---|
| 81 | return what_; |
|---|
| 82 | } |
|---|
| 83 | }; |
|---|
| 84 | class registry_key { |
|---|
| 85 | HKEY hKey_; |
|---|
| 86 | std::wstring path_; |
|---|
| 87 | BYTE *bData_; |
|---|
| 88 | TCHAR *buffer_; |
|---|
| 89 | public: |
|---|
| 90 | registry_key(HKEY hRootKey, std::wstring path) : path_(path), hKey_(NULL), bData_(NULL), buffer_(NULL) { |
|---|
| 91 | LONG lRet = ERROR_SUCCESS; |
|---|
| 92 | if (lRet = RegOpenKeyEx(hRootKey, path.c_str(), 0, KEY_QUERY_VALUE|KEY_READ, &hKey_) != ERROR_SUCCESS) |
|---|
| 93 | throw registry_exception(path, _T("Failed to open key: ") + error::format::from_system(lRet)); |
|---|
| 94 | } |
|---|
| 95 | ~registry_key() { |
|---|
| 96 | if (hKey_ != NULL) |
|---|
| 97 | RegCloseKey(hKey_); |
|---|
| 98 | delete [] bData_; |
|---|
| 99 | delete [] buffer_; |
|---|
| 100 | } |
|---|
| 101 | std::wstring get_string(std::wstring key, DWORD buffer_length = 2048) { |
|---|
| 102 | DWORD type; |
|---|
| 103 | std::wstring ret; |
|---|
| 104 | DWORD cbData = buffer_length; |
|---|
| 105 | delete [] bData_; |
|---|
| 106 | bData_ = new BYTE[cbData+2]; |
|---|
| 107 | // TODO: add get size here ! |
|---|
| 108 | LONG lRet = RegQueryValueEx(hKey_, key.c_str(), NULL, &type, bData_, &cbData); |
|---|
| 109 | if (lRet != ERROR_SUCCESS) |
|---|
| 110 | throw registry_exception(path_, key, _T("Failed to get value: ") + error::format::from_system(lRet)); |
|---|
| 111 | if (cbData >= buffer_length || cbData < 0) |
|---|
| 112 | throw registry_exception(path_, key, _T("Failed to get value: buffer to small")); |
|---|
| 113 | bData_[cbData] = 0; |
|---|
| 114 | if (type == REG_SZ) { |
|---|
| 115 | ret = reinterpret_cast<LPCTSTR>(bData_); |
|---|
| 116 | } else if (type == REG_EXPAND_SZ) { |
|---|
| 117 | std::wstring s = reinterpret_cast<LPCTSTR>(bData_); |
|---|
| 118 | delete [] buffer_; |
|---|
| 119 | buffer_ = new TCHAR[buffer_length+1]; |
|---|
| 120 | DWORD expRet = ExpandEnvironmentStrings(s.c_str(), buffer_, buffer_length); |
|---|
| 121 | if (expRet >= buffer_length) |
|---|
| 122 | throw registry_exception(path_, key, _T("Buffer to small (expand)")); |
|---|
| 123 | else |
|---|
| 124 | ret = buffer_; |
|---|
| 125 | } else { |
|---|
| 126 | throw registry_exception(path_, key, _T("Unknown type (not a string)")); |
|---|
| 127 | } |
|---|
| 128 | return ret; |
|---|
| 129 | } |
|---|
| 130 | DWORD get_int(std::wstring key) { |
|---|
| 131 | DWORD type; |
|---|
| 132 | DWORD cbData = sizeof(DWORD); |
|---|
| 133 | DWORD ret = 0; |
|---|
| 134 | LONG lRet = RegQueryValueEx(hKey_, key.c_str(), NULL, &type, reinterpret_cast<LPBYTE>(&ret), &cbData); |
|---|
| 135 | if (lRet != ERROR_SUCCESS) |
|---|
| 136 | throw registry_exception(path_, key, _T("Failed to get value: ") + error::format::from_system(lRet)); |
|---|
| 137 | if (type != REG_DWORD) |
|---|
| 138 | throw registry_exception(path_, key, _T("Unknown type (not a DWORD)")); |
|---|
| 139 | return ret; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | std::list<std::wstring> get_keys(DWORD buffer_length = 2048) { |
|---|
| 143 | std::list<std::wstring> ret; |
|---|
| 144 | DWORD cSubKeys=0; |
|---|
| 145 | DWORD cMaxKeyLen; |
|---|
| 146 | // Get the class name and the value count. |
|---|
| 147 | LONG lRet = RegQueryInfoKey(hKey_,NULL,NULL,NULL,&cSubKeys,&cMaxKeyLen,NULL,NULL,NULL,NULL,NULL,NULL); |
|---|
| 148 | if (lRet != ERROR_SUCCESS) |
|---|
| 149 | throw registry_exception(path_, _T("Failed to query key info: ") + error::format::from_system(lRet)); |
|---|
| 150 | if (cSubKeys == 0) |
|---|
| 151 | return ret; |
|---|
| 152 | delete [] buffer_; |
|---|
| 153 | buffer_ = new TCHAR[cMaxKeyLen+20]; |
|---|
| 154 | for (unsigned int i=0; i<cSubKeys; i++) { |
|---|
| 155 | lRet = RegEnumKey(hKey_, i, buffer_, cMaxKeyLen+10); |
|---|
| 156 | if (lRet != ERROR_SUCCESS) { |
|---|
| 157 | throw registry_exception(path_, _T("Failed to enumerate: ") + error::lookup::last_error(lRet)); |
|---|
| 158 | } |
|---|
| 159 | std::wstring str = buffer_; |
|---|
| 160 | ret.push_back(str); |
|---|
| 161 | } |
|---|
| 162 | return ret; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | }; |
|---|
| 166 | |
|---|
| 167 | std::wstring get_string(HKEY hKey, std::wstring path, std::wstring key) { |
|---|
| 168 | registry_key reg(hKey, path); |
|---|
| 169 | return reg.get_string(key); |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | std::wstring find_eventlog_name(std::wstring name) { |
|---|
| 174 | try { |
|---|
| 175 | simple_registry::registry_key key(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\Services\\EventLog")); |
|---|
| 176 | std::list<std::wstring> list = key.get_keys(); |
|---|
| 177 | for (std::list<std::wstring>::const_iterator cit = list.begin(); cit != list.end(); ++cit) { |
|---|
| 178 | try { |
|---|
| 179 | simple_registry::registry_key sub_key(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\") + *cit); |
|---|
| 180 | std::wstring file = sub_key.get_string(_T("DisplayNameFile")); |
|---|
| 181 | int id = sub_key.get_int(_T("DisplayNameID")); |
|---|
| 182 | std::wstring real_name = error::format::message::from_module(file, id); |
|---|
| 183 | strEx::replace(real_name, _T("\n"), _T("")); |
|---|
| 184 | strEx::replace(real_name, _T("\r"), _T("")); |
|---|
| 185 | NSC_DEBUG_MSG(_T("Attempting to match: ") + real_name + _T(" with ") + name); |
|---|
| 186 | if (real_name == name) |
|---|
| 187 | return *cit; |
|---|
| 188 | } catch (simple_registry::registry_exception &e) {} |
|---|
| 189 | } |
|---|
| 190 | return name; |
|---|
| 191 | } catch (simple_registry::registry_exception &e) { |
|---|
| 192 | NSC_DEBUG_MSG(_T("Failed to get eventlog name (assuming shorthand): ") + e.what()); |
|---|
| 193 | return name; |
|---|
| 194 | } catch (...) { |
|---|
| 195 | NSC_DEBUG_MSG(_T("Failed to get eventlog name (assuming shorthand)")); |
|---|
| 196 | return name; |
|---|
| 197 | } |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | class EventLogRecord { |
|---|
| 201 | EVENTLOGRECORD *pevlr_; |
|---|
| 202 | __int64 currentTime_; |
|---|
| 203 | std::wstring file_; |
|---|
| 204 | public: |
|---|
| 205 | EventLogRecord(std::wstring file, EVENTLOGRECORD *pevlr, __int64 currentTime) : file_(file), pevlr_(pevlr), currentTime_(currentTime) { |
|---|
| 206 | } |
|---|
| 207 | inline __int64 timeGenerated() const { |
|---|
| 208 | return (currentTime_-pevlr_->TimeGenerated)*1000; |
|---|
| 209 | } |
|---|
| 210 | inline __int64 timeWritten() const { |
|---|
| 211 | return (currentTime_-pevlr_->TimeWritten)*1000; |
|---|
| 212 | } |
|---|
| 213 | inline std::wstring eventSource() const { |
|---|
| 214 | return reinterpret_cast<WCHAR*>(reinterpret_cast<LPBYTE>(pevlr_) + sizeof(EVENTLOGRECORD)); |
|---|
| 215 | } |
|---|
| 216 | inline DWORD eventID() const { |
|---|
| 217 | return (pevlr_->EventID&0xffff); |
|---|
| 218 | } |
|---|
| 219 | inline DWORD severity() const { |
|---|
| 220 | return (pevlr_->EventID>>30); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | inline DWORD eventType() const { |
|---|
| 224 | return pevlr_->EventType; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | std::wstring userSID() const { |
|---|
| 228 | if (pevlr_->UserSidOffset == 0) |
|---|
| 229 | return _T(""); |
|---|
| 230 | PSID p = reinterpret_cast<PSID>(reinterpret_cast<LPBYTE>(pevlr_) + + pevlr_->UserSidOffset); |
|---|
| 231 | DWORD userLen = 0; |
|---|
| 232 | DWORD domainLen = 0; |
|---|
| 233 | SID_NAME_USE sidName; |
|---|
| 234 | |
|---|
| 235 | LookupAccountSid(NULL, p, NULL, &userLen, NULL, &domainLen, &sidName); |
|---|
| 236 | LPTSTR user = new TCHAR[userLen+10]; |
|---|
| 237 | LPTSTR domain = new TCHAR[domainLen+10]; |
|---|
| 238 | |
|---|
| 239 | LookupAccountSid(NULL, p, user, &userLen, domain, &domainLen, &sidName); |
|---|
| 240 | user[userLen] = 0; |
|---|
| 241 | domain[domainLen] = 0; |
|---|
| 242 | std::wstring ustr = user; |
|---|
| 243 | std::wstring dstr = domain; |
|---|
| 244 | delete [] user; |
|---|
| 245 | delete [] domain; |
|---|
| 246 | if (!dstr.empty()) |
|---|
| 247 | dstr = dstr + _T("\\"); |
|---|
| 248 | if (ustr.empty() && dstr.empty()) |
|---|
| 249 | return _T("missing"); |
|---|
| 250 | |
|---|
| 251 | return dstr + ustr; |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | std::wstring enumStrings() const { |
|---|
| 255 | std::wstring ret; |
|---|
| 256 | TCHAR* p = reinterpret_cast<TCHAR*>(reinterpret_cast<LPBYTE>(pevlr_) + pevlr_->StringOffset); |
|---|
| 257 | for (unsigned int i =0;i<pevlr_->NumStrings;i++) { |
|---|
| 258 | std::wstring s = p; |
|---|
| 259 | if (!s.empty()) |
|---|
| 260 | s += _T(", "); |
|---|
| 261 | ret += s; |
|---|
| 262 | p+= wcslen(p)+1; |
|---|
| 263 | } |
|---|
| 264 | return ret; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | static DWORD appendType(DWORD dwType, std::wstring sType) { |
|---|
| 268 | return dwType | translateType(sType); |
|---|
| 269 | } |
|---|
| 270 | static DWORD subtractType(DWORD dwType, std::wstring sType) { |
|---|
| 271 | return dwType & (!translateType(sType)); |
|---|
| 272 | } |
|---|
| 273 | static DWORD translateType(std::wstring sType) { |
|---|
| 274 | if (sType == _T("error")) |
|---|
| 275 | return EVENTLOG_ERROR_TYPE; |
|---|
| 276 | if (sType == _T("warning")) |
|---|
| 277 | return EVENTLOG_WARNING_TYPE; |
|---|
| 278 | if (sType == _T("info")) |
|---|
| 279 | return EVENTLOG_INFORMATION_TYPE; |
|---|
| 280 | if (sType == _T("auditSuccess")) |
|---|
| 281 | return EVENTLOG_AUDIT_SUCCESS; |
|---|
| 282 | if (sType == _T("auditFailure")) |
|---|
| 283 | return EVENTLOG_AUDIT_FAILURE; |
|---|
| 284 | return strEx::stoi(sType); |
|---|
| 285 | } |
|---|
| 286 | static std::wstring translateType(DWORD dwType) { |
|---|
| 287 | if (dwType == EVENTLOG_ERROR_TYPE) |
|---|
| 288 | return _T("error"); |
|---|
| 289 | if (dwType == EVENTLOG_WARNING_TYPE) |
|---|
| 290 | return _T("warning"); |
|---|
| 291 | if (dwType == EVENTLOG_INFORMATION_TYPE) |
|---|
| 292 | return _T("info"); |
|---|
| 293 | if (dwType == EVENTLOG_AUDIT_SUCCESS) |
|---|
| 294 | return _T("auditSuccess"); |
|---|
| 295 | if (dwType == EVENTLOG_AUDIT_FAILURE) |
|---|
| 296 | return _T("auditFailure"); |
|---|
| 297 | return strEx::itos(dwType); |
|---|
| 298 | } |
|---|
| 299 | static DWORD translateSeverity(std::wstring sType) { |
|---|
| 300 | if (sType == _T("success")) |
|---|
| 301 | return 0; |
|---|
| 302 | if (sType == _T("informational")) |
|---|
| 303 | return 1; |
|---|
| 304 | if (sType == _T("warning")) |
|---|
| 305 | return 2; |
|---|
| 306 | if (sType == _T("error")) |
|---|
| 307 | return 3; |
|---|
| 308 | return strEx::stoi(sType); |
|---|
| 309 | } |
|---|
| 310 | static std::wstring translateSeverity(DWORD dwType) { |
|---|
| 311 | if (dwType == 0) |
|---|
| 312 | return _T("success"); |
|---|
| 313 | if (dwType == 1) |
|---|
| 314 | return _T("informational"); |
|---|
| 315 | if (dwType == 2) |
|---|
| 316 | return _T("warning"); |
|---|
| 317 | if (dwType == 3) |
|---|
| 318 | return _T("error"); |
|---|
| 319 | return strEx::itos(dwType); |
|---|
| 320 | } |
|---|
| 321 | std::wstring get_dll() { |
|---|
| 322 | try { |
|---|
| 323 | return simple_registry::get_string(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\") + file_ + (std::wstring)_T("\\") + eventSource(), _T("EventMessageFile")); |
|---|
| 324 | } catch (simple_registry::registry_exception &e) { |
|---|
| 325 | NSC_LOG_ERROR_STD(_T("Could not extract DLL for eventsource: ") + eventSource() + _T(": ") + e.what()); |
|---|
| 326 | return _T(""); |
|---|
| 327 | } |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | std::wstring render_message() { |
|---|
| 331 | std::vector<std::wstring> args; |
|---|
| 332 | TCHAR* *pArgs = new TCHAR*[pevlr_->NumStrings+1]; |
|---|
| 333 | TCHAR* p = reinterpret_cast<TCHAR*>(reinterpret_cast<LPBYTE>(pevlr_) + pevlr_->StringOffset); |
|---|
| 334 | for (unsigned int i =0;i<pevlr_->NumStrings;i++) { |
|---|
| 335 | args.push_back(p); |
|---|
| 336 | pArgs[i] = p; |
|---|
| 337 | DWORD len = wcslen(p); |
|---|
| 338 | p = &(p[len+1]); |
|---|
| 339 | //p += len+1; |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | std::wstring ret; |
|---|
| 343 | strEx::splitList dlls = strEx::splitEx(get_dll(), _T(";")); |
|---|
| 344 | for (strEx::splitList::const_iterator cit = dlls.begin(); cit != dlls.end(); ++cit) { |
|---|
| 345 | //std::wstring msg = error::format::message::from_module((*cit), eventID(), _sz); |
|---|
| 346 | std::wstring msg; |
|---|
| 347 | try { |
|---|
| 348 | msg = error::format::message::from_module_x64((*cit), eventID(), pArgs, pevlr_->NumStrings); |
|---|
| 349 | if (msg.empty()) { |
|---|
| 350 | msg = error::format::message::from_module_x64((*cit), pevlr_->EventID, pArgs, pevlr_->NumStrings); |
|---|
| 351 | } |
|---|
| 352 | } catch (...) { |
|---|
| 353 | msg = _T("Unknown exception getting message"); |
|---|
| 354 | } |
|---|
| 355 | strEx::replace(msg, _T("\n"), _T(" ")); |
|---|
| 356 | strEx::replace(msg, _T("\t"), _T(" ")); |
|---|
| 357 | std::string::size_type pos = msg.find_last_not_of(_T("\n\t ")); |
|---|
| 358 | if (pos != std::string::npos) { |
|---|
| 359 | msg = msg.substr(0,pos); |
|---|
| 360 | } |
|---|
| 361 | if (!msg.empty()) { |
|---|
| 362 | if (!ret.empty()) |
|---|
| 363 | ret += _T(", "); |
|---|
| 364 | ret += msg; |
|---|
| 365 | } |
|---|
| 366 | } |
|---|
| 367 | delete [] pArgs; |
|---|
| 368 | return ret; |
|---|
| 369 | } |
|---|
| 370 | SYSTEMTIME get_time(DWORD time) { |
|---|
| 371 | FILETIME FileTime, LocalFileTime; |
|---|
| 372 | SYSTEMTIME SysTime; |
|---|
| 373 | __int64 lgTemp; |
|---|
| 374 | __int64 SecsTo1970 = 116444736000000000; |
|---|
| 375 | |
|---|
| 376 | lgTemp = Int32x32To64(time,10000000) + SecsTo1970; |
|---|
| 377 | |
|---|
| 378 | FileTime.dwLowDateTime = (DWORD) lgTemp; |
|---|
| 379 | FileTime.dwHighDateTime = (DWORD)(lgTemp >> 32); |
|---|
| 380 | |
|---|
| 381 | FileTimeToLocalFileTime(&FileTime, &LocalFileTime); |
|---|
| 382 | FileTimeToSystemTime(&LocalFileTime, &SysTime); |
|---|
| 383 | return SysTime; |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | SYSTEMTIME get_time_generated() { |
|---|
| 387 | return get_time(pevlr_->TimeGenerated); |
|---|
| 388 | } |
|---|
| 389 | SYSTEMTIME get_time_written() { |
|---|
| 390 | return get_time(pevlr_->TimeWritten); |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | std::wstring render(bool propper, std::wstring syntax, std::wstring date_format = DATE_FORMAT) { |
|---|
| 394 | if (propper) { |
|---|
| 395 | // To obtain the appropriate message string from the message file, load the message file with the LoadLibrary function and use the FormatMessage function |
|---|
| 396 | strEx::replace(syntax, _T("%message%"), render_message()); |
|---|
| 397 | } else { |
|---|
| 398 | strEx::replace(syntax, _T("%message%"), _T("%message% needs the descriptions flag set!")); |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | strEx::replace(syntax, _T("%source%"), eventSource()); |
|---|
| 402 | strEx::replace(syntax, _T("%generated%"), strEx::format_date(get_time_generated(), date_format)); |
|---|
| 403 | strEx::replace(syntax, _T("%written%"), strEx::format_date(get_time_written(), date_format)); |
|---|
| 404 | strEx::replace(syntax, _T("%type%"), translateType(eventType())); |
|---|
| 405 | strEx::replace(syntax, _T("%severity%"), translateSeverity(severity())); |
|---|
| 406 | strEx::replace(syntax, _T("%strings%"), enumStrings()); |
|---|
| 407 | strEx::replace(syntax, _T("%id%"), strEx::itos(eventID())); |
|---|
| 408 | strEx::replace(syntax, _T("%user%"), userSID()); |
|---|
| 409 | return syntax; |
|---|
| 410 | } |
|---|
| 411 | }; |
|---|
| 412 | /* |
|---|
| 413 | return (pevlr_->EventID&0xffff); |
|---|
| 414 | } |
|---|
| 415 | inline DWORD severity() const { |
|---|
| 416 | return (pevlr_->EventID>>30); |
|---|
| 417 | */ |
|---|
| 418 | class uniq_eventlog_record { |
|---|
| 419 | DWORD ID; |
|---|
| 420 | WORD type; |
|---|
| 421 | WORD category; |
|---|
| 422 | public: |
|---|
| 423 | std::wstring message; |
|---|
| 424 | uniq_eventlog_record(EVENTLOGRECORD *pevlr) : ID(pevlr->EventID&0xffff), type(pevlr->EventType), category(pevlr->EventCategory) {} |
|---|
| 425 | bool operator< (const uniq_eventlog_record &other) const { |
|---|
| 426 | return (ID < other.ID) || ((ID==other.ID)&&(type < other.type)) || (ID==other.ID&&type==other.type)&&(category < other.category); |
|---|
| 427 | } |
|---|
| 428 | std::wstring to_string() const { |
|---|
| 429 | return _T("id=") + strEx::itos(ID) + _T("type=") + strEx::itos(type) + _T("category=") + strEx::itos(category); |
|---|
| 430 | } |
|---|
| 431 | }; |
|---|
| 432 | typedef std::map<uniq_eventlog_record,unsigned int> uniq_eventlog_map; |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | struct eventlog_filter { |
|---|
| 436 | filters::filter_all_strings eventSource; |
|---|
| 437 | filters::filter_all_numeric<unsigned int, filters::handlers::eventtype_handler> eventType; |
|---|
| 438 | filters::filter_all_numeric<unsigned int, filters::handlers::eventseverity_handler> eventSeverity; |
|---|
| 439 | filters::filter_all_strings message; |
|---|
| 440 | filters::filter_all_times timeWritten; |
|---|
| 441 | filters::filter_all_times timeGenerated; |
|---|
| 442 | filters::filter_all_numeric<DWORD, filters::handlers::eventtype_handler> eventID; |
|---|
| 443 | std::wstring value_; |
|---|
| 444 | |
|---|
| 445 | inline bool hasFilter() { |
|---|
| 446 | return eventSource.hasFilter() || eventType.hasFilter() || eventID.hasFilter() || eventSeverity.hasFilter() || message.hasFilter() || |
|---|
| 447 | timeWritten.hasFilter() || timeGenerated.hasFilter(); |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | #define NSCP_EL_DEBUG(key) if (key.hasFilter()) strEx::append_list(str, std::wstring(_T( # key )) + _T(" ") + key.to_string(), _T(",")); |
|---|
| 451 | std::wstring to_string() const { |
|---|
| 452 | std::wstring str; |
|---|
| 453 | NSCP_EL_DEBUG(eventSource); |
|---|
| 454 | NSCP_EL_DEBUG(eventType); |
|---|
| 455 | NSCP_EL_DEBUG(eventSeverity); |
|---|
| 456 | NSCP_EL_DEBUG(eventID); |
|---|
| 457 | NSCP_EL_DEBUG(message); |
|---|
| 458 | NSCP_EL_DEBUG(timeWritten); |
|---|
| 459 | NSCP_EL_DEBUG(timeGenerated); |
|---|
| 460 | return str; |
|---|
| 461 | } |
|---|
| 462 | bool matchFilter(const EventLogRecord &value) const { |
|---|
| 463 | bool ret = false; |
|---|
| 464 | if ((eventSource.hasFilter())&&(eventSource.matchFilter(value.eventSource()))) |
|---|
| 465 | ret = true; |
|---|
| 466 | else if (eventSource.hasFilter()) |
|---|
| 467 | return false; |
|---|
| 468 | else if ((eventType.hasFilter())&&(eventType.matchFilter(value.eventType()))) |
|---|
| 469 | ret = true; |
|---|
| 470 | else if (eventType.hasFilter()) |
|---|
| 471 | return false; |
|---|
| 472 | else if ((eventSeverity.hasFilter())&&(eventSeverity.matchFilter(value.severity()))) |
|---|
| 473 | ret = true; |
|---|
| 474 | else if (eventSeverity.hasFilter()) |
|---|
| 475 | return false; |
|---|
| 476 | else if ((eventID.hasFilter())&&(eventID.matchFilter(value.eventID()))) |
|---|
| 477 | ret = true; |
|---|
| 478 | else if (eventID.hasFilter()) |
|---|
| 479 | return false; |
|---|
| 480 | else if ((message.hasFilter())&&(message.matchFilter(value.enumStrings()))) |
|---|
| 481 | ret = true; |
|---|
| 482 | else if (message.hasFilter()) |
|---|
| 483 | return false; |
|---|
| 484 | else if ((timeWritten.hasFilter())&&(timeWritten.matchFilter(value.timeWritten()))) |
|---|
| 485 | ret = true; |
|---|
| 486 | else if (timeWritten.hasFilter()) |
|---|
| 487 | return false; |
|---|
| 488 | else if ((timeGenerated.hasFilter())&&(timeGenerated.matchFilter(value.timeGenerated()))) |
|---|
| 489 | ret = true; |
|---|
| 490 | else if (timeGenerated.hasFilter()) |
|---|
| 491 | return false; |
|---|
| 492 | return ret; |
|---|
| 493 | } |
|---|
| 494 | }; |
|---|
| 495 | |
|---|
| 496 | |
|---|
| 497 | #define MAP_FILTER(value, obj, filtermode) \ |
|---|
| 498 | else if (p__.first == value) { filter.obj = p__.second; if (bPush) { filter_chain.push_back(filteritem_type(filtermode, filter)); filter = eventlog_filter(); } } |
|---|
| 499 | #define MAP_FILTER_LAST(value, obj) \ |
|---|
| 500 | else if (p__.first == value) { filter_chain.front().second.obj = p__.second; } |
|---|
| 501 | |
|---|
| 502 | struct event_log_buffer { |
|---|
| 503 | BYTE *bBuffer; |
|---|
| 504 | DWORD bufferSize_; |
|---|
| 505 | event_log_buffer(DWORD bufferSize) : bufferSize_(bufferSize) { |
|---|
| 506 | bBuffer = new BYTE[bufferSize+10]; |
|---|
| 507 | } |
|---|
| 508 | ~event_log_buffer() { |
|---|
| 509 | delete [] bBuffer; |
|---|
| 510 | } |
|---|
| 511 | EVENTLOGRECORD* getBufferUnsafe() { |
|---|
| 512 | return reinterpret_cast<EVENTLOGRECORD*>(bBuffer); |
|---|
| 513 | } |
|---|
| 514 | DWORD getBufferSize() { |
|---|
| 515 | return bufferSize_; |
|---|
| 516 | } |
|---|
| 517 | }; |
|---|
| 518 | |
|---|
| 519 | NSCAPI::nagiosReturn CheckEventLog::handleCommand(const strEx::blindstr command, const unsigned int argLen, TCHAR **char_args, std::wstring &message, std::wstring &perf) { |
|---|
| 520 | if (command != _T("CheckEventLog")) |
|---|
| 521 | return NSCAPI::returnIgnored; |
|---|
| 522 | typedef checkHolders::CheckContainer<checkHolders::MaxMinBoundsULongInteger> EventLogQuery1Container; |
|---|
| 523 | typedef checkHolders::CheckContainer<checkHolders::ExactBoundsULongInteger> EventLogQuery2Container; |
|---|
| 524 | |
|---|
| 525 | typedef std::pair<int,eventlog_filter> filteritem_type; |
|---|
| 526 | typedef std::list<filteritem_type > filterlist_type; |
|---|
| 527 | NSCAPI::nagiosReturn returnCode = NSCAPI::returnOK; |
|---|
| 528 | std::list<std::wstring> stl_args = arrayBuffer::arrayBuffer2list(argLen, char_args); |
|---|
| 529 | |
|---|
| 530 | std::list<std::wstring> files; |
|---|
| 531 | filterlist_type filter_chain; |
|---|
| 532 | EventLogQuery1Container query1; |
|---|
| 533 | EventLogQuery2Container query2; |
|---|
| 534 | |
|---|
| 535 | bool bPerfData = true; |
|---|
| 536 | bool bFilterIn = true; |
|---|
| 537 | bool bFilterAll = false; |
|---|
| 538 | bool bFilterNew = true; |
|---|
| 539 | bool bShowDescriptions = false; |
|---|
| 540 | bool unique = false; |
|---|
| 541 | unsigned int truncate = 0; |
|---|
| 542 | std::wstring syntax = syntax_; |
|---|
| 543 | const int filter_plus = 1; |
|---|
| 544 | const int filter_minus = 2; |
|---|
| 545 | const int filter_normal = 3; |
|---|
| 546 | const int filter_compat = 3; |
|---|
| 547 | event_log_buffer buffer(buffer_length_); |
|---|
| 548 | bool bPush = true; |
|---|
| 549 | bool bDebug = debug_; |
|---|
| 550 | eventlog_filter filter; |
|---|
| 551 | /* |
|---|
| 552 | try { |
|---|
| 553 | event_log_buffer buffer(buffer_length_); |
|---|
| 554 | } catch (std::exception e) { |
|---|
| 555 | message = std::wstring(_T("Failed to allocate memory: ")) + strEx::string_to_wstring(e.what()); |
|---|
| 556 | return NSCAPI::returnUNKNOWN; |
|---|
| 557 | } |
|---|
| 558 | */ |
|---|
| 559 | |
|---|
| 560 | try { |
|---|
| 561 | MAP_OPTIONS_BEGIN(stl_args) |
|---|
| 562 | MAP_OPTIONS_NUMERIC_ALL(query1, _T("")) |
|---|
| 563 | MAP_OPTIONS_EXACT_NUMERIC_ALL(query2, _T("")) |
|---|
| 564 | MAP_OPTIONS_STR2INT(_T("truncate"), truncate) |
|---|
| 565 | MAP_OPTIONS_BOOL_TRUE(_T("unique"), unique) |
|---|
| 566 | MAP_OPTIONS_BOOL_TRUE(_T("descriptions"), bShowDescriptions) |
|---|
| 567 | MAP_OPTIONS_PUSH(_T("file"), files) |
|---|
| 568 | MAP_OPTIONS_BOOL_FALSE(IGNORE_PERFDATA, bPerfData) |
|---|
| 569 | MAP_OPTIONS_BOOL_EX(_T("filter"), bFilterNew, _T("new"), _T("old")) |
|---|
| 570 | MAP_OPTIONS_BOOL_EX(_T("filter"), bFilterIn, _T("in"), _T("out")) |
|---|
| 571 | MAP_OPTIONS_BOOL_EX(_T("filter"), bFilterAll, _T("all"), _T("any")) |
|---|
| 572 | MAP_OPTIONS_BOOL_EX(_T("auto-push"), bPush, _T("true"), _T("false")) |
|---|
| 573 | MAP_OPTIONS_BOOL_EX(_T("debug"), bDebug, _T("true"), _T("false")) |
|---|
| 574 | MAP_OPTIONS_STR(_T("syntax"), syntax) |
|---|
| 575 | /* |
|---|
| 576 | MAP_FILTER_OLD("filter-eventType", eventType) |
|---|
| 577 | MAP_FILTER_OLD("filter-severity", eventSeverity) |
|---|
| 578 | MAP_FILTER_OLD("filter-eventID", eventID) |
|---|
| 579 | MAP_FILTER_OLD("filter-eventSource", eventSource) |
|---|
| 580 | MAP_FILTER_OLD("filter-generated", timeGenerated) |
|---|
| 581 | MAP_FILTER_OLD("filter-written", timeWritten) |
|---|
| 582 | MAP_FILTER_OLD("filter-message", message) |
|---|
| 583 | */ |
|---|
| 584 | MAP_FILTER(_T("filter+eventType"), eventType, filter_plus) |
|---|
| 585 | MAP_FILTER(_T("filter+severity"), eventSeverity, filter_plus) |
|---|
| 586 | MAP_FILTER(_T("filter+eventID"), eventID, filter_plus) |
|---|
| 587 | MAP_FILTER(_T("filter+eventSource"), eventSource, filter_plus) |
|---|
| 588 | MAP_FILTER(_T("filter+generated"), timeGenerated, filter_plus) |
|---|
| 589 | MAP_FILTER(_T("filter+written"), timeWritten, filter_plus) |
|---|
| 590 | MAP_FILTER(_T("filter+message"), message, filter_plus) |
|---|
| 591 | |
|---|
| 592 | MAP_FILTER(_T("filter.eventType"), eventType, filter_normal) |
|---|
| 593 | MAP_FILTER(_T("filter.severity"), eventSeverity, filter_normal) |
|---|
| 594 | MAP_FILTER(_T("filter.eventID"), eventID, filter_normal) |
|---|
| 595 | MAP_FILTER(_T("filter.eventSource"), eventSource, filter_normal) |
|---|
| 596 | MAP_FILTER(_T("filter.generated"), timeGenerated, filter_normal) |
|---|
| 597 | MAP_FILTER(_T("filter.written"), timeWritten, filter_normal) |
|---|
| 598 | MAP_FILTER(_T("filter.message"), message, filter_normal) |
|---|
| 599 | |
|---|
| 600 | MAP_FILTER(_T("filter-eventType"), eventType, filter_minus) |
|---|
| 601 | MAP_FILTER(_T("filter-severity"), eventSeverity, filter_minus) |
|---|
| 602 | MAP_FILTER(_T("filter-eventID"), eventID, filter_minus) |
|---|
| 603 | MAP_FILTER(_T("filter-eventSource"), eventSource, filter_minus) |
|---|
| 604 | MAP_FILTER(_T("filter-generated"), timeGenerated, filter_minus) |
|---|
| 605 | MAP_FILTER(_T("filter-written"), timeWritten, filter_minus) |
|---|
| 606 | MAP_FILTER(_T("filter-message"), message, filter_minus) |
|---|
| 607 | |
|---|
| 608 | MAP_FILTER_LAST(_T("append-filter-eventType"), eventType) |
|---|
| 609 | MAP_FILTER_LAST(_T("append-filter-severity"), eventSeverity) |
|---|
| 610 | MAP_FILTER_LAST(_T("append-filter-eventID"), eventID) |
|---|
| 611 | MAP_FILTER_LAST(_T("append-filter-eventSource"), eventSource) |
|---|
| 612 | MAP_FILTER_LAST(_T("append-filter-generated"), timeGenerated) |
|---|
| 613 | MAP_FILTER_LAST(_T("append-filter-written"), timeWritten) |
|---|
| 614 | MAP_FILTER_LAST(_T("append-filter-message"), message) |
|---|
| 615 | |
|---|
| 616 | |
|---|
| 617 | MAP_OPTIONS_MISSING(message, _T("Unknown argument: ")) |
|---|
| 618 | MAP_OPTIONS_END() |
|---|
| 619 | } catch (filters::parse_exception e) { |
|---|
| 620 | message = e.getMessage(); |
|---|
| 621 | return NSCAPI::returnUNKNOWN; |
|---|
| 622 | } catch (filters::filter_exception e) { |
|---|
| 623 | message = e.getMessage(); |
|---|
| 624 | return NSCAPI::returnUNKNOWN; |
|---|
| 625 | } catch (checkHolders::parse_exception e) { |
|---|
| 626 | message = e.getMessage(); |
|---|
| 627 | return NSCAPI::returnUNKNOWN; |
|---|
| 628 | } catch (...) { |
|---|
| 629 | message = _T("Invalid command line!"); |
|---|
| 630 | return NSCAPI::returnUNKNOWN; |
|---|
| 631 | } |
|---|
| 632 | |
|---|
| 633 | unsigned long int hit_count = 0; |
|---|
| 634 | if (files.empty()) { |
|---|
| 635 | message = _T("No file specified try adding: file=Application"); |
|---|
| 636 | return NSCAPI::returnUNKNOWN; |
|---|
| 637 | } |
|---|
| 638 | bool buffer_error_reported = false; |
|---|
| 639 | if (bDebug) { |
|---|
| 640 | std::wstring str; |
|---|
| 641 | BOOST_FOREACH(filteritem_type item, filter_chain) { |
|---|
| 642 | if (item.first == filter_normal) |
|---|
| 643 | str += _T(". {"); |
|---|
| 644 | else if (item.first == filter_plus) |
|---|
| 645 | str += _T("+ {"); |
|---|
| 646 | else if (item.first == filter_minus) |
|---|
| 647 | str += _T("- {"); |
|---|
| 648 | else |
|---|
| 649 | str += _T("? {"); |
|---|
| 650 | |
|---|
| 651 | str += item.second.to_string() + _T(" }"); |
|---|
| 652 | } |
|---|
| 653 | NSC_DEBUG_MSG_STD(_T("Filter: ") + str); |
|---|
| 654 | } |
|---|
| 655 | |
|---|
| 656 | bDebug = false; |
|---|
| 657 | for (std::list<std::wstring>::const_iterator cit2 = files.begin(); cit2 != files.end(); ++cit2) { |
|---|
| 658 | std::wstring name = *cit2; |
|---|
| 659 | if (lookup_names_) { |
|---|
| 660 | name = find_eventlog_name(*cit2); |
|---|
| 661 | if ((*cit2) != name) { |
|---|
| 662 | NSC_DEBUG_MSG_STD(_T("Opening alternative log: ") + name); |
|---|
| 663 | } |
|---|
| 664 | } |
|---|
| 665 | HANDLE hLog = OpenEventLog(NULL, name.c_str()); |
|---|
| 666 | if (hLog == NULL) { |
|---|
| 667 | message = _T("Could not open the '") + (*cit2) + _T("' event log: ") + error::lookup::last_error(); |
|---|
| 668 | return NSCAPI::returnUNKNOWN; |
|---|
| 669 | } |
|---|
| 670 | uniq_eventlog_map uniq_records; |
|---|
| 671 | |
|---|
| 672 | //DWORD dwThisRecord; |
|---|
| 673 | DWORD dwRead, dwNeeded; |
|---|
| 674 | |
|---|
| 675 | |
|---|
| 676 | __time64_t ltime; |
|---|
| 677 | _time64(<ime); |
|---|
| 678 | |
|---|
| 679 | //GetOldestEventLogRecord(hLog, &dwThisRecord); |
|---|
| 680 | |
|---|
| 681 | while (true) { |
|---|
| 682 | BOOL bStatus = ReadEventLog(hLog, EVENTLOG_FORWARDS_READ|EVENTLOG_SEQUENTIAL_READ, |
|---|
| 683 | 0, buffer.getBufferUnsafe(), buffer.getBufferSize(), &dwRead, &dwNeeded); |
|---|
| 684 | if (bStatus == FALSE) { |
|---|
| 685 | DWORD err = GetLastError(); |
|---|
| 686 | if (err == ERROR_INSUFFICIENT_BUFFER) { |
|---|
| 687 | if (!buffer_error_reported) { |
|---|
| 688 | NSC_LOG_ERROR_STD(_T("EvenlogBuffer is too small change the value of ") + EVENTLOG_BUFFER + _T("=") + strEx::itos(dwNeeded+1) + _T(" under [EventLog] in nsc.ini : ") + error::lookup::last_error(err)); |
|---|
| 689 | buffer_error_reported = true; |
|---|
| 690 | } |
|---|
| 691 | } else if (err == ERROR_HANDLE_EOF) { |
|---|
| 692 | break; |
|---|
| 693 | } else { |
|---|
| 694 | NSC_LOG_ERROR_STD(_T("Failed to read from eventlog: ") + error::lookup::last_error(err)); |
|---|
| 695 | message = _T("Failed to read from eventlog: ") + error::lookup::last_error(err); |
|---|
| 696 | CloseEventLog(hLog); |
|---|
| 697 | return NSCAPI::returnUNKNOWN; |
|---|
| 698 | } |
|---|
| 699 | } |
|---|
| 700 | EVENTLOGRECORD *pevlr = buffer.getBufferUnsafe(); |
|---|
| 701 | while (dwRead > 0) { |
|---|
| 702 | //bool bMatch = bFilterAll; |
|---|
| 703 | bool bMatch = !bFilterIn; |
|---|
| 704 | EventLogRecord record((*cit2), pevlr, ltime); |
|---|
| 705 | |
|---|
| 706 | if (filter_chain.empty()) { |
|---|
| 707 | message = _T("No filters specified try adding: filter+generated=>2d"); |
|---|
| 708 | return NSCAPI::returnUNKNOWN; |
|---|
| 709 | } |
|---|
| 710 | |
|---|
| 711 | |
|---|
| 712 | for (filterlist_type::const_iterator cit3 = filter_chain.begin(); cit3 != filter_chain.end(); ++cit3 ) { |
|---|
| 713 | std::wstring reason; |
|---|
| 714 | int mode = (*cit3).first; |
|---|
| 715 | bool bTmpMatched = (*cit3).second.matchFilter(record); |
|---|
| 716 | if (!bFilterNew) { |
|---|
| 717 | if (bFilterAll) { |
|---|
| 718 | if (!bTmpMatched) { |
|---|
| 719 | bMatch = false; |
|---|
| 720 | break; |
|---|
| 721 | } |
|---|
| 722 | } else { |
|---|
| 723 | if (bTmpMatched) { |
|---|
| 724 | bMatch = true; |
|---|
| 725 | break; |
|---|
| 726 | } |
|---|
| 727 | } |
|---|
| 728 | } else { |
|---|
| 729 | if ((mode == filter_minus)&&(bTmpMatched)) { |
|---|
| 730 | // a -<filter> hit so thrash item and bail out! |
|---|
| 731 | if (bDebug) |
|---|
| 732 | NSC_DEBUG_MSG_STD(_T("Matched: - ") + (*cit3).second.to_string() + _T(" for: ") + record.render(bShowDescriptions, syntax)); |
|---|
| 733 | bMatch = false; |
|---|
| 734 | break; |
|---|
| 735 | } else if ((mode == filter_plus)&&(!bTmpMatched)) { |
|---|
| 736 | // a +<filter> missed hit so thrash item and bail out! |
|---|
| 737 | if (bDebug) |
|---|
| 738 | NSC_DEBUG_MSG_STD(_T("Matched: + ") + (*cit3).second.to_string() + _T(" for: ") + record.render(bShowDescriptions, syntax)); |
|---|
| 739 | bMatch = false; |
|---|
| 740 | break; |
|---|
| 741 | } else if (bTmpMatched) { |
|---|
| 742 | if (bDebug) |
|---|
| 743 | NSC_DEBUG_MSG_STD(_T("Matched: . (contiunue): ") + (*cit3).second.to_string() + _T(" for: ") + record.render(bShowDescriptions, syntax)); |
|---|
| 744 | bMatch = true; |
|---|
| 745 | } |
|---|
| 746 | } |
|---|
| 747 | } |
|---|
| 748 | bool match = false; |
|---|
| 749 | if ((!bFilterNew)&&((bFilterIn&&bMatch)||(!bFilterIn&&!bMatch))) { |
|---|
| 750 | match = true; |
|---|
| 751 | } else if (bFilterNew&&bMatch) { |
|---|
| 752 | match = true; |
|---|
| 753 | } |
|---|
| 754 | if (match&&unique) { |
|---|
| 755 | match = false; |
|---|
| 756 | uniq_eventlog_record uniq_record = pevlr; |
|---|
| 757 | uniq_eventlog_map::iterator it = uniq_records.find(uniq_record); |
|---|
| 758 | if (it != uniq_records.end()) { |
|---|
| 759 | (*it).second ++; |
|---|
| 760 | //match = false; |
|---|
| 761 | } |
|---|
| 762 | else { |
|---|
| 763 | if (!syntax.empty()) { |
|---|
| 764 | uniq_record.message = record.render(bShowDescriptions, syntax); |
|---|
| 765 | } else if (!bShowDescriptions) { |
|---|
| 766 | uniq_record.message = record.eventSource(); |
|---|
| 767 | } else { |
|---|
| 768 | uniq_record.message = record.eventSource(); |
|---|
| 769 | uniq_record.message += _T("(") + EventLogRecord::translateType(record.eventType()) + _T(", ") + |
|---|
| 770 | strEx::itos(record.eventID()) + _T(", ") + EventLogRecord::translateSeverity(record.severity()) + _T(")"); |
|---|
| 771 | uniq_record.message += _T("[") + record.enumStrings() + _T("]"); |
|---|
| 772 | uniq_record.message += _T("{%count%}"); |
|---|
| 773 | } |
|---|
| 774 | uniq_records[uniq_record] = 1; |
|---|
| 775 | } |
|---|
| 776 | hit_count++; |
|---|
| 777 | } else if (match) { |
|---|
| 778 | if (!syntax.empty()) { |
|---|
| 779 | strEx::append_list(message, record.render(bShowDescriptions, syntax)); |
|---|
| 780 | } else if (!bShowDescriptions) { |
|---|
| 781 | strEx::append_list(message, record.eventSource()); |
|---|
| 782 | } else { |
|---|
| 783 | strEx::append_list(message, record.eventSource()); |
|---|
| 784 | message += _T("(") + EventLogRecord::translateType(record.eventType()) + _T(", ") + |
|---|
| 785 | strEx::itos(record.eventID()) + _T(", ") + EventLogRecord::translateSeverity(record.severity()) + _T(")"); |
|---|
| 786 | message += _T("[") + record.enumStrings() + _T("]"); |
|---|
| 787 | } |
|---|
| 788 | hit_count++; |
|---|
| 789 | } |
|---|
| 790 | dwRead -= pevlr->Length; |
|---|
| 791 | pevlr = reinterpret_cast<EVENTLOGRECORD*>((LPBYTE)pevlr + pevlr->Length); |
|---|
| 792 | } |
|---|
| 793 | } |
|---|
| 794 | CloseEventLog(hLog); |
|---|
| 795 | for (uniq_eventlog_map::const_iterator cit = uniq_records.begin(); cit != uniq_records.end(); ++cit) { |
|---|
| 796 | std::wstring msg = (*cit).first.message; |
|---|
| 797 | strEx::replace(msg, _T("%count%"), strEx::itos((*cit).second)); |
|---|
| 798 | strEx::append_list(message, msg); |
|---|
| 799 | } |
|---|
| 800 | } |
|---|
| 801 | |
|---|
| 802 | if (!bPerfData) { |
|---|
| 803 | query1.perfData = false; |
|---|
| 804 | query2.perfData = false; |
|---|
| 805 | } |
|---|
| 806 | if (query1.alias.empty()) |
|---|
| 807 | query1.alias = _T("eventlog"); |
|---|
| 808 | if (query2.alias.empty()) |
|---|
| 809 | query2.alias = _T("eventlog"); |
|---|
| 810 | if (query1.hasBounds()) |
|---|
| 811 | query1.runCheck(hit_count, returnCode, message, perf); |
|---|
| 812 | else if (query2.hasBounds()) |
|---|
| 813 | query2.runCheck(hit_count, returnCode, message, perf); |
|---|
| 814 | else { |
|---|
| 815 | message = _T("No bounds specified!"); |
|---|
| 816 | return NSCAPI::returnUNKNOWN; |
|---|
| 817 | } |
|---|
| 818 | if ((truncate > 0) && (message.length() > (truncate-4))) |
|---|
| 819 | message = message.substr(0, truncate-4) + _T("..."); |
|---|
| 820 | if (message.empty()) |
|---|
| 821 | message = _T("Eventlog check ok"); |
|---|
| 822 | return returnCode; |
|---|
| 823 | } |
|---|
| 824 | |
|---|
| 825 | |
|---|
| 826 | NSC_WRAPPERS_MAIN_DEF(gCheckEventLog); |
|---|
| 827 | NSC_WRAPPERS_IGNORE_MSG_DEF(); |
|---|
| 828 | NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckEventLog); |
|---|