Changeset 47b843a in nscp for modules/CheckSystem


Ignore:
Timestamp:
12/16/07 21:49:16 (5 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2, stable
Children:
5ac88c0a
Parents:
3f69109
Message:

2007-12-16 MickeM

+ A lot of new features in the LUA module only "arguments" missing (as well as exposing more of the API)

  • Changed some exceptions that were thrown wrong
Location:
modules/CheckSystem
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • modules/CheckSystem/PDHCollector.cpp

    r3f69109 r47b843a  
    3737} 
    3838 
     39bool PDHCollector::loadCounter(PDH::PDHQuery &pdh) { 
     40  if (NSCModuleHelper::getSettingsInt(C_SYSTEM_SECTION_TITLE, C_SYSTEM_AUTODETECT_PDH, C_SYSTEM_AUTODETECT_PDH_DEFAULT) != 1) { 
     41    NSC_DEBUG_MSG_STD(_T("Autodetect disabled from nsc.ini via: ") + C_SYSTEM_AUTODETECT_PDH); 
     42    return false; 
     43  } 
     44  std::wstring prefix; 
     45  std::wstring section = NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_FORCE_LANGUAGE, C_SYSTEM_FORCE_LANGUAGE_DEFAULT); 
     46  int noIndex = NSCModuleHelper::getSettingsInt(C_SYSTEM_SECTION_TITLE, C_SYSTEM_NO_INDEX, C_SYSTEM_NO_INDEX_DEFAULT); 
     47  bool bUseIndex = false; 
     48 
     49  // Investigate enviornment and find out what to use 
     50  try { 
     51    OSVERSIONINFO osVer = systemInfo::getOSVersion(); 
     52    if (!systemInfo::isNTBased(osVer)) { 
     53      NSC_LOG_ERROR_STD(_T("Detected Windows 3.x or Windows 9x, PDH will be disabled.")); 
     54      NSC_LOG_ERROR_STD(_T("To manual set performance counters you need to first set ") C_SYSTEM_AUTODETECT_PDH _T("=0 in the config file, and then you also need to configure the various counter.")); 
     55      return false; 
     56    } 
     57 
     58    LANGID langId = -1; 
     59    if (systemInfo::isBelowNT4(osVer)) { 
     60      NSC_DEBUG_MSG_STD(_T("Autodetected NT4, using NT4 PDH counters.")); 
     61      prefix = _T("NT4"); 
     62      bUseIndex = false; 
     63      langId = systemInfo::GetSystemDefaultLangID(); 
     64    } else if (systemInfo::isAboveW2K(osVer)) { 
     65      NSC_DEBUG_MSG_STD(_T("Autodetected w2k or later, using w2k PDH counters.")); 
     66      bUseIndex = true; 
     67      prefix = _T("W2K"); 
     68      langId = systemInfo::GetSystemDefaultUILanguage(); 
     69    } else { 
     70      NSC_LOG_ERROR_STD(_T("Unknown OS detected, PDH will be disabled.")); 
     71      NSC_LOG_ERROR_STD(_T("To manual set performance counters you need to first set ") C_SYSTEM_AUTODETECT_PDH _T("=0 in the config file, and then you also need to configure the various counter.")); 
     72      return false; 
     73    } 
     74 
     75    if (!section.empty()) { 
     76      NSC_DEBUG_MSG_STD(_T("Overriding language with: ") + section); 
     77    } else { 
     78      section = _T("0000") + strEx::ihextos(langId); 
     79      section = _T("0x") + section.substr(section.length()-4); 
     80    } 
     81    if (bUseIndex&&noIndex==1) { 
     82      NSC_DEBUG_MSG_STD(_T("We wanted to use index but were forced not to use them due to: ") + C_SYSTEM_NO_INDEX); 
     83      bUseIndex = false; 
     84    } 
     85  } catch (const systemInfo::SystemInfoException &e) { 
     86    NSC_LOG_ERROR_STD(_T("To manual set performance counters you need to first set ") C_SYSTEM_AUTODETECT_PDH _T("=0 in the config file, and then you also need to configure the various counter.")); 
     87    NSC_LOG_ERROR_STD(_T("The Error: ") + e.getError()); 
     88    return false; 
     89  } catch (...) { 
     90    NSC_LOG_ERROR_STD(_T("To manual set performance counters you need to first set ") C_SYSTEM_AUTODETECT_PDH _T("=0 in the config file, and then you also need to configure the various counter.")); 
     91    NSC_LOG_ERROR_STD(_T("The Error: UNKNOWN_EXCEPTION")); 
     92    return false; 
     93  } 
     94 
     95  // Open counters via .defs file or index. 
     96  try { 
     97    std::wstring proc; 
     98    std::wstring uptime; 
     99    std::wstring memCl; 
     100    std::wstring memCb; 
     101    if (bUseIndex) { 
     102      NSC_DEBUG_MSG_STD(_T("Using index to retrive counternames")); 
     103      proc = _T("\\") + pdh.lookupIndex(238) + _T("(_total)\\") + pdh.lookupIndex(6); 
     104      uptime = _T("\\") + pdh.lookupIndex(2) + _T("\\") + pdh.lookupIndex(674); 
     105      memCl = _T("\\") + pdh.lookupIndex(4) + _T("\\") + pdh.lookupIndex(30); 
     106      memCb = _T("\\") + pdh.lookupIndex(4) + _T("\\") + pdh.lookupIndex(26); 
     107    } else { 
     108      SettingsT settings; 
     109      settings.setFile(NSCModuleHelper::getBasePath(),  _T("counters.defs"), true); 
     110      NSC_DEBUG_MSG_STD(_T("Detected language: ") + settings.getString(section, _T("Description"), _T("Not found")) + _T(" (") + section + _T(")")); 
     111      if (settings.getString(section, _T("Description"), _T("_NOT_FOUND")) == _T("_NOT_FOUND")) { 
     112        NSC_LOG_ERROR_STD(_T("Detected language: ") + section + _T(" but it could not be found in: counters.defs")); 
     113        NSC_LOG_ERROR_STD(_T("You need to manually configure performance counters!")); 
     114        return false; 
     115      } 
     116      NSC_DEBUG_MSG_STD(_T("Attempting to get localized PDH values from the .defs file")); 
     117      proc = settings.getString(section, prefix + _T("_") + C_SYSTEM_CPU, C_SYSTEM_MEM_CPU_DEFAULT); 
     118      uptime = settings.getString(section, prefix + _T("_") + C_SYSTEM_UPTIME, C_SYSTEM_UPTIME_DEFAULT); 
     119      memCl = settings.getString(section, prefix + _T("_") + C_SYSTEM_MEM_PAGE_LIMIT, C_SYSTEM_MEM_PAGE_LIMIT_DEFAULT); 
     120      memCb = settings.getString(section, prefix + _T("_") + C_SYSTEM_MEM_PAGE, C_SYSTEM_MEM_PAGE_DEFAULT); 
     121    } 
     122    NSC_DEBUG_MSG_STD(_T("Found countername: CPU:    ") + proc); 
     123    NSC_DEBUG_MSG_STD(_T("Found countername: UPTIME: ") + uptime); 
     124    NSC_DEBUG_MSG_STD(_T("Found countername: MCL:    ") + memCl); 
     125    NSC_DEBUG_MSG_STD(_T("Found countername: MCB:    ") + memCb); 
     126    pdh.addCounter(proc, &cpu); 
     127    pdh.addCounter(uptime, &upTime); 
     128    pdh.addCounter(memCl, &memCmtLim); 
     129    pdh.addCounter(memCb, &memCmt); 
     130    pdh.open(); 
     131  } catch (const PDH::PDHException &e) { 
     132    NSC_LOG_ERROR_STD(_T("Failed to open performance counters: ") + e.getError()); 
     133    return false; 
     134  } 
     135  return true; 
     136} 
     137 
    39138/** 
    40139* Thread that collects the data every "CHECK_INTERVAL" seconds. 
     
    58157  } 
    59158  PDH::PDHQuery pdh; 
    60  
    61  
    62   if (NSCModuleHelper::getSettingsInt(C_SYSTEM_SECTION_TITLE, C_SYSTEM_AUTODETECT_PDH, C_SYSTEM_AUTODETECT_PDH_DEFAULT) == 1) { 
    63  
    64     SettingsT settings; 
    65     std::wstring prefix; 
    66     settings.setFile(NSCModuleHelper::getBasePath(),  _T("counters.defs"), true); 
    67     std::wstring section = NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_FORCE_LANGUAGE, C_SYSTEM_FORCE_LANGUAGE_DEFAULT); 
    68     bool bUseIndex = false; 
    69  
    70     try { 
    71       OSVERSIONINFO osVer = systemInfo::getOSVersion(); 
    72       if (!systemInfo::isNTBased(osVer)) { 
    73         NSC_LOG_ERROR_STD(_T("Detected Windows 3.x or Windows 9x, PDH will be disabled.")); 
    74         NSC_LOG_ERROR_STD(_T("To manual set performance counters you need to first set ") C_SYSTEM_AUTODETECT_PDH _T("=0 in the config file, and then you also need to configure the various counter.")); 
    75         return 0; 
    76       } 
    77  
    78       LANGID langId = -1; 
    79       if (systemInfo::isBelowNT4(osVer)) { 
    80         NSC_DEBUG_MSG_STD(_T("Autodetected NT4, using NT4 PDH counters.")); 
    81         prefix = _T("NT4"); 
    82         bUseIndex = false; 
    83         langId = systemInfo::GetSystemDefaultLangID(); 
    84       } else if (systemInfo::isAboveW2K(osVer)) { 
    85         NSC_DEBUG_MSG_STD(_T("Autodetected w2k or later, using w2k PDH counters.")); 
    86         bUseIndex = true; 
    87         prefix = _T("W2K"); 
    88         langId = systemInfo::GetSystemDefaultUILanguage(); 
    89       } else { 
    90         NSC_LOG_ERROR_STD(_T("Unknown OS detected, PDH will be disabled.")); 
    91         NSC_LOG_ERROR_STD(_T("To manual set performance counters you need to first set ") C_SYSTEM_AUTODETECT_PDH _T("=0 in the config file, and then you also need to configure the various counter.")); 
    92         return 0; 
    93       } 
    94  
    95       if (!section.empty()) { 
    96         NSC_DEBUG_MSG_STD(_T("Overriding language with: ") + section); 
    97       } else { 
    98         section = _T("0000") + strEx::ihextos(langId); 
    99         section = _T("0x") + section.substr(section.length()-4); 
    100       } 
    101       if (settings.getString(section, _T("Description"), _T("_NOT_FOUND")) == _T("_NOT_FOUND")) { 
    102         NSC_LOG_ERROR_STD(_T("Detected language: ") + section + _T(" but it could not be found in: counters.defs")); 
    103         NSC_LOG_ERROR_STD(_T("You need to manually configure performance counters!")); 
    104         return 0; 
    105       } 
    106       NSC_DEBUG_MSG_STD(_T("Detected language: ") + settings.getString(section, _T("Description"), _T("Not found")) + _T(" (") + section + _T(")")); 
    107     } catch (const systemInfo::SystemInfoException &e) { 
    108       NSC_LOG_ERROR_STD(_T("To manual set performance counters you need to first set ") C_SYSTEM_AUTODETECT_PDH _T("=0 in the config file, and then you also need to configure the various counter.")); 
    109       NSC_LOG_ERROR_STD(_T("The Error: ") + e.getError()); 
    110       return -1; 
    111     } 
    112  
    113     try { 
    114       std::wstring proc; 
    115       std::wstring uptime; 
    116       std::wstring memCl; 
    117       std::wstring memCb; 
    118       if (bUseIndex) { 
    119         NSC_DEBUG_MSG_STD(_T("Using index to retrive counternames")); 
    120         proc = _T("\\") + pdh.lookupIndex(238) + _T("(_total)\\") + pdh.lookupIndex(6); 
    121         uptime = _T("\\") + pdh.lookupIndex(2) + _T("\\") + pdh.lookupIndex(674); 
    122         memCl = _T("\\") + pdh.lookupIndex(4) + _T("\\") + pdh.lookupIndex(30); 
    123         memCb = _T("\\") + pdh.lookupIndex(4) + _T("\\") + pdh.lookupIndex(26); 
    124       } else { 
    125         proc = settings.getString(section, prefix + _T("_") + C_SYSTEM_CPU, C_SYSTEM_MEM_CPU_DEFAULT); 
    126         uptime = settings.getString(section, prefix + _T("_") + C_SYSTEM_UPTIME, C_SYSTEM_UPTIME_DEFAULT); 
    127         memCl = settings.getString(section, prefix + _T("_") + C_SYSTEM_MEM_PAGE_LIMIT, C_SYSTEM_MEM_PAGE_LIMIT_DEFAULT); 
    128         memCb = settings.getString(section, prefix + _T("_") + C_SYSTEM_MEM_PAGE, C_SYSTEM_MEM_PAGE_DEFAULT); 
    129       } 
    130       NSC_DEBUG_MSG_STD(_T("Found counternames: CPU:    ") + proc); 
    131       NSC_DEBUG_MSG_STD(_T("Found counternames: UPTIME: ") + uptime); 
    132       NSC_DEBUG_MSG_STD(_T("Found counternames: MCL:    ") + memCl); 
    133       NSC_DEBUG_MSG_STD(_T("Found counternames: MCB:    ") + memCb); 
    134       pdh.addCounter(proc, &cpu); 
    135       pdh.addCounter(uptime, &upTime); 
    136       pdh.addCounter(memCl, &memCmtLim); 
    137       pdh.addCounter(memCb, &memCmt); 
    138       pdh.open(); 
    139     } catch (const PDH::PDHException &e) { 
    140       NSC_LOG_ERROR_STD(_T("Failed to open performance counters: ") + e.getError()); 
    141       pdh.removeAllCounters(); 
    142       NSC_LOG_ERROR_STD(_T("Trying to use default (English) counters")); 
    143       SetThreadLocale(MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT)); 
    144       pdh.addCounter(C_SYSTEM_MEM_PAGE_LIMIT_DEFAULT, &memCmtLim); 
    145       pdh.addCounter(C_SYSTEM_MEM_PAGE_DEFAULT, &memCmt); 
    146       pdh.addCounter(C_SYSTEM_UPTIME_DEFAULT, &upTime); 
    147       pdh.addCounter(C_SYSTEM_MEM_CPU_DEFAULT, &cpu); 
    148       try { 
    149         pdh.open(); 
    150       } catch (const PDH::PDHException &e) { 
    151         NSC_LOG_ERROR_STD(_T("Failed to open default (English) performance counters: ") + e.getError()); 
    152         NSC_LOG_ERROR_STD(_T("We will now terminate the collection thread!")); 
    153         return 0; 
    154       } 
    155     } 
    156   } else { 
     159  bool bInit = true; 
     160 
     161  if (!loadCounter(pdh)) { 
     162    pdh.removeAllCounters(); 
     163    NSC_DEBUG_MSG_STD(_T("We aparently failed to load counters trying to use default (English) counters or those configured in nsc.ini")); 
     164    SetThreadLocale(MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT)); 
    157165    pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_MEM_PAGE_LIMIT, C_SYSTEM_MEM_PAGE_LIMIT_DEFAULT), &memCmtLim); 
    158166    pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_MEM_PAGE, C_SYSTEM_MEM_PAGE_DEFAULT), &memCmt); 
    159167    pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_UPTIME, C_SYSTEM_UPTIME_DEFAULT), &upTime); 
    160168    pdh.addCounter(NSCModuleHelper::getSettingsString(C_SYSTEM_SECTION_TITLE, C_SYSTEM_CPU, C_SYSTEM_MEM_CPU_DEFAULT), &cpu); 
    161  
    162169    try { 
    163170      pdh.open(); 
    164171    } catch (const PDH::PDHException &e) { 
    165172      NSC_LOG_ERROR_STD(_T("Failed to open performance counters: ") + e.getError()); 
    166       return 0; 
     173      bInit = false; 
    167174    } 
    168175  } 
    169176 
    170177  DWORD waitStatus = 0; 
    171   bool first = true; 
    172   do { 
    173     MutexLock mutex(mutexHandler); 
    174     if (!mutex.hasMutex())  
    175       NSC_LOG_ERROR(_T("Failed to get Mutex!")); 
    176     else { 
    177       try { 
    178         pdh.gatherData(); 
    179       } catch (const PDH::PDHException &e) { 
    180         if (first) {  // If this is the first run an error will be thrown since the data is not yet avalible 
    181                 // This is "ok" but perhaps another solution would be better, but this works :) 
    182           first = false; 
    183         } else { 
    184           NSC_LOG_ERROR_STD(_T("Failed to query performance counters: ") + e.getError()); 
     178  if (bInit) { 
     179    bool first = true; 
     180    do { 
     181      MutexLock mutex(mutexHandler); 
     182      if (!mutex.hasMutex())  
     183        NSC_LOG_ERROR(_T("Failed to get Mutex!")); 
     184      else { 
     185        try { 
     186          pdh.gatherData(); 
     187        } catch (const PDH::PDHException &e) { 
     188          if (first) {  // If this is the first run an error will be thrown since the data is not yet avalible 
     189            // This is "ok" but perhaps another solution would be better, but this works :) 
     190            first = false; 
     191          } else { 
     192            NSC_LOG_ERROR_STD(_T("Failed to query performance counters: ") + e.getError()); 
     193          } 
    185194        } 
    186       } 
    187     }  
    188   } while (((waitStatus = WaitForSingleObject(hStopEvent_, checkIntervall_*100)) == WAIT_TIMEOUT)); 
     195      }  
     196    } while (((waitStatus = WaitForSingleObject(hStopEvent_, checkIntervall_*100)) == WAIT_TIMEOUT)); 
     197  } else { 
     198    NSC_LOG_ERROR_STD(_T("No performance counters were found we will not wait for the end instead...")); 
     199    waitStatus = WaitForSingleObject(hStopEvent_, INFINITE); 
     200  } 
    189201  if (waitStatus != WAIT_OBJECT_0) { 
    190     NSC_LOG_ERROR(_T("Something odd happened, terminating PDH collection thread!")); 
     202    NSC_LOG_ERROR(_T("Something odd happened when terminating PDH collection thread!")); 
    191203  } 
    192204 
  • modules/CheckSystem/PDHCollector.h

    r99e4d8f r47b843a  
    6666  unsigned long long getMemCommitLimit(); 
    6767  unsigned long long getMemCommit(); 
     68  bool loadCounter(PDH::PDHQuery &pdh); 
    6869 
    6970 
Note: See TracChangeset for help on using the changeset viewer.