bool DiskTimeMuninNodePlugin::OpenCounter() { PDH_STATUS status; OSVERSIONINFO osvi; ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); if (!GetVersionEx(&osvi) || (osvi.dwPlatformId != VER_PLATFORM_WIN32_NT)) return false; //unknown OS or not NT based // Create a PDH query status = PdhOpenQuery(NULL, 0, &m_PerfQuery); if (status != ERROR_SUCCESS) return false; TString logicalDiskCounterName = A2TConvert(g_Config.GetValue("DiskTimePlugin", "CounterNameLogicalDisk", "LogicalDisk")); TString diskTimeCounterName = A2TConvert(g_Config.GetValue("DiskTimePlugin", "CounterNameDiskTime", "% Disk Time")); DWORD counterListLength = 0; DWORD instanceListLength = 0; status = PdhEnumObjectItems(NULL, NULL, logicalDiskCounterName.c_str(), NULL, &counterListLength, NULL, &instanceListLength, PERF_DETAIL_EXPERT, 0); if (status != PDH_MORE_DATA) return false; TCHAR *counterList = new TCHAR[counterListLength]; TCHAR *instanceList = new TCHAR[instanceListLength]; counterList[0] = NULL; instanceList[0] = NULL; status = PdhEnumObjectItems(NULL, NULL, logicalDiskCounterName.c_str(), counterList, &counterListLength, instanceList, &instanceListLength, PERF_DETAIL_EXPERT, 0); if (status != ERROR_SUCCESS) { delete [] counterList; delete [] instanceList; return false; } int pos = 0; TCHAR *instanceName = instanceList; while (instanceName[0] != NULL && instanceName[1] != NULL) { std::string diskName = T2AConvert(instanceName); m_DiskTimeNames.push_back(diskName); while (instanceName[0] != NULL) instanceName++; instanceName++; } delete [] counterList; delete [] instanceList; // We drop the last instance name as it is _Total m_DiskTimeNames.pop_back(); TCHAR diskTimeCounterPath[MAX_PATH] = {0}; HCOUNTER diskTimeCounter; for (size_t i = 0; i < m_DiskTimeNames.size(); i++) { _sntprintf(diskTimeCounterPath, MAX_PATH, _T("\\%s(%s)\\%s"), logicalDiskCounterName.c_str(), m_DiskTimeNames[i].c_str(), diskTimeCounterName.c_str()); // Associate the uptime counter with the query status = PdhAddCounter(m_PerfQuery, diskTimeCounterPath, 0, &diskTimeCounter); if (status != ERROR_SUCCESS) return false; m_DiskTimeCounters.push_back(diskTimeCounter); } // Collect init data status = PdhCollectQueryData(m_PerfQuery); if (status != ERROR_SUCCESS) return false; return true; }
bool PerfCounterMuninNodePlugin::OpenCounter() { PDH_STATUS status; m_Name = m_SectionName.substr(strlen(PerfCounterMuninNodePlugin::SectionPrefix)); OSVERSIONINFO osvi; ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); if (!GetVersionEx(&osvi) || (osvi.dwPlatformId != VER_PLATFORM_WIN32_NT)) { _Module.LogError("PerfCounter plugin: %s: unknown OS or not NT based", m_Name.c_str()); return false; //unknown OS or not NT based } // Create a PDH query status = PdhOpenQuery(NULL, 0, &m_PerfQuery); if (status != ERROR_SUCCESS) { _Module.LogError("PerfCounter plugin: %s: PdhOpenQuery error=%x", m_Name.c_str(), status); return false; } TString objectName = A2TConvert(g_Config.GetValue(m_SectionName, "Object", "LogicalDisk")); TString counterName = A2TConvert(g_Config.GetValue(m_SectionName, "Counter", "% Disk Time")); DWORD counterListLength = 0; DWORD instanceListLength = 0; if (g_Config.GetValueB(m_SectionName, "UseEnglishObjectNames", true)) { counterName = GetPdhCounterLocalizedName(counterName.c_str()); objectName = GetPdhCounterLocalizedName(objectName.c_str()); } status = PdhEnumObjectItems(NULL, NULL, objectName.c_str(), NULL, &counterListLength, NULL, &instanceListLength, PERF_DETAIL_EXPERT, 0); if (status != PDH_MORE_DATA) { _Module.LogError("PerfCounter plugin: %s: PdhEnumObjectItems error=%x", m_Name.c_str(), status); return false; } TCHAR *counterList = new TCHAR[counterListLength+2]; TCHAR *instanceList = new TCHAR[instanceListLength+2]; counterList[0] = NULL; instanceList[0] = NULL; counterList[1] = NULL; instanceList[1] = NULL; status = PdhEnumObjectItems(NULL, NULL, objectName.c_str(), counterList, &counterListLength, instanceList, &instanceListLength, PERF_DETAIL_EXPERT, 0); if (status != ERROR_SUCCESS) { delete [] counterList; delete [] instanceList; _Module.LogError("PerfCounter plugin: %s: PdhEnumObjectItems error=%x", m_Name.c_str(), status); return false; } int pos = 0; TCHAR *instanceName = instanceList; while (instanceName[0] != NULL) { std::string counterInstanceName = T2AConvert(instanceName); m_CounterNames.push_back(counterInstanceName); while (instanceName[0] != NULL) instanceName++; instanceName++; } delete [] counterList; delete [] instanceList; TCHAR counterPath[MAX_PATH] = {0}; HCOUNTER counterHandle; if (!m_CounterNames.empty()) { if (g_Config.GetValueB(m_SectionName, "DropTotal", true)) { assert(m_CounterNames.back().compare("_Total") == 0); // We drop the last instance name as it is _Total m_CounterNames.pop_back(); } for (size_t i = 0; i < m_CounterNames.size(); i++) { TString instanceNameStr = A2TConvert(m_CounterNames[i]); _sntprintf(counterPath, MAX_PATH, _T("\\%s(%s)\\%s"), objectName.c_str(), instanceNameStr.c_str(), counterName.c_str()); // Associate the uptime counter with the query status = PdhAddCounter(m_PerfQuery, counterPath, 0, &counterHandle); if (status != ERROR_SUCCESS) { _Module.LogError("PerfCounter plugin: %s: PDH add counter error=%x", m_Name.c_str(), status); return false; } m_Counters.push_back(counterHandle); } } else { // A counter with a single instance (Uptime for example) m_CounterNames.push_back("0"); _sntprintf(counterPath, MAX_PATH, _T("\\%s\\%s"), objectName.c_str(), counterName.c_str()); // Associate the uptime counter with the query status = PdhAddCounter(m_PerfQuery, counterPath, 0, &counterHandle); if (status != ERROR_SUCCESS) { _Module.LogError("PerfCounter plugin: %s: PDH add counter error=%x", m_Name.c_str(), status); return false; } m_Counters.push_back(counterHandle); } // Collect init data status = PdhCollectQueryData(m_PerfQuery); if (status != ERROR_SUCCESS) { if (status == PDH_INVALID_HANDLE) { _Module.LogError("PerfCounter plugin: %s: PDH collect data: PDH_INVALID_HANDLE", m_Name.c_str()); return false; } if (status == PDH_NO_DATA) { _Module.LogError("PerfCounter plugin: %s: PDH collect data: PDH_NO_DATA", m_Name.c_str()); return false; } _Module.LogError("PerfCounter plugin: %s: PDH collect data error", m_Name.c_str()); return false; } // Setup Counter Format m_dwCounterFormat = PDH_FMT_DOUBLE; std::string counterFormatStr = g_Config.GetValue(m_SectionName, "CounterFormat", "double"); if (!counterFormatStr.compare("double") || !counterFormatStr.compare("float")) { m_dwCounterFormat = PDH_FMT_DOUBLE; } else if (!counterFormatStr.compare("int") || !counterFormatStr.compare("long")) { m_dwCounterFormat = PDH_FMT_LONG; } else if (!counterFormatStr.compare("int64") || !counterFormatStr.compare("longlong") || !counterFormatStr.compare("large")) { m_dwCounterFormat = PDH_FMT_LARGE; } else { _Module.LogError("PerfCounter plugin: %s: Unknown CounterFormat", m_Name.c_str()); assert(!"Unknown CounterFormat!"); } m_CounterMultiply = g_Config.GetValueF(m_SectionName, "CounterMultiply", 1.0); return true; }