Changeset 5ac88c0a in nscp


Ignore:
Timestamp:
01/27/08 15:16:05 (5 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2, stable
Children:
bf6e9b4
Parents:
47b843a
Message:

2008-01-27 MickeM

  • Fixed a memory-leak in format_time
  • Fixed so time/date is displayed in "local time" (not UTC) for the eventlog check + Added %message% to evenlog check. Will display the "proper formated eventlog message" as in the eventlog viewer. For this to work you need to have the descriptions flag set In short: /nrpe -H 192.168.0.147 -c checkEventLog -a truncate=1024 "syntax=%generated%: %message%" descriptions=true file=System MaxWarn=1 MaxCrit=1 filter+eventType==error filter+generated=\<12h filter=new filter=all filter=in
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • changelog

    r47b843a r5ac88c0a  
    55 * Add module for relaying events 
    66 * Add API for rehashing the daemon (or implement it the API is there but does nothing) 
     7 
     82008-01-27 MickeM 
     9 * Fixed a memory-leak in format_time 
     10 * Fixed so time/date is displayed in "local time" (not UTC) for the eventlog check 
     11 + Added %message% to evenlog check. Will display the "proper formated eventlog message" as in the eventlog viewer. 
     12   For this to work you need to have the descriptions flag set 
     13   In short: /nrpe -H 192.168.0.147 -c checkEventLog -a truncate=1024 "syntax=%generated%: %message%" descriptions=true file=System MaxWarn=1 MaxCrit=1 filter+eventType==error filter+generated=\<12h filter=new filter=all filter=in 
    714 
    8152007-12-16 MickeM 
  • include/error.hpp

    r99e4d8f r5ac88c0a  
    1111      unsigned long dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,NULL,dwError,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&lpMsgBuf,0,NULL); 
    1212      if (dwRet == 0) { 
    13         return _T("failed to lookup error code: ") + strEx::itos(dwError); 
     13        return _T("failed to lookup error code: ") + strEx::itos(dwError) + _T("( reson: ") + strEx::itos(GetLastError()) + _T(")"); 
    1414      } 
    1515      TCHAR *szBuf = new TCHAR[dwRet + 100]; 
     
    2121    static std::wstring from_module(std::wstring module, unsigned long dwError) { 
    2222      LPVOID lpMsgBuf; 
    23       unsigned long dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_HMODULE,GetModuleHandle(module.c_str()),dwError,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&lpMsgBuf,0,NULL); 
     23      unsigned long dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_HMODULE|FORMAT_MESSAGE_IGNORE_INSERTS,GetModuleHandle(module.c_str()),dwError,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&lpMsgBuf,0,NULL); 
    2424      if (dwRet == 0) { 
    25         return _T("failed to lookup error code: ") + strEx::itos(dwError); 
     25        return _T("failed to lookup error code: ") + strEx::itos(dwError) + _T("( reson: ") + strEx::itos(GetLastError()) + _T(")"); 
    2626      } 
    2727      TCHAR *szBuf = new TCHAR[dwRet + 100]; 
     
    3131      return str; 
    3232    } 
     33    static std::wstring from_module(std::wstring module, unsigned long dwError, DWORD *arguments) { 
     34      LPVOID lpMsgBuf; 
     35      HMODULE hevt = LoadLibraryEx(module.c_str(), NULL, DONT_RESOLVE_DLL_REFERENCES); 
     36      unsigned long dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_HMODULE|FORMAT_MESSAGE_ARGUMENT_ARRAY,hevt, 
     37        dwError,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&lpMsgBuf,0,reinterpret_cast<va_list*>(arguments)); 
     38      if (dwRet == 0) { 
     39        FreeLibrary(hevt); 
     40        return _T("failed to lookup error code: ") + strEx::itos(dwError) + _T(" from DLL: ") + module + _T("( reson: ") + strEx::itos(GetLastError()) + _T(")"); 
     41      } 
     42      TCHAR *szBuf = new TCHAR[dwRet + 100]; 
     43      wsprintf(szBuf, _T("%d: %s"), dwError, lpMsgBuf);  
     44      std::wstring str = szBuf; 
     45      LocalFree(lpMsgBuf); 
     46      FreeLibrary(hevt); 
     47      return str; 
     48    } 
     49    class message { 
     50    public: 
     51      static std::wstring from_module(std::wstring module, unsigned long dwError, DWORD *arguments) { 
     52        LPVOID lpMsgBuf; 
     53        HMODULE hevt = LoadLibraryEx(module.c_str(), NULL, DONT_RESOLVE_DLL_REFERENCES); 
     54        unsigned long dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_HMODULE|FORMAT_MESSAGE_ARGUMENT_ARRAY,hevt, 
     55          dwError,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&lpMsgBuf,0,reinterpret_cast<va_list*>(arguments)); 
     56        if (dwRet == 0) { 
     57          FreeLibrary(hevt); 
     58          return _T("failed to lookup error code: ") + strEx::itos(dwError) + _T(" from DLL: ") + module + _T("( reson: ") + strEx::itos(GetLastError()) + _T(")"); 
     59        } 
     60        std::wstring str = reinterpret_cast<TCHAR*>(lpMsgBuf); 
     61        LocalFree(lpMsgBuf); 
     62        FreeLibrary(hevt); 
     63        return str; 
     64      } 
     65    }; 
    3366  }; 
    3467  class lookup { 
  • include/strEx.h

    r5ca3931 r5ac88c0a  
    9191  inline std::wstring format_date(time_t time, std::wstring format = _T("%Y-%m-%d %H:%M:%S")) { 
    9292    TCHAR buf[51]; 
    93     struct tm *nt = new struct tm; 
     93    struct tm nt; // = new struct tm; 
    9494#if (_MSC_VER > 1300)  // 1300 == VC++ 7.0 
    95     if (gmtime_s(nt, &time) != 0) 
     95    if (gmtime_s(&nt, &time) != 0) 
    9696      return _T(""); 
    9797#else 
     
    100100      return ""; 
    101101#endif 
    102     size_t l = wcsftime(buf, 50, format.c_str(), nt); 
     102    size_t l = wcsftime(buf, 50, format.c_str(), &nt); 
     103    if (l <= 0 || l >= 50) 
     104      return _T(""); 
     105    buf[l] = 0; 
     106    return buf; 
     107  } 
     108  inline std::wstring format_date(SYSTEMTIME &time, std::wstring format = _T("%Y-%m-%d %H:%M:%S")) { 
     109    TCHAR buf[51]; 
     110 
     111    struct tm tmTime; 
     112    memset(&tmTime, 0, sizeof(tmTime)); 
     113 
     114    tmTime.tm_sec = time.wSecond; // seconds after the minute - [0,59] 
     115    tmTime.tm_min = time.wMinute; // minutes after the hour - [0,59] 
     116    tmTime.tm_hour = time.wHour;  // hours since midnight - [0,23] 
     117    tmTime.tm_mday = time.wDay;  // day of the month - [1,31] 
     118    tmTime.tm_mon = time.wMonth-1; // months since January - [0,11] 
     119    tmTime.tm_year = time.wYear-1900; // years since 1900 
     120    tmTime.tm_wday = time.wDayOfWeek; // days since Sunday - [0,6] 
     121 
     122    size_t l = wcsftime(buf, 50, format.c_str(), &tmTime); 
    103123    if (l <= 0 || l >= 50) 
    104124      return _T(""); 
  • modules/CheckEventLog/CheckEventLog.cpp

    r3f569d3 r5ac88c0a  
    6262  EVENTLOGRECORD *pevlr_; 
    6363  __int64 currentTime_; 
     64  std::wstring file_; 
    6465public: 
    65   EventLogRecord(EVENTLOGRECORD *pevlr, __int64 currentTime) : pevlr_(pevlr), currentTime_(currentTime) { 
     66  EventLogRecord(std::wstring file, EVENTLOGRECORD *pevlr, __int64 currentTime) : file_(file), pevlr_(pevlr), currentTime_(currentTime) { 
    6667  } 
    6768  inline __int64 timeGenerated() const { 
     
    168169    return strEx::itos(dwType); 
    169170  } 
    170   std::wstring render(std::wstring syntax) { 
     171  std::wstring get_dll() { 
     172    HKEY hKey = HKEY_LOCAL_MACHINE; 
     173    std::wstring path = ((std::wstring)_T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\") + file_ + (std::wstring)_T("\\") + eventSource()).c_str(); 
     174    std::wstring ret; 
     175    HKEY hTemp; 
     176    LONG lRet = ERROR_SUCCESS; 
     177    if (lRet = RegOpenKeyEx(hKey, path.c_str(), 0, KEY_QUERY_VALUE, &hTemp) != ERROR_SUCCESS) { 
     178      NSC_LOG_ERROR_STD(_T("Could not extract DLL for eventsource: ") + eventSource() + error::format::from_system(lRet)); 
     179      return ret; 
     180    } 
     181    DWORD type; 
     182    const DWORD data_length = 2048; 
     183    DWORD cbData = data_length; 
     184    BYTE *bData = new BYTE[cbData]; 
     185    lRet = RegQueryValueEx(hTemp, _T("EventMessageFile"), NULL, &type, bData, &cbData); 
     186    if (lRet == ERROR_SUCCESS) { 
     187      if (type == REG_SZ) { 
     188        if (cbData < data_length-1) { 
     189          bData[cbData] = 0; 
     190          ret = reinterpret_cast<LPCTSTR>(bData); 
     191        } else { 
     192          NSC_LOG_ERROR_STD(_T("Could not extract DLL for eventsource: ") + eventSource()); 
     193        } 
     194      } else if (type == REG_EXPAND_SZ) { 
     195#define EXPAND_BUFFER_SIZE 2048 
     196        if (cbData < data_length-1) { 
     197          bData[cbData] = 0; 
     198          std::wstring s = reinterpret_cast<LPCTSTR>(bData); 
     199          TCHAR *buffer = new TCHAR[EXPAND_BUFFER_SIZE+1]; 
     200          DWORD expRet = ExpandEnvironmentStrings(s.c_str(), buffer, EXPAND_BUFFER_SIZE); 
     201          if (expRet >= EXPAND_BUFFER_SIZE) 
     202            NSC_LOG_ERROR_STD(_T("Could not extract DLL for eventsource: ") + eventSource()); 
     203          else 
     204            ret = buffer; 
     205        } else { 
     206          NSC_LOG_ERROR_STD(_T("Could not extract DLL for eventsource: ") + eventSource()); 
     207        } 
     208      } else { 
     209        NSC_LOG_ERROR_STD(_T("Could not extract DLL for eventsource: ") + eventSource()); 
     210      } 
     211    } else { 
     212      NSC_LOG_ERROR_STD(_T("Could not extract DLL for eventsource: ") + eventSource() + error::format::from_system(lRet)); 
     213    } 
     214    RegCloseKey(hTemp); 
     215    delete [] bData; 
     216    return ret; 
     217  } 
     218 
     219  std::wstring render_message() { 
     220    DWORD *dwArgs = new DWORD[pevlr_->NumStrings+1]; 
     221    TCHAR* p = reinterpret_cast<TCHAR*>(reinterpret_cast<LPBYTE>(pevlr_) + pevlr_->StringOffset); 
     222    for (unsigned int i =0;i<pevlr_->NumStrings;i++) { 
     223      dwArgs[i] = reinterpret_cast<DWORD>(p); 
     224      DWORD len = wcslen(p); 
     225      p += len+1; 
     226    } 
     227    std::wstring ret = error::format::message::from_module(get_dll(), eventID(), dwArgs); 
     228    delete [] dwArgs; 
     229    std::string::size_type pos = ret.find_last_not_of(_T("\n\t\m")); 
     230    if (pos != std::string::npos) { 
     231      ret = ret.substr(0,pos-1); 
     232    } 
     233    return ret; 
     234  } 
     235  SYSTEMTIME get_time(DWORD time) { 
     236    FILETIME FileTime, LocalFileTime; 
     237    SYSTEMTIME SysTime; 
     238    __int64 lgTemp; 
     239    __int64 SecsTo1970 = 116444736000000000; 
     240 
     241    lgTemp = Int32x32To64(time,10000000) + SecsTo1970; 
     242 
     243    FileTime.dwLowDateTime = (DWORD) lgTemp; 
     244    FileTime.dwHighDateTime = (DWORD)(lgTemp >> 32); 
     245 
     246    FileTimeToLocalFileTime(&FileTime, &LocalFileTime); 
     247    FileTimeToSystemTime(&LocalFileTime, &SysTime); 
     248    return SysTime; 
     249  } 
     250 
     251  SYSTEMTIME get_time_generated() { 
     252    return get_time(pevlr_->TimeGenerated); 
     253  } 
     254  SYSTEMTIME get_time_written() { 
     255    return get_time(pevlr_->TimeWritten); 
     256  } 
     257 
     258  std::wstring render(bool propper, std::wstring syntax, std::wstring date_format = DATE_FORMAT) { 
     259    if (propper) { 
     260      // To obtain the appropriate message string from the message file, load the message file with the LoadLibrary function and use the FormatMessage function 
     261      strEx::replace(syntax, _T("%message%"), render_message()); 
     262    } else { 
     263      strEx::replace(syntax, _T("%message%"), _T("%message% needs the descriptions flag set!")); 
     264    } 
     265 
    171266    strEx::replace(syntax, _T("%source%"), eventSource()); 
    172     strEx::replace(syntax, _T("%generated%"), strEx::format_date(pevlr_->TimeGenerated, DATE_FORMAT)); 
    173     strEx::replace(syntax, _T("%written%"), strEx::format_date(pevlr_->TimeWritten, DATE_FORMAT)); 
     267    strEx::replace(syntax, _T("%generated%"), strEx::format_date(get_time_generated(), date_format)); 
     268    strEx::replace(syntax, _T("%written%"), strEx::format_date(get_time_written(), date_format)); 
    174269    strEx::replace(syntax, _T("%type%"), translateType(eventType())); 
    175270    strEx::replace(syntax, _T("%severity%"), translateSeverity(severity())); 
     
    326421      {  
    327422        bool bMatch = bFilterAll; 
    328         EventLogRecord record(pevlr, ltime); 
     423        EventLogRecord record((*cit2), pevlr, ltime); 
    329424 
    330425        if (filter_chain.empty()) { 
     
    372467        if (match) { 
    373468          if (!syntax.empty()) { 
    374             strEx::append_list(message, record.render(syntax)); 
     469            strEx::append_list(message, record.render(bShowDescriptions, syntax)); 
    375470          } else if (!bShowDescriptions) { 
    376471            strEx::append_list(message, record.eventSource()); 
  • modules/LUAScript/script_wrapper.hpp

    r47b843a r5ac88c0a  
    110110    return NSCAPI::returnUNKNOWN; 
    111111  } 
     112  void push_code(lua_State *L, NSCAPI::nagiosReturn  code) { 
     113    if (code == NSCAPI::returnOK) 
     114      lua_pushstring(L, strEx::wstring_to_string(_T("ok")).c_str()); 
     115    else if (code == NSCAPI::returnWARN) 
     116      lua_pushstring(L, strEx::wstring_to_string(_T("warning")).c_str()); 
     117    else if (code == NSCAPI::returnCRIT) 
     118      lua_pushstring(L, strEx::wstring_to_string(_T("critical")).c_str()); 
     119    else 
     120    lua_pushstring(L, strEx::wstring_to_string(_T("unknown")).c_str()); 
     121  } 
    112122 
    113123  static int inject(lua_State *L) { 
     
    125135    std::wstring msg; 
    126136    std::wstring perf; 
    127     NSCModuleHelper::InjectCommand(command.c_str(), argLen, arguments, msg, perf); 
    128     lua_pushstring(L, strEx::wstring_to_string(_T("ok")).c_str()); 
     137    NSCAPI::nagiosReturn ret = NSCModuleHelper::InjectCommand(command.c_str(), argLen, arguments, msg, perf); 
     138    push_code(L, ret); 
    129139    lua_pushstring(L, strEx::wstring_to_string(msg).c_str()); 
    130140    lua_pushstring(L, strEx::wstring_to_string(perf).c_str()); 
     
    200210      int nargs = lua_gettop( L ); 
    201211      if (nargs < 2) { 
    202         return luaL_error(L, "Missing argument for register_command!"); 
     212        return luaL_error(L, "Missing argument for register_command! usage: register_command(<key>, <function>);"); 
    203213      } 
    204214      if (nargs > 2) { 
    205         return luaL_error(L, "To many arguments for register_command!"); 
     215        return luaL_error(L, "To many arguments for register_command! usage: register_command(<key>, <function>);"); 
    206216      } 
    207217      handler->register_command(script, pop_string(L), pop_string(L)); 
     
    222232    void load() { 
    223233      luaL_openlibs(L); 
    224  
    225234      //Luna<Account>::Register(L); 
    226235      lua_register(L, "inject", inject); 
    227236      lua_register(L, "register_command", register_command); 
    228  
    229237 
    230238      if (luaL_loadfile(L, strEx::wstring_to_string(script_).c_str()) != 0) { 
Note: See TracChangeset for help on using the changeset viewer.